Java 假装Hystrix命令名不起作用
如果我只是将一个Hystrix命令定义为class,那么我可以控制定义组键和命令键,如下所示Java 假装Hystrix命令名不起作用,java,web-services,hystrix,feign,Java,Web Services,Hystrix,Feign,如果我只是将一个Hystrix命令定义为class,那么我可以控制定义组键和命令键,如下所示 private static class MyHystrixCommand extends HystrixCommand<MyResponseDto> { public MyHystrixCommand() { super(HystrixCommandGroupKey.Factory.asKey("MyHystrixGroup"));
private static class MyHystrixCommand extends HystrixCommand<MyResponseDto> {
public MyHystrixCommand() {
super(HystrixCommandGroupKey.Factory.asKey("MyHystrixGroup"));
}
作为默认值
ConfigurationManager.getConfigInstance().setProperty(
"hystrix.command.default.execution.timeout.enabled", false);
现在,当我使用Feign Hystrix时,我没有定义命令名/组名。根据文档,组密钥与目标名称匹配,命令密钥与日志密钥相同
如果我有一个像这样的假客户
interface TestInterface {
@RequestLine("POST /")
String invoke() throws Exception;
}
我在一个工厂类中创建我的假客户机的实例
class TestFactory {
public TestInterface newInstance() {
ConfigurationManager.getConfigInstance()
.setProperty("hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds", 500);
return HystrixFeign.builder()
.target(TestInterface.class, "http://localhost:" + server.getPort(), (FallbackFactory) new FallbackApiRetro());
}
}
正如您在返回客户端之前看到的,我想设置hystrix命令的超时配置
我正在用MockWebServer测试它
@Test
public void fallbackFactory_example_timeout_fail() throws Exception {
server.start();
server.enqueue(new MockResponse().setResponseCode(200)
.setBody("ABCD")
.setBodyDelay(1000, TimeUnit.MILLISECONDS));
TestFactory factory = new TestFactory();
TestInterface api = factory.newInstance();
// as the timeout is set to 500 ms, this case should fail since i added 1second delay in mock service response.
assertThat(api.invoke()).isEqualTo("Fallback called : foo");
}
仅当我在默认hystrix参数上设置超时时,此选项才起作用
hystrix.command.default.execution.isolation.thread.timeouting毫秒
ConfigurationManager.getConfigInstance()
.setProperty("hystrix.command.invoke.execution.isolation.thread.timeoutInMilliseconds", 500);
这不管用。
同样地,我尝试了以下价值观,它们都不起作用
hystrix.command.TestInterface#invoke(String).execution.isolation.thread.timeoutInMilliseconds
hystrix.command.TestInterface#invoke.execution.isolation.thread.timeoutInMilliseconds
我想出来了
ConfigurationManager.getConfigInstance().setProperty("hystrix.command.TestInterface#invoke().execution.isolation.thread.timeoutInMilliseconds",500);
他正在工作。我犯的错误是我的方法名没有传入任何参数。因此,对于一个假的hystrix客户端,命令名是
FeignClientInterfaceName#MethodNameWithSignatures
例如,问题中引用的是
TestInterface#invoke()
我不明白为什么会有反对票。我花了两天的时间苦苦思考这个问题,在我的发现之后,我把它贴在了这里,因为它可能对某些人有用。当人们否决投票时,最好也给出一个理由。
TestInterface#invoke()