zoukankan      html  css  js  c++  java
  • SpringCloud的Hystrix服务降级和熔断

    一、原理:

    一个单体服务,拆成多个微服务A、B、C,一切正常的时候还好,万一A有个功能需要调用B的方法,
    B的这个方法又要调用C的方法。这个时候C出幺蛾子了,B一直请求不到C就没办法返回结果,A又不断的在请求B。
    这个时候就会耗尽资源,导致整个系统崩溃掉,就是所谓的雪崩效应。

    那么有什么防止措施吗?那就用Hystrix服务降级和熔断吧。
    这东西听起来好像有点高级,其实说白了,
    熔断就是请求一个服务的时候, 如果多次请求都还是请求失败,那么就让后面的不要再请求了。
    降级就是,请求在规定时间内还没有返回结果的话,就执行另外一个方法(用于快速返回结果,不要一直堵在那里!)

    二、实现:

    1、pom引入依赖:

     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-hystrix</artifactId>
     </dependency>
    

    2、启动类引入注解:

    @EnableCircuitBreaker:用于启动熔断功能(就和断路器差不多,压力大了就不干了,关掉,免得失火)
    

    3、yml配置:

    #feign:
    #  hystrix:
    #    enabled: true
    #hystrix:
    #  command:
    #    default:
    #      execution:
    #        isolation:
    #          thread:
    #            timeoutInMilliseconds: 10000   #超时时间
    #        circuitBreaker:
    #          requestVolumeThreshold: 10 #超时时间内,多少个请求失败后,打开断路器
    #          sleepWindowInMilliseconds: 5000 #打开短路器后,多少秒开始尝试恢复
    #          errorThresholdPercentage: 50      #在滚动时间内,失败频率达到百分之50,就打开断路器
    #      metrics:
    #        rollingStats:
    #          timeInMilliseconds: 10000  #设置滚动时间,10000ms,就是在这个时间内,
    									  #requestVolumeThreshold: 10,有10次请求。
    									  #失败率是 errorThresholdPercentage: 50,百分之50,就打开断路器。

    4、代码:

    1、通过@HystrixCommand来实现退回机制:

       @HystrixCommand(fallbackMethod = "fallback")
       @GetMapping("/postRestTemplateRibbonConsumer")
       public String postRestTemplateRibbonConsumer(){
           Map<String,String> paramMap = new HashMap();
           paramMap.put("start", "1");
           paramMap.put("page", "5");
           return this.restTemplateRibbon.postForObject("http://eureka-client/"+"/postDcRequestBody",paramMap,String.class);
       }
    	
    	public String fallback() {
           	return "fallback";
      	}
    

    2、通过@FeignClient(value =“eureka-client”,fallback = FeignFallBack.class),指定退回类,实现退回机制:

    • 定义一个Feign接口:
    @FeignClient(value ="eureka-client",fallback = FeignFallBack.class)
    public interface FeignInterface {
    
        @PostMapping("/postDcRequestBody")
        public String postDcRequestBody(@RequestBody Map<String,String> paramMap);
    
        @PostMapping("/postFeignClientConsumerHystrix")
        public String postFeignClientConsumerHystrix();
    }
    
    • 创建一个FeignFallBack,实现接口:
    @Component
    public class FeignFallBack implements FeignInterface {
    
       @Override
       public String postDcRequestBody(Map<String, String> paramMap) {
           return "调用postDcRequestBody失败!";
       }
    
       @Override
       public String postFeignClientConsumerHystrix() {
           return "调用postFeignClientConsumerHystrix失败!";
       }
    }
    
    • 调用接口方法:
     /**
         * post请求(Feign接口)
         * @return
         */
        @GetMapping("/postFeignClientConsumer")
        public String postFeignClientConsumer(){
            Map<String,String> paramMap = new HashMap();
            paramMap.put("start", "1");
            paramMap.put("page", "5");
            return this.feignInterface.postDcRequestBody(paramMap);
        }
    

    说明:方法请求,在规定的时间内,没有返回,就触发降级,执行对应的降级方法。

  • 相关阅读:
    石子合并问题(直线版)
    Python_07-常用函数
    Python_06-函数与模块
    C++中的头文件和源文件
    sell 项目 商品表 设计 及 创建
    SpringBoot集成Mybatis
    SpringBoot集成jdbcTemplate/JPA
    SpringBoot使用JSP渲染页面
    SpringBoot引入freemaker前端模板
    使用SpringBoot创建Web项目
  • 原文地址:https://www.cnblogs.com/47Gamer/p/13727460.html
Copyright © 2011-2022 走看看