Alex.Bai Alex.Bai - 5 months ago 149
Java Question

Hystrix runtime configuration with Spring-Cloud-Config instead of Archaius?

I'm studying Spring-Cloud-Netflix and I learned that Archaius was used for Hystrix runtime configuration. (https://ahus1.github.io/hystrix-examples/manual.html#archaius)

I also found that Archaius was a soft-dependency since V1.5.0 (https://github.com/Netflix/Hystrix/pull/1083): "Archaius is now a soft-dependency of Hystrix, so you can supply your own configuration mechanism."

My question is, is it easy to configure Hystrix with Spring-Cloud-Config at runtime? I did some research but haven't found any examples.

Appreciate any ideas.

Answer

After several days' research, I managed to dynamically configure Hystrix properties with Spring Cloud Config. I also made a small demo on configuring the Hystrix instance property at runtime.

First, each Hystrix property has four levels of precendence:

  1. Global default
  2. Dynamic global default
  3. Instance default
  4. Dynamic instance property.

Where the 1st and 3rd levels only support static configurations. Since the 2nd level (dynamic global default) was not discussed a lot in the Hystrix Wiki, I choose the Dynamic Instance Property for runtime configuration. However, I believe my method should apply to Dynamic Global Default as well.

The practice is simple. First pull a config value from Spring Cloud Config using the @Value annotation:

@Value("{timeoutInMilliseconds:1500}")
String timeout;

Then use the string timeout in your Hystrix instance:

Configurationmanager.getConfigInstance().setProperty("hystrix.command.HystrixHelloWorld.execution.isolation.thread.timeoutInMilliseconds", timeout);

Where the Configurationmanager.getConfigInstance() is an Archaius method, returns a configuration instance. .setProperty() sets the property