Hystrix配置簡單說明(官方文件簡譯)
詳細資訊,見官方文件。
Hystrix屬性的4中優先順序
1. 內建全域性預設值(Global default from code)
如果下面3種都沒有設定,預設是使用此種,後面用“預設值”代指這種。
2. 動態全域性預設屬性(Dynamic global default property)
可以通過屬性配置來更改全域性預設值,後面用“預設屬性”代指這種。
3. 內建例項預設值(Instance default from code)
在程式碼中,設定的屬性值,後面用“例項預設”來代指這種。
4. 動態配置例項屬性(Dynamic instance property)
可以針對特定的例項,動態配置屬性值,來代替前面三種,後面用“例項屬性”來代指這種。
優先順序:1 < 2 < 3 < 4
命令屬性
執行
execution.isolation.strategy
設定HystrixCommand.run()的隔離策略,有兩種選項:
THREAD —— 在固定大小執行緒池中,以單獨執行緒執行,併發請求數受限於執行緒池大小。
SEMAPHORE —— 在呼叫執行緒中執行,通過訊號量來限制併發量。
預設值:THREAD(ExecutionIsolationStrategy.THREAD)
可選值:THREAD,SEMAPHORE
預設屬性:hystrix.command.default.execution.isolation.strategy
例項屬性:
hystrix.command.HystrixCommandKey.execution.isolation.strategy
例項預設的設定:
// to use thread isolation
HystrixCommandProperties.Setter()
.withExecutionIsolationStrategy(ExecutionIsolationStrategy.THREAD)
// to use semaphore isolation
HystrixCommandProperties.Setter()
.withExecutionIsolationStrategy(ExecutionIsolationStrategy.SEMAPHORE)
execution.isolation.thread.timeoutInMilliseconds
設定呼叫者等待命令執行的超時限制,超過此時間,HystrixCommand被標記為TIMEOUT,並執行回退邏輯。
注意:超時會作用在HystrixCommand.queue(),即使呼叫者沒有呼叫get()去獲得Future物件。
預設值:1000(毫秒)
預設屬性:hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds
例項屬性:hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds
例項預設的設定:HystrixCommandProperties.Setter()
.withExecutionTimeoutInMilliseconds(int value)
execution.timeout.enabled
設定HystrixCommand.run()的執行是否有超時限制。
預設值:true
預設屬性:hystrix.command.default.execution.timeout.enabled
例項屬性:hystrix.command.HystrixCommandKey.execution.timeout.enabled
例項預設的設定:
HystrixCommandProperties.Setter()
.withExecutionTimeoutEnabled(boolean value)
execution.isolation.thread.interruptOnTimeout
設定HystrixCommand.run()的執行是否在超時發生時被中斷。
預設值:true
預設屬性:hystrix.command.default.execution.isolation.thread.interruptOnTimeout
例項屬性:hystrix.command.HystrixCommandKey.execution.isolation.thread.interruptOnTimeout
例項預設的設定:
HystrixCommandProperties.Setter()
.withExecutionIsolationThreadInterruptOnTimeout(boolean value)
execution.isolation.thread.interruptOnCancel
設定HystrixCommand.run()的執行但取消動作發生時候可以響應中斷。
預設值:false
預設屬性:hystrix.command.default.execution.isolation.thread.interruptOnCancel
例項屬性:hystrix.command.HystrixCommandKey.execution.isolation.thread.interruptOnCancel
例項預設的設定:
HystrixCommandProperties.Setter()
.withExecutionIsolationThreadInterruptOnCancel(boolean value)
execution.isolation.semaphore.maxConcurrentRequests
設定當使用ExecutionIsolationStrategy.SEMAPHORE時,HystrixCommand.run()方法允許的最大請求數。如果達到最大併發數時,後續請求會被拒絕。
訊號量應該是容器(比如Tomcat)執行緒池一小部分,不能等於或者略小於容器執行緒池大小,否則起不到保護作用。
預設值:10
預設屬性:hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests
例項屬性:hystrix.command.HystrixCommandKey.execution.isolation.semaphore.maxConcurrentRequests
例項預設的設定:
HystrixCommandProperties.Setter()
.withExecutionIsolationSemaphoreMaxConcurrentRequests(int value)
回退
下面的屬性控制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
例項預設:
HystrixCommandProperties.Setter()
.withFallbackIsolationSemaphoreMaxConcurrentRequests(int value)
fallback.enabled
該屬性決定當故障或者拒絕發生時,一個呼叫將會去嘗試HystrixCommand.getFallback()。
預設值:true
預設屬性:hystrix.command.default.fallback.enabled
例項屬性:hystrix.command.HystrixCommandKey.fallback.enabled
例項預設的設定:HystrixCommandProperties.Setter()
.withFallbackEnabled(boolean value)
斷路器(Circuit Breaker)
circuitBreaker.enabled
設定斷路器是否起作用。
預設值:true
預設屬性:hystrix.command.default.circuitBreaker.enabled
例項屬性:hystrix.command.HystrixCommandKey.circuitBreaker.enabled
例項預設的設定:HystrixCommandProperties.Setter()
.withCircuitBreakerEnabled(boolean value)
circuitBreaker.requestVolumeThreshold
設定在一個滾動視窗中,開啟斷路器的最少請求數。
比如:如果值是20,在一個視窗內(比如10秒),收到19個請求,即使這19個請求都失敗了,斷路器也不會開啟。
預設值:20
預設屬性:hystrix.command.default.circuitBreaker.requestVolumeThreshold
例項屬性:hystrix.command.HystrixCommandKey.circuitBreaker.requestVolumeThreshold
例項預設的設定:HystrixCommandProperties.Setter()
.withCircuitBreakerRequestVolumeThreshold(int value)
circuitBreaker.sleepWindowInMilliseconds
設定在迴路被開啟,拒絕請求到再次嘗試請求並決定迴路是否繼續開啟的時間。
預設值:5000(毫秒)
預設屬性:hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds
例項屬性:hystrix.command.HystrixCommandKey.circuitBreaker.sleepWindowInMilliseconds
例項預設的設定:
HystrixCommandProperties.Setter()
.withCircuitBreakerSleepWindowInMilliseconds(int value)
circuitBreaker.errorThresholdPercentage
設定打開回路並啟動回退邏輯的錯誤比率。
預設值:50
預設屬性:hystrix.command.default.circuitBreaker.errorThresholdPercentage
例項屬性:hystrix.command.HystrixCommandKey.circuitBreaker.errorThresholdPercentage
例項預設的設定:HystrixCommandProperties.Setter()
.withCircuitBreakerErrorThresholdPercentage(int value)
circuitBreaker.forceOpen
如果該屬性設定為true,強制斷路器進入開啟狀態,將會拒絕所有的請求。
該屬性優先順序比circuitBreaker.forceClosed高。
預設值:false
預設屬性:hystrix.command.default.circuitBreaker.forceOpen
例項屬性:hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen
例項預設的設定:HystrixCommandProperties.Setter()
.withCircuitBreakerForceOpen(boolean value)
circuitBreaker.forceClosed
如果該屬性設定為true,強制斷路器進入關閉狀態,將會允許所有的請求,無視錯誤率。
預設值:false
預設屬性:hystrix.command.default.circuitBreaker.forceClosed
例項屬性:hystrix.command.HystrixCommandKey.circuitBreaker.forceClosed
例項預設的設定:HystrixCommandProperties.Setter()
.withCircuitBreakerForceClosed(boolean value)
請求上下文
requestCache.enabled
設定HystrixCommand.getCacheKey()是否啟用,由HystrixRequestCache通過請求快取提供去重複資料功能。
預設值:true
預設屬性:hystrix.command.default.requestCache.enabled
例項屬性:hystrix.command.HystrixCommandKey.requestCache.enabled
例項預設的設定:HystrixCommandProperties.Setter()
.withRequestCacheEnabled(boolean value)
requestLog.enabled
設定HystrixCommand執行和事件是否要記錄日誌到HystrixRequestLog。
預設值:true
預設屬性:hystrix.command.default.requestLog.enabled
例項屬性:hystrix.command.HystrixCommandKey.requestLog.enabled
例項預設的設定:HystrixCommandProperties.Setter()
.withRequestLogEnabled(boolean value)
壓縮器屬性
下面的屬性可以控制HystrixCollapser行為。
maxRequestsInBatch
設定觸發批處理執行之前,在批處理中允許的最大請求數。
預設值:Integer.MAX_VALUE
預設屬性:hystrix.collapser.default.maxRequestsInBatch
例項屬性:hystrix.collapser.HystrixCollapserKey.maxRequestsInBatch
例項預設的設定:HystrixCollapserProperties.Setter()
.withMaxRequestsInBatch(int value)
timerDelayInMilliseconds
設定批處理建立到執行之間的毫秒數。
預設值:10
預設屬性:hystrix.collapser.default.timerDelayInMilliseconds
例項屬性:hystrix.collapser.HystrixCollapserKey.timerDelayInMilliseconds
例項預設的設定:HystrixCollapserProperties.Setter()
.withTimerDelayInMilliseconds(int value)
requestCache.enabled
設定請求快取是否對HystrixCollapser.execute()和HystrixCollapser.queue()的呼叫起作用。
預設值:true
預設屬性:hystrix.collapser.default.requestCache.enabled
例項屬性:hystrix.collapser.HystrixCollapserKey.requestCache.enabled
例項預設的設定:HystrixCollapserProperties.Setter()
.withRequestCacheEnabled(boolean value)
執行緒池屬性
coreSize
設定核心執行緒池大小。
預設值:10
預設屬性:hystrix.threadpool.default.coreSize
例項屬性:hystrix.threadpool.HystrixThreadPoolKey.coreSize
例項預設的設定:HystrixThreadPoolProperties.Setter()
.withCoreSize(int value)
maximumSize
1.5.9新增屬性,設定執行緒池最大值。這個是在不開始拒絕HystrixCommand的情況下支援的最大併發數。這個屬性起作用的前提是設定了allowMaximumSizeToDrivergeFromCoreSize。1.5.9之前,核心執行緒池大小和最大執行緒池大小總是相同的。
maxQueueSize
設定BlockingQueue最大的佇列值。
如果設定為-1,那麼使用SynchronousQueue,否則正數將會使用LinkedBlockingQueue。
如果需要去除這些限制,允許佇列動態變化,可以參考queueSizeRejectionThreshold屬性。
修改SynchronousQueue和LinkedBlockingQueue需要重啟。
預設值:-1
預設屬性:hystrix.threadpool.default.maxQueueSize
例項屬性:hystrix.threadpool.HystrixThreadPoolKey.maxQueueSize
例項預設的設定:HystrixThreadPoolProperties.Setter()
.withMaxQueueSize(int value)
queueSizeRejectionThreshold
設定佇列拒絕的閾值——一個人為設定的拒絕訪問的最大佇列值,即使maxQueueSize還沒有達到。
當將一個執行緒放入佇列等待執行時,HystrixCommand使用該屬性。
注意:如果maxQueueSize設定為-1,該屬性不可用。
預設值:5
預設屬性:hystrix.threadpool.default.queueSizeRejectionThreshold
例項屬性:hystrix.threadpool.HystrixThreadPoolKey.queueSizeRejectionThreshold
例項預設的設定:HystrixThreadPoolProperties.Setter()
.withQueueSizeRejectionThreshold(int value)
keepAliveTimeMinutes
設定存活時間,單位分鐘。如果coreSize小於maximumSize,那麼該屬性控制一個執行緒從實用完成到被釋放的時間。
預設值:1
預設屬性:hystrix.threadpool.default.keepAliveTimeMinutes
例項屬性:hystrix.threadpool.HystrixThreadPoolKey.keepAliveTimeMinutes
例項預設的設定:HystrixThreadPoolProperties.Setter()
.withKeepAliveTimeMinutes(int value)
allowMaximumSizeToDivergeFromCoreSize
在1.5.9中新增的屬性。該屬性允許maximumSize起作用。屬性值可以等於或者大於coreSize值,設定coreSize小於maximumSize的執行緒池能夠支援maximumSize的併發數,但是會將不活躍的執行緒返回到系統中去。(詳見KeepAliveTimeMinutes)
預設值:false
預設屬性:hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize
例項屬性:hystrix.threadpool.HystrixThreadPoolKey.allowMaximumSizeToDivergeFromCoreSize
例項預設的設定:HystrixThreadPoolProperties.Setter()
.withAllowMaximumSizeToDivergeFromCoreSize(boolean value)
metrics.rollingStats.timeInMilliseconds
設定統計的滾動視窗的時間段大小。該屬性是執行緒池保持指標時間長短。
預設值:10000(毫秒)
預設屬性:hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds
例項屬性:hystrix.threadpool.HystrixThreadPoolKey.metrics.rollingStats.timeInMilliseconds
例項預設的設定:HystrixThreadPoolProperties.Setter()
.withMetricsRollingStatisticalWindowInMilliseconds(int value)
metrics.rollingStats.numBuckets
設定滾動的統計視窗被分成的桶(bucket)的數目。
注意:”metrics.rollingStats.timeInMilliseconds % metrics.rollingStats.numBuckets == 0"必須為true,否則會丟擲異常。
預設值:10
可能的值:任何能被metrics.rollingStats.timeInMilliseconds整除的值。
預設屬性:hystrix.threadpool.default.metrics.rollingStats.numBuckets
例項屬性:hystrix.threadpool.HystrixThreadPoolProperties.metrics.rollingStats.numBuckets
例項預設的設定:HystrixThreadPoolProperties.Setter()
.withMetricsRollingStatisticalWindowBuckets(int value)