服務容錯保護斷路器Hystrix之五:配置--temp https://blog.csdn.net/tongtong_use/article/details/78611225


接着《服務容錯保護斷路器Hystrix之二:Hystrix工作流程解析》中的《2.8、關於配置》再列舉重要的配置如下

一、hystrix在生產中的建議

1、保持timeout的默認值(1000ms),除非需要修改(其實通常會修改)

2、保持threadpool的的線程數為10個,除非需要更多

3、依賴標准的報警和監控系統來捕獲問題

4、通過dashboards的實時監控來動態修改配置,直到滿意為止

 

二、配置信息(default或HystrixCommandKey最常用的幾項

  • 超時時間(默認1000ms,單位:ms)
    • hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds
      • 在調用方配置,被該調用方的所有方法的超時時間都是該值,優先級低於下邊的指定配置
    • hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds
      • 在調用方配置,被該調用方的指定方法(HystrixCommandKey方法名)的超時時間是該值
  • 線程池核心線程數
    • hystrix.threadpool.default.coreSize(默認為10)
  • Queue
    • hystrix.threadpool.default.maxQueueSize(最大排隊長度。默認-1,使用SynchronousQueue。其他值則使用 LinkedBlockingQueue。如果要從-1換成其他值則需重啟,即該值不能動態調整,若要動態調整,需要使用到下邊這個配置)
    • hystrix.threadpool.default.queueSizeRejectionThreshold(排隊線程數量閾值,默認為5,達到時拒絕,如果配置了該選項,隊列的大小是該隊列)
      • 注意:如果maxQueueSize=-1的話,則該選項不起作用
  • 斷路器
    • hystrix.command.default.circuitBreaker.requestVolumeThreshold(當在配置時間窗口內達到此數量的失敗后,進行短路。默認20個)
      • For example, if the value is 20, then if only 19 requests are received in the rolling window (say a window of 10 seconds) the circuit will not trip open even if all 19 failed.
    • hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds(短路多久以后開始嘗試是否恢復,默認5s)
    • hystrix.command.default.circuitBreaker.errorThresholdPercentage(出錯百分比閾值,當達到此閾值后,開始短路。默認50%)
  • fallback
    • hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests(調用線程允許請求HystrixCommand.GetFallback()的最大數量,默認10。超出時將會有異常拋出,注意:該項配置對於THREAD隔離模式也起作用)

 spring-cloud的版本有 Brixton.RELEASE和Dalston.RELEASE,現在創建的新項目應該是引用Dalston.RELEASE這個版本,該版本下默認hystrix是關閉的,所以需要在屬性文件中進行打開,如果沒有設置為true,在后面的調試過程會發現熔斷機制不生效

#開啟hystrix熔斷機制
feign.hystrix.enabled=true

 

hystrix的超時設置:

#開啟hystrix請求超時機制   也可以設置成永久不超時
hystrix.command.default.execution.timeout.enabled=false
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=60000

 

每種配置都有4種優先級,以下為優先級從低到高的解釋

1.基於代碼的全局缺省值

2.基於properties配置表的全局配置

3.基於代碼對配置更改

4.基於代碼對配置動態更改

注:同一個配置,采用不同方法更改,那么配置的key會有不同

coreSize
設置核心線程池的大小(這個值和ThreadPoolExecutor的coreSize的含義不一樣)。

默認值:10
默認屬性:hystrix.threadpool.default.coreSize
實例屬性:hystrix.threadpool.HystrixThreadPoolKey.coreSize
實例配置:HystrixThreadPoolProperties.Setter().withCoreSize(int Value)
注解使用: @HystrixCommand(threadPoolProperties = {@HystrixProperty(name = “coreSize”,value = “10”)})
maximumSize
1.5.9新增屬性,設置線程池最大值。這個是在不開始拒絕HystrixCommand的情況下支持的最大並發數。這個屬性起作用的前提是設置了allowMaximumSizeToDrivergeFromCoreSize。1.5.9之前,核心線程池大小和最大線程池大小總是相同的。

默認值:10
默認屬性:hystrix.threadpool.default.maximumSize
實例屬性:hystrix.threadpool.HystrixThreadPoolKey.maximumSize
實例配置:HystrixThreadPoolProperties.Setter().withMaximumSize(int Value)
注解使用: @HystrixCommand(threadPoolProperties = {@HystrixProperty(name = “maximumSize”,value = “10”)})
maxQueueSize
設置BlockingQueue最大的隊列值。如果設置為-1,那么使用SynchronousQueue,否則正數將會使用LinkedBlockingQueue。如果需要去除這些限制,允許隊列動態變化,可以參考queueSizeRejectionThreshold屬性。 修改SynchronousQueue和LinkedBlockingQueue需要重啟。

默認值:-1
默認屬性:hystrix.threadpool.default.maxQueueSize
實例屬性:hystrix.threadpool.HystrixThreadPoolKey.maxQueueSize
實例配置:HystrixThreadPoolProperties.Setter().withMaxQueueSize(int Value)
注解使用: @HystrixCommand(threadPoolProperties = {@HystrixProperty(name = “maxQueueSize”,value = “10”)})
queueSizeRejectionThreshold
設置隊列拒絕的閾值—-一個人為設置的拒絕訪問的最大隊列值,即使當前隊列元素還沒達到maxQueueSize。 當將一個線程放入隊列等待執行時,HystrixCommand使用該屬性。注意:如果maxQueueSize設置為-1,該屬性不可用。

默認值:5
默認屬性:hystrix.threadpool.default.queueSizeRejectionThreshold
實例屬性:hystrix.threadpool.HystrixThreadPoolKey.queueSizeRejectionThreshold
實例默認的設置:HystrixThreadPoolProperties.Setter().withQueueSizeRejectionThreshold(int Value)
注解使用: @HystrixCommand(threadPoolProperties = {@HystrixProperty(name = “queueSizeRejectionThreshold”,value = “5”)})
keepAliveTimeMinutes
設置存活時間,單位分鍾。如果coreSize小於maximumSize,那么該屬性控制一個線程從實用完成到被釋放的時間。

默認值:1
默認屬性:hystrix.threadpool.default.keepAliveTimeMinutes
實例屬性:hystrix.threadpool.HystrixThreadPoolKey.keepAliveTimeMinutes
實例配置:HystrixThreadPoolProperties.Setter().withKeepAliveTimeMinutes(int Value)
注解使用: @HystrixCommand(threadPoolProperties = {@HystrixProperty(name = “keepAliveTimeMinutes”,value = “1”)})
allowMaximumSizeToDivergeFromCoreSize
在1.5.9中新增的屬性。該屬性允許maximumSize起作用。屬性值可以等於或者大於coreSize值,設置coreSize小於maximumSize的線程池能夠支持maximumSize的並發數,但是會將不活躍的線程返回到系統中去。(詳見KeepAliveTimeMinutes)
* 默認值:false
* 默認屬性:hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize
* 實例屬性:hystrix.threadpool.HystrixThreadPoolKey.allowMaximumSizeToDivergeFromCoreSize
* 實例配置:HystrixThreadPoolProperties.Setter().withAllowMaximumSizeToDivergeFromCoreSize(boolean Value)

(7)度量屬性配置
PS:不知道什么原因,計量屬性的配置都是放在了線程池配置里面。可能是由於線程池隔離是計量屬性隔離的基准。

metrics.rollingStats.timeInMilliseconds
設置統計的滾動窗口的時間段大小。該屬性是線程池保持指標時間長短。
* 默認值:10000(毫秒)
* 默認屬性:hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds
* 實例屬性:hystrix.threadpool.HystrixThreadPoolKey.metrics.rollingStats.timeInMilliseconds
* 實例配置:HystrixThreadPoolProperties.Setter().withMetricsRollingStatisticalWindowInMilliseconds(int Value)
* 注解使用: @HystrixCommand(threadPoolProperties = {@HystrixProperty(name = “metrics.rollingStats.timeInMilliseconds”,value = “10000”)})

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)
* 注解使用: @HystrixCommand(threadPoolProperties = {@HystrixProperty(name = “metrics.rollingStats.numBuckets”,value = “10”)})

https://github.com/Netflix/Hystrix/wiki/Configuration#coreSize

示例說明:

hystrix.command.default和hystrix.threadpool.default中的default為默認CommandKey

Command Properties
Execution相關的屬性的配置:
hystrix.command.default.execution.isolation.strategy 隔離策略,默認是Thread, 可選Thread|Semaphore

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds 命令執行超時時間,默認1000ms

hystrix.command.default.execution.timeout.enabled 執行是否啟用超時,默認啟用true
hystrix.command.default.execution.isolation.thread.interruptOnTimeout 發生超時是是否中斷,默認true
hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests 最大並發請求數,默認10,該參數當使用ExecutionIsolationStrategy.SEMAPHORE策略時才有效。如果達到最大並發請求數,請求會被拒絕。理論上選擇semaphore size的原則和選擇thread size一致,但選用semaphore時每次執行的單元要比較小且執行速度快(ms級別),否則的話應該用thread。
semaphore應該占整個容器(tomcat)的線程池的一小部分。
Fallback相關的屬性
這些參數可以應用於Hystrix的THREAD和SEMAPHORE策略

hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests 如果並發數達到該設置值,請求會被拒絕和拋出異常並且fallback不會被調用。默認10
hystrix.command.default.fallback.enabled 當執行失敗或者請求被拒絕,是否會嘗試調用hystrixCommand.getFallback() 。默認true
Circuit Breaker相關的屬性
hystrix.command.default.circuitBreaker.enabled 用來跟蹤circuit的健康性,如果未達標則讓request短路。默認true
hystrix.command.default.circuitBreaker.requestVolumeThreshold 一個rolling window內最小的請求數。如果設為20,那么當一個rolling window的時間內(比如說1個rolling window是10秒)收到19個請求,即使19個請求都失敗,也不會觸發circuit break。默認20
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds 觸發短路的時間值,當該值設為5000時,則當觸發circuit break后的5000毫秒內都會拒絕request,也就是5000毫秒后才會關閉circuit。默認5000
hystrix.command.default.circuitBreaker.errorThresholdPercentage錯誤比率閥值,如果錯誤率>=該值,circuit會被打開,並短路所有請求觸發fallback。默認50
hystrix.command.default.circuitBreaker.forceOpen 強制打開熔斷器,如果打開這個開關,那么拒絕所有request,默認false
hystrix.command.default.circuitBreaker.forceClosed 強制關閉熔斷器 如果這個開關打開,circuit將一直關閉且忽略circuitBreaker.errorThresholdPercentage
Metrics相關參數
hystrix.command.default.metrics.rollingStats.timeInMilliseconds 設置統計的時間窗口值的,毫秒值,circuit break 的打開會根據1個rolling window的統計來計算。若rolling window被設為10000毫秒,則rolling window會被分成n個buckets,每個bucket包含success,failure,timeout,rejection的次數的統計信息。默認10000
hystrix.command.default.metrics.rollingStats.numBuckets 設置一個rolling window被划分的數量,若numBuckets=10,rolling window=10000,那么一個bucket的時間即1秒。必須符合rolling window % numberBuckets == 0。默認10
hystrix.command.default.metrics.rollingPercentile.enabled 執行時是否enable指標的計算和跟蹤,默認true
hystrix.command.default.metrics.rollingPercentile.timeInMilliseconds 設置rolling percentile window的時間,默認60000
hystrix.command.default.metrics.rollingPercentile.numBuckets 設置rolling percentile window的numberBuckets。邏輯同上。默認6
hystrix.command.default.metrics.rollingPercentile.bucketSize 如果bucket size=100,window=10s,若這10s里有500次執行,只有最后100次執行會被統計到bucket里去。增加該值會增加內存開銷以及排序的開銷。默認100
hystrix.command.default.metrics.healthSnapshot.intervalInMilliseconds 記錄health 快照(用來統計成功和錯誤綠)的間隔,默認500ms
Request Context 相關參數
hystrix.command.default.requestCache.enabled 默認true,需要重載getCacheKey(),返回null時不緩存
hystrix.command.default.requestLog.enabled 記錄日志到HystrixRequestLog,默認true

Collapser Properties 相關參數
hystrix.collapser.default.maxRequestsInBatch 單次批處理的最大請求數,達到該數量觸發批處理,默認Integer.MAX_VALUE
hystrix.collapser.default.timerDelayInMilliseconds 觸發批處理的延遲,也可以為創建批處理的時間+該值,默認10
hystrix.collapser.default.requestCache.enabled 是否對HystrixCollapser.execute() and HystrixCollapser.queue()的cache,默認true

ThreadPool 相關參數
線程數默認值10適用於大部分情況(有時可以設置得更小),如果需要設置得更大,那有個基本得公式可以follow:
requests per second at peak when healthy × 99th percentile latency in seconds + some breathing room
每秒最大支撐的請求數 (99%平均響應時間 + 緩存值)
比如:每秒能處理1000個請求,99%的請求響應時間是60ms,那么公式是:
(0.060+0.012)

基本得原則時保持線程池盡可能小,他主要是為了釋放壓力,防止資源被阻塞。
當一切都是正常的時候,線程池一般僅會有1到2個線程激活來提供服務

hystrix.threadpool.default.coreSize 並發執行的最大線程數,默認10
hystrix.threadpool.default.maxQueueSize BlockingQueue的最大隊列數,當設為-1,會使用SynchronousQueue,值為正時使用LinkedBlcokingQueue。該設置只會在初始化時有效,之后不能修改threadpool的queue size,除非reinitialising thread executor。默認-1。
hystrix.threadpool.default.queueSizeRejectionThreshold 即使maxQueueSize沒有達到,達到queueSizeRejectionThreshold該值后,請求也會被拒絕。因為maxQueueSize不能被動態修改,這個參數將允許我們動態設置該值。if maxQueueSize == -1,該字段將不起作用
hystrix.threadpool.default.keepAliveTimeMinutes 如果corePoolSize和maxPoolSize設成一樣(默認實現)該設置無效。如果通過plugin(https://github.com/Netflix/Hystrix/wiki/Plugins)使用自定義實現,該設置才有用,默認1.
hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds 線程池統計指標的時間,默認10000
hystrix.threadpool.default.metrics.rollingStats.numBuckets 將rolling window划分為n個buckets,默認10

 

三、監控hystrix

說明:hystrix為每一個commandKey提供了計數器。原理:

附:清晰大圖

 

四、常見的hystrix事件類型

  • run()
    • SUCCESS:run()成功,不觸發getFallback()
    • FAILURE:run()拋異常,觸發getFallback()
    • TIMEOUT:run()超時,觸發getFallback()
    • BAD_REQUEST:run()拋出HystrixBadRequestException,不觸發getFallback()
    • SHORT_CIRCUITED:斷路器開路,觸發getFallback()
    • THREAD_POOL_REJECTED:線程池耗盡,觸發getFallback()
    • FALLBACK_MISSING:沒有實現getFallback(),拋出異常
  • getFallback()
    • FALLBACK_SUCCESS:getFallback()成功,不拋異常
    • FALLBACK_FAILURE:getFallback()失敗,拋異常
    • FALLBACK_REJECTION:調用getFallback()的線程數超量,拋異常

五、Metrics storage and Dashboard

僅僅記錄hystrix1.5.0及其后續版本,之前的版本的數據結構不一樣。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM