Hystrix配置屬性詳解
Hystrix可以配置屬性的有以下類型:
- Execution:控制HystrixCommand.run() 的如何執行
- Fallback: 控制HystrixCommand.getFallback() 如何執行
- Circuit Breaker: 控制斷路器的行為
- Metrics: 捕獲和HystrixCommand 和 HystrixObservableCommand 執行信息相關的配置屬性
- Request Context:設置請求上下文的屬性
- Collapser Properties:設置請求合並的屬性
- Thread Pool Properties:設置線程池的屬性
Execution
以下屬性控制HystrixCommand.run() 的如何執行
1. execution.isolation.strategy
表示HystrixCommand.run()的執行時的隔離策略,有以下兩種策略
- 1 THREAD: 在單獨的線程上執行,並發請求受線程池中的線程數限制
- 2 SEMAPHORE: 在調用線程上執行,並發請求量受信號量計數限制
在默認情況下,推薦HystrixCommands 使用 thread 隔離策略,HystrixObservableCommand 使用 semaphore 隔離策略。
只有在高並發(單個實例每秒達到幾百個調用)的調用時,才需要修改HystrixCommands 的隔離策略為semaphore 。semaphore 隔離策略通常只用於非網絡調用
默認值:THREAD,
// 設置所有實例的默認值
hystrix.command.default.execution.isolation.strategy=.. // 設置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.execution.isolation.strategy=...
2. execution.isolation.thread.timeoutInMilliseconds
設置調用者執行的超時時間(單位毫秒)
默認值:1000
// 設置所有實例的默認值
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=... // 設置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds=...
3.execution.isolation.thread.interruptOnTimeout
表示設置是否在執行超時時,中斷HystrixCommand.run() 的執行
默認值:true
// 設置所有實例的默認值
hystrix.command.default.execution.isolation.thread.interruptOnTimeout=... // 設置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.execution.isolation.thread.interruptOnTimeout=...
4.execution.isolation.thread.interruptOnCancel
表示設置是否在取消任務執行時,中斷HystrixCommand.run() 的執行
默認值:false
// 設置所有實例的默認值
hystrix.command.default.execution.isolation.thread.interruptOnCancel=... // 設置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.execution.isolation.thread.interruptOnCancel
5.execution.isolation.semaphore.maxConcurrentRequests
當HystrixCommand.run()使用SEMAPHORE的隔離策略時,設置最大的並發量
默認值:10
// 設置所有實例的默認值
hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests=... // 設置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.execution.isolation.semaphore.maxConcurrentRequests=...
Fallback
以下屬性控制HystrixCommand.getFallback() 如何執行。這些屬性對隔離策略THREAD 和SEMAPHORE都起作用.
1. fallback.isolation.semaphore.maxConcurrentRequests
此屬性設置從調用線程允許HystrixCommand.getFallback()方法允許的最大並發請求數
如果達到最大的並發量,則接下來的請求會被拒絕並且拋出異常.
默認值:10
// 設置所有實例的默認值
hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests=... // 設置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.fallback.isolation.semaphore.maxConcurrentRequests=...
2. fallback.enabled
是否開啟fallback功能
默認值:true
// 設置所有實例的默認值
hystrix.command.default.fallback.enabled=... // 設置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.fallback.enabled=...
Circuit Breaker
控制斷路器的行為
1. circuitBreaker.enabled
是否開啟斷路器功能
默認值:true
// 設置所有實例的默認值
hystrix.command.default.circuitBreaker.enabled=... // 設置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.circuitBreaker.enabled=...
2. circuitBreaker.requestVolumeThreshold
該屬性設置滾動窗口中將使斷路器跳閘的最小請求數量
如果此屬性值為20,則在窗口時間內(如10s內),如果只收到19個請求且都失敗了,則斷路器也不會開啟。
默認值:20
// 設置所有實例的默認值
hystrix.command.default.circuitBreaker.requestVolumeThreshold=... // 設置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.circuitBreaker.requestVolumeThreshold=...
4. circuitBreaker.errorThresholdPercentage
設置失敗百分比的閾值。如果失敗比率超過這個值,則斷路器跳閘並且進入fallback邏輯
默認值:50
// 設置所有實例的默認值
hystrix.command.default.circuitBreaker=... // 設置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.circuitBreaker.errorThresholdPercentage=...
5. circuitBreaker.forceOpen
如果設置true,則強制使斷路器跳閘,則會拒絕所有的請求.此值會覆蓋circuitBreaker.forceClosed的值
默認值:false
// 設置所有實例的默認值
hystrix.command.default.circuitBreaker.forceOpen=... // 設置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen=...
6. circuitBreaker.forceClosed
如果設置true,則強制使斷路器進行關閉狀態,此時會允許執行所有請求,無論是否失敗的次數達到circuitBreaker.errorThresholdPercentage值
默認值:false
// 設置所有實例的默認值
hystrix.command.default.circuitBreaker.forceClosed=... // 設置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.circuitBreaker.forceClosed=...
Metrics
捕獲和HystrixCommand 和 HystrixObservableCommand 執行信息相關的配置屬性
1. metrics.rollingStats.timeInMilliseconds
設置統計滾動窗口的時間長度
如果此值為10s,將窗口分成10個桶,每個桶表示1s時間,則統計信息如下圖:
默認值: 10000
// 設置所有實例的默認值
hystrix.command.default.metrics.rollingStats.timeInMilliseconds=.... // 設置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.metrics.rollingStats.timeInMilliseconds=...
2. metrics.rollingStats.numBuckets
設置統計滾動窗口的桶數量,
注意:以下配置必須成立,否則會拋出異常。
metrics.rollingStats.timeInMilliseconds % metrics.rollingStats.numBuckets == 0
如:10000/10、10000/20是正確的配置,但是10000/7錯誤的
在高並發的環境里,每個桶的時間長度建議大於100ms
默認值:10
// 設置所有實例的默認值
hystrix.command.default.metrics.rollingStats.numBuckets=... // 設置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.metrics.rollingStats.numBuckets=...
3. metrics.rollingPercentile.enabled
設置執行延遲是否被跟蹤,並且被計算在失敗百分比中。如果設置為false,則所有的統計數據返回-1
默認值: true
// 設置所有實例的默認值
hystrix.command.default.metrics.rollingPercentile.enabled=... // 設置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.metrics.rollingPercentile.enabled=...
4. metrics.rollingPercentile.timeInMilliseconds
此屬性設置統計滾動百分比窗口的持續時間
默認值:60000
// 設置所有實例的默認值
hystrix.command.default.metrics.rollingPercentile.timeInMilliseconds=... // 設置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.metrics.rollingPercentile.timeInMilliseconds=...
5. metrics.rollingPercentile.numBuckets
設置統計滾動百分比窗口的桶數量
注意:以下配置必須成立,否則會拋出異常。
metrics.rollingPercentile.timeInMilliseconds % metrics.rollingPercentile.numBuckets == 0
如: 60000/6、60000/60是正確的配置,但是10000/7錯誤的
在高並發的環境里,每個桶的時間長度建議大於1000ms
默認值:6
// 設置所有實例的默認值
hystrix.command.default.metrics.rollingPercentile.numBuckets=... // 設置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.metrics.rollingPercentile.numBuckets=...
6. metrics.rollingPercentile.bucketSize
此屬性設置每個桶保存的執行時間的最大值。如果桶數量是100,統計窗口為10s,如果這10s里有500次執行,只有最后100次執行會被統計到bucket里去
默認值:100
// 設置所有實例的默認值
hystrix.command.default.metrics.rollingPercentile.bucketSize=... // 設置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.metrics.rollingPercentile.bucketSize=...
7. metrics.healthSnapshot.intervalInMilliseconds
采樣時間間隔
默認值:500
// 設置所有實例的默認值
hystrix.command.default.metrics.healthSnapshot.intervalInMilliseconds=... // 設置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.metrics.healthSnapshot.intervalInMilliseconds=...
Request Context
此屬性控制HystrixCommand使用到的Hystrix的上下文
1. requestCache.enabled
是否開啟請求緩存功能
默認值:true
// 設置所有實例的默認值
hystrix.command.default.requestCache.enabled=... // 設置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.requestCache.enabled=...
2. requestLog.enabled
表示是否開啟日志,打印執行HystrixCommand的情況和事件
默認值:true
// 設置所有實例的默認值
hystrix.command.default.requestLog.enabled=... // 設置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.requestLog.enabled=...
Collapser Properties
設置請求合並的屬性
1. maxRequestsInBatch
設置同時批量執行的請求的最大數量
默認值:Integer.MAX_VALUE // 設置所有實例的默認值
hystrix.collapser.default.maxRequestsInBatch=... // 設置實例HystrixCommandKey的此屬性值
hystrix.collapser.HystrixCollapserKey.maxRequestsInBatch=...
2. timerDelayInMilliseconds
批量執行創建多久之后,再觸發真正的請求
默認值:10
// 設置所有實例的默認值
hystrix.collapser.default.timerDelayInMilliseconds=... // 設置實例HystrixCommandKey的此屬性值
hystrix.collapser.HystrixCollapserKey.timerDelayInMilliseconds=...
3. requestCache.enabled
是否對HystrixCollapser.execute() 和 HystrixCollapser.queue()開啟請求緩存
默認值:true
// 設置所有實例的默認值
hystrix.collapser.default.requestCache.enabled=... // 設置實例HystrixCommandKey的此屬性值
hystrix.collapser.HystrixCollapserKey.requestCache.enabled=...
Thread Pool Properties
設置Hystrix Commands的線程池行為,大部分情況線程數量是10。
線程池數量的計算公式如下:
最高峰時每秒的請求數量 × 99%命令執行時間 + 喘息空間
設置線程池數量的主要原則是保持線程池越小越好,因為它是減輕負載並防止資源在延遲發生時被阻塞的主要工具
1. coreSize
設置線程池的core的大小
默認值:10
// 設置所有實例的默認值
hystrix.threadpool.default.coreSize=... // 設置實例HystrixCommandKey的此屬性值
hystrix.threadpool.HystrixThreadPoolKey.coreSize=...
2. maximumSize
設置最大的線程池的大小,只有設置allowMaximumSizeToDivergeFromCoreSize時,此值才起作用
默認值:10
// 設置所有實例的默認值
hystrix.threadpool.default.maximumSize=... // 設置實例HystrixCommandKey的此屬性值
hystrix.threadpool.HystrixThreadPoolKey.maximumSize=...
3. maxQueueSize
設置最大的BlockingQueue隊列的值。如果設置-1,則使用SynchronousQueue隊列,如果設置正數,則使用LinkedBlockingQueue隊列
默認值:-1
// 設置所有實例的默認值
hystrix.threadpool.default.maxQueueSize=... // 設置實例HystrixCommandKey的此屬性值
hystrix.threadpool.HystrixThreadPoolKey.maxQueueSize=...
4. queueSizeRejectionThreshold
因為maxQueueSize值不能被動態修改,所有通過設置此值可以實現動態修改等待隊列長度。即等待的隊列的數量大於queueSizeRejectionThreshold時(但是沒有達到maxQueueSize值),則開始拒絕后續的請求進入隊列。
如果設置-1,則屬性不啟作用
默認值:5
// 設置所有實例的默認值
hystrix.threadpool.default.queueSizeRejectionThreshold=... // 設置實例HystrixCommandKey的此屬性值
hystrix.threadpool.HystrixThreadPoolKey.queueSizeRejectionThreshold=...
5. keepAliveTimeMinutes
設置線程多久沒有服務后,需要釋放(maximumSize-coreSize )個線程
默認值:1
// 設置所有實例的默認值
hystrix.threadpool.default.keepAliveTimeMinutes=... // 設置實例HystrixCommandKey的此屬性值
hystrix.threadpool.HystrixThreadPoolKey.keepAliveTimeMinutes=...
6. allowMaximumSizeToDivergeFromCoreSize
設置allowMaximumSizeToDivergeFromCoreSize值為true時,maximumSize才有作用
默認值:false
// 設置所有實例的默認值
hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize=.... // 設置實例HystrixCommandKey的此屬性值
hystrix.threadpool.HystrixThreadPoolKey.allowMaximumSizeToDivergeFromCoreSize=...
7. metrics.rollingStats.timeInMilliseconds
設置滾動窗口的時間
默認值:10000
// 設置所有實例的默認值
hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds=true
// 設置實例HystrixCommandKey的此屬性值
hystrix.threadpool.HystrixThreadPoolKey.metrics.rollingStats.timeInMilliseconds=true
8. metrics.rollingStats.numBuckets
設置滾動靜態窗口分成的桶的數量
配置的值必須滿足如下條件:
metrics.rollingStats.timeInMilliseconds % metrics.rollingStats.numBuckets == 0
默認值:10
建議每個桶的時間長度大於100ms
// 設置所有實例的默認值
hystrix.threadpool.default.metrics.rollingStats.numBuckets=... // 設置實例HystrixCommandKey的此屬性值
hystrix.threadpool.HystrixThreadPoolProperties.metrics.rollingStats.numBuckets=...