1
votes

I am using hystrix for implementing circuit breaker concept in my springboot service. The HystrixCommand annotation that I use on the method that needs to be throttle the external service calls in case of threshhold number of failures is as follows:

 @HystrixCommand(commandKey = "myCommandKey",
            groupKey = "myGroupKey",
            threadPoolKey = "myThreadPoolKey")
    public String myHystrixMethod(String someParam) { ....}

Now, the service where myHystrixMethod resides is called by different clients. Based on the type of client that invokes the service, there is a logic to call a backend service designated for that particular client.

What I would like to do is to register all the threads (requests) invoked by a specific client in it's own hystrix threadpool. Which means, requests from client A would get registered in hystrix threadpool for client A and for client B in a hystrix threadpool for client B only. This would ensure that client B's requests to my service aren't throttled due to failures in client A.

For this, I can create separate service methods (one shown above) for every client and name the hystrix thread pool accordingly. However, with this approach, whenever there is a new client, i'll have to modify the source code to add it's service method.

Is there a way I can dynamically create the options inside of @HystrixCommand annotation based on a parameter, which is client ID in this case ?

1
I don't think it is possible. have a look. github.com/Netflix/Hystrix/issues/350 - pvpkiran
Looks like with hystrix it isn't possible. Is there any other circuit breaker solution like hystriix that has nice option of creating such properties on the fly dynamically ? - Hary
there are other librarires like jrugged and resilience4j. But I dont think you will find this feature there. have a look and let us know. :) - pvpkiran

1 Answers

0
votes

I don't know any way to do it with Hystrix library, but since you've asked for any other circuit breaker implementation that can do it, here is solution based on Resilience4J library.

In your application context you can register CircuitBreakerRegistry bean with default configuration for your clients.

@Bean
public CircuitBreakerRegistry circuitBreakerRegistry() {
    CircuitBreakerConfig config = CircuitBreakerConfig.custom()
            .failureRateThreshold(35)
            .ringBufferSizeInClosedState(1000)
            .ringBufferSizeInHalfOpenState(100)
            .waitDurationInOpenState(Duration.ofMinutes(1))
            .build();
    return CircuitBreakerRegistry.of(config);
}

Then you simply inject this registry to your service and inside your protected method you can get CB for any client you need.

CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("ClientA");

This operation will find already registered circuit breaker or create new one if necessary using registry default config.

And now you can use any CircuitBreaker method of choice: isCallPermitted() or executeRunnable() or any other.

For more information take a look at User Guide.