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) |