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
三個KEY是HystrixCommand
的重要標識。下面分別分析一下它們的含義。git
HystrixCommandKey
是Hystrix
命令的惟一標識,準確來講是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
是用於對Hystrix
命令進行分組,分組以後便於統計展現於儀表盤、上傳報告和預警等等,也就是說,HystrixCommandGroupKey
是Hystrix
內部進行度量統計時候的分組標識,數據上報和統計的最小維度就是分組的KEY。HystrixCommandGroupKey
是必須配置的,配置方式以下:網絡
HystrixCommandGroupKey.Factory.asKey("Group Key") public Command() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("Group Key"))); }
HystrixThreadPoolKey
主要標識用於監控、度量和緩存等等做用的HystrixThreadPool
實例。一個HystrixCommand
會和一個獨立的HystrixThreadPool
實例關聯,也就是說一類HystrixCommand
老是在同一個HystrixThreadPool
實例中執行。若是不顯式配置HystrixThreadPoolKey
,那麼會使用HystrixCommandGroupKey
的值去配置HystrixThreadPoolKey
。HystrixThreadPoolKey
的配置方式以下:多線程
HystrixThreadPoolKey.Factory.asKey("ThreadPoolKey") public Command() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("xxx")) .andCommandKey(HystrixCommandKey.Factory.asKey("YYY")) .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("ThreadPoolKey"))); }
隔離策略決定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
決定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
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 |
可選值 | true 、false |
默認全局配置 | 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
此配置項決定HystrixCommand#run()
執行的時候取消調用的狀況下是否中斷。
項 | 值 |
---|---|
默認值 | false |
可選值 | true 、false |
默認全局配置 | 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
此配置項決定使用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
命令降級配置控制HystrixCommand#getFallback()
的執行邏輯,全部命令降級配置對策略ExecutionIsolationStrategy.THREAD
或者ExecutionIsolationStrategy.SEMAPHORE
都生效。
這個屬性用於控制一個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
此屬性控制當HystrixCommand
執行失敗以後是否調用HystrixCommand#getFallback()
。
項 | 值 |
---|---|
默認值 | true |
可選值 | false 、true |
默認全局配置 | 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
斷路器配置用於控制HystrixCircuitBreaker
實例的行爲。
此屬性肯定斷路器是否用於跟蹤健康情況,以及當斷路器打開的時候是否用於短路請求(使請求快速失敗進入降級邏輯)。
項 | 值 |
---|---|
默認值 | true |
可選值 | false 、true |
默認全局配置 | 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
此屬性設置將使斷路器打開的滑動窗口中的最小請求數量。
例如,若是值是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
此屬性設置斷路器打開後拒絕請求的時間量,每隔一段時間(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
此屬性設置一個錯誤百分比,當請求錯誤率超過設定值,斷路器就會打開。
項 | 值 |
---|---|
默認值 | 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
針對錯誤請求百分比。此屬性控制斷路器是否強制打開,強制打開斷路器會使全部請求直接進入降級邏輯,也就是包裹在HystrixCommand#run()
的邏輯不會執行。circuitBreaker.forceOpen
屬性和circuitBreaker.forceClosed
屬性互斥。
項 | 值 |
---|---|
默認值 | false |
可選值 | false 、true |
默認全局配置 | 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
此屬性控制斷路器是否強制關閉,強制關閉斷路器會致使全部和斷路器相關的配置和功能都失效,HystrixCommand#run()
拋出異常會正常進入降級邏輯。circuitBreaker.forceClosed
屬性和circuitBreaker.forceOpen
屬性互斥。
項 | 值 |
---|---|
默認值 | false |
可選值 | false 、true |
默認全局配置 | 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
度量統計配置會對HystrixCommand
或者HystrixObservableCommand
執行時候的統計數據收集動做生效。
項 | 值 |
---|---|
默認值 | 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
項 | 值 |
---|---|
默認值 | 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
項 | 值 |
---|---|
默認值 | true |
可選值 | true 、false |
默認全局配置 | 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
項 | 值 |
---|---|
默認值 | 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
項 | 值 |
---|---|
默認值 | 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
項 | 值 |
---|---|
默認值 | 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
項 | 值 |
---|---|
默認值 | 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
請求上下文屬性主要涉及到HystrixRequestContext
和HystrixCommand
的使用。
項 | 值 |
---|---|
默認值 | true |
可選值 | true 、false |
默認全局配置 | 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
項 | 值 |
---|---|
默認值 | true |
可選值 | true 、false |
默認全局配置 | 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
的行爲。
項 | 值 |
---|---|
默認值 | 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
項 | 值 |
---|---|
默認值 | 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
項 | 值 |
---|---|
默認值 | true |
可選值 | true 、false |
默認全局配置 | 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
的命令執行選用了線程池策略,那麼就是經過線程池隔離執行的,最好爲每個分組設立獨立的線程池。筆者在生產實踐的時候,通常把HystrixCommandGroupKey
和HystrixThreadPoolKey
設置爲一致。
項 | 值 |
---|---|
默認值 | 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
此屬性只有在allowMaximumSizeToDivergeFromCoreSize
爲true
的時候才生效。
項 | 值 |
---|---|
默認值 | 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
此屬性配置爲-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
當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
當allowMaximumSizeToDivergeFromCoreSize
爲true
而且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
項 | 值 |
---|---|
默認值 | false |
可選值 | true 、false |
默認全局配置 | 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
項 | 值 |
---|---|
默認值 | 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
項 | 值 |
---|---|
默認值 | 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)