@GetMapping("/dept/{id}")
@HystrixCommand(fallbackMethod = "fallback",
commandProperties ={
@HystrixProperty(name = "circuitBreaker.enabled",value ="true"),
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60")
} )
public Dept getById(@PathVariable("id") Long id) throws IOException {
if (id<2){
System.out.println("dept----8001");
return deptService.findDeptById(id);
}
else {
/*制造异常,引起服务降级*/
throw new IOException();
}
}
- 当在一个时间窗口(sleepWindowInMilliseconds)内请求数量达到requestVolumeThreshold(10)个会进行判断熔断与否,请求中错误请求的比例到达errorThresholdPercentage(60%),
就打开熔断器,一律将请求交给降级服务,打开状态有个时间(sleepWindowInMilliseconds)来限制,过了这个时间就会进入
半熔断状态,此时允许一部分请求访问未降级服务,如果调用正常服务全都成功或者达到了一定比例
就会恢复到关闭状态,否则认为还有问题,就回到打开状态,限制时间刷新重置:
还有其他@HystrixProperty:
protected HystrixCommandProperties(HystrixCommandKey key, HystrixCommandProperties.Setter builder, String propertyPrefix) {
this.key = key;
this.circuitBreakerEnabled = getProperty(propertyPrefix, key, "circuitBreaker.enabled", builder.getCircuitBreakerEnabled(), default_circuitBreakerEnabled);
this.circuitBreakerRequestVolumeThreshold = getProperty(propertyPrefix, key, "circuitBreaker.requestVolumeThreshold", builder.getCircuitBreakerRequestVolumeThreshold(), default_circuitBreakerRequestVolumeThreshold);
this.circuitBreakerSleepWindowInMilliseconds = getProperty(propertyPrefix, key, "circuitBreaker.sleepWindowInMilliseconds", builder.getCircuitBreakerSleepWindowInMilliseconds(), default_circuitBreakerSleepWindowInMilliseconds);
this.circuitBreakerErrorThresholdPercentage = getProperty(propertyPrefix, key, "circuitBreaker.errorThresholdPercentage", builder.getCircuitBreakerErrorThresholdPercentage(), default_circuitBreakerErrorThresholdPercentage);
this.circuitBreakerForceOpen = getProperty(propertyPrefix, key, "circuitBreaker.forceOpen", builder.getCircuitBreakerForceOpen(), default_circuitBreakerForceOpen);
this.circuitBreakerForceClosed = getProperty(propertyPrefix, key, "circuitBreaker.forceClosed", builder.getCircuitBreakerForceClosed(), default_circuitBreakerForceClosed);
this.executionIsolationStrategy = getProperty(propertyPrefix, key, "execution.isolation.strategy", builder.getExecutionIsolationStrategy(), default_executionIsolationStrategy);
this.executionTimeoutInMilliseconds = getProperty(propertyPrefix, key, "execution.isolation.thread.timeoutInMilliseconds", builder.getExecutionIsolationThreadTimeoutInMilliseconds(), default_executionTimeoutInMilliseconds);
this.executionTimeoutEnabled = getProperty(propertyPrefix, key, "execution.timeout.enabled", builder.getExecutionTimeoutEnabled(), default_executionTimeoutEnabled);
this.executionIsolationThreadInterruptOnTimeout = getProperty(propertyPrefix, key, "execution.isolation.thread.interruptOnTimeout", builder.getExecutionIsolationThreadInterruptOnTimeout(), default_executionIsolationThreadInterruptOnTimeout);
this.executionIsolationThreadInterruptOnFutureCancel = getProperty(propertyPrefix, key, "execution.isolation.thread.interruptOnFutureCancel", builder.getExecutionIsolationThreadInterruptOnFutureCancel(), default_executionIsolationThreadInterruptOnFutureCancel);
this.executionIsolationSemaphoreMaxConcurrentRequests = getProperty(propertyPrefix, key, "execution.isolation.semaphore.maxConcurrentRequests", builder.getExecutionIsolationSemaphoreMaxConcurrentRequests(), default_executionIsolationSemaphoreMaxConcurrentRequests);
this.fallbackIsolationSemaphoreMax
都在这个类:
HystrixCommandProperties
- 当我快速访问了多次能引起错误的请求后,
- 然后我发起了正常请求,会变为closed:
- =======================================
还有个值得注意的参数timeoutInMilliseconds,
即超时时间:
当A调用B服务时,可能因为网络或者业务复杂所以需要一些处理时间,如下单,支付等,如果超时时间太小,那么得到的结果总是服务降级后的错误页面,所以要合理配置。
然后服务模拟耗时处理:
结果访问正常url也是降级结果:
如果是消费方调用直接异常:
接下来设置为3秒超时(默认1s,小于2s),大于sleep的2s:
结果结果正常返回,查看请求用时:
表明sleep结束才返回的,问题解决。
当我用消费服务调用时,报了个错:
feign.RetryableException: Read timed out
在消费方添加配置:
#feign超时
DEPT-PROVIDER: #服务名
ribbon:
ReadTimeout: 60000
ConnectTimeout: 60000
MaxAutoRetries: 0
MaxAutoRetriesNextServer: 1
其他属性意思参考:
http://www.imooc.com/article/76515
https://www.jianshu.com/p/138f92aa83dc