【问题标题】:Feign Hystrix command name not workingFeign Hystrix 命令名称不起作用
【发布时间】:2017-02-22 21:02:24
【问题描述】:

如果我只是将 Hystrix 命令定义为类,我可以控制定义组键和命令键,如下所示。

     private static class MyHystrixCommand extends HystrixCommand<MyResponseDto> {
               public MyHystrixCommand() {
        super(HystrixCommandGroupKey.Factory.asKey("MyHystrixGroup"));
     }

所以对于上面的代码组键是 MyHystrixGroup,Command Key 是 MyHystrixCommand。

如果我想设置这个 hystrix 命令的任何配置,我可以这样做

      ConfigurationManager.getConfigInstance().setProperty(
                                "hystrix.command.MyHystrixCommand.execution.timeout.enabled", false); 

默认的位置,

       ConfigurationManager.getConfigInstance().setProperty(
                "hystrix.command.default.execution.timeout.enabled", false);

现在当我使用 Feign Hystrix 时,我没有定义命令名/组名。根据文档here,组键与目标名称匹配,命令键与日志键相同。

所以如果我有这样的 FeignClient,

     interface TestInterface {
        @RequestLine("POST /")
        String invoke() throws Exception;
     }

我在工厂类中创建了我的 Feign 客户端的实例。

   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.timeoutInMilliseconds

    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

【问题讨论】:

  • 为什么会有一个我不明白的反对票。我努力解决这个问题 2 天,并在我的发现后将其发布在这里,因为它可能对某人有用。当人们投反对票时,最好也给出理由。

标签: java web-services hystrix feign


【解决方案1】:

我想通了。

  ConfigurationManager.getConfigInstance().setProperty("hystrix.command.TestInterface#invoke().execution.isolation.thread.timeoutInMilliseconds",500);

正在工作。我犯的错误是我的方法名称没有传入任何参数。所以对于 feign hystrix 客户端,命令名称是

 FeignClientInterfaceName#MethodNameWithSignatures

例如问题中引用的,就是

 TestInterface#invoke()

【讨论】:

    猜你喜欢
    • 2018-03-07
    • 2017-08-22
    • 2017-02-20
    • 2017-04-06
    • 2019-08-18
    • 2015-12-05
    • 2016-05-17
    • 2018-08-01
    • 2018-07-15
    相关资源
    最近更新 更多