Hystrix使用小结
通过服务熔断实现服务降级
1 @HystrixCommand(fallbackMethod = "reliable", 2 commandProperties = { 3 @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "888") 4 } 5 ) 6 @RequestMapping("/echo1") 7 public String echo1(String param) throws InterruptedException { 8 if (1 == 1) { 9 Thread.sleep(880L); 10 } 11 /*if (s.hashCode() % 2 == 1) { 12 throw new IllegalArgumentException("abccc"); 13 }*/ 14 return param; 15 } 16 17 18 public String reliable(String param) { 19 log.warn("reliable fallback:{}", param); 20 return "reliable fallback:" + param; 21 }
可以独立使用,不需要绑定注册中心等其他spring cloud组件
可以用在spring mvc的controller层, 通常用在service层
fallback方法需要和当前方法的参数相同
抛出异常, 超时都会触发熔断, 但是可以配置忽略哪些异常和超时时间的大小
具体配置项@See com.netflix.hystrix.HystrixCommandProperties com.netflix.hystrix.HystrixThreadPoolProperties
circuitBreakerErrorThresholdPercentage:错误比例触发熔断
@@@build beautiful things, share happiness@@@