The Hystrix plug-in uses semaphore to handle requests and is the core implementation of the Soul Gateway’s fusing of traffic. Here’s how Hystrix works:
- Each invocation creates a HystrixCommand or HystrixObservableCommand object, which represents a request to a dependent service.
- HystrixCommand is primarily used for calls that return only one result.
- HystrixObservableCommand is used primarily for calls that may return more than one result.
-
- Execute () for synchronous calls or queue() for asynchronous calls.
- Observe () subscribes to an Observable, which represents the result returned by the dependent service, and gets a copy of that Observable. ToObservable () returns an Observable, and if we subscribe to it, the corresponding command is executed and returns the result
- If the command has request caching enabled and request and response exist in the cache, the result is immediately returned from the cache.
- If the circuit-breaker is on, skip to Step 8 and perform the downgrade strategy. If not, go to Step 5.
- Determine if the thread pool/semaphore is full, if it is, go to step 8, otherwise continue.
- Call HystrixCommand. The run () or HystrixObservableCommand. The construct () :
- Hystrixcommand-run () : Returns a response or throws an exception.
- HystrixObservableCommand. The construct () : returns a observables object, you can get more results or send an onError notification
- If the run() or construct() call times out, the executing thread will throw a TimeoutException (a separate timer thread will throw a TimeoutException if the command itself is not running in its own thread). Perform Step 8 to invoke the Fallback degradation mechanism. Values returned from the run() or construct() methods are also discarded.
- Calculate the status of fuses and report all operating status (success, failure, rejection, timeout) to fuses for statistics to determine the status of fuses.
- GetFallback () fallback logic.
- The getFallback call is triggered in four ways:
- When the construct () or the run () method to throw a non HystrixBadRequestException anomalies.
- Command Execution Timeout
- The fuse turns on the interception call
- Whether the thread pool/queue/semaphore is full
- A Command that does not implement getFallback will throw an exception directly
- Fallback returns if the fallback logic call succeeds
- The degraded logical call failed to throw an exception
- The getFallback call is triggered in four ways:
- Returns an Observable and transforms the desired object based on the method performed in Step 2.
conclusion
Hystrix is a complex process, and in the next section we’ll use the Hystrix plug-in wrapped in Soul to fuse and degrade.