Hytrix service degradation and fusing

Posted by mahenderp on Wed, 09 Feb 2022 06:34:59 +0100

Hystrix service degradation

First, open Hystrix in the startup class and add @ EnableHystrix annotation on the startup class

Demote with @ HystrixCommand annotation

service degradation

@FeignClient(value = "dm-admin-service", fallbackFactory = AdminFeignServiceFallbackFactory.class)
public interface AdminFeignService 
    @RequestMapping(value = "/account/info", method = RequestMethod.POST)
    Object getAccountInfo(@RequestBody SessionPARM sessionPARM);

public class AdminFeignServiceFallbackFactory implements FallbackFactory<AdminFeignService> 
    public AdminFeignService create(Throwable arg0) {
        return new AdminFeignService() {
            public Object getAccountInfo(SessionPARM sessionPARM) {
                throw new RuntimeException(arg0);

Open Hystrix in YAML

    enabled: true #Open hystrix in feign

Service fuse

Add @ HystrixCommand directly to the method

public class PaymentController
	private PaymentService paymentService;
@HystrixProperty(name="circuitBreaker.enable",value="true"),//Is the circuit breaker on
@HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value="20"),//Number of requests
@HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value="10000"),//Time window period
@HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value="60"),//What is the failure rate and trip 60%
	public String paymentInfo(@PathVariable("id) Integer id)
		return paymentService.paymentInfo(id);
	public String paymentTimeOut(@PathVariable("id) Integer id)
		return paymentService.paymentTimeOut(id);
	public String payment_Global_FallbackMethod()
		return "Global degradation"

Fuse summary:

1, When does the circuit breaker start to work:
Three important parameters of circuit breaker are involved: snapshot time window, threshold value of total requests and threshold value of error percentage.
1. Snapshot time window: the circuit breaker needs to count some request and error data to determine whether to open, and the statistical time range is the snapshot time window, which defaults to the last 10 seconds

2. Threshold of total requests: within the snapshot time window, the threshold of total requests must be met to be eligible for fusing. The default is 20, which means that if the number of calls of the hystrix command is less than 20 within 10 seconds, the circuit breaker will not open even if all requests timeout or fail for other reasons.

3. Error percentage threshold: when the total number of requests exceeds the threshold within the snapshot time, for example, 30 calls occur. If the timeout exception occurs 15 times in this call, that is, the error percentage exceeds 50%, the circuit breaker will be opened under the default setting of 50%.

2, Conditions for fuse opening or closing:
1. When a certain threshold is met (more than 20 requests in 10 seconds by default)
2. When the failure rate reaches a certain level (more than 50% requests fail within 10 seconds by default)
3. When the above threshold is reached, the circuit breaker will open
4. When the circuit breaker is on, all requests will not be forwarded
5. After a period of time (5 seconds by default), the circuit breaker is half open at this time, which will allow one of the requests to be forwarded. If successful, the circuit breaker will close. If failed, it will continue to open. Repeat 4 and 5

3, After the circuit breaker is opened

1. When another request is called, the main logic will be called, but the degraded fallback will be called directly. Through the circuit breaker, the automatic error detection is realized, and the degraded logic is switched to the main logic to reduce the response delay.

2. How to restore the original main logic?
For this problem, hystrix also realizes the automatic repair function for us.
When the circuit breaker is opened and the main logic is fused, hystrix will start a sleep time window. In this time window, the degraded logic is temporary and becomes the main logic. When the sleep time window expires, the circuit breaker will enter the semi open state and release a request to the original main logic. If the request returns normally, the circuit breaker will continue to close and the main logic will recover, If there is still a problem with this request, the circuit breaker will continue to open and the sleep time window will be timed again.

Topics: Spring Cloud