Spring Cloud_17_Hystrix的使用-断路器(二)

Hystrix的使用-断路器(二)

  • 执行逻辑
  • 执行流程

  • 看看断路器是如何被开启?如何被关闭?

1、断路器开启

  • 整个链路达到一定的阀值,默认情况下,10秒内产生超过20次请求,则符合第一个条件
  • 满足第一个条件的情况下,如果请求的错误百分比大于阀值,则会开启断路器,默认为50%(如:10内发送30次请求,其中15次是失败的,则会开启断路器)
package com.atm.cloud.open;

import com.netflix.config.ConfigurationManager;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;

public class OpenMain {

    public static void main(String[] args) {
        // 断路器配置(此处配置成:10s内大于10次请求)
        ConfigurationManager
                .getConfigInstance()
                .setProperty(
                        "hystrix.command.default.circuitBreaker.requestVolumeThreshold",
                        10);

        // 循环遍历发送请求,发送15次请求(满足第一个要求)
        for (int i = 0; i < 15; i++) {
            ErrorCommand c = new ErrorCommand();
            c.execute();
            // 输出断路器状态
            System.out.println(c.isCircuitBreakerOpen());
        }
    }

    static class ErrorCommand extends HystrixCommand<String> {

        public ErrorCommand() {
            // 如果超过500毫秒无响应就执行回退
            super(Setter.withGroupKey(
                    HystrixCommandGroupKey.Factory.asKey("ExampleGroup"))
                    .andCommandPropertiesDefaults(
                            HystrixCommandProperties.Setter()
                                    .withExecutionTimeoutInMilliseconds(500)));
        }

        @Override
        protected String run() throws Exception {
            // 方法执行需要800毫秒,也就是说每次方法执行均是无响应的
            Thread.sleep(800);
            return "";
        }

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

    }
}

2、断路器关闭

  • 当断路器打开的时候,不再执行命令,直接进行回退方法,这段时间称为休眠期,默认时间为5s
  • 休眠期结束之后,会尝试性执行一次命令,此时,断路器状态处于半开状态,尝试执行成功之后,就会关闭断路器并且清空健康信息,如果失败,仍然处于关闭状态
package com.atm.cloud.open;

import com.netflix.config.ConfigurationManager;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandMetrics.HealthCounts;
import com.netflix.hystrix.HystrixCommandProperties;

public class CloseMain {

    public static void main(String[] args) throws Exception {
        // 10秒内大于3次请求,满足第一个条件
        ConfigurationManager
                .getConfigInstance()
                .setProperty(
                        "hystrix.command.default.circuitBreaker.requestVolumeThreshold",
                        3);
        boolean isTimeout = true;
        for (int i = 0; i < 10; i++) {// 发送10次请求,一开始均为失败,则会打开断路器
            TestCommand c = new TestCommand(isTimeout);// 总会超时
            c.execute();
            // 输出健康信息
            HealthCounts hc = c.getMetrics().getHealthCounts();
            System.out.println("断路器状态:" + c.isCircuitBreakerOpen() + ", 请求数量:"
                    + hc.getTotalRequests());
            if (c.isCircuitBreakerOpen()) {// 如果断路器打开
                isTimeout = false;//
                System.out.println("============  断路器打开了,等待休眠期结束");
                Thread.sleep(6000);// 等待休眠期结束,6s之后尝试性发一次请求
            }
        }
    }

    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)));// 500毫秒没有响应,则打开断路器
            this.isTimeout = isTimeout;
        }

        @Override
        protected String run() throws Exception {
            if (isTimeout) {
                Thread.sleep(800);// 方法执行时间为800毫秒
            } else {
                Thread.sleep(200);
            }
            return "";
        }

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

'嗯哼。

生生不息,“折腾”不止,Thx

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值