Hystrix-熔断测试代码-通过测试

Hystrix-熔断测试代码-通过测试


package test;


import com.netflix.hystrix.*;

import java.util.Random;

public class GetOrderCircuitBreakerCommand extends HystrixCommand<String> {

    public GetOrderCircuitBreakerCommand(String name) {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ThreadPoolTestGroup"))
                        .andCommandKey(HystrixCommandKey.Factory.asKey("testCommandKey"))
                        .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey(name))
                        .andCommandPropertiesDefaults(
                                HystrixCommandProperties.Setter()
                                        .withCircuitBreakerEnabled(true)//默认是true,本例中为了展现该参数
                                        .withCircuitBreakerForceOpen(false)//默认是false,本例中为了展现该参数
                                        .withCircuitBreakerForceClosed(false)//默认是false,本例中为了展现该参数
                                        .withCircuitBreakerErrorThresholdPercentage(5)//(1)错误百分比超过5%
                                        .withCircuitBreakerRequestVolumeThreshold(10)//(2)10s以内调用次数10次,同时满足(1)(2)熔断器打开
                                        .withCircuitBreakerSleepWindowInMilliseconds(5000)//隔5s之后,熔断器会尝试半开(关闭),重新放进来请求
//                                .withExecutionTimeoutInMilliseconds(1000)
                        )
                        .andThreadPoolPropertiesDefaults(
                                HystrixThreadPoolProperties.Setter()
                                        .withMaxQueueSize(10)   //配置队列大小
                                        .withCoreSize(2)    // 配置线程池里的线程数
                        )
        );
    }

    @Override
    protected String run() throws Exception {
        Random rand = new Random();
        //模拟错误百分比(方式比较粗鲁但可以证明问题)
        if (1 == rand.nextInt(2)) {
//            System.out.println("make exception");
            throw new Exception("make exception");
        }
        return "running:  ";
    }

    @Override
    protected String getFallback() {
//        System.out.println("FAILBACK");
        return "fallback: ";
    }



    public static void testCircuitBreaker() throws Exception {
        for (int i = 0; i < 25; i++) {
            Thread.sleep(500);
            HystrixCommand<String> command = new GetOrderCircuitBreakerCommand("testCircuitBreaker");
            String result = command.execute();
            //本例子中从第11次,熔断器开始打开
            System.out.println("call times:" + (i + 1) + "   result:" + result + " isCircuitBreakerOpen: " + command.isCircuitBreakerOpen());
            //本例子中5s以后,熔断器尝试关闭,放开新的请求进来
        }
    }

    public static void main(String[] args)  throws Exception {

        testCircuitBreaker();

    }

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值