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)配置和請求上下文配置。java

HystrixCommandKey、HystrixCommandGroupKey和HystrixThreadPoolKey

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

HystrixCommandKey

HystrixCommandKeyHystrix命令的惟一標識,準確來講是HystrixCommand實例或者HystrixObservableCommand實例的惟一標識。它是必須的,若是不自定義配置,它會經過下面方式肯定默認值:github

[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)?文檔中給出的建議是:ide

使用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

原文連接

  • Github Page:http://throwable.club/2019/05/29/framework-hystrix-full-configuration
  • Coding Page:http://throwable.coding.me/2019/05/29/framework-hystrix-full-configuration

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

相關文章
相關標籤/搜索