天天看點

SpringCloud 2020.0.4 系列之服務降級的其他用法與熔斷

1. 概述

老話說的好:控制好自己的情緒,才能控制好自己的人生。沖動是魔鬼,冷靜才最重要。

言歸正傳,之前聊了在 Feign 調用時,如何給整個 Feign接口類 增加降級政策。

今天我們來聊一下 Hystrix 關于服務降級的其他用法,也聊一下如何使用 Hystrix 實作熔斷機制。

閑話不多說,直接上代碼。

2. Hystrix服務降級的其他用法

2.1 為本地Service業務方法配置降級政策

在Service實作類中編寫降級方法,在Service 方法上使用 @HystrixCommand 注解指定降級方法即可。

@HystrixCommand(fallbackMethod = "businessFunctionFallback")
    public String businessFunction() {
        throw new RuntimeException("模拟異常");
    }

    public String businessFunctionFallback() {
        return "businessFunction 降級";
    }      

2.2 多級降級的配置

如果降級方法也有抛異常的風險,那不防設定多級降級。

@HystrixCommand(fallbackMethod = "exception2")
    public String exception() {
        System.out.println("執行 Service exception");
        throw new RuntimeException("模拟異常");
    }

    @HystrixCommand(fallbackMethod = "exception3")
    public String exception2() {
        System.out.println("執行 Service exception2");
        throw new RuntimeException("模拟異常");
    }

    public String exception3() {
        System.out.println("執行 Service exception3");
        return "fail";
    }      

2.3 使用注解對降級方法進行配置

可以使用注解配置服務逾時時間,可以配置多項屬性,逗号分隔

  @HystrixCommand(commandKey = "myTimeout", fallbackMethod = "timeoutFallback",
            commandProperties = {
                @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    })
    public String timeout(Integer timeoutSecond) {
        System.out.println("調用 Service timeout");
        return eurekaClientService.timeout(timeoutSecond);
    }

  public String timeoutFallback(Integer timeoutSecond) {
        System.out.println("timeoutFallback:" + timeoutSecond);
        return "timeoutFallback:" + timeoutSecond;
    }      

2.4 利用 commandKey,在yml檔案中對某個降級方法進行配置

commandKey 是 @HystrixCommand 注解的一個屬性 

hystrix:
  command:
    myTimeout:   # 根據 commandKey 設定具體方法
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 4000     # 逾時時間配置          

3. Hystrix實作熔斷

3.1 熔斷概述

熔斷通常配合降級來使用,在指定的時間視窗内,請求的失敗率達到設定的門檻值時,就會開啟熔斷,直接調用降級方法,而不去調用真實的邏輯,一段時間後會嘗試恢複,但如果繼續失敗,則會繼續熔斷。 

3.2 熔斷的配置

hystrix:
  command:
    default:
      fallback:
        enabled: true     # 開啟服務降級
      execution:
        timeout:
          enabled: true   # 全局逾時配置
        isolation:
          thread:
            timeoutInMilliseconds: 1000    # 逾時時間配置
            interruptOnTimeout: true       # 逾時後是否終止線程
            interruptOnFutureCancel: true  # 取消後是否終止線程

      circuitBreaker:
        enabled: true                      # 是否開啟熔斷
        requestVolumeThreshold: 5          # 在時間視窗内,請求數量達到多少個,才判斷熔斷
        errorThresholdPercentage: 50       # 失敗百分比
        sleepWindowInMilliseconds: 15000   # 進入熔斷後,多少ms後進入半開狀态,會嘗試發送請求,失敗繼續熔斷,成功則正常提供服務
      metrics:
        rollingStats:
          timeInMilliseconds: 20000        # 配置時間視窗的時間間隔,機關ms      

4. 綜述

今天聊了一下 服務降級的其他用法與熔斷 的相關知識,希望可以對大家的工作有所幫助。

歡迎幫忙點贊、評論、轉發、加關注 :)

關注追風人聊Java,每天更新Java幹貨。

5. 個人公衆号

追風人聊Java,歡迎大家關注

SpringCloud 2020.0.4 系列之服務降級的其他用法與熔斷