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...

相關文章
相關標籤/搜索