wiki
https://github.com/Netflix/Hystrix/wiki/Configuration#CommandCircuitBreaker
原理圖
官網有時候圖片需要翻牆才能加載出來
https://github.com/Netflix/Hystrix/wiki/How-it-Works
1.配置
circuitBreaker.enabled
This property determines whether a circuit breaker will be used to track health and to short-circuit requests if it trips.
是否開啓斷路器,默認時開啓的,如果設置爲false,下游服務掛掉會把自己給託死,相當於漏電了把人電死
Default Value | true |
---|---|
Default Property | hystrix.command.default.circuitBreaker.enabled |
Instance Property | hystrix.command.HystrixCommandKey.circuitBreaker.enabled |
How to Set Instance Default | HystrixCommandProperties.Setter() .withCircuitBreakerEnabled(boolean value) |
circuitBreaker.requestVolumeThreshold
This property sets the minimum number of requests in a rolling window that will trip the circuit.
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.
這裏大家很容易誤解,認爲10秒內有20個錯誤就打開斷路器,其實是錯誤的
10秒內有20個錯誤纔會去走下一步判斷是否要打開斷路器,關鍵不在20,而在下一步判斷
Default Value | 20 |
---|---|
Default Property | hystrix.command.default.circuitBreaker.requestVolumeThreshold |
Instance Property | hystrix.command.HystrixCommandKey.circuitBreaker.requestVolumeThreshold |
How to Set Instance Default | HystrixCommandProperties.Setter().withCircuitBreakerRequestVolumeThreshold(int value) |
circuitBreaker.errorThresholdPercentage
This property sets the error percentage at or above which the circuit should trip open and start short-circuiting requests to fallback logic.
這就是上一個參數的下一步判斷,當錯誤達到百分之50時,斷路器打開,錯誤包含error和timeout和拒絕
Default Value | 50 |
---|---|
Default Property | hystrix.command.default.circuitBreaker.errorThresholdPercentage |
Instance Property | hystrix.command.HystrixCommandKey.circuitBreaker.errorThresholdPercentage |
How to Set Instance Default | HystrixCommandProperties.Setter().withCircuitBreakerErrorThresholdPercentage(int value) |
circuitBreaker.sleepWindowInMilliseconds
This property sets the amount of time, after tripping the circuit, to reject requests before allowing attempts again to determine if the circuit should again be closed.
當斷路器打開了,會有一個定時任務,一定的時間去放過來一部分請求,如果成功就把斷連器關了,如果失敗reset下時間再跑一次
Default Value | 5000 |
---|---|
Default Property | hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds |
Instance Property | hystrix.command.HystrixCommandKey.circuitBreaker.sleepWindowInMilliseconds |
How to Set Instance Default | HystrixCommandProperties.Setter().withCircuitBreakerSleepWindowInMilliseconds(int value) |