I have been exploring using Hystrix support in Spring Cloud Netflix for the service methods.
I see that there is a annotation based way of using @HystrixCommand with every service method and providing a fallback method in a attribute. I feel this ties the fallback methods to the code and is not flexible for all scenarios.
I am looking for a way to do the same without the coupling the code with annotations. I still want to have fallback methods per service method but would want them to be decoupled and at the same I want to have the flexibility to enable/disable hystrix per method based on configuration.
Is there way I can achieve the same with Spring Cloud Netflix without using annotations and using java/xml/properties file configuration.
Update:
I found a way to enable/disable fallback methods for each of the service methods as below.
I had to add a "commandKey" attribute to the hystrixCommand and identify the hystrix command.
@HystrixCommand(
commandKey="version1",
fallbackMethod = "getFromFileSystem"
)
@GetMapping("/api/v1/addnos/{id1}/{id2}")
public String getMessage(@PathVariable String id1, @PathVariable String id2) {
String uri = "http://localhost:2080/api/addnos";
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.POST, getHttpEntityRequest(id1, id2),
String.class);
return response.getBody().toString();
}
I was then able to enable/disable the fallback methods for each of the service methods wrapped with hystrix command from being called as below in application.properties.
hystrix.command.version1.fallback.enabled=false
hystrix.command.version2.fallback.enabled=true
hystrix.command.version3.fallback.enabled=false
Not sure if this the correct way to enable/disable circuitbreaker per method level. With this change, either the fallback executes when enabled or exception is thrown when it is disabled.
I also found a alternate property ,
hystrix.command.version1.circuitBreaker.enabled=false
But, this only disables the circuit breaker functionality for all methods with a commandkey it is applied to. It does not work as expected. Not sure how it differs from fallback method enable/disable.
But, I still am not sure of how I can change the fallback method name when enabled.
For Example, if I have 2 fallback methods("getfromCache" and "getFromFileSystem") either in the same class or in a different one and want to choose one over the other.
I tried,
hystrix.command.version2.fallbackMethod=getFromCache
But, this does not have any effect. I am not able to override it with a configurable value.
It always uses the static method name given as the attribute property of @HystrixCommand annotation in the code.