參數配置
| 參數說明 | 值 | 備注 |
|---|---|---|
| groupKey | productStockOpLog | group標識,一個group使用一個線程池 |
| commandKey | addProductStockOpLog | command標識 |
| fallbackMethod | addProductStockOpLogFallback | fallback方法,兩者需要返回值和參數相同 |
| 超時時間設置 | 400ms | 執行策略,在THREAD模式下,達到超時時間,可以中斷 For most circuits, you should try to set their timeout values close to the 99.5th percentile of a normal healthy system so they will cut off bad requests and not let them take up system resources or affect user behavior. |
| 統計窗口(10s)內最少請求數 | 10 | 熔斷策略 |
| 熔斷多少秒后去嘗試請求 | 5s | 熔斷策略,默認值 |
| 熔斷閥值 | 10% | 熔斷策略:一個統計窗口內有10%的請求處理失敗,會觸發熔斷 |
| 線程池coreSize | 10 | 默認值(推薦值).在當前項目中,需要做依賴隔離的方法為發送一條MQ消息,發送MQ消息方法的TP99耗時在1ms以下,近2周單機QPS最高值在18左右,經過灰度驗證了午高峰后(當日QPS>上周末QPS),ActiveThreads<=2,rejected=0,經過壓測后得出結論:線程池大小為10足以應對2000QPS,前提發送MQ消息時耗時正常(該部分為實際項目中的情況,在此不做詳述) |
| 線程池maxQueueSize | -1 | 即線程池隊列為SynchronousQueue |
參數說明
其他參數可參見 https://github.com/Netflix/Hystrix/wiki/Con
| 分類 | 參數 | 作用 | 默認值 | 備注 |
|---|---|---|---|---|
| 基本參數 | groupKey | 表示所屬的group,一個group共用線程池 | getClass().getSimpleName(); | |
| 基本參數 | commandKey | 當前執行方法名 | ||
| Execution ( 控制HystrixCommand.run()的執行策略) | execution.isolation.strategy | 隔離策略,有THREAD和SEMAPHORE THREAD | 以下幾種可以使用SEMAPHORE模式: 只想控制並發度 外部的方法已經做了線程隔離 調用的是本地方法或者可靠度非常高、耗時特別小的方法(如medis) | |
| Execution | execution.isolation.thread.timeoutInMilliseconds | 超時時間 | 1000ms | 默認值:1000 在THREAD模式下,達到超時時間,可以中斷 在SEMAPHORE模式下,會等待執行完成后,再去判斷是否超時 設置標准: 有retry,99meantime+avg meantime 沒有retry,99.5meantime |
| Execution | execution.timeout.enabled | 是否打開超時 | true | |
| Execution | execution.isolation.thread.interruptOnTimeout | 是否打開超時線程中斷 | true | THREAD模式有效 |
| Execution | execution.isolation.semaphore.maxConcurrentRequests | 信號量最大並發度 | 10 | SEMAPHORE模式有效 |
| Fallback ( 設置當fallback降級發生時的策略) | fallback.isolation.semaphore.maxConcurrentRequests | fallback最大並發度 | 10 | |
| Fallback | fallback.enabled | fallback是否可用 | true | |
| Circuit Breaker (配置熔斷的策略) | circuitBreaker.enabled | 是否開啟熔斷 | true | |
| Circuit Breaker | circuitBreaker.requestVolumeThreshold | 一個統計窗口內熔斷觸發的最小個數/10s | 20 | |
| Circuit Breaker | circuitBreaker.sleepWindowInMilliseconds | 熔斷多少秒后去嘗試請求 | 5000ms | |
| Circuit Breaker | circuitBreaker.errorThresholdPercentage | 失敗率達到多少百分比后熔斷 | 50 | 主要根據依賴重要性進行調整 |
| Circuit Breaker | circuitBreaker.forceOpen | 是否強制開啟熔斷 | ||
| Circuit Breaker | circuitBreaker.forceClosed | 是否強制關閉熔斷 | 如果是強依賴,應該設置為true | |
| Metrics (設置關於HystrixCommand執行需要的統計信息) | metrics.rollingStats.timeInMilliseconds | 設置統計滾動窗口的長度,以毫秒為單位。用於監控和熔斷器。 | 10000 | 滾動窗口被分隔成桶(bucket),並且進行滾動。 例如這個屬性設置10s(10000),一個桶是1s。 |
| Metrics | metrics.rollingStats.numBuckets 設置統計窗口的桶數量 | 10 | metrics.rollingStats.timeInMilliseconds必須能被這個值整除 | |
| Metrics | metrics.rollingPercentile.enabled | 設置執行時間是否被跟蹤,並且計算各個百分比,50%,90%等的時間 | true | |
| Metrics | metrics.rollingPercentile.timeInMilliseconds | 設置執行時間在滾動窗口中保留時間,用來計算百分比 | 60000ms | |
| Metrics | metrics.rollingPercentile.numBuckets | 設置rollingPercentile窗口的桶數量 | 6 | metrics.rollingPercentile.timeInMilliseconds必須能被這個值整除 |
| Metrics | metrics.rollingPercentile.bucketSize | 此屬性設置每個桶保存的執行時間的最大值。 | 100 | 如果設置為100,但是有500次求情,則只會計算最近的100次 |
| Metrics | metrics.healthSnapshot.intervalInMilliseconds | 采樣時間間隔 | 500 | |
| Request Context ( 設置HystrixCommand使用的HystrixRequestContext相關的屬性) | requestCache.enabled | 設置是否緩存請求,request-scope內緩存 | true | |
| Request Context | requestLog.enabled | 設置HystrixCommand執行和事件是否打印到HystrixRequestLog中 | ||
| ThreadPool Properties(配置HystrixCommand使用的線程池的屬性) | coreSize | 設置線程池的core size,這是最大的並發執行數量。 | 10 | 設置標准:coreSize = requests per second at peak when healthy × 99th percentile latency in seconds + some breathing room 大多數情況下默認的10個線程都是值得建議的 |
| ThreadPool Properties | maxQueueSize | 最大隊列長度。設置BlockingQueue的最大長度 | -1 | 默認值:-1 如果使用正數,隊列將從SynchronousQueue改為LinkedBlockingQueue |
| ThreadPool Properties | queueSizeRejectionThreshold | 設置拒絕請求的臨界值 | 5 | 此屬性不適用於maxQueueSize = - 1時 設置設個值的原因是maxQueueSize值運行時不能改變,我們可以通過修改這個變量動態修改允許排隊的長度 |
| ThreadPool Properties | keepAliveTimeMinutes | 設置keep-live時間 | 1分鍾 | 這個一般用不到因為默認corePoolSize和maxPoolSize是一樣的。 |
