微服务Hystrix的断路器

 Hystrix的断路器设计,是为防止其他服务故障导致服务雪崩。

Having an open circuit stops cascading failures and allows overwhelmed or failing services time to recover. The fallback can be another Hystrix protected call, static data, or a sensible empty value. Fallbacks may be chained so that the first fallback makes some other business call, which in turn falls back to static data.

 

 Hystrix的断路器 包括三个状态(close 有可用资源,open无可用资源,half-open允许某些流量通过,并关注这些流量的结果,如果出现超时、异常等情况,将进入OPEN状态,如果成功,那么将进入CLOSED状态。)

为了能做到状态能按照指定的顺序来流转,并且是线程安全的,断路器的实现类HystrixCircuitBreakerImpl使用了AtomicReference:private final AtomicReference<Status> status = new AtomicReference<Status>(Status.CLOSED);

1请求是否有缓存,有从缓存中返回

2没有缓存,进入路由判断(短路直接返回failed)

3没有短路,通过发送请求,(超时,执行失败返回failed)(通过线程池统计回路状态,返回给路由)

4返回成功通知观察者。

 

微服务Hystrix的断路器