1. Fuse mode
The health of the service = number of failed requests / total number of requests
The state transition of the fuse switch from closed to open is determined by comparing the current service health status with the set threshold
1) When the fuse switch is closed, the request is allowed to pass through the fuse. If the current health status is higher than the set threshold, the switch continues to remain closed. If the current health status is lower than the set threshold, the switch will switch to the open state
2) When the fuse switch is opened, the request is forbidden to pass
3) When the fuse switch is in the open state, after a period of time, the fuse will automatically enter the half-open state. At this time, the fuse only allows one request to pass. When the request is successfully called, the fuse is restored to the closed state. If the request fails, the fuse continues to remain open, and subsequent requests are forbidden to pass
The switch of the fuse can ensure that the service caller returns the result quickly when calling the abnormal service, avoiding a lot of synchronous waiting. And the fuse can continue to detect the execution result of the request after a period of time, providing the possibility of restoring the service call
2. The working principle of Hystrix
1) Each call will create a HystrixCommand
2) Execute execute or queue to make synchronous or asynchronous calls
3) Judge whether the fuse is open, if open, skip to step 8; otherwise, go to step 4
4) Determine whether the thread pool/semaphore is full, if it is full, go to step 8, otherwise go to step 5
5) Call the run method of HystrixCommand, if the call times out, go to step 8
6) Determine whether the call is successful, and return the result of the successful call, if it fails, go to step 8
7) Calculate the status of the fuse, and report all the operating status to the fuse for statistics to determine the status of the fuse
8) If there is a downgrade method to follow the downgrade processing logic, an exception will be thrown if there is no downgrade method. According to the above steps, it can be concluded that the following four situations will enter the downgrading process: the fuse is opened, the thread pool/semaphore is full, the call timeout, and the call fails.
9) Return the result of successful execution
3. Hystrix thread and semaphore isolation
Thread pool isolation : Thread isolation creates a separate thread pool for each dependent service for isolation, and each call to the service occurs in its corresponding thread pool, and the background service is called through the threads in the thread pool.
Semaphore isolation : When the number of concurrent requests reaches the threshold, the request thread can fail quickly and the execution is degraded
Semaphore isolation | Thread pool isolation | |
---|---|---|
advantage | Lightweight, no extra overhead | Support queuing and timeout; support asynchronous call |
Disadvantage | Does not support task queuing and active timeout; does not support asynchronous calls | Thread calls will incur additional overhead |
Applicable scene | Trusted client; high fan-out (gateway); high-frequency and high-speed calling (cache) | Untrusted customers; limited fan-out |
4. Hystrix core configuration
The properties of Hystrix have the following 4 different priority configurations (priority from low to high)
- Global default value: If the following three levels of attributes are not set, then this attribute is the default value
- Global configuration default value: by defining global attribute values in the configuration file
- Instance default value: the default value defined for the instance through code
- Instance configuration properties: configure properties for the specified instance through the configuration file
1), command attribute
1) execution.isolation.strategy
: This property is used to set the isolation strategy executed by HystrixCommand.run(), with the following two options
- THREAD: A strategy of isolation through thread pools. It executes on a separate thread, and its concurrency limit is limited by the number of threads in the thread pool (default value)
- SEMAPHORE: A strategy of isolation through semaphores. It is executed on the calling thread, and its concurrency limit is limited by the semaphore count
Global configuration properties:hystrix.command.default.execution.isolation.strategy
Example default value:
@HystrixCommand(commandProperties = {
@HystrixProperty(name = "execution.isolation.strategy",value ="SEMAPHORE")})
Instance configuration properties:hystrix.command.HystrixCommandKey.execution.isolation.strategy
2) execution.isolation.thread.timeoutInMilliseconds
: This property is used to configure the timeout period of HystrixCommand execution, the default value is 1000 milliseconds. When the execution time of HystrixCommand exceeds the configuration value, Hystrix will mark the execution command as TIMEOUT and enter the service degradation processing logic
Global configuration properties:hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds
Example default value:
@HystrixCommand(commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")})
Instance configuration properties:hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds
3) execution.isolation.semaphore.maxConcurrentRequests
: When the HystrixCommand isolation strategy uses a semaphore, this property is used to configure the size of the semaphore (the number of concurrent requests, the default is 10). When the maximum number of concurrent requests reaches the set value, subsequent requests will be rejected
Global configuration properties:hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests
Example default value:
@HystrixCommand(commandProperties = {
@HystrixProperty(name = "execution.isolation.semaphore.maxConcurrentRequests", value = "10")})
Instance configuration properties:hystrix.command.HystrixCommandKey.execution.isolation.semaphore.maxConcurrentRequests
2), threadPool attribute
1) coreSize: This parameter is used to set the number of core threads in the command thread pool. This value is the maximum concurrent command execution (the default value is 10)
Global configuration properties:hystrix.threadpool.default.coreSize
Example default value:
@HystrixCommand(commandKey = "helloKey", threadPoolProperties = {
@HystrixProperty(name = "coreSize", value = "10")})
Instance configuration properties:hystrix.threadpool.HystrixThreadPoolKey.coreSize
2) maxQueueSize: This parameter is used to set the maximum queue size of the thread pool. When set to -1, the thread pool will use the queue implemented by SynchronousQueue, otherwise it will use the queue implemented by LinkedBlockingQueue (the default value is -1)
Global configuration properties:hystrix.threadpool.default.maxQueueSize
Example default value:
@HystrixCommand(commandKey = "helloKey", threadPoolProperties = {
@HystrixProperty(name = "maxQueueSize", value = "-1")})
Instance configuration properties:hystrix.threadpool.HystrixThreadPoolKey.maxQueueSize
The configuration is summarized as follows :
Configuration items (prefix hystrix.command.*.) | meaning |
---|---|
execution.isolation.strategy | Thread THREAD or semaphore SEMAPHORE isolation (default value: THREAD) |
execution.isolation.thread.timeoutInMilliseconds | Run() method execution timeout (default value: 1000) |
execution.isolation.semaphore.maxConcurrentRequests | The maximum number of concurrent semaphores isolation (default value: 10) |
circuitBreaker.errorThresholdPercentage | Threshold of fusing error percentage (default value: 50) |
circuitBreaker.requestVolumeThreshold | The flow threshold that must be met for the circuit breaker to take effect (default: 20) |
circuitBreaker.sleepWindowInMilliseconds | The time interval to reset the circuit breaker after fusing (default: 5000) |
circuitBreaker.forceOpen | Set true to force the fuse to enter the open state (default value: false) |
circuitBreaker.forceClosed | Set true to force the fuse to enter the closed state (default value: false) |
Configuration items (prefix hystrix.threadpool.*.) | meaning |
---|---|
coreSize | Maximum concurrent requests when using thread pool (default value: 10) |
maxQueueSize | Maximum LinkedBlockingQueue size, -1 means SynchronousQueue (default value: -1) |
default.queueSizeRejectionThreshold | Queue size threshold, reject if exceeded (default value: 5) |
Recommended reading :
https://segmentfault.com/a/1190000005988895