Setting Request Timeout Using Netflix Feign and Hystrix

I am creating a REST client using Feign. My calls are working for me, but I want to add some timeout support, and I have time to figure out how to do this.

The Feign documentation says: "To use Hystrix with Feign, add the Hystrix module to your class path, and then use the HystrixFeign builder." So now I have this:

service = HystrixFeign.builder() .decoder(new GsonDecoder()) .target(ProjectService.class, URL_TO_BE_MOVED_TO_PROPS); 

Now all my methods return HystrixCommands, which I can execute or queue, but I still don’t see how to configure them.

The Hystrix wiki ( https://github.com/Netflix/Hystrix/wiki/Configuration ) says that the configuration should be added to the HystrixCommand constructor as follows:

 public HystrixCommandInstance(int id) { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ExampleGroup")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withExecutionTimeoutInMilliseconds(500))); this.id = id; 

But my commands are built / returned using Feign, so I don't have access to the constructors.

Another thing worth noting is that in the Feign-Hystrix read mode ( https://github.com/Netflix/feign/tree/master/hystrix ) it says: "To use Hystrix with Feign, add the Hystrix module for your class path. Then configure the Use function to use the HystrixInvocationHandler, but a google search for the HystrixInvocationHandler points me to a non-Netflix repo. Even if I used this, I don’t see how to configure Feign to use it.

Please tell me that I am stupid and that it is very simple, which will make me feel joy that I passed this problem, and a shame that I could not figure it out myself.

TL DR: I want to set timeouts for requests made by my Feign client. How to make?

+5
source share
1 answer

Turns out you can set the Hystrix properties using an instance of com.netflix.config.ConfigurationManager (from com.netflix.archaius: archaius-core).

Feign uses method names like HystrixCommandKeys, so you can access their properties using these names:

  ConfigurationManager.getConfigInstance().setProperty("hystrix.command." + methodName + ".execution.isolation.thread.timeoutInMilliseconds", 1500); 

It is assumed that you used HystrixFeign to create your client, which transfers each call to HystrixCommand objects.

To simplify, I created a loop of my methods so that I can apply a timeout throughout the service:

 private void configureHystrix() { Method[] methods = ProjectService.class.getMethods(); String methodName; for(int i = 0; i < methods.length; i++) { methodName = methods[i].getName(); ConfigurationManager.getConfigInstance().setProperty(String.format("hystrix.command.%s.execution.isolation.thread.timeoutInMilliseconds", methodName), config.getTimeoutInMillis()); } } 
+11
source

Source: https://habr.com/ru/post/1235317/


All Articles