Hystrix在运行时忽略超时
我正在试验一下Hystrix。
我支持文档,即使是通过’run’同步调用Hystrix命令也默认在一个线程中运行,并且应该受Hystrix中配置的超时限制。 但是当我尝试它时,似乎没有超时。
我是否误解了文档? 或者我做错了什么? 有没有办法通过同步调用获得超时行为?
更具体:我有一个’SimpleService’需要5秒才能返回。 这包含在Hystrix命令中,超时为500ms:
public class WebRequestCommand extends HystrixCommand { 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超时在500ms后发生并返回回退。
我认为你应该为同步方式调用execute()而不是run()。