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,歡迎大家關注


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM