Spring Cloud Alibaba [Cold start of flow control effect, queuing of flow control effect, current limit of hotspot parameters, thread isolation, circuit breaker downgrade, abnormal number of circuit breaker downgrade] (7)

 

Table of contents

Distributed traffic protection_cold start of flow control effect

Distributed traffic protection_Queuing and waiting for flow control effects

Distributed Traffic Protection_ Hotspot Parameter Current Limiting

Distributed Traffic Protection_Thread Isolation

Distributed traffic protection_fuse downgrade

Distributed traffic protection_Slow call of circuit breaker downgrade

 Distributed Traffic Protection_ Abnormal Ratio of Fuse Degradation

 Jemeter stress test

Distributed Traffic Protection_ Abnormal Number of Fuse Degradation 


 

Distributed traffic protection_cold start of flow control effect

Warm up is also called warm-up mode, which is a solution to cold start of services.

Case combat 

Requirements: Set the current limit for the resource /payment/index, the maximum QPS is 10, use the warm up effect, and the warm-up time is 5 seconds.

 

 Jmeter test

view result tree

 

Note: The QPS is 10. When it was just started, most of the requests failed, and only 3 succeeded, indicating that the QPS is limited to 3. As time goes by, the success rate is getting higher and higher. 

Real-time effect feedback

1. Sentienl flow control effect cold start mainly solves the _____ problem.

A for source configuration items 

B fail fast

The C service has just started, and all resources have not been initialized

All of the above are wrong

2. Sentienl flow control effect cold start is also called ____ mode.

A warm up

B fail fast

C association

D link

Distributed traffic protection_Queuing and waiting for flow control effects

Queuing is to let all requests enter a queue, and then execute them sequentially according to the time interval allowed by the threshold. Subsequent requests must wait for previous executions to complete, and will be rejected if the expected waiting time for the request exceeds the maximum duration. 

working principle 

For example: the QPS threshold is 5, which means that a request in the queue is processed every 200ms, and the timeout period is 2s. Now there are 100 requests, and the server can process up to 5, and the others are queued slowly.

 

If the queuing mode is not used, there are now 12 requests coming, and 10 requests are received at the same time in the first second, but only one request is received in the second second, and the QPS curve at this time is like this. 

 

If you use the queue mode for flow control, all incoming requests must be queued and executed at a fixed interval of 200ms, and the QPS will become very smooth. 

 

Note: A smooth QPS curve is more friendly to the server. 

Application Scenario 

Note: This method is mainly used to deal with intermittent burst traffic, such as message queue. Imagine a scenario where a large number of requests arrive in a certain second, and the next few seconds are idle. We hope that the system can gradually process these requests during the next idle period, instead of directly rejecting redundant requests in the first second. 

the case 

Requirements: Set a current limit for the resource /payment/queue, with a maximum QPS of 10, use the flow control effect of queuing, and set the timeout period to 5s.

Jmeter test 

The QPS is 15, which has exceeded the 10 we set. If it is the previous fast failure and warmup mode, the excess request should directly report an error. But let's look at the running results of the queue mode: 

 

Real-time effect feedback

1. Sentienl flow control effect queuing up mainly solves the _____ problem.

A uniform flow rate

B Steady and large flow

C burst traffic

D above are all wrong 

Distributed Traffic Protection_ Hotspot Parameter Current Limiting

The previous current limiting is to count all the requests to access a certain resource and judge whether it exceeds the QPS threshold. The hotspot parameter current limit is to count requests with the same parameter value separately , and judge whether it exceeds the QPS threshold. 

Global parameter current limit

For example, an interface for querying products by id:

In the request to access /goods/{id}, the value of the id parameter will change, and the hotspot parameter current limit will count the QPS separately according to the parameter value, and the statistical results are as follows: 

When the request with id=1 triggers the threshold to be limited, the request with id value other than 1 will not be affected.

Configuration example:

 

Note: Make statistics on parameter 0 (the first parameter) of the hot resource, and the number of requests for  the same parameter value per second cannot exceed 5

Hotspot parameter current limit 

In the configuration just now, all products of the interface for querying products are treated equally, and the QPS is limited to 1. In actual development, some products may be hot products, such as flash sale products. We hope that the QPS limit of these products is different from other products, and is higher. Then you need to configure the advanced options of hotspot parameter current limit:

Note: Combined with the previous configuration, the meaning here is to limit the current of the long type parameter of number 0, and the QPS of the same parameter cannot exceed 5 per second, with two exceptions: If the parameter value is 100, the allowed QPS per second is 10 If the parameter value is 101, the allowed QPS per second is 15 

case requirements 

Add a hotspot parameter current limit to the resource /order/findById, the rules are as follows:

The default hotspot parameter rule is that the number of requests per second should not exceed 2

Set an exception for the parameter 102: the number of requests per second should not exceed 4

Set an exception for the parameter 103: the number of requests per second should not exceed 10

Note: The hotspot parameter current limit is invalid for the default SpringMVC resource, and the @SentinelResource annotation needs to be used to mark the resource

@RestController
@RequestMapping("goods")
public class GoodsController {
/**
     * 热点key限流
     * @param id
     * @return
     */
    @GetMapping("findById")
    public String getGoods(String  id){
        return id;
   }
}

 Hotspot parameter current limiting rules

Jmeter test

 

Real-time effect feedback

1. Sentienl hotspot Key current limiting mainly solves the _____ problem. 

A uniform flow rate

B Upstream interface current limiting

C interface current limiting

D Separate statistics of requests with the same parameter value

2. How to configure the Sentienl resource to configure the hotspot Key current limiting rule and specify the specific parameter value as 102 for current limiting_____.

A parameter configuration

B flow control rules

C fuse rules

D permission rules 

Distributed Traffic Protection_Thread Isolation

Thread Isolation (Bulkhead Mode)

1. Thread pool isolation

2. Semaphore isolation (Sentinel uses it by default) 

Notice:

Thread pool isolation: assign a thread pool to each service call business, and use the thread pool itself to achieve the isolation effect

Semaphore isolation: Instead of creating a thread pool, it uses a counter mode to record the number of threads used by the business. When the upper limit of the semaphore is reached, new requests are prohibited. 

sentinel's thread isolation 

When adding a throttling rule, you can choose two threshold types:

Notice:

QPS: the number of requests per second

Number of threads: The maximum number of Tomcat threads that can be used by this resource. That is, by limiting the number of threads, thread isolation (bulkwall mode) is achieved. 

Flow control test 

new thread group 

We create a new thread group and send 10 requests at the same time when starting, as follows:

New HTTP request 

Create View Results Tree

 

Real-time effect feedback

1. How to implement semaphore thread isolation in Sentienl technology____.

A threshold type setting QPS

B threshold type sets the number of threads

C Select hotspot rules

D above are all wrong 

Distributed traffic protection_fuse downgrade

Fuse downgrade is an important means to solve the avalanche problem. The idea is that the circuit breaker counts the abnormal proportion of service calls and the proportion of slow requests, and if the threshold is exceeded, the service will be broken. That is, all requests to access the service are intercepted; and when the service is restored, the circuit breaker will release the request to access the service. 

Circuit breaker control fusing and release is done through the state machine:

The state machine consists of three states:

1. Closed: Closed state, the circuit breaker releases all requests, and starts to count the proportion of exceptions and slow requests. If the threshold is exceeded, switch to the open state

2. open: In the open state, the service call is interrupted, and the request to access the interrupted service will be rejected, fail quickly, and go directly to the downgrade logic. After 5 seconds in the Open state, it will enter the half-open state

3. half-open: In the half-open state, a request is released, and the next operation is judged according to the execution result. Successful request: switch to closed state Request failure: switch to open state 

Circuit breaker downgrade strategy 

slow call

A request whose service response time (RT) is greater than the specified time is considered a slow call request. Within the specified time, if the number of requests exceeds the set minimum number and the proportion of slow calls is greater than the set threshold, a circuit breaker will be triggered.

abnormal ratio, abnormal number 

Count the calls within the specified time. If the number of calls exceeds the specified number of requests and the proportion of abnormalities reaches the set ratio threshold (or exceeds the specified number of abnormalities), a circuit breaker will be triggered.

Real-time effect feedback

1. In Sentienl technology, the following belongs to the circuit breaker downgrade strategy is ____.

A slow call

B Abnormal proportion

C exception count

All of the above are correct

2. The fuse downgrade in Sentienl technology solves the problem of __.

A service single point of failure

B service high availability

C service response is slow

D service avalanche

Distributed traffic protection_Slow call of circuit breaker downgrade

Average Response Time When 5 requests continue to enter within 1 second, and the average response time (second level) at the corresponding time exceeds the threshold, then within the next time window, calls to this method will be automatically broken (throwing DegradeException). 

new interface

@GetMapping("/testC")
    public String testC(Integer id){
        if (id = 1){
          try {
            TimeUnit.SECONDS.sleep(1);
       } catch (InterruptedException e) {
            e.printStackTrace();
         }  
       }
        return "------------testC";
   }

Added RT configuration

Parameters: Requests exceeding 50ms will be considered as slow requests. When the abnormal ratio reaches 40%, the circuit breaker is opened (the fuse trips), the microservice is unavailable, and the fuse trips and the power is cut off. After 5 seconds, the circuit breaker is turned from the open state to the half-open state and some requests come in. 

Jemeter stress test 

Create thread group 

Set HTTP request path

 

test 

-Test/testC interface without using Jemeter

Test/testC interface with Jemeter.

 

result access failed

 

Note: I stopped Jmeter later, there was no such a large amount of visits, the circuit breaker was closed (the fuse was restored), and the microservice resumed OK. 

 Distributed Traffic Protection_ Abnormal Ratio of Fuse Degradation

overview 

When the ratio of the total number of resource exceptions per second to the throughput exceeds the threshold, the resource enters a degraded state. The threshold range for the outlier ratio is [0.0,1.0].

new interface

    /**
     * 测试异常比例
     * RT 平均响应时间
     * @return
     */
    @GetMapping("testD")
    public String  testD(Integer id) {
        if (id == 1){
            throw new RuntimeException("故意抛出异常,触发异常比例熔断。");
       }
        return "testD";
   }

 Set circuit breaker rules

Note: In 5 requests, as long as the exception ratio exceeds 0.4, that is, there are more than 2 exceptions, the circuit breaker will be triggered. 

 Jemeter stress test

Create thread group 

Configure HTTP requests 

test interface 

Send request localhost:8001/testD?id=2

Distributed Traffic Protection_ Abnormal Number of Fuse Degradation 

concept 

Number of exceptions: When the number of exceptions in the resource in the past 1 minute exceeds the threshold, a circuit breaker will be performed.

new interface

/*
     * 测试异常数
     */
    @GetMapping("/testF")
    public String testF()
   {
        int age = 10/0;
        return "------testF 测试异常数";
   }

Configure outlier rules

Note: Set exception number 5. 

test 

Request http://localhost:8001/testF, the first visit will definitely report an error, because the divisor cannot be zero, we see the error window

 

But after reaching 5 error reports, it will be downgraded after entering the fuse. 

 

Guess you like

Origin blog.csdn.net/m0_58719994/article/details/131860217