“This is the fourth day of my participation in the August Gwen Challenge.


In microservices scenarios, there are often many layers of service invocations. If an underlying service fails, the failure is propagated upward to the user. We need a mechanism to stop the spread of failures when the underlying services are not available. That’s what a circuit breaker does. It is the last guarantee of system service stability.

The circuit breaker component in springcloud is Hystrix. Hystrix is also part of the Netflix suite. His function is, when the calls to a service in a certain period of time (the default of 10 s, by the metrics. RollingStats. TimeInMilliseconds configuration), more than a certain number of times (the default 20 times, By the circuitBreaker. RequestVolumeThreshold parameters configuration) and failure rate exceeds a certain value (the default 50%, by the circuitBreaker. ErrorThresholdPercentage configuration), the service of circuit breaker opens. Returns a fallback set by the developer

How to use

How to use not much nonsense, direct official website plus Springboot, a few minutes will be, mainly introduced the following parameters is the most effective

Parameters,

Hystrix.com mand. Default is corresponding hystrix.command.com actually mandkey, commandkey is used to define the command

Hystrix. Threadpool. The default is the corresponding is hystrix.com mand threadpoolkey

Command Properties

Configuration of Execution related properties

  • Hystrix.com mand. Default. Execution. The isolation. The strategy isolation strategy, Thread by default, the optional Thread | Semaphore
  • Hystrix.com mand. Default. Execution. The isolation. Thread. TimeoutInMilliseconds command timeout, the default 1000 ms
  • Hystrix.com mand. Default. Execution. A timeout. Whether to enable timeout enabled implementation, enabled by default is true
  • Hystrix.com mand. Default. Execution. The isolation. Thread. InterruptOnTimeout timeouts are disrupted, the default is true
  • Hystrix.com mand. Default. Execution. The isolation. Semaphore. MaxConcurrentRequests maximum number of concurrent requests, the default 10, This parameter when using ExecutionIsolationStrategy. The SEMAPHORE strategy is effective. If the maximum number of concurrent requests is reached, the request is rejected. Theoretically, the principle of selecting Semaphoresize is the same as that of selecting Thread size. However, if semaphore is selected, each execution unit should be small and the execution speed should be fast (ms level); otherwise, Thread should be used.

    Semaphore should be a small part of the overall container (Tomcat) thread pool.

Fallback related properties

These parameters can be applied to Hystrix’s THREAD and SEMAPHORE policies

  • Hystrix.com mand. Default. Fallback. Isolation. Semaphore. MaxConcurrentRequests if concurrency reaches the set value, the request is rejected and throw an exception and fallback will not be invoked. The default 10
  • Hystrix.com mand. Default. Fallback. Enabled when the failure or the request was refused, will try to call hystrixCommand. GetFallback (). The default true

Circuit Breaker related attributes

  • Hystrix.com mand. Default. CircuitBreaker, enabled to track circuit of health, if not done to request a short-circuit. The default true
  • Hystrix.com mand. Default. CircuitBreaker. RequestVolumeThreshold a rolling window within the minimum number of requests. If set to 20, then when 19 requests are received in a rolling Window time (say, 10 seconds for a Rolling Window), a circuit break will not be triggered even if all 19 requests fail. The default 20
  • Hystrix.com mand. Default. The circuitBreaker. SleepWindowInMilliseconds trigger the time value of the short circuit, when this value is set to 5000, If the circuit break is triggered, the request will be rejected within 5000 milliseconds, that is, the circuit will be closed after 5000 milliseconds. The default is 5000
  • Hystrix.com mand. Default. The circuitBreaker. ErrorThresholdPercentage error ratio threshold, if the error rate > = the value, circuit is open and short-circuit all requests and trigger a fallback. The default 50
  • Hystrix.com mand. Default. The circuitBreaker. ForceOpen forced open the fuse, if you open the switch, then reject all request, false by default
  • Hystrix.com mand. Default. The circuitBreaker. ForceClosed shutdowns of fuse If the switch is open, Circuit will have been shut down and ignore the circuitBreaker errorThresholdPercentage

Metrics Related parameters

  • Hystrix.com mand. Default. The metrics. RollingStats. TimeInMilliseconds set statistics window of time value, millisecond value, The opening of circuit break will be calculated according to the statistics of one Rolling Window. If Rolling Window is set to 10000 ms, rolling Window will be divided into N buckets, and each bucket contains the statistics of The Times of success, failure, timeout and rejection. The default is 10000
  • Hystrix.com mand. Default. The metrics. RollingStats. NumBuckets set up a rolling window is divided into the number of, if numBuckets = 10, rolling window = 10000, The time of a bucket is then 1 second. Must match rolling Window % numberBuckets == 0. The default 10
  • Hystrix.com mand. Default. The metrics. RollingPercentile. Enabled if executes the enable index calculation and tracking, the default is true
  • Hystrix.com mand. Default. The metrics. RollingPercentile. TimeInMilliseconds set rolling the percentile window of time, the default of 60000
  • Hystrix.com mand. Default. The metrics. RollingPercentile. NumBuckets set rolling the percentile numberBuckets window. Same logic. The default 6
  • Hystrix.com mand. Default. The metrics. RollingPercentile. BucketSize if the bucket size = 100, window = 10 s, if are there 500 times in the 10 s, Only the last 100 executions are counted in the bucket. Increasing this value increases memory overhead as well as sorting overhead. The default is 100
  • Hystrix.com mand. Default. The metrics. HealthSnapshot. IntervalInMilliseconds record health snapshot (for statistical success and error rate) of interval, the default 500 ms

Request Context parameters

  • Hystrix.com mand. Default. RequestCache. Enabled by default true, be overloaded getCacheKey (), returns null when no cache
  • Hystrix.com mand. Default. RequestLog. Enabled logging to HystrixRequestLog, true by default

Collapser Properties related parameters

  • Hystrix. Collapser. Default. MaxRequestsInBatch single batch the maximum number of requests, to reach the trigger the batch number, the default Integer. MAX_VALUE
  • Hystrix. Collapser. Default. TimerDelayInMilliseconds trigger delay of the batch, can also create a batch of time + the value, the default 10
  • Hystrix. Collapser. Default. RequestCache. If enabled for HystrixCollapser. The execute () and HystrixCollapser. Queue () cache, true by default

Parameter description ThreadPool parameters

The default number of threads is 10, which works for most cases (sometimes smaller), but if larger is required, there is a basic formula to follow:

Requests per second at peak when healthy × 99th percentile Latency in seconds + Some Breathing room maximum support requests per second (99% average response time + cache value) For example, if 1000 requests can be processed per second, 99% of the request response time is 60ms, then the formula is: (0.060+0.012) The basic principle is to keep the thread pool as small as possible, which is mainly to relieve pressure and prevent resources from blocking. When all is well, the thread pool will typically have only one or two threads active to serveCopy the code
  • Hystrix. Threadpool. Default. The maximum number of threads coreSize concurrent execution, 10 by default
  • Hystrix. Threadpool. Default. MaxQueueSize BlockingQueue the maximum number of queue, when set to 1, will use SynchronousQueue will, value use LinkedBlcokingQueue for timing. This setting will only be effective on initialization and thereafter you cannot change threadPool queue size unless reinitiinitializing thread executor. 1 by default.
  • Hystrix. Threadpool. Default. QueueSizeRejectionThreshold even maxQueueSize not reached, after reaching queueSizeRejectionThreshold the value, the request will be rejected. Because maxQueueSize cannot be modified dynamically, this parameter will allow us to set the value dynamically. If maxQueueSize == -1, this field will have no effect
  • Hystrix. Threadpool. Default keepAliveTimeMinutes if corePoolSize and maxPoolSize set (the default implementation) as the setting is invalid. If available via (plugin)[github.com/Netflix/Hys…