[SpringBoot] The working principle of Hystrix of SpringBoot (13)

How Hystrix works

  flow chart:

  

  Each section will explain this process in more detail HystrixCommand.run

  1. Construct a HystrixCommand or HystrixObservableCommand object
  2. Excuting an order
  3. Is the response cached?
  4. Is the circuit breaker open?
  5. Is the thread pool / queue / semaphore full?
  6. HystrixObservableCommand.construct () or HystrixCommand.run()
  7. Computing circuit health
  8. Get Fallback
  9. Return a successful response

1. Construct a HystrixCommand or HystrixObservableCommand object

  The first step is to construct a HystrixCommand or HystrixObservableCommand object to represent your request for dependencies. Pass any parameters needed to make a request to the constructor.

  HystrixCommand constructs an object if it expects the dependency to return a single response. E.g:

HystrixCommand command = new HystrixCommand(arg1, arg2);
 

  HystrixObservableCommand constructs an object if it expects the dependency to return an Observable that emits a response . E.g:

HystrixObservableCommand command = new HystrixObservableCommand(arg1, arg2);

 

2. Execute the command

  By using one of the following four methods of the Hystrix command object, you can execute the four methods of the command (the first two only apply to simple HystrixCommand objects, not to HystrixObservableCommand):

  execute () — block and then return the single response received from the dependency (or raise an exception if an error occurs)
  queue () — return a Future from which you can get the single response of the dependency
  observe () — subscribe, The Observable represents the response returned from the dependency and returns the Observable to copy the source. Observable
  toObservable ()-returns an Observable, when you subscribe to it, it will execute the Hystrix command and issue its response

K             value   = command.execute();
Future<K>     fValue  = command.queue();
Observable<K> ohValue = command.observe();         //hot observable
Observable<K> ocValue = command.toObservable();    //cold observable

  Call execute () synchronously to call queue (). Get (). queue () calls toObservable (). toBlocking (). toFuture () in turn. This means that in the end each implementation of HystrixCommand is supported by the Observable implementation, even those that are designed to return a single simple value.

3. Is the response cached?

  If request caching is enabled for this command, and if the response to the request is available in the cache, the cached response will be returned as Observable immediately.

4. Is the circuit breaker open?

  When you execute this command, Hystrix will check the circuit breaker to see if the circuit is open.

  If the circuit is open (or "tripped"), Hystrix will not execute the command, but will route the flow to (8) for a fallback.

  If the circuit is closed, the flow proceeds to (5) to check whether there is capacity available for running commands.

5. Is the thread pool / queue / semaphore full?

  If the thread pool and queue (or semaphore, if not running in a thread) associated with the command are full, Hystrix will not execute the command, but will immediately route the flow to (8) for a rollback.

6、HystrixObservableCommand.construct()或HystrixCommand.run()

  Here, Hystrix invokes a request for a dependency by a method written for this purpose (one of the following):

  HystrixCommand.run () — returns a single response or raises an exception
  HystrixObservableCommand.construct () — returns an Observable that sends a response or sends an onError notification
  If the run () or construct () method exceeds the command timeout value, the thread will throw TimeoutException (if the command itself is not running in its own thread, that thread will throw a separate timer thread). In that case, Hystrix responded with 8 routes. Get Fallback, if the method does not cancel / interrupt, it will discard the final return value of the run () or construct () method.

7. Computing circuit health

  Hystrix reports success, failure, rejection, and timeout to the circuit breaker, and the circuit breaker maintains a rolling set of counters to calculate statistics.

  It uses these statistics to determine when the circuit should "trip", at this point it will short circuit all subsequent requests until the recovery period has passed, after which, after first checking certain health checks, it will again Close the circuit.

8. Get Fallback

  Hystrix attempts to recover to your backup state when the command execution fails: when construct () or an exception run () is thrown (6.), the command is short-circuited due to a circuit disconnection (4.), the thread pool and queue or signal of the command The amount is the maximum capacity (5.), or the command has exceeded its timeout length.

  Write fallbacks to provide a generic response from cache in memory or through other static logic without any network dependencies. If you must use a network call in the fallback, you should use another HystrixCommand or HystrixObservableCommand.

  For HystrixCommand, to provide fallback logic, you can implement HystrixCommand.getFallback (), which returns a single fallback value.

  For HystrixObservableCommand, to provide fallback logic, you can implement HystrixObservableCommand.resumeWithFallback () the logic, which returns an Observable, which may emit one or more fallback values.

  If the fallback method returns a response, Hystrix will return this response to the caller. For a HystrixCommand.getFallback (), it will return an Observable, which emits the value returned from the method. In this case, HystrixObservableCommand.resumeWithFallback () will return the same Observable returned from the method.

  If a fallback method has not been implemented for the Hystrix command, or the fallback itself raises an exception, Hystrix will still return an Observable, but it will not issue anything and immediately terminate with an onError notification. With this onError notification, the exception that caused the command to fail is sent back to the caller. (Implementing a fallback implementation may fail, which is a bad practice. You should implement a fallback so that it does not execute any logic that might fail.)

9. Return a successful response

  If the Hystrix command is successfully executed, it will return one or more responses to the caller Observable in the form of

 

Guess you like

Origin www.cnblogs.com/h--d/p/12716997.html