circuit breaker:短路器,快速熔斷(一旦後端服務故障,立刻熔斷,阻止對其的訪問)
8.1 短路器的流量在一定的時間窗口內超過了一定的閾值
HystrixCommandProperties.circuitBreakerRequestVolumeThreshold()
短路器的流量在一定的時間窗口內超過了一定的閾值。舉個例子,要求在10s內,經過短路器的流量必須達到20個;在10s內,經過短路器的流量才10個,那麼根本不會去判斷要不要短路
//(2)circuitBreaker.requestVolumeThreshold:經過短路器的流量超過了一定的閾值(默認10s 20個) .withCircuitBreakerRequestVolumeThreshold(20) |
8.2 短路器統計到的異常調用的佔比超過了一定的閾值HystrixCommandProperties.circuitBreakerErrorThresholdPercentage()
如果達到了上面8.1的要求,比如說在10s內,經過短路器的流量(只要執行一個command,這個請求就一定會經過短路器),達到了30個;同時其中異常的訪問數量,佔到了一定的比例,比如說60%的請求都是異常(報錯,timeout,reject),會開啓短路
//(3)circuitBreaker.errorThresholdPercentage:短路器統計到的異常調用的佔比超過了一定的閾值(50) .withCircuitBreakerErrorThresholdPercentage(50) |
8.3 短路器從close狀態轉換到open狀態
8.4 短路器打開不調用後端服務,直接走fallback降級
短路器打開的時候,所有經過該短路器的請求全部被短路,不調用後端服務,直接走fallback降級
8.5 一段時間後半開、一條經過、自動恢復、到Close狀態
HystrixCommandProperties.circuitBreakerSleepWindowInMilliseconds()
經過了一段時間之後,HystrixCommandProperties.circuitBreakerSleepWindowInMilliseconds(),會half-open,讓一條請求經過短路器,看能不能正常調用。如果調用成功了,那麼就自動恢復,轉到close狀態
短路器,會自動恢復的,half-open,半開狀態
//(4)circuitBreaker.sleepWindowInMilliseconds:短路器,會自動恢復的,half-open,半開狀態,默認5s .withCircuitBreakerSleepWindowInMilliseconds(5000) |
8.6 circuit breaker短路器的配置
(1)circuitBreaker.enabled
控制短路器是否允許工作,包括跟蹤依賴服務調用的健康狀況,以及對異常情況過多時是否允許觸發短路,默認是true
HystrixCommandProperties.Setter()
.withCircuitBreakerEnabled(boolean value)
//(1)circuitBreaker.enabled:控制短路器是否允許工作 .withCircuitBreakerEnabled(true) |
(2)circuitBreaker.requestVolumeThreshold
設置一個rolling window,滑動窗口中,最少要有多少個請求時,才觸發開啓短路
舉例來說,如果設置爲20(默認值),那麼在一個10秒的滑動窗口內,如果只有19個請求,即使這19個請求都是異常的,也是不會觸發開啓短路器的
HystrixCommandProperties.Setter()
.withCircuitBreakerRequestVolumeThreshold(int value)
//(2)circuitBreaker.requestVolumeThreshold:經過短路器的流量超過了一定的閾值(默認10s 20個) .withCircuitBreakerRequestVolumeThreshold(20) |
(3)circuitBreaker.sleepWindowInMilliseconds
設置在短路之後,需要在多長時間內直接reject請求,然後在這段時間之後,再重新導holf-open狀態,嘗試允許請求通過以及自動恢復,默認值是5000毫秒
HystrixCommandProperties.Setter()
.withCircuitBreakerSleepWindowInMilliseconds(int value)
//(3)circuitBreaker.sleepWindowInMilliseconds:短路器,會自動恢復的,half-open,半開狀態,默認5s .withCircuitBreakerSleepWindowInMilliseconds(5000) |
(4)circuitBreaker.errorThresholdPercentage
設置異常請求量的百分比,當異常請求達到這個百分比時,就觸發打開短路器,默認是50,也就是50%
HystrixCommandProperties.Setter()
.withCircuitBreakerErrorThresholdPercentage(int value)
//(4)circuitBreaker.errorThresholdPercentage:短路器統計到的異常調用的佔比超過了一定的閾值(50) .withCircuitBreakerErrorThresholdPercentage(50) |
(5)circuitBreaker.forceOpen(不推薦生產環境使用)
如果設置爲true的話,直接強迫打開短路器,相當於是手動短路了,手動降級,默認false
HystrixCommandProperties.Setter()
.withCircuitBreakerForceOpen(boolean value)
//(5)circuitBreaker.forceClosed:強迫關閉短路器 //.withCircuitBreakerForceClosed(true) |
(6)circuitBreaker.forceClosed
如果設置爲ture的話,直接強迫關閉短路器,相當於是手動停止短路了,手動升級,默認false
HystrixCommandProperties.Setter()
.withCircuitBreakerForceClosed(boolean value)
//(6)circuitBreaker.forceOpen:直接強迫打開短路器 //.withCircuitBreakerForceOpen(true) |