java - Feign Hystrix 命令名称不起作用

标签 java web-services hystrix feign

如果我只有一个定义为类的 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 ,组 key 与目标名称匹配,命令 key 与日志记录 key 相同。

所以如果我有一个这样的 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

最佳答案

我想通了。

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

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

 FeignClientInterfaceName#MethodNameWithSignatures

比如题中引用的,是

 TestInterface#invoke()

关于java - Feign Hystrix 命令名称不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40026066/

相关文章:

hystrix - Spring-Cloud Hystrix(未找到备用方法)

java - Controller 中构造函数的参数 0 需要一个类型为 Service 类的 bean,但无法找到

java - 在 CentOs 中将 java 应用程序作为服务运行时遇到错误 : nested exception is java. lang.UnsatisfiedLinkError

web-services - SoapUI负载测试,计算方差策略中的cnt

java - Java 中的 SSL key 和客户端身份验证

php - 有没有什么好的通用 PHP MySQL HTTP 隧道?

spring-boot - 修复 "The web application [ROOT] created a ThreadLocal with key of type [com.netflix.hystrix.Hystrix$1]"

java - JAXB JXC 为枚举生成模式而不管 @XmlTransient

c# - 螺旋矩阵算法问题

java - 在 Quarkus Websocket 中阻塞 IO 线程