Hystrix断路器

一 命令执行流程

二 断路器开启

  • 整个链路达到一定的阀值,默认情况下,10秒内产生超过20次请求,则符合第一个条件。

  • 满足第一个条件的情况下,如果请求的错误百分比大于阀值,则会打开断路器,默认为50%。

三 开启断路器

1 代码

package org.crazyit.cloud.open;

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

public class OpenMain {

    public static void main(String[] args) {
        // 10秒内大于10次请求,满足第一个条件,目的,流量到达一定的值,才可能开启断路器
        ConfigurationManager
                .getConfigInstance()
                .setProperty(
                        "hystrix.command.default.circuitBreaker.requestVolumeThreshold",
                        10);
        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() {
            super(Setter.withGroupKey(
                    HystrixCommandGroupKey.Factory.asKey("ExampleGroup"))
                    .andCommandPropertiesDefaults(
                            HystrixCommandProperties.Setter()
                                    .withExecutionTimeoutInMilliseconds(500)));
        }

        @Override
        protected String run() throws Exception {
            Thread.sleep(800);
            return "";
        }

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

2 测试结果——第11次才打开断路器

false

false

false

false

false

false

false

false

false

false

true

true

true

true

true

四 关闭断路器

1 代码

package org.crazyit.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++) {
            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("============  断路器打开了,等待休眠期(默认5秒)结束");
                Thread.sleep(6000);
            }
        }
    }

    static class TestCommand extends HystrixCommand<String> {
        //控制命令执行时间
        private boolean isTimeout;
        //设置命令默认超时时间为500Ms
        public TestCommand(boolean isTimeout) {
            super(Setter.withGroupKey(
                    HystrixCommandGroupKey.Factory.asKey("ExampleGroup"))
                    .andCommandPropertiesDefaults(
                            HystrixCommandProperties.Setter()
                                    .withExecutionTimeoutInMilliseconds(500)));
            this.isTimeout = isTimeout;
        }

        @Override
        protected String run() throws Exception {
            //超时执行
            if(isTimeout) {
                Thread.sleep(800);
            } else {
                Thread.sleep(200);
            }            
            return "";
        }

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

2 测试结果

断路器状态:false, 请求数量:0

断路器状态:false, 请求数量:1

断路器状态:false, 请求数量:2

断路器状态:true, 请求数量:3

============  断路器打开了,等待休眠期(默认5秒)结束

断路器状态:false, 请求数量:0

断路器状态:false, 请求数量:0

断路器状态:false, 请求数量:0

断路器状态:false, 请求数量:3

断路器状态:false, 请求数量:3

断路器状态:false, 请求数量:5

3 说明

休眠期默认:5秒

然后尝试执行一次,如果成功,将断路器关闭,如果还是失败,断路器还是处于打开状态。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值