Hystrix在运行时忽略超时

时间:2015-01-05 14:52:06

标签: java configuration timeout hystrix

我正在试验一下Hystrix。

我支持文档,即使通过'运行'同步调用Hystrix命令也是如此。默认情况下在一个线程中运行,并且应该受Hystrix中配置的超时限制。但是当我尝试它时,似乎没有超时。

我是否误解了文档?或者我做错了什么?有没有办法通过同步调用获得超时行为?

更具体:我有一个简单的服务'返回需要5秒钟。这包含在Hystrix命令中,超时为500毫秒:

public class WebRequestCommand extends HystrixCommand<String> {
    private final SimpleService baneService;

    protected WebRequestCommand(SimpleService baneService) {

        super(
                Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("test"))
                        .andCommandPropertiesDefaults(
                                HystrixCommandProperties.Setter()
                                        .withExecutionIsolationThreadTimeoutInMilliseconds(500)));
        this.baneService = baneService;
    }

    @Override
    protected String run() {
        return baneService.connectToBane();

    }

    @Override
    protected String getFallback() {
       return "SERVICE NOT AVAILABLE";
    }
}

如果我这样称呼它:

WebRequestCommand webService = new WebRequestCommand(baneService);
result = webService.run();

我在5秒后获得结果=&gt;没有超时

如果我这样称呼它:

WebRequestCommand webService = new WebRequestCommand(baneService);
result = webService.queue().get();

Hystrix超时在500ms后发生并返回后备。

1 个答案:

答案 0 :(得分:7)

我认为你应该以同步方式调用execute()而不是run()。

相关问题