[每日一句]
也許你度過了很糟糕的一天,但這并不代表你會因此度過糟糕的一生。
[溫馨提示]
承接上一篇文章??【Hystrix技術(shù)指南】(1)基本使用和配置說明
在這里推薦給大家martinfowler的熔斷器介紹和權(quán)威指南,有興趣的小伙伴們可以研究一下哈。
主要介紹相關(guān):官網(wǎng)說明
關(guān)于 【Hystrix如何運行的介紹】的介紹
[背景介紹]
分布式系統(tǒng)的規(guī)模和復(fù)雜度不斷增加,隨著而來的是對分布式系統(tǒng)可用性的要求越來越高。在各種高可用設(shè)計模式中,【熔斷、隔離、降級、限流】是經(jīng)常被使用的。而相關(guān)的技術(shù),Hystrix本身早已算不上什么新技術(shù),但它卻是最經(jīng)典的技術(shù)體系!。
Hystrix以實現(xiàn)熔斷降級的設(shè)計,從而提高了系統(tǒng)的可用性。
Hystrix是一個在調(diào)用端上,實現(xiàn)斷路器模式,以及隔艙模式,通過避免級聯(lián)故障,提高系統(tǒng)容錯能力,從而實現(xiàn)高可用設(shè)計的一個Java服務(wù)組件庫。
Hystrix實現(xiàn)了資源隔離機制
[熔斷器狀態(tài)]
closed:closed是關(guān)閉狀態(tài),服務(wù)調(diào)用方每次請求都到服務(wù)提供方;
open:是open打開狀態(tài),意思是如果服務(wù)提供方的異常率或者是請求的并發(fā)量超過設(shè)置的閾值之后,就會開啟熔斷機制,開啟熔斷機制之后服務(wù)調(diào)用方所有的請求都不會在請求到服務(wù)提供方,而是直接使用本地的服務(wù)降級方法;
half-open:是半打開狀態(tài),服務(wù)調(diào)用方所有的請求依然會請求到服務(wù)端,hystrix也有自我恢復(fù)機制,意思是當(dāng)服務(wù)提供方的熔斷機制處于打開狀態(tài)時,會在開啟一個時間窗口,就是一定時間后或者是下一次請求的時間大于時間窗口的時間,hystrix就會重新將這次請求再次發(fā)送到服務(wù)提供方,如果成功就將狀態(tài)改為half-open狀態(tài),如果失敗就繼續(xù)處于開啟狀態(tài),并且重新刷新時間窗口的時間。
[配置介紹]
主要參考: https://github.com/Netflix/Hystrix/wiki/Configuration
上一篇文章??【Hystrix技術(shù)指南】(1)基本使用和配置說明主要是介紹一下相關(guān)的使用方式、以及相關(guān)的一些實戰(zhàn)方面的配置,本篇文章會針對于配置進行具體介紹。
Hystrix屬性的4中優(yōu)先級
- 內(nèi)置全局默認(rèn)值(Global default from code)
如果下面3種都沒有設(shè)置,默認(rèn)是使用此種,后面用“默認(rèn)值”代指這種。
- 動態(tài)全局默認(rèn)屬性(Dynamic global default property)
可以通過屬性配置來更改全局默認(rèn)值,后面用“默認(rèn)屬性”代指這種。
- 內(nèi)置實例默認(rèn)值(Instance default from code)
在代碼中,設(shè)置的屬性值,后面用“實例默認(rèn)”來代指這種。
- 動態(tài)配置實例屬性(Dynamic instance property)
可以針對特定的實例,動態(tài)配置屬性值,來代替前面三種,后面用“實例屬性”來代指這種。
優(yōu)先級:1 < 2 < 3 < 4
[命令屬性]
execution.isolation.strategy
設(shè)置HystrixCommand.run()的隔離策略,有兩種選項:
THREAD — 固定大小線程池中,以單獨線程執(zhí)行,并發(fā)請求數(shù)受限于線程池大小。
SEMAPHORE — 在調(diào)用線程中執(zhí)行,通過信號量來限制并發(fā)量。
默認(rèn)值:THREAD(ExecutionIsolationStrategy.THREAD)
可選值:THREAD,SEMAPHORE
默認(rèn)屬性:hystrix.command.default.execution.isolation.strategy
實例屬性:hystrix.command.HystrixCommandKey.execution.isolation.strategy
實例默認(rèn)的設(shè)置:
// to use thread isolation
HystrixCommandProperties.Setter()
.withExecutionIsolationStrategy(ExecutionIsolationStrategy.THREAD)
// to use semaphore isolation
HystrixCommandProperties.Setter()
.withExecutionIsolationStrategy(ExecutionIsolationStrategy.SEMAPHORE)
execution.isolation.thread.timeoutInMilliseconds
設(shè)置調(diào)用者等待命令執(zhí)行的超時限制,超過此時間,HystrixCommand被標(biāo)記為TIMEOUT,并執(zhí)行回退邏輯。
注意:超時會作用在HystrixCommand.queue(),即使調(diào)用者沒有調(diào)用get()去獲得Future對象。
默認(rèn)值:1000(毫秒)
默認(rèn)屬性: hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds
實例屬性:hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds
實例默認(rèn)的設(shè)置:
HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(int value)
execution.timeout.enabled
設(shè)置HystrixCommand.run()的執(zhí)行是否有超時限制。
- 默認(rèn)值:true
默認(rèn)屬性:hystrix.command.default.execution.timeout.enabled
實例屬性:hystrix.command.HystrixCommandKey.execution.timeout.enabled
實例默認(rèn)的設(shè)置:
HystrixCommandProperties.Setter().withExecutionTimeoutEnabled(boolean value)
execution.isolation.thread.interruptOnTimeout
設(shè)置HystrixCommand.run()的執(zhí)行是否在超時發(fā)生時被中斷。
- 默認(rèn)值:true
默認(rèn)屬性:hystrix.command.default.execution.isolation.thread.interruptOnTimeout
實例屬性:hystrix.command.HystrixCommandKey.execution.isolation.thread.interruptOnTimeout
實例默認(rèn)的設(shè)置:
HystrixCommandProperties.Setter()
.withExecutionIsolationThreadInterruptOnTimeout(boolean value)
execution.isolation.thread.interruptOnCancel
設(shè)置HystrixCommand.run()的執(zhí)行但取消動作發(fā)生時候可以響應(yīng)中斷。
- 默認(rèn)值:false
默認(rèn)屬性:hystrix.command.default.execution.isolation.thread.interruptOnCancel
實例屬性:hystrix.command.HystrixCommandKey.execution.isolation.thread.interruptOnCancel
實例默認(rèn)的設(shè)置:
HystrixCommandProperties.Setter().withExecutionIsolationThreadInterruptOnCancel(boolean value)
execution.isolation.semaphore.maxConcurrentRequests
設(shè)置當(dāng)使用ExecutionIsolationStrategy.SEMAPHORE時,HystrixCommand.run()方法允許的最大請求數(shù)。如果達到最大并發(fā)數(shù)時,后續(xù)請求會被拒絕。
信號量應(yīng)該是容器(比如Tomcat)線程池一小部分,不能等于或者略小于容器線程池大小,否則起不到保護作用。
- 默認(rèn)值:10
默認(rèn)屬性:hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests
實例屬性:hystrix.command.HystrixCommandKey.execution.isolation.semaphore.maxConcurrentRequests
實例默認(rèn)的設(shè)置:
HystrixCommandProperties.Setter()
.withExecutionIsolationSemaphoreMaxConcurrentRequests(int value)
回退方法
下面的屬性控制HystrixCommand.getFallback()執(zhí)行。這些屬性對ExecutionIsolationStrategy.THREAD和ExecutionIsolationStrategy.SEMAPHORE都有效。
fallback.isolation.semaphore.maxConcurrentRequests
設(shè)置調(diào)用線程產(chǎn)生的HystrixCommand.getFallback()方法的允許最大請求數(shù)目。如果達到最大并發(fā)數(shù)目,后續(xù)請求將會被拒絕,如果沒有實現(xiàn)回退,則拋出異常。
- 默認(rèn)值:10
默認(rèn)屬性:hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests
實例屬性:hystrix.command.HystrixCommandKey.fallback.isolation.semaphore.maxConcurrentRequests
實例默認(rèn):
HystrixCommandProperties.Setter()
.withFallbackIsolationSemaphoreMaxConcurrentRequests(int value)
fallback.enabled
該屬性決定當(dāng)故障或者拒絕發(fā)生時,一個調(diào)用將會去嘗試HystrixCommand.getFallback()。
- 默認(rèn)值:true
默認(rèn)屬性:hystrix.command.default.fallback.enabled
實例屬性:hystrix.command.HystrixCommandKey.fallback.enabled
實例默認(rèn)的設(shè)置:
HystrixCommandProperties.Setter().withFallbackEnabled(boolean value)
斷路器(Circuit Breaker)
circuitBreaker.enabled
設(shè)置斷路器是否起作用。
- 默認(rèn)值:true
默認(rèn)屬性:hystrix.command.default.circuitBreaker.enabled
實例屬性:hystrix.command.HystrixCommandKey.circuitBreaker.enabled
實例默認(rèn)的設(shè)置:
HystrixCommandProperties.Setter().withCircuitBreakerEnabled(boolean value)
circuitBreaker.requestVolumeThreshold
設(shè)置在一個滾動窗口中,打開斷路器的最少請求數(shù)。
比如:如果值是20,在一個窗口內(nèi)(比如10秒),收到19個請求,即使這19個請求都失敗了,斷路器也不會打開。
默認(rèn)值:20
默認(rèn)屬性:hystrix.command.default.circuitBreaker.requestVolumeThreshold
實例屬性:hystrix.command.HystrixCommandKey.circuitBreaker.requestVolumeThreshold
實例默認(rèn)的設(shè)置:
HystrixCommandProperties.Setter().withCircuitBreakerRequestVolumeThreshold(int value)
circuitBreaker.sleepWindowInMilliseconds
設(shè)置在回路被打開,拒絕請求到再次嘗試請求并決定回路是否繼續(xù)打開的時間。
- 默認(rèn)值:5000(毫秒)
默認(rèn)屬性:hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds
實例屬性:hystrix.command.HystrixCommandKey.circuitBreaker.sleepWindowInMilliseconds
實例默認(rèn)的設(shè)置:
HystrixCommandProperties.Setter().withCircuitBreakerSleepWindowInMilliseconds(int value)
circuitBreaker.errorThresholdPercentage
設(shè)置打開回路并啟動回退邏輯的錯誤比率。
- 默認(rèn)值:50
默認(rèn)屬性:hystrix.command.default.circuitBreaker.errorThresholdPercentage
實例屬性:hystrix.command.HystrixCommandKey.circuitBreaker.errorThresholdPercentage
實例默認(rèn)的設(shè)置:
HystrixCommandProperties.Setter().withCircuitBreakerErrorThresholdPercentage(int value)
circuitBreaker.forceOpen
如果該屬性設(shè)置為true,強制斷路器進入打開狀態(tài),將會拒絕所有的請求。
該屬性優(yōu)先級比circuitBreaker.forceClosed高。
- 默認(rèn)值:false
默認(rèn)屬性:hystrix.command.default.circuitBreaker.forceOpen
實例屬性:hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen
實例默認(rèn)的設(shè)置:
HystrixCommandProperties.Setter().withCircuitBreakerForceOpen(boolean value)
circuitBreaker.forceClosed
如果該屬性設(shè)置為true,強制斷路器進入關(guān)閉狀態(tài),將會允許所有的請求,無視錯誤率。
- 默認(rèn)值:false
默認(rèn)屬性:hystrix.command.default.circuitBreaker.forceClosed
實例屬性:hystrix.command.HystrixCommandKey.circuitBreaker.forceClosed
實例默認(rèn)的設(shè)置
HystrixCommandProperties.Setter().withCircuitBreakerForceClosed(boolean value)
請求上下文
requestCache.enabled
設(shè)置HystrixCommand.getCacheKey()是否啟用,由HystrixRequestCache通過請求緩存提供去重復(fù)數(shù)據(jù)功能。
- 默認(rèn)值:true
默認(rèn)屬性:hystrix.command.default.requestCache.enabled
實例屬性:hystrix.command.HystrixCommandKey.requestCache.enabled
實例默認(rèn)的設(shè)置:
HystrixCommandProperties.Setter().withRequestCacheEnabled(boolean value)
requestLog.enabled
設(shè)置HystrixCommand執(zhí)行和事件是否要記錄日志到HystrixRequestLog。
默認(rèn)值:true
默認(rèn)屬性:hystrix.command.default.requestLog.enabled
實例屬性:hystrix.command.HystrixCommandKey.requestLog.enabled
實例默認(rèn)的設(shè)置:
HystrixCommandProperties.Setter().withRequestLogEnabled(boolean value)
壓縮器屬性
下面的屬性可以控制HystrixCollapser行為。
maxRequestsInBatch
設(shè)置觸發(fā)批處理執(zhí)行之前,在批處理中允許的最大請求數(shù)。
- 默認(rèn)值:Integer.MAX_VALUE
默認(rèn)屬性:hystrix.collapser.default.maxRequestsInBatch
實例屬性:hystrix.collapser.HystrixCollapserKey.maxRequestsInBatch
實例默認(rèn)的設(shè)置
HystrixCollapserProperties.Setter().withMaxRequestsInBatch(int value)
timerDelayInMilliseconds
設(shè)置批處理創(chuàng)建到執(zhí)行之間的毫秒數(shù)。
- 默認(rèn)值:10
默認(rèn)屬性:hystrix.collapser.default.timerDelayInMilliseconds
實例屬性:hystrix.collapser.HystrixCollapserKey.timerDelayInMilliseconds
實例默認(rèn)的設(shè)置
HystrixCollapserProperties.Setter().withTimerDelayInMilliseconds(int value)
requestCache.enabled
設(shè)置請求緩存是否對HystrixCollapser.execute()和HystrixCollapser.queue()的調(diào)用起作用。
- 默認(rèn)值:true
默認(rèn)屬性:hystrix.collapser.default.requestCache.enabled
實例屬性:hystrix.collapser.HystrixCollapserKey.requestCache.enabled
實例默認(rèn)的設(shè)置
HystrixCollapserProperties.Setter().withRequestCacheEnabled(boolean value)
線程池屬性
coreSize
設(shè)置核心線程池大小。
- 默認(rèn)值:10
默認(rèn)屬性:hystrix.threadpool.default.coreSize
實例屬性:hystrix.threadpool.HystrixThreadPoolKey.coreSize
實例默認(rèn)的設(shè)置:
HystrixThreadPoolProperties.Setter().withCoreSize(int value)
maximumSize
1.5.9新增屬性,設(shè)置線程池最大值。這個是在不開始拒絕HystrixCommand的情況下支持的最大并發(fā)數(shù)。這個屬性起作用的前提是設(shè)置了allowMaximumSizeToDrivergeFromCoreSize。1.5.9之前,核心線程池大小和最大線程池大小總是相同的。
maxQueueSize
設(shè)置BlockingQueue最大的隊列值。
如果設(shè)置為-1,那么使用SynchronousQueue,否則正數(shù)將會使用LinkedBlockingQueue。
如果需要去除這些限制,允許隊列動態(tài)變化,可以參考queueSizeRejectionThreshold屬性。
修改SynchronousQueue和LinkedBlockingQueue需要重啟。
默認(rèn)值:-1
默認(rèn)屬性:hystrix.threadpool.default.maxQueueSize
實例屬性:hystrix.threadpool.HystrixThreadPoolKey.maxQueueSize
實例默認(rèn)的設(shè)置:
HystrixThreadPoolProperties.Setter().withMaxQueueSize(int value)
queueSizeRejectionThreshold
設(shè)置隊列拒絕的閾值——一個人為設(shè)置的拒絕訪問的最大隊列值,即使maxQueueSize還沒有達到。
當(dāng)將一個線程放入隊列等待執(zhí)行時,HystrixCommand使用該屬性。
注意:如果maxQueueSize設(shè)置為-1,該屬性不可用。
- 默認(rèn)值:5
默認(rèn)屬性:hystrix.threadpool.default.queueSizeRejectionThreshold
實例屬性:hystrix.threadpool.HystrixThreadPoolKey.queueSizeRejectionThreshold
實例默認(rèn)的設(shè)置:
HystrixThreadPoolProperties.Setter().withQueueSizeRejectionThreshold(int value)
keepAliveTimeMinutes
設(shè)置存活時間,單位分鐘。如果coreSize小于maximumSize,那么該屬性控制一個線程從實用完成到被釋放的時間。
- 默認(rèn)值:1
默認(rèn)屬性:hystrix.threadpool.default.keepAliveTimeMinutes
實例屬性:hystrix.threadpool.HystrixThreadPoolKey.keepAliveTimeMinutes
實例默認(rèn)的設(shè)置:
HystrixThreadPoolProperties.Setter().withKeepAliveTimeMinutes(int value)
allowMaximumSizeToDivergeFromCoreSize
在1.5.9中新增的屬性。該屬性允許maximumSize起作用。屬性值可以等于或者大于coreSize值,設(shè)置coreSize小于maximumSize的線程池能夠支持maximumSize的并發(fā)數(shù),但是會將不活躍的線程返回到系統(tǒng)中去。(詳見KeepAliveTimeMinutes)
默認(rèn)值:false
默認(rèn)屬性:hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize
實例屬性:hystrix.threadpool.HystrixThreadPoolKey.allowMaximumSizeToDivergeFromCoreSize
實例默認(rèn)的設(shè)置:HystrixThreadPoolProperties.Setter()
.withAllowMaximumSizeToDivergeFromCoreSize(boolean value)
metrics.rollingStats.timeInMilliseconds
設(shè)置統(tǒng)計的滾動窗口的時間段大小。該屬性是線程池保持指標(biāo)時間長短。
默認(rèn)值:10000(毫秒)
默認(rèn)屬性:hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds
實例屬性:hystrix.threadpool.HystrixThreadPoolKey.metrics.rollingStats.timeInMilliseconds
實例默認(rèn)的設(shè)置:HystrixThreadPoolProperties.Setter()
.withMetricsRollingStatisticalWindowInMilliseconds(int value)
metrics.rollingStats.numBuckets
設(shè)置滾動的統(tǒng)計窗口被分成的桶(bucket)的數(shù)目。
注意:”metrics.rollingStats.timeInMilliseconds % metrics.rollingStats.numBuckets == 0"必須為true,否則會拋出異常。
默認(rèn)值:10
可能的值:任何能被metrics.rollingStats.timeInMilliseconds整除的值。
默認(rèn)屬性:hystrix.threadpool.default.metrics.rollingStats.numBuckets
實例屬性:hystrix.threadpool.HystrixThreadPoolProperties.metrics.rollingStats.numBuckets
實例默認(rèn)的設(shè)置:HystrixThreadPoolProperties.Setter()
.withMetricsRollingStatisticalWindowBuckets(int value)