Teach you how to build a SpringCloud project (11) Integrating Hystrix's service fuse

What are microservices? A series will be seen at a glance!

1. Teach you how to build a SpringCloud project (1) Detailed explanation with pictures and texts, fool-like operation

2. Teach you how to build a SpringCloud project (2) Producers and consumers

3. Teach you how to build a SpringCloud project (3) Integrate the Eureka service registration center

4. Teach you how to build the SpringCloud project (4) Eureka cluster version construction

5. Teach you how to build the SpringCloud project (5) Build the producer cluster version

6. Teach you how to build a SpringCloud project (6) Eureka realizes service discovery

7. Teach you how to build a SpringCloud project (7) Integrate the Consul service registration center

8. Teach you how to build a SpringCloud project (8) Integrated Ribbon load balancer

9. Teach you how to build a SpringCloud project (9) Integrate OpenFeign service interface calls

10. Teach you how to build a SpringCloud project (10) Integrate Hystrix service downgrade

11. Teach you to build a SpringCloud project (11) Integrating Hystrix's service fuse

12. Teach you how to build a SpringCloud project (12) Integrate Hystrix's graphical Dashboard real-time monitoring

13. Teach you how to build a SpringCloud project (13) Integrate a new generation of Gateway

14. Teach you how to build a SpringCloud project (14) Integrated Config Distributed Configuration Center

15. Teach you how to build a SpringCloud project (15) Integrated Bus message bus

16. Teach you how to build a SpringCloud project (16) Integrated Stream message driver

17. Teach you how to build a SpringCloud project (17) Integrating Sleuth distributed link tracking

Continue to update, welcome to like and follow!
In this article, to learn about service fuses, first understand a few concepts. ( partial summary, click to view )

What is a circuit breaker?

The "circuit breaker" itself is a switching device. When a service unit fails to monitor (similar to a blown fuse), it returns an expected and processable alternative response (FallBack) to the calling method instead of waiting for a long time Or throw an exception that cannot be handled by the calling method, so as to ensure that the thread of the service caller will not be occupied for a long time and unnecessarily, thereby avoiding the spread of faults in the distributed system. Even an avalanche.

What is the fuse mechanism?

The fuse mechanism is a microservice link protection mechanism to deal with the avalanche effect. When a microservice of the fanout link fails or the response time is too long, the service will be downgraded, and then the call of the node microservice will be broken. Quickly return the wrong response information, and when it is detected that the microservice call response of the node is normal, the call link is restored.

In the Spring Cloud framework, the fuse mechanism is implemented through Hystrix. Hystrix will monitor the status of microservice calls. When the failed calls reach a certain threshold, the default is 20 call failures within 5 seconds, and the fuse mechanism will be activated. The annotation of the fuse mechanism is @HystrixCommand.

1. Example of code implementation

Modify the business class PaymentService in the service of the newly created producer cloud-provider-hystrix-payment8001 in our previous article. The logic in this paymentCircuitBreaker method is to pass a parameter id. When id<0, a runtime exception will be thrown , otherwise the condition is met and a uuid is printed. Specifically configure. As shown below:

@HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = {
    
    
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),// 是否开启断路器
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),// 请求次数
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"), // 时间窗口期
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"),// 失败率达到多少后跳闸
    })
    public String paymentCircuitBreaker( Integer id)
    {
    
    
        if(id < 0)
        {
    
    
            throw new RuntimeException("******id 不能负数");
        }
        String serialNumber = IdUtil.simpleUUID();
        return Thread.currentThread().getName()+"\t"+"调用成功,流水号: " + serialNumber;
    }
//兜底降级的方法
 
    public String paymentCircuitBreaker_fallback(Integer id)
    {
    
    
        return "id 不能负数,请稍后再试,/(ㄒoㄒ)/~~   id: " +id;
    }

The three most commonly used important indicator parameters when using Hystrix circuit breakers

When using Hystrix as a circuit breaker in microservices, it usually involves the following three important indicator parameters (here are written in the @HystrixProperties annotation, of course, the actual project can be configured globally in yml or properties)

1、circuitBreaker.sleepWindowInMilliseconds

The snapshot time window of the circuit breaker is also called the window period. It can be understood as a cycle time value to trigger the circuit breaker, and the default is 10 seconds (10000).

2、circuitBreaker.requestVolumeThreshold

The request threshold for triggering a circuit breaker within the window period of the circuit breaker, the default is 20. In other words, if the total number of requests in a certain window period is less than the configured value, then the circuit breaker is not even eligible to occur. The circuit breaker will not be opened during this window period.

3、circuitBreaker.errorThresholdPercentage

The error percentage threshold that can be tolerated during the window period of the circuit breaker, the default is 50 (that is to say, a 50% error rate is tolerated by default). For example, if there are 100 service requests during a window period, 50 of them have errors. In such a case, the circuit breaker will be opened. Before the end of the window period, even if no exception occurs in the 51st request, the fallback logic will be executed.

To sum up, when the above three parameters are defaulted, the default strategy triggered by the Hystrix circuit breaker is:

In 10 seconds, when more than 20 requests occur, if the error rate reaches more than 50%, the circuit breaker will be opened. (When a window period passes, the circuit breaker will become half-open (HALF-OPEN) state, if the request that occurs at this time is normal, it will be closed, otherwise it will be opened again)

Then call it at the control layer, as shown in the figure below:

 @GetMapping("/payment/circuit/{id}")
    public String paymentCircuitBreaker(@PathVariable("id") Integer id)
    {
    
    
        String result = paymentService.paymentCircuitBreaker(id);
        log.info("****result: "+result);
        return result;
    }

Start the Eureka7001 service and the 8001 service, and then visit http://localhost:8001/payment/circuit/1, the passed parameter id is 1, which is greater than 0, so the serial number uuid will be returned. As shown below:
insert image description here

Then we pass the parameter id a negative number to visit http://localhost:8001/payment/circuit/-1, and it will return a prompt of exception at runtime. As shown below:

insert image description here

Next, we will test. Since the above configuration is accessed 10 times within 10 seconds, if there are more than 6 errors, our circuit breaker will be opened. Opening the circuit breaker means that we pass the correct parameters and return the flow if it fails number uuid. As shown below:
insert image description here

This means that the circuit breaker is turned on, and then as time increases and our correct access, the fault tolerance rate becomes lower, it slowly recovers, and the state of the circuit breaker becomes a half-open state, and as the correct rate increases, it will automatically close . You can access it correctly, as shown in the figure below:
insert image description here

Three types of fusing:

熔断打开:请求不再调用当前服务,内部设置一般为MTTR(平均故障处理时间),当打开时长达到所设时钟则进入半熔断状态。

熔断关闭:熔断关闭不会对服务进行熔断。

熔断半开:部分请求根据规则调用当前服务,如果请求成功且符合规则认为当前服务恢复正常,关闭熔断。

insert image description here

What are the conditions for a circuit breaker to open or close?

1. When a certain threshold is met (the default is more than 20 requests in 10 seconds)
2. When the failure rate reaches a certain level (the default is more than 50% of the requests in 10 seconds)
3. When the above threshold is reached, the circuit breaker It will be turned on
4. When it is turned on, all requests will not be forwarded
5. After a period of time (default 5 seconds), the circuit breaker is half-opened at this time, and another request will be forwarded
6. If successful, the circuit breaker The device will be closed, if it fails, continue to open and repeat steps 4 and 5

What happens after the circuit breaker is opened?

1. When there is a request to call again, the main logic will not be called, but the downgrade fallback will be called directly. Through the circuit breaker, the error is automatically found and the downgrade logic is switched to the main logic to reduce the effect of response delay.
2. How to restore the original main logic?
For this problem, Hystrix also implements an automatic recovery function for us. When the circuit breaker is opened and the main logic is fused, Hystrix will start a sleep time window. During this time window, the degraded logic temporarily becomes the main logic. When the sleep time window expires, the circuit breaker will enter a half-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 resume. If there is still a problem with this request , the circuit breaker continues to enter the open state, and the sleep time window is restarted.

Hystrix workflow

insert image description here

The flowchart on the official website describes the workflow of Hystrix:

1、每次调用都会创建一个HystrixCommand
2、执行execute或queue做同步\异步调用
3、判断熔断器是否打开,如果打开跳到步骤8,否则进入步骤4
4、判断线程池/信号量是否跑满,如果跑满进入步骤8,否则进入步骤5
5、调用HystrixCommand的run方法,如果调用超时进入步骤8
6、判断是否调用成功,返回成功调用结果,如果失败进入步骤8
7、计算熔断器状态,所有的运行状态(成功, 失败, 拒绝,超时)上报给熔断器,用于统计从而判断熔断器状态
8、降级处理逻辑,根据上方的步骤可以得出以下四种情况会进入降级处理:熔断器打开、线程池/信号量跑满、调用超时、调用失、
9、返回执行成功结果

Hystrix's service fuse is probably learned here, and it is so easy!

insert image description here

The next article will continue to learn Hystrix's graphical Dashboard real-time monitoring, the article will continue to be updated, welcome to pay attention!

Guess you like

Origin blog.csdn.net/weixin_39570655/article/details/131824189