In our application we use Hystrix because we call several external services. We want to configure one thread pool -- with a specific size -- for each external service that we call.
Let's assume there are three external Services, called S1, S2, S3. Furthermore, we have 10 classes which extend HystrixCommand
, called C1 through C10.
C1 and C2 make calls to S1 and should use the same thread pool, with 15 threads. Inside the constructor of C1, we make the following call to super
:
super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("S1"))
.andThreadPoolKey(ThreadPools.S1)
.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withCoreSize(15))
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(timeout)));
Inside the constructor of one command (C1) we specify the thread pool size for S1 to be 15. ThreadPools
is a custom class where the final
static
attribute S1
is defined by
S1 = HystrixThreadPoolKey.Factory.asKey("S1");
Now the actual question is, (1) why the thread pool core size (for S1 it is 15) is specified inside a HystrixCommand
instead of a central thread pool definition (which does not seem to be the concept of Hystrix).
Suppose inside the constructor of C2
(which looks the same as the snippet above) we were to call withCoreSize with an argument other than 15. (2) Which one would be used?
(3) Is there a way to define the three thread pools for the services S1, S2, and S3 in one class and reference them from the command classes?
The Hystrix How to Use guide does not seem to contain information related to that. It would be great if someone had time to answer this question.
First, we need to add the Spring Cloud Starter Hystrix dependency in our build configuration file. Now, add the @EnableHystrix annotation into your main Spring Boot application class file. The @EnableHystrix annotation is used to enable the Hystrix functionalities into your Spring Boot application.
Hystrix various thread pool related properties determines thread pool characteristics. Hystrix uses Java ThreadPoolExecutor to handle concurrent requests. By default Hystrix uses coreSize of 10 threads and it does not uses any waiting queue.
Akka, Envoy, Istio, Zuul, and Polly are the most popular alternatives and competitors to Hystrix.
Hystrix Status. Hystrix is no longer in active development, and is currently in maintenance mode. Hystrix (at version 1.5. 18) is stable enough to meet the needs of Netflix for our existing applications.
You can define a properties file called config.properties
that looks like:
hystrix.threadpool.S1.coreSize=15
hystrix.threadpool.S1.maximumSize=15
hystrix.threadpool.S1.maxQueueSize=15
hystrix.threadpool.S1.queueSizeRejectionThreshold=300
hystrix.threadpool.S1.allowMaximumSizeToDivergeFromCoreSize=true
hystrix.threadpool.S1.keepAliveTimeMinutes=1
hystrix.threadpool.S2.coreSize=20
hystrix.threadpool.S2.maximumSize=20
hystrix.threadpool.S2.maxQueueSize=20
hystrix.threadpool.S2.queueSizeRejectionThreshold=300
hystrix.threadpool.S2.allowMaximumSizeToDivergeFromCoreSize=true
hystrix.threadpool.S2.keepAliveTimeMinutes=1
...
Here is a nice explanation about the difference between coreSize, maximumSize & maxQueueSize:
https://github.com/Netflix/Hystrix/issues/1554
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With