Hystrix的超时策略

Scroll Down

超时策略

通过设置execution.isolation.thread.timeoutInMilliseconds来进行执行的超时限制,超过此时间,HystrixCommand被标记为TIMEOUT,并执行回退逻辑。

超时策略代码

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.HystrixThreadPoolProperties;

/**
 * @author 王守钰
 * @program quan
 * @date 2020年05月16日 18:32
 * @description: 超时策略配置
 */
public class HystrixTimeOutCommand extends HystrixCommand<String> {

    public static final Integer DEFAULT_TIMEOUT = 3 * 1000;

    private Integer id;

    protected HystrixTimeOutCommand(Integer id) {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("HelloWorldGroup"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.defaultSetter()
                        .withExecutionTimeoutInMilliseconds(DEFAULT_TIMEOUT)));
        this.id = id;
    }

    @Override
    protected String run() throws Exception {
        if(id % 2 == 0){
            Thread.sleep(DEFAULT_TIMEOUT);
        }
        return "success";
    }

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

测试代码

@Test
public void testTimeOut(){
    CountDownLatch countDownLatch = new CountDownLatch(6);
    for (int i = 0; i < 6; i++){
        HystrixTimeOutCommand command = new HystrixTimeOutCommand(i);
        int finalI = i;
        new Thread(()->{
            String result = command.execute();
            log.info("{}, execute id:{} result:{}", Thread.currentThread().getName(), finalI, result);
            countDownLatch.countDown();
        }).start();
    }
    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

执行结果

19:32:15.898 [Thread-1] INFO com.wangshouyu.hystrix.HystrixTimeOutTest - Thread-1, execute id:1 result:success
19:32:15.898 [Thread-3] INFO com.wangshouyu.hystrix.HystrixTimeOutTest - Thread-3, execute id:3 result:success
19:32:15.899 [Thread-5] INFO com.wangshouyu.hystrix.HystrixTimeOutTest - Thread-5, execute id:5 result:success
19:32:18.875 [Thread-4] INFO com.wangshouyu.hystrix.HystrixTimeOutTest - Thread-4, execute id:4 result:timeout
19:32:18.875 [Thread-2] INFO com.wangshouyu.hystrix.HystrixTimeOutTest - Thread-2, execute id:2 result:timeout
19:32:18.875 [Thread-0] INFO com.wangshouyu.hystrix.HystrixTimeOutTest - Thread-0, execute id:0 result:timeout

我们在上面执行的结果,我是当id为双数的时候进行了线程休眠3m,系统配置了超时时间为3m,HystrixCommandProperties.defaultSetter().withExecutionTimeoutInMilliseconds(DEFAULT_TIMEOUT),那么就会进入熔断。

关闭超时策略

protected HystrixTimeOutCommand(Integer id) {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("HelloWorldGroup"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.defaultSetter()
                        .withExecutionTimeoutInMilliseconds(DEFAULT_TIMEOUT)
                        .withExecutionTimeoutEnabled(false)
                ));
        this.id = id;
    }

执行结果

19:35:24.663 [Thread-3] INFO com.wangshouyu.hystrix.HystrixTimeOutTest - Thread-3, execute id:3 result:success
19:35:24.663 [Thread-5] INFO com.wangshouyu.hystrix.HystrixTimeOutTest - Thread-5, execute id:5 result:success
19:35:24.663 [Thread-1] INFO com.wangshouyu.hystrix.HystrixTimeOutTest - Thread-1, execute id:1 result:success
19:35:27.653 [Thread-4] INFO com.wangshouyu.hystrix.HystrixTimeOutTest - Thread-4, execute id:4 result:success
19:35:27.654 [Thread-2] INFO com.wangshouyu.hystrix.HystrixTimeOutTest - Thread-2, execute id:2 result:success
19:35:27.654 [Thread-0] INFO com.wangshouyu.hystrix.HystrixTimeOutTest - Thread-0, execute id:0 result:success

因为我们在上面设置中配置了启用超时策略为false,withExecutionTimeoutEnabled(false),那么我们再次执行的时候就不会走我们原有的超时策略。