Spring Cloud - Hystrix

服务容错和Hystrix

一旦下游服务C因某些缘由变得不可用,积压了大量请求,服务B的请求线程也随之阻塞。线程资源逐渐耗尽,使得服务B也变得不可用。紧接着,服务A也变为不可用,整个调用链路被拖垮(雪崩)html

image-20181022155844375

主要为了解决雪崩效应java

功能

  1. 服务降级git

    1. 双十一,网站被挤爆了
    2. 网络开小差了
    3. 区分业务,优先核心服务,非核心服务不可用或弱可用
    4. HystrixCommand注解指定
    5. fallbackMethod(回退函数)中具体实现降级逻辑
  2. 服务熔断github

    1. 开启熔断

      在固定时间窗口内,接口调用超时比率达到一个阈值,会开启熔断。进入熔断状态后,后续对该服务接口的调用再也不通过网络,直接执行本地的默认方法,达到服务降级的效果。spring

    2. 熔断恢复

      熔断不多是永久的。当通过了规定时间以后,服务将从熔断状态回复过来,再次接受调用方的远程调用。网络

  3. 依赖隔离
  4. 监控(Hystrix DashBord)

image-20181105172733183

使用

  1. 导入pomapp

    1. <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-hystrix</artifactId>
      </dependency>
  2. 开启注解@EnableCircuitBreakeride

    1. 或者替换为@EnableSpringCloud函数

      1. image-20181022171636152
  3. 对应方法上面使用注解(这个方法必须写)post

    1. @HystrixCommand(fallbackMethod = "fallback")
    2. 设置全局

      1. @DefaultProperties(defaultFallback = "defaultFallBack")
    3. 设置默认fallback和超时时间
    4. @RestController
      @DefaultProperties(defaultFallback = "defaultFallBack")
      public class HystrixController {
          //@HystrixCommand(fallbackMethod = "fallback")
          @HystrixCommand(commandProperties = {
                  @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
          })
          @GetMapping("/getProductInfoList")
          public String getProductInfo() {
              RestTemplate restTemplate = new RestTemplate();
              return restTemplate.postForObject("http://127.0.0.1:9083/product/listForOrder", Collections.singletonList("157875196366160022"), String.class);
          }
      
          private String fallback() {
              return "太拥挤了,请稍后再试~";
          }
      
          private String defaultFallBack() {
              return "默认提示:太拥挤了,请稍后重试~";
          }
      }
      1. 对应配置查找
      2. image-20181022173624811
      3. image-20181022173916566

依赖隔离-线程池隔离

会自动实现依赖隔离,进行容错保护

服务熔断

参数参考:http://zyouwei.com/%E6%8A%80%...

circuitBreaker断路器

circuitBreaker.requestVolumeThreshold

circuitBreaker.sleepWindowInMilliseconds

circuitBreaker.errorThresholdPercentage

https://martinfowler.com/blik...

状态机

image-20181102181315770

容错:重试机制,第一次不成功再重试一次就成功了

故障问题:使用断路器模式,故障达到必定的值,断路器断闸

熔断的三个状态

close(关闭状态)

服务没有故障时,熔断器所处的状态,对调用方的调用不作任何限制。

open(熔断器打开状态)

在固定时间窗口内(Hystrix默认是10秒),接口调用出错比率达到一个阈值(Hystrix默认为50%),会进入熔断开启状态。进入熔断状态后,后续对该服务接口的调用再也不通过网络,直接执行本地的fallback方法

half open (半熔断)

在进入熔断开启状态一段时间以后(Hystrix默认是5秒),熔断器会进入半熔断状态。所谓半熔断就是尝试恢复服务调用,容许有限的流量调用该服务,并监控调用成功率。若是成功率达到预期,则说明服务已恢复,进入熔断关闭状态;若是成功率仍旧很低,则从新进入熔断关闭状态

image-20181102182806035

image-20181102182826212

``

@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),
//时间窗口,统计时间范围
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "1000"),
//错误百分比
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60")

使用配置项

​ 必须在对应的方法上使用注解 @HystrixCommand

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 3000
#      circuitBreaker:
#        enabled: true

给特定方法设置:commandKey

image-20181105144215627

Feign-hystrix的使用

  1. 配置

    feign:
      hystrix:
        enabled: true
  2. 修改对应client增长fallback,类注解@Component不要忘记了
  3. @FeignClient(name = "product",fallback =ProductClient.FallBackClass.class )
    public interface ProductClient {
        @PostMapping("/product/listForOrder")
        List<ProductInfoOutPut> listForOrder(List<String> productIdList);
    
        @PostMapping("/product/decreaseStock")
        void decreaseStock(List<DecreaseStockInput> CartDTO);
    
        @Component
        class FallBackClass implements ProductClient{
    
            @Override
            public List<ProductInfoOutPut> listForOrder(List<String> productIdList) {
                return null;
            }
    
            @Override
            public void decreaseStock(List<DecreaseStockInput> CartDTO) {
            }
        }
    }
  4. 更改扫描包的路径,可以读取到client

可视化配置

导入pom

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

开启注解

@EnableHystrixDashboard

访问http://localhost:8899/hystrix

image-20181105164959036

image-20181105165043006

问题

使用了zuul会让类懒加载,因此第一次访问会超时,这时候咱们须要把配置直接放到zuul里面

代码地址:https://github.com/zzy0-0/ord...

相关文章
相关标签/搜索