我正在尝试使用 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 毫秒后并返回回退。
最佳答案
我认为您应该以同步方式调用 execute() 而不是 run()。
关于java - Hystrix 忽略运行超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27781982/