8Hystrix 断路器关闭 · SpringCloud微服务实战 · 看云

8. Hystrix 断路器关闭

导航

上面我们分析了断路器的开启,那么断路器的状态是如何变化的,怎样让断路器关闭已实现正常的系统访问。

本节代码地址


断路器有三个状态 :OPEN、 CLOSED(默认状态) 、HALF_OPEN状态,这些状态在什么情况下出现呢?

当断路器打开后,对应接口的请求会有段休眠期,这个休眠期内接口请求不会被正真的执行,但是如果休眠期时间过了,
这个时候断路器的状态就到了HALF_OPEN状态,这个时候断路器允许一次真实的接口请求,如果这次请求失败,则断路
器打开OPEN,循环上面的动作,如果请求成功则断路器关CLOSED

    enum Status {
        CLOSED, OPEN, HALF_OPEN;
    }


8.1 新建FwHystrixCommondCircuitClose类

笔者设置一个变量,初始化为true,并且在run()方法中我们将根据传入的值设置时间,先让其超时,开启断路器,然后休眠6秒后,调用的时间减少值不超时。断路器关闭,并且10内满足3个请求就会触发断路器第一个条件。


@Slf4j
public class FwHystrixCommondCircuitClose {
    public static void main(String[] args) throws InterruptedException {
        ConfigurationManager.getConfigInstance().setProperty(
                "hystrix.command.default.circuitBreaker.requestVolumeThreshold", 3);
        boolean isTimeout = true;
        for (int i = 0; i < 10; i++) {
            TestCommand c = new TestCommand(isTimeout);
            c.execute();

            HystrixCommandMetrics.HealthCounts hc = c.getMetrics().getHealthCounts();
            System.out.println("健康信息:" + hc.getTotalRequests());
            if (c.isCircuitBreakerOpen()) {
                isTimeout = false;
                log.info("断路器打开了,第{}索引,等待休眠期结束",i);
                log.info("休眠6秒");
                Thread.sleep(6000);
            }
        }
    }

    static class TestCommand extends HystrixCommand<String> {
        private boolean isTimeout;

        public TestCommand(boolean isTimeout) {
            super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"))
                    .andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(500)));
            this.isTimeout = isTimeout;
        }

        protected String run() throws InterruptedException {
            if (isTimeout) {
                Thread.sleep(800);
            } else {
                Thread.sleep(200);
            }
            return "";
        }

        protected String getFallback() {
            return "fallback";
        }
    }
}

8.2 运行main方法

健康数量:0
健康数量:1
健康数量:2
健康数量:3
15:28:22.157 [main] INFO com.yisu.hystrix.without.FwHystrixCommondCircuitClose - 断路器打开了,第3索引,等待休眠期结束
15:28:22.157 [main] INFO com.yisu.hystrix.without.FwHystrixCommondCircuitClose - 休眠6秒
健康数量:0
健康数量:1
健康数量:1
健康数量:3
健康数量:3
健康数量:5