【问题标题】:Hystrix ignores timeout on runHystrix 忽略运行超时
【发布时间】:2015-03-03 03:15:16
【问题描述】:

我正在尝试一些 Hystrix。

我理解文档,即使通过“运行”对 Hystrix 命令的同步调用默认在线程中运行,并且应该受 Hystrix 中配置的超时限制。但是当我尝试它时,似乎没有发生超时。

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

更具体地说:我有一个需要 5 秒才能返回的“SimpleService”。这包含在一个超时为 500 毫秒的 Hystrix 命令中:

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 秒后得到结果 => 没有超时

如果我这样称呼它:

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

Hystrix 超时发生在 500 毫秒后并返回回退。

【问题讨论】:

    标签: java configuration timeout hystrix


    【解决方案1】:

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

    【讨论】:

    • 只是想知道 'run()' 是否不应该在超类中设置为“final”以防止此类错误。
    • @BertrandRenuart 不起作用,因为您需要在子类中覆盖它。这里的问题在于protected 可见性,在Java 中,它不仅限制了对子类的访问,还限制了对同一包中其他类的访问。我猜这里的 Jens 是在与 WebRequestCommand 属于同一包的类中调用他的方法
    猜你喜欢
    • 1970-01-01
    • 2016-10-07
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 2020-01-30
    相关资源
    最近更新 更多