Spring cloud:熔断器-服务端降级

2021-02-07 21:18

阅读:346

标签:conf   enc   nts   list()   tin   java   mode   xsd   org   

环境

  1. spring cloud Edgware.SR6
  2. jdk 7
  3. sts 4.6.0
  4. mysql 5.7

背景

在服务端发生超时或者错误时,服务端进行服务降级处理。

搭建步骤

增加依赖

org.springframework.cloud
    spring-cloud-starter-netflix-hystrix

控制层

@HystrixCommand(fallbackMethod = "fallbackAllPayments")
@GetMapping
public List allPayments() {

    LoggerFactory.getLogger(getClass()).info("invoke payment port: {}", port);
    int i = 1 / 0;
    return paymentService.queryAllPayments();
}

public List fallbackAllPayments() {

    LoggerFactory.getLogger(getClass()).info("invoke payment error");
    return Collections.emptyList();
}

启动类

package jiangbo.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class JiangBoApplication {

    public static void main(String[] args) {

        SpringApplication.run(JiangBoApplication.class, args);
    }
}

验证

浏览器访问 http://localhost:4420/payment ,发现返回了一个空数组,并且控制台打印了如下的内容:

2020-04-25 17:52:58.153  INFO 3976 --- [mentContrller-1] j.s.controller.PaymentContrller          : invoke payment port: 4420
2020-04-25 17:52:58.191  INFO 3976 --- [mentContrller-1] j.s.controller.PaymentContrller          : invoke payment error

附录

pom.xml

4.0.0org.springframework.cloud
        spring-cloud-starter-parent
        Edgware.SR6jiangbo.springcloud
    13spring-cloud-payment
    1.0.0jar1.7org.springframework.cloud
            spring-cloud-starter-netflix-hystrix
        org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        org.springframework.boot
            spring-boot-starter-jdbc
        mysql
            mysql-connector-java
            runtimeorg.springframework.boot
            spring-boot-devtools
            providedorg.springframework.boot
                spring-boot-maven-plugin
            

Spring cloud:熔断器-服务端降级

标签:conf   enc   nts   list()   tin   java   mode   xsd   org   

原文地址:https://www.cnblogs.com/jiangbo44/p/12751166.html


评论


亲,登录后才可以留言!