SpringCloud Hystrix 服务降级和服务熔断

记录一下Hystrix的相关内容,好记性不如烂笔头,也给自己增加一下记忆

前置配置

  • 1.pom依赖
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
  • 2.启动类上包含注解@EnableCircuitBreaker
    • 注意,@SpringCloudApplication注解是包含@EnableCircuitBreaker注解的

达到服务降级

  • 使用场景,eg:双十一,确保核心下单业务,而搁置积分等业务
  • 使用:在需要降级的业务上添加注解@HystrixCommand(fallbackMethod = "fallback"),fallback是当此方法无法正常返回请求时,其他逻辑

@HystrixCommand

    @HystrixCommand(fallbackMethod = "fallback")
    @RequestMapping("/getproductlist")
    public String getProductList() {
        RestTemplate restTemplate = new RestTemplate();
        // 这是写在订单系统中的方法,用于请求产品系统的方法
        String response = restTemplate.getForObject("http://127.0.0.1:8501/product/list", String.class);
        return response;
    }

   // 当产品系统的方法,无法正常返回结果时,则执行此方法,并返回结果
    public String fallback() {
        return "稍后再试!";
    }

defaultFallback

  • 使用场景,默认的返回结果,当此类中有多个熔断需求,且处理方式相同,则不必在每个方法单独定义fallbackMethod
@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
public class HystrixController {

@HystrixCommand
@RequestMapping("/getproductlist")
    public String getProductList() {
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject("http://127.0.0.1:8501/product/list", String.class);

        return response;
    }


    public String defaultFallback() {
        return "休息一下,马上回来";
    }
}

超时配置

  • 比如在以上情况下,在订单系统请求产品系统的方法时,由于相应时间过长,而导致服务降级,原默认超时事件为1s
    // 此时,超时时间设定为2s,具体参数可在com.netflix.hystrix.HystrixCommandProperties中查看
    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "2000")
    })
    @RequestMapping("/getproductlist")
    public String getProductList() {
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject("http://127.0.0.1:8501/product/list", String.class);

        return response;
    }

服务熔断

大概过程图示

参数介绍

circuitBreaker.requestVolumeThreshold // 允许最小访问请求数量,
circuitBreaker.sleepWindowInMilliseconds // 休眠时间窗的时间范围
circuitBreaker.errorThresholdPercentage // 开启熔断的错误率,

使用

  • 1.注解方式
	@HystrixCommand(commandProperties = {
          //设置熔断
	   @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),  	
          //请求数达到后才计算		 
	   @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
           //休眠时间窗	 
	   @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"), 
           //错误率
	   @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),	 
	})
      @RequestMapping("/getproductlist")
       public String getProductList() {
            RestTemplate restTemplate = new RestTemplate();
            String response = restTemplate.getForObject("http://127.0.0.1:8501/product/list", String.class);
            return response;
        }

dashboad 使用

TODO

posted @ 2020-03-29 23:53  VVII  阅读(398)  评论(0)    收藏  举报