Hystrix 熔断机制原理

匿名 (未验证) 提交于 2019-12-02 21:53:52
circuitBreaker.enabled  是否开启熔断 circuitBreaker.requestVolumeThreshold  熔断最低触发请求数阈值 circuitBreaker.sleepWindowInMilliseconds  产生熔断后恢复窗口 circuitBreaker.errorThresholdPercentage  错误率阈值 circuitBreaker.forceOpen  强制打开熔断 circuitBreaker.forceClosed  强制关闭熔断

״̬ͼ

命令执行前调用circuitBreaker.attemptExecution(),正常情况下会执行返回true,但是如果发生熔断,则需要通过sleepWindows来进行恢复

public boolean attemptExecution() {     if (properties.circuitBreakerForceOpen().get()) {         return false;     }     if (properties.circuitBreakerForceClosed().get()) {         return true;     }     if (circuitOpened.get() == -1) {         return true;     } else {         if (isAfterSleepWindow()) {             if (status.compareAndSet(Status.OPEN, Status.HALF_OPEN)) {                 //only the first request after sleep window should execute                 return true;             } else {                 return false;             }         } else {             return false;         }     } }

在新版本1.5.12中,会有一个后台线程订阅metrics流实时计算:

  1. 如果没有达到RequestVolume,则直接返回,不计算是否需要熔断
  2. 如果当前错误率大于设置的阈值,则触发熔断,状态由CLOSED切换到OPEN,并设置当前熔断的时间(用于后续SleepWindows判断使用)

    if (hc.getTotalRequests() < properties.circuitBreakerRequestVolumeThreshold().get()) { // we are not past the minimum volume threshold for the stat window, // so no change to circuit status. // if it was CLOSED, it stays CLOSED // if it was half-open, we need to wait for a successful command execution // if it was open, we need to wait for sleep window to elapse } else { if (hc.getErrorPercentage() < properties.circuitBreakerErrorThresholdPercentage().get()) {     //we are not past the minimum error threshold for the stat window,     // so no change to circuit status.     // if it was CLOSED, it stays CLOSED     // if it was half-open, we need to wait for a successful command execution     // if it was open, we need to wait for sleep window to elapse } else {     // our failure rate is too high, we need to set the state to OPEN     if (status.compareAndSet(Status.CLOSED, Status.OPEN)) {         circuitOpened.set(System.currentTimeMillis());     } } }

// 执行成功后调用 public void markSuccess() {     if (status.compareAndSet(Status.HALF_OPEN, Status.CLOSED)) {         //This thread wins the race to close the circuit - it resets the stream to start it over from 0         metrics.resetStream();         Subscription previousSubscription = activeSubscription.get();         if (previousSubscription != null) {             previousSubscription.unsubscribe();         }         Subscription newSubscription = subscribeToStream();         activeSubscription.set(newSubscription);         circuitOpened.set(-1L);     } }
// 执行失败后调用 public void markNonSuccess() {     if (status.compareAndSet(Status.HALF_OPEN, Status.OPEN)) {         //This thread wins the race to re-open the circuit - it resets the start time for the sleep window         circuitOpened.set(System.currentTimeMillis());     } }
  1. https://github.com/Netflix/Hystrix/wiki/Configuration
  2. https://github.com/Netflix/Hystrix/wiki/How-it-Works#CircuitBreaker
  3. HystrixCircuitBreaker源码
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!