Search code examples
javaweb-serviceshystrixfeign

Feign Hystrix command name not working


If I just have a Hystrix Command defined as class, i have control over defining the group key and command key like below.

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

So for the above code group key is MyHystrixGroup and Command Key is MyHystrixCommand.

If i want to set any configurations of this hystrix command i can do like

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

Where as the default ones will be,

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

Now when I am using Feign Hystrix, I am not defining the command name/ group name. As per the documentation here, the group key matches the target name and command key are same as logging keys.

So if I have a FeignClient like this,

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

I create the instance of my Feign client in a factory class.

   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());
    }

 }

As you see before returning the client, i want to set the timeout configuration of my hystrix command.

I am testing it with a 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");

}

This is working only when i set the time out on default hystrix paramater hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds

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

This didn't work. Similarly i tried below values none of them worked.

  hystrix.command.TestInterface#invoke(String).execution.isolation.thread.timeoutInMilliseconds
hystrix.command.TestInterface#invoke.execution.isolation.thread.timeoutInMilliseconds

Solution

  • I figured it out.

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

    is working. The mistake i did was my method name was not having any parameters passed in. So for a feign hystrix client, the command name is

     FeignClientInterfaceName#MethodNameWithSignatures
    

    For example quoted in the question, it is

     TestInterface#invoke()