1. 程式人生 > >Hystrix 引數詳解及設定樣例

Hystrix 引數詳解及設定樣例

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,那麼公式是:
1000 
(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: 
  threadpool: 
    default: 
      coreSize: 500  #併發執行的最大執行緒數,預設10
  command: 
    paas-file: 
      execution: 
        isolation: 
          thread: 
            timeoutInMilliseconds: 3600000 
    default: 
      execution: 
        isolation: 
          thread: 
            timeoutInMilliseconds: 8000  #命令執行超時時間,預設1000ms