Hystrix完整配置列表


前提

Hystrix在2018年11月20日之后已經停止維護,最后一個提交記錄是:Latest commit 3cb2158 on 20 Nov 2018,最后一個正式版本為1.5.18。鑒於目前所在公司的技術棧是Spring Cloud,熔斷和降級組件主要用的還是Hystrix,這里就Hystrix的完整列表做一個分析記錄,方便以后可以隨時查詢。本文主要參考:Hystrix Configuration。其中,命令配置是針對HystrixCommand,主要包括命令執行(execution)配置、命令降級(fallback)配置、熔斷器(circuit breaker)配置、度量統計(metrics)配置和請求上下文配置。

HystrixCommandKey、HystrixCommandGroupKey和HystrixThreadPoolKey

HystrixCommandKeyHystrixCommandGroupKeyHystrixThreadPoolKey三個KEY是HystrixCommand的重要標識。下面分別分析一下它們的含義。

HystrixCommandKey

HystrixCommandKeyHystrix命令的唯一標識,准確來說是HystrixCommand實例或者HystrixObservableCommand實例的唯一標識。它是必須的,如果不自定義配置,它會通過下面方式確定默認值:

[HystrixCommand或者HystrixObservableCommand的具體子類].getClass().getSimpleName();

編程式配置如下:

HystrixCommandKey.Factory.asKey("Your Key");


public Command() {
    super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("Group Key"))
                .andCommandKey(HystrixCommandKey.Factory.asKey("Command Key")));
}

注意一點:大部分Hystrix的配置都是和HystrixCommandKey綁定,所以HystrixCommandKey是比較重要的。

HystrixCommandGroupKey

HystrixCommandGroupKey是用於對Hystrix命令進行分組,分組之后便於統計展示於儀表盤、上傳報告和預警等等,也就是說,HystrixCommandGroupKeyHystrix內部進行度量統計時候的分組標識,數據上報和統計的最小維度就是分組的KEY。HystrixCommandGroupKey是必須配置的,配置方式如下:

HystrixCommandGroupKey.Factory.asKey("Group Key")

public Command() {
    super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("Group Key")));
}

HystrixThreadPoolKey

HystrixThreadPoolKey主要標識用於監控、度量和緩存等等作用的HystrixThreadPool實例。一個HystrixCommand會和一個獨立的HystrixThreadPool實例關聯,也就是說一類HystrixCommand總是在同一個HystrixThreadPool實例中執行。如果不顯式配置HystrixThreadPoolKey,那么會使用HystrixCommandGroupKey的值去配置HystrixThreadPoolKeyHystrixThreadPoolKey的配置方式如下:

HystrixThreadPoolKey.Factory.asKey("ThreadPoolKey")

public Command() {
    super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("xxx"))
                .andCommandKey(HystrixCommandKey.Factory.asKey("YYY"))
                .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("ThreadPoolKey")));
}

命令執行(execution)配置

隔離策略

  • execution.isolation.strategy

隔離策略決定Hystrix命令執行的時候采用什么類型的策略進行依賴隔離。

默認值 THREAD (見ExecutionIsolationStrategy.THREAD)
可選值 THREAD,SEMAPHORE
默認全局配置 hystrix.command.default.execution.isolation.strategy
實例配置 hystrix.command.[HystrixCommandKey].execution.isolation.strategy

執行隔離策略到底選擇線程池(THREAD)還是信號量(SEMAPHORE)?文檔中給出的建議是:

使用HystrixCommand的時候建議用THREAD策略,使用HystrixObservableCommand的時候建議使用SEMAPHORE策略。

使用THREAD策略讓HystrixCommand在線程中執行可以提供額外的保護層,以防止因為網絡超時導致的延時失敗。

一般情況下,只有這種特殊例子下HystrixCommand會搭配SEMAPHORE策略使用:調用的頻次太高(例如每個實例每秒數百次調用),這種情況如果選用THREAD策略有可能導致超過線程隔離的上限(有可能需要太多的線程或者命令太多線程不足夠用於隔離請求),這種情況一般是非網絡請求調用。

筆者想說的是:建議選用默認值,因為目前很少遇到使用信號量隔離的場景。

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.command.default.execution.isolation.strategy=THREAD

# 實例配置
hystrix.command.CustomCommand.execution.isolation.strategy=THREAD

是否允許超時

  • execution.timeout.enabled

決定HystrixCommand#run()執行時是否允許超時,只有設置為true的時候,下面提到的“超時時間上限”才會有效。

默認值 true
可選值 true,false
默認全局配置 hystrix.command.default.execution.timeout.enabled
實例配置 hystrix.command.[HystrixCommandKey].execution.timeout.enabled
建議(筆者備注) 保持選用默認值

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withExecutionTimeoutEnabled(true)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.command.default.execution.timeout.enabled=true

# 實例配置
hystrix.command.CustomCommand.execution.timeout.enabled=true

超時時間上限

  • execution.isolation.thread.timeoutInMilliseconds

HystrixCommand執行時候超時的最大上限,單位是毫秒,如果命令執行耗時超過此時間值那么會進入降級邏輯。這個配置生效的前提是hystrix.command.default.execution.timeout.enabled或者hystrix.command.[HystrixCommandKey].execution.timeout.enabled為true。

默認值 1000
可選值 -
默認全局配置 hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds
實例配置 hystrix.command.[HystrixCommandKey].execution.isolation.thread.timeoutInMilliseconds
建議(筆者備注) 保持選用默認值

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withExecutionTimeoutInMilliseconds(1000)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000

# 實例配置
hystrix.command.CustomCommand.execution.isolation.thread.timeoutInMilliseconds=1000

超時是否中斷

此配置項決定HystrixCommand#run()執行的時候調用超時的情況下是否中斷。

默認值 true
可選值 truefalse
默認全局配置 hystrix.command.default.execution.isolation.thread.interruptOnTimeout
實例配置 hystrix.command.[HystrixCommandKey].execution.isolation.thread.interruptOnTimeout
建議(筆者備注) 保持選用默認值

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withExecutionIsolationThreadInterruptOnTimeout(true)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.command.default.execution.isolation.thread.interruptOnTimeout=true

# 實例配置
hystrix.command.CustomCommand.execution.isolation.thread.interruptOnTimeout=true

取消是否中斷

  • execution.isolation.thread.interruptOnCancel

此配置項決定HystrixCommand#run()執行的時候取消調用的情況下是否中斷。

默認值 false
可選值 truefalse
默認全局配置 hystrix.command.default.execution.isolation.thread.interruptOnCancel
實例配置 hystrix.command.[HystrixCommandKey].execution.isolation.thread.interruptOnCancel
建議(筆者備注) 保持選用默認值

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withExecutionIsolationThreadInterruptOnFutureCancel(false)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.command.default.execution.isolation.thread.interruptOnCancel=false

# 實例配置
hystrix.command.CustomCommand.execution.isolation.thread.interruptOnCancel=false

最大並發請求上限(SEMAPHORE)

  • execution.isolation.semaphore.maxConcurrentRequests

此配置項決定使用HystrixCommand#run()方法和ExecutionIsolationStrategy.SEMAPHORE隔離策略下並發請求數量的最高上限。

默認值 10
可選值 -
默認全局配置 hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests
實例配置 hystrix.command.[HystrixCommandKey].execution.isolation.semaphore.maxConcurrentRequests
建議(筆者備注) 必須根據實際情況設定此值

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE)
                        .withExecutionIsolationSemaphoreMaxConcurrentRequests(100)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests=100

# 實例配置
hystrix.command.CustomCommand.execution.isolation.semaphore.maxConcurrentRequests=100

命令降級(fallback)配置

命令降級配置控制HystrixCommand#getFallback()的執行邏輯,所有命令降級配置對策略ExecutionIsolationStrategy.THREAD或者ExecutionIsolationStrategy.SEMAPHORE都生效。

最大並發降級請求處理上限

  • fallback.isolation.semaphore.maxConcurrentRequests

這個屬性用於控制一個HystrixCommand#getFallback()實例方法在執行線程中調用的最大上限,如果超過此上限,降級邏輯不會執行並且會拋出一個異常。

默認值 10
可選值 -
默認全局配置 hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests
實例配置 hystrix.command.[HystrixCommandKey].fallback.isolation.semaphore.maxConcurrentRequests
建議(筆者備注) 必須根據實際情況設定此值

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withFallbackIsolationSemaphoreMaxConcurrentRequests(20)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests=20

# 實例配置
hystrix.command.CustomCommand.fallback.isolation.semaphore.maxConcurrentRequests=20

是否開啟降級

  • fallback.enabled

此屬性控制當HystrixCommand執行失敗之后是否調用HystrixCommand#getFallback()

默認值 true
可選值 falsetrue
默認全局配置 hystrix.command.default.fallback.enabled
實例配置 hystrix.command.[HystrixCommandKey].fallback.enabled
建議(筆者備注) 建議保持默認值

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withFallbackEnabled(true)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.command.default.fallback.enabled=true

# 實例配置
hystrix.command.CustomCommand.fallback.enabled=true

斷路器(circuit breaker)配置

斷路器配置用於控制HystrixCircuitBreaker實例的行為。

是否啟用斷路器

  • circuitBreaker.enabled

此屬性確定斷路器是否用於跟蹤健康狀況,以及當斷路器打開的時候是否用於短路請求(使請求快速失敗進入降級邏輯)。

默認值 true
可選值 falsetrue
默認全局配置 hystrix.command.default.circuitBreaker.enabled
實例配置 hystrix.command.[HystrixCommandKey].circuitBreaker.enabled
建議(筆者備注) 建議保持默認值

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withCircuitBreakerEnabled(true)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.command.default.circuitBreaker.enabled=true

# 實例配置
hystrix.command.CustomCommand.circuitBreaker.enabled=true

斷路器請求量閾值

  • circuitBreaker.requestVolumeThreshold

此屬性設置將使斷路器打開的滑動窗口中的最小請求數量。

例如,如果值是20,那么如果在滑動窗口中只接收到19個請求(比如一個10秒的窗口),即使所有19個請求都失敗了,斷路器也不會打開。

默認值 20
可選值 -
默認全局配置 hystrix.command.default.circuitBreaker.requestVolumeThreshold
實例配置 hystrix.command.[HystrixCommandKey].circuitBreaker.requestVolumeThreshold
建議(筆者備注) 建議保持默認值,如果部分接口不能容忍默認閾值可以單獨配置

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withCircuitBreakerRequestVolumeThreshold(10)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.command.default.circuitBreaker.requestVolumeThreshold=10

# 實例配置
hystrix.command.CustomCommand.circuitBreaker.requestVolumeThreshold=10

斷路器等待窗口時間

  • circuitBreaker.sleepWindowInMilliseconds

此屬性設置斷路器打開后拒絕請求的時間量,每隔一段時間(sleepWindowInMilliseconds,單位是毫秒)允許再次嘗試(也就是放行一個請求)確定是否應該關閉斷路器。

默認值 5000
可選值 -
默認全局配置 hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds
實例配置 hystrix.command.[HystrixCommandKey].circuitBreaker.sleepWindowInMilliseconds
建議(筆者備注) 建議保持默認值

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withCircuitBreakerSleepWindowInMilliseconds(5000)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=5000

# 實例配置
hystrix.command.CustomCommand.circuitBreaker.sleepWindowInMilliseconds=5000

斷路器錯誤百分比閾值

  • circuitBreaker.errorThresholdPercentage

此屬性設置一個錯誤百分比,當請求錯誤率超過設定值,斷路器就會打開。

默認值 50
可選值 -
默認全局配置 hystrix.command.default.circuitBreaker.errorThresholdPercentage
實例配置 hystrix.command.[HystrixCommandKey].circuitBreaker.errorThresholdPercentage
建議(筆者備注) 建議保持默認值

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withCircuitBreakerErrorThresholdPercentage(50)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.command.default.circuitBreaker.errorThresholdPercentage=50

# 實例配置
hystrix.command.CustomCommand.circuitBreaker.errorThresholdPercentage=50

注意:

  • 配置項circuitBreaker.requestVolumeThreshold針對錯誤請求數量。
  • 配置項circuitBreaker.errorThresholdPercentage針對錯誤請求百分比。

是否強制打開斷路器

  • circuitBreaker.forceOpen

此屬性控制斷路器是否強制打開,強制打開斷路器會使所有請求直接進入降級邏輯,也就是包裹在HystrixCommand#run()的邏輯不會執行。circuitBreaker.forceOpen屬性和circuitBreaker.forceClosed屬性互斥。

默認值 false
可選值 falsetrue
默認全局配置 hystrix.command.default.circuitBreaker.forceOpen
實例配置 hystrix.command.[HystrixCommandKey].circuitBreaker.forceOpen
建議(筆者備注) 建議保持默認值

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withCircuitBreakerForceOpen(true)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.command.default.circuitBreaker.forceOpen=true

# 實例配置
hystrix.command.CustomCommand.circuitBreaker.forceOpen=true

是否強制關閉斷路器

  • circuitBreaker.forceClosed

此屬性控制斷路器是否強制關閉,強制關閉斷路器會導致所有和斷路器相關的配置和功能都失效,HystrixCommand#run()拋出異常會正常進入降級邏輯。circuitBreaker.forceClosed屬性和circuitBreaker.forceOpen屬性互斥。

默認值 false
可選值 falsetrue
默認全局配置 hystrix.command.default.circuitBreaker.forceClosed
實例配置 hystrix.command.[HystrixCommandKey].circuitBreaker.forceClosed
建議(筆者備注) 建議保持默認值

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withCircuitBreakerForceClosed(true)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.command.default.circuitBreaker.forceClosed=true

# 實例配置
hystrix.command.CustomCommand.circuitBreaker.forceClosed=true

度量統計(metrics)配置

度量統計配置會對HystrixCommand或者HystrixObservableCommand執行時候的統計數據收集動作生效。

滑動窗口持續時間

  • metrics.rollingStats.timeInMilliseconds
默認值 10000
可選值 -
默認全局配置 hystrix.command.default.metrics.rollingStats.timeInMilliseconds
實例配置 hystrix.command.[HystrixCommandKey].metrics.rollingStats.timeInMilliseconds
建議(筆者備注) 建議保持默認值

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withMetricsRollingStatisticalWindowInMilliseconds(10000)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.command.default.metrics.rollingStats.timeInMilliseconds=10000

# 實例配置
hystrix.command.CustomCommand.metrics.rollingStats.timeInMilliseconds=10000

滑動窗口Bucket總數

  • metrics.rollingStats.numBuckets
默認值 10
可選值 需要滿足metrics.rollingStats.timeInMilliseconds % metrics.rollingStats.numBuckets == 0,要盡量小,否則有可能影響性能
默認全局配置 hystrix.command.default.metrics.rollingStats.numBuckets
實例配置 hystrix.command.[HystrixCommandKey].metrics.rollingStats.numBuckets
建議(筆者備注) 建議保持默認值

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withMetricsRollingStatisticalWindowBuckets(100)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.command.default.metrics.rollingStats.numBuckets=10

# 實例配置
hystrix.command.CustomCommand.metrics.rollingStats.numBuckets=10

是否啟用百分數計算

  • metrics.rollingPercentile.enabled
默認值 true
可選值 truefalse
默認全局配置 hystrix.command.default.metrics.rollingPercentile.enabled
實例配置 hystrix.command.[HystrixCommandKey].metrics.rollingPercentile.enabled
建議(筆者備注) 建議保持默認值

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withMetricsRollingPercentileEnabled(true)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.command.default.metrics.rollingPercentile.enabled=true

# 實例配置
hystrix.command.CustomCommand.metrics.rollingPercentile.enabled=true

百分數計算使用的滑動窗口持續時間

  • metrics.rollingPercentile.timeInMilliseconds
默認值 60000
可選值 -
默認全局配置 hystrix.command.default.metrics.rollingPercentile.timeInMilliseconds
實例配置 hystrix.command.[HystrixCommandKey].metrics.rollingPercentile.timeInMilliseconds
建議(筆者備注) 建議保持默認值

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withMetricsRollingPercentileWindowInMilliseconds(60000)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.command.default.metrics.rollingPercentile.timeInMilliseconds=60000

# 實例配置
hystrix.command.CustomCommand.metrics.rollingPercentile.timeInMilliseconds=60000

百分數計算使用的Bucket總數

  • metrics.rollingPercentile.numBuckets
默認值 6
可選值 滿足metrics.rollingPercentile.timeInMilliseconds % metrics.rollingPercentile.numBuckets == 0,要盡量小,否則有可能影響性能
默認全局配置 hystrix.command.default.metrics.rollingPercentile.numBuckets
實例配置 hystrix.command.[HystrixCommandKey].metrics.rollingPercentile.numBuckets
建議(筆者備注) 建議保持默認值

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withMetricsRollingPercentileWindowBuckets(6)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.command.default.metrics.rollingPercentile.numBuckets=6

# 實例配置
hystrix.command.CustomCommand.metrics.rollingPercentile.numBuckets=6

百分數計算使用的Bucket容量

  • metrics.rollingPercentile.bucketSize
默認值 100
可選值 -
默認全局配置 hystrix.command.default.metrics.rollingPercentile.bucketSize
實例配置 hystrix.command.[HystrixCommandKey].metrics.rollingPercentile.bucketSize
建議(筆者備注) 建議保持默認值

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withMetricsRollingPercentileBucketSize(100)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.command.default.metrics.rollingPercentile.bucketSize=100

# 實例配置
hystrix.command.CustomCommand.metrics.rollingPercentile.bucketSize=100

健康狀態快照收集的周期

  • metrics.healthSnapshot.intervalInMilliseconds
默認值 500
可選值 -
默認全局配置 hystrix.command.default.metrics.healthSnapshot.intervalInMilliseconds
實例配置 hystrix.command.[HystrixCommandKey].metrics.healthSnapshot.intervalInMilliseconds
建議(筆者備注) 建議保持默認值

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withMetricsHealthSnapshotIntervalInMilliseconds(500)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.command.default.metrics.healthSnapshot.intervalInMilliseconds=500

# 實例配置
hystrix.command.CustomCommand.metrics.healthSnapshot.intervalInMilliseconds=500

請求上下文配置

請求上下文屬性主要涉及到HystrixRequestContextHystrixCommand的使用。

是否啟用請求緩存

  • requestCache.enabled
默認值 true
可選值 truefalse
默認全局配置 hystrix.command.default.requestCache.enabled
實例配置 hystrix.command.[HystrixCommandKey].requestCache.enabled
建議(筆者備注) 建議保持默認值

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withRequestCacheEnabled(true)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.command.default.requestCache.enabled=true

# 實例配置
hystrix.command.CustomCommand.requestCache.enabled=true

是否啟用請求日志

  • requestLog.enabled
默認值 true
可選值 truefalse
默認全局配置 hystrix.command.default.requestLog.enabled
實例配置 hystrix.command.[HystrixCommandKey].requestLog.enabled
建議(筆者備注) 建議保持默認值

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withRequestLogEnabled(true)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.command.default.requestLog.enabled=true

# 實例配置
hystrix.command.CustomCommand.requestLog.enabled=true

請求合成器配置

請求合成器配置主要控制HystrixCollapser的行為。

請求合成的最大批次量

  • maxRequestsInBatch
默認值 Integer.MAX_VALUE
可選值 -
默認全局配置 hystrix.collapser.default.maxRequestsInBatch
實例配置 hystrix.collapser.[HystrixCollapserKey].maxRequestsInBatch
建議(筆者備注) 建議保持默認值

編程式配置:

public class CustomHystrixCollapser extends HystrixCollapser<List<String>, String, String> {

    public CustomHystrixCollapser(Setter setter) {
        super(Setter.withCollapserKey(HystrixCollapserKey.Factory.asKey("CustomHystrixCollapser"))
                .andCollapserPropertiesDefaults(HystrixCollapserProperties.Setter()
                        .withMaxRequestsInBatch(10)));
    }

    @Override
    public String getRequestArgument() {
        return null;
    }

    @Override
    protected HystrixCommand<List<String>> createCommand(Collection<CollapsedRequest<String, String>> collapsedRequests) {
        return null;
    }

    @Override
    protected void mapResponseToRequests(List<String> batchResponse, Collection<CollapsedRequest<String, String>> collapsedRequests) {

    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.collapser.default.maxRequestsInBatch=10

# 實例配置
hystrix.collapser.CustomHystrixCollapser.maxRequestsInBatch=10

延遲執行時間

  • timerDelayInMilliseconds
默認值 10
可選值 -
默認全局配置 hystrix.collapser.default.timerDelayInMilliseconds
實例配置 hystrix.collapser.[HystrixCollapserKey].timerDelayInMilliseconds
建議(筆者備注) 建議保持默認值

編程式配置:

public class CustomHystrixCollapser extends HystrixCollapser<List<String>, String, String> {

    public CustomHystrixCollapser(Setter setter) {
        super(Setter.withCollapserKey(HystrixCollapserKey.Factory.asKey("CustomHystrixCollapser"))
                .andCollapserPropertiesDefaults(HystrixCollapserProperties.Setter()
                        .withTimerDelayInMilliseconds(10)));
    }

    @Override
    public String getRequestArgument() {
        return null;
    }

    @Override
    protected HystrixCommand<List<String>> createCommand(Collection<CollapsedRequest<String, String>> collapsedRequests) {
        return null;
    }

    @Override
    protected void mapResponseToRequests(List<String> batchResponse, Collection<CollapsedRequest<String, String>> collapsedRequests) {

    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.collapser.default.timerDelayInMilliseconds=10

# 實例配置
hystrix.collapser.CustomHystrixCollapser.timerDelayInMilliseconds=10

是否啟用請求合成緩存

  • requestCache.enabled
默認值 true
可選值 truefalse
默認全局配置 hystrix.collapser.default.requestCache.enabled
實例配置 hystrix.collapser.[HystrixCollapserKey].requestCache.enabled
建議(筆者備注) 建議保持默認值

編程式配置:

public class CustomHystrixCollapser extends HystrixCollapser<List<String>, String, String> {

    public CustomHystrixCollapser(Setter setter) {
        super(Setter.withCollapserKey(HystrixCollapserKey.Factory.asKey("CustomHystrixCollapser"))
                .andCollapserPropertiesDefaults(HystrixCollapserProperties.Setter()
                        .withTimerDelayInMilliseconds(10)));
    }

    @Override
    public String getRequestArgument() {
        return null;
    }

    @Override
    protected HystrixCommand<List<String>> createCommand(Collection<CollapsedRequest<String, String>> collapsedRequests) {
        return null;
    }

    @Override
    protected void mapResponseToRequests(List<String> batchResponse, Collection<CollapsedRequest<String, String>> collapsedRequests) {

    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.collapser.default.requestCache.enabled=true

# 實例配置
hystrix.collapser.CustomHystrixCollapser.requestCache.enabled=true

線程池配置

Hystrix使用的是JUC線程池ThreadPoolExecutor,線程池相關配置直接影響ThreadPoolExecutor實例。Hystrix的命令執行選用了線程池策略,那么就是通過線程池隔離執行的,最好為每一個分組設立獨立的線程池。筆者在生產實踐的時候,一般把HystrixCommandGroupKeyHystrixThreadPoolKey設置為一致。

核心線程數

  • coreSize
默認值 10
可選值 -
默認全局配置 hystrix.threadpool.default.coreSize
實例配置 hystrix.threadpool.[HystrixThreadPoolKey].coreSize
建議(筆者備注) 根據真實情況自行配置和調整

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
                        .withCoreSize(10)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.threadpool.default.coreSize=10

# 實例配置
hystrix.threadpool.CustomCommand.coreSize=10

最大線程數

  • maximumSize

此屬性只有在allowMaximumSizeToDivergeFromCoreSizetrue的時候才生效。

默認值 10
可選值 -
默認全局配置 hystrix.threadpool.default.maximumSize
實例配置 hystrix.threadpool.[HystrixThreadPoolKey].maximumSize
建議(筆者備注) 根據真實情況自行配置和調整

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
                        .withMaximumSize(10)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.threadpool.default.maximumSize=10

# 實例配置
hystrix.threadpool.CustomCommand.maximumSize=10

最大任務隊列容量

  • maxQueueSize

此屬性配置為-1時使用的是SynchronousQueue,配置為大於1的整數時使用的是LinkedBlockingQueue

默認值 -1
可選值 -1或者大於0的整數
默認全局配置 hystrix.threadpool.default.maxQueueSize
實例配置 hystrix.threadpool.[HystrixThreadPoolKey].maxQueueSize
建議(筆者備注) 根據真實情況自行配置和調整

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
                        .withMaxQueueSize(-1)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.threadpool.default.maxQueueSize=-1

# 實例配置
hystrix.threadpool.CustomCommand.maxQueueSize=-1

任務拒絕的任務隊列閾值

  • queueSizeRejectionThreshold

maxQueueSize配置為-1的時候,此配置項不生效。

默認值 5
可選值 大於0的整數
默認全局配置 hystrix.threadpool.default.queueSizeRejectionThreshold
實例配置 hystrix.threadpool.[HystrixThreadPoolKey].queueSizeRejectionThreshold
建議(筆者備注) 根據真實情況自行配置和調整

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
                        .withQueueSizeRejectionThreshold(5)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.threadpool.default.queueSizeRejectionThreshold=5

# 實例配置
hystrix.threadpool.CustomCommand.queueSizeRejectionThreshold=5

非核心線程存活時間

  • keepAliveTimeMinutes

allowMaximumSizeToDivergeFromCoreSizetrue並且maximumSize大於coreSize時此配置才生效。

默認值 1
可選值 大於0的整數
默認全局配置 hystrix.threadpool.default.keepAliveTimeMinutes
實例配置 hystrix.threadpool.[HystrixThreadPoolKey].keepAliveTimeMinutes
建議(筆者備注) 根據真實情況自行配置和調整

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
                        .withKeepAliveTimeMinutes(1)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.threadpool.default.keepAliveTimeMinutes=1

# 實例配置
hystrix.threadpool.CustomCommand.keepAliveTimeMinutes=1

是否允許最大線程數生效

  • allowMaximumSizeToDivergeFromCoreSize
默認值 false
可選值 truefalse
默認全局配置 hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize
實例配置 hystrix.threadpool.[HystrixThreadPoolKey].allowMaximumSizeToDivergeFromCoreSize
建議(筆者備注) 根據真實情況自行配置和調整

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
                        .withAllowMaximumSizeToDivergeFromCoreSize(true)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize=true

# 實例配置
hystrix.threadpool.CustomCommand.allowMaximumSizeToDivergeFromCoreSize=true

線程池滑動窗口持續時間

  • metrics.rollingStats.timeInMilliseconds
默認值 10000
可選值 -
默認全局配置 hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds
實例配置 hystrix.threadpool.[HystrixThreadPoolKey].metrics.rollingStats.timeInMilliseconds
建議(筆者備注) 建議使用默認值

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
                        .withMetricsRollingStatisticalWindowInMilliseconds(10000)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds=10000

# 實例配置
hystrix.threadpool.CustomCommand.metrics.rollingStats.timeInMilliseconds=10000

線程池滑動窗口Bucket總數

  • metrics.rollingStats.numBuckets
默認值 10
可選值 滿足metrics.rollingStats.timeInMilliseconds % metrics.rollingStats.numBuckets == 0,值要盡量少,否則會影響性能
默認全局配置 hystrix.threadpool.default.metrics.rollingStats.numBuckets
實例配置 hystrix.threadpool.[HystrixThreadPoolKey].metrics.rollingStats.numBuckets
建議(筆者備注) 建議使用默認值

編程式配置:

public class CustomCommand extends HystrixCommand<String> {

    public CustomCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand"))
                .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
                        .withMetricsRollingStatisticalWindowBuckets(10)));
    }

    @Override
    protected String run() throws Exception {
        return null;
    }
}

配置文件中(Properties)配置:

# 下面配置二選一

# 默認全局配置
hystrix.threadpool.default.metrics.rollingStats.numBuckets=10

# 實例配置
hystrix.threadpool.CustomCommand.metrics.rollingStats.numBuckets=10

原文鏈接

(本文完 e-a-201890602 1:00 AM c-3-d)

技術公眾號(《Throwable文摘》),不定期推送筆者原創技術文章(絕不抄襲或者轉載):

娛樂公眾號(《天天沙雕》),甄選奇趣沙雕圖文和視頻不定期推送,緩解生活工作壓力:


免責聲明!

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



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