[SpringCloud Alibaba] (7) Learning Sentinel core technology and configuration rules (Part 1)

In the previous article, we implemented
remote calls between user microservices, product microservices and order microservices, and implemented load balancing of service calls. It also implements current limiting and fault tolerance of services based on Ali's open source Sentinel.

Today, let’s talk with you about Sentinel’s core technology and configuration rules.

1. Sentinel core functions

With the popularity of microservices, the stability between services and services has become more and more important. Sentinel takes traffic as the entry point to protect the stability of services from multiple dimensions such as traffic control, circuit breaker degradation, and system load protection.

Therefore, Sentinel’s core functions include: flow control, circuit breaker degradation, and system load protection.

1.1 Flow control

In high-concurrency and high-traffic scenarios, if the traffic entering the system is not controlled, the system is likely to be overwhelmed by the traffic. Therefore, before the flow officially enters the system, it is necessary to control the flow so that the flow can enter the system in a uniform and controllable manner.

As an excellent fault-tolerant component, Sentinel can process uncontrollable traffic and transform it into uniform and controllable traffic.

1.2 Circuit breaker degradation

If it is detected that a node in a call link in the system fails, such as request timeout, service downtime, or abnormality ratio exceeds a certain threshold, the call frequency of the failed node will be limited, or even no calls will occur. Faulty nodes allow requests to fail and return quickly to avoid affecting the services of other nodes and causing cascading failures of the system to the greatest extent.

Sentinel mainly degrades resource access by limiting the number of concurrent threads and response time.

Limit the number of concurrent threads for downgrade

Sentinel can reduce the impact on other service nodes by limiting the number of concurrent threads on the service node. For example, when a service node fails, for example, the response time becomes longer, or it goes down directly. At this time, the direct impact on the service is that the number of request threads will continue to accumulate. If the number of these accumulated threads reaches a certain number, subsequent requests to the current service node will be rejected. Wait until the accumulated threads complete their tasks before continuing to receive new requests.

Downgrade via response time

In addition to downgrading by limiting the number of concurrent threads, Sentinel can also be downgraded by response time. If the response time of a dependent service is too long, all requests to the service will be rejected, and the service cannot be accessed again until the specified time window has passed.

1.3 System load protection

Sentinel provides system-dimensional adaptive protection capabilities. When the pressure and load of the system are relatively high, if a large number of requests continue to enter the system, the system may be overwhelmed and cause system downtime.

Sentinel will forward the traffic that should be carried by server A to other servers, such as server B, in a cluster environment. If server B is also under high load at this time, Sentinel will provide corresponding protection mechanisms to balance the inlet traffic of the system and the overall load of the system, making the overall system available and able to process requests to the maximum extent.

2. Sentinel Core Rules

Sentinel's core rules include flow control rules, circuit breaker rules, hotspot rules, authorization rules and system rules . Each rule is configured differently.

Next, we will introduce in detail the functions and effects of each rule in Sentinel.

2.1 Flow control rules

Sentinel can control traffic, mainly monitoring indicators such as QPS traffic or the number of concurrent threads of the application. If it reaches the specified threshold, it will be controlled by the traffic to avoid the service being overwhelmed by the instantaneous high concurrent traffic and ensure the service quality. High reliability

2.1.1 Cluster point link rules

1. Click the cluster point link menu to see the interfaces you have visited before, as shown below:

insert image description here
2. Click the flow control button on the right, and a prompt box for adding new flow control rules will pop up, as shown below:

insert image description here
Here, the description of each configuration item is as follows:

  • Resource name: The unique name of the resource. The default is the requested interface path. You can modify it yourself, but it must be unique.
  • Targeting the source: Limit the current for a specific microservice. The default value is default, which means that the source is not distinguished and all current is limited.
  • Threshold type: QPS means current limiting through QPS, and the number of concurrent threads means current limiting through the number of concurrent threads.
  • Standalone threshold: Used in combination with threshold type. If the threshold type is QPS, it means that when the QPS of the calling interface reaches the threshold, the current limiting operation will be performed. If the threshold type selects the number of concurrent threads, it means that when the number of concurrent threads calling the interface reaches the threshold, the current limiting operation will be performed.
  • Whether to cluster: If selected, it indicates a cluster environment, if not selected, it indicates a non-cluster environment.

2.1.2 Configure simple current limiting

Here, http://localhost:8080/order/test_sentinela simple configuration is performed for the interface. In the new flow control rule, select QPS as the threshold type and enter 3 for the single-machine threshold. This means that if the number of requests per second exceeds 3, Sentinel's current limiting operation will be triggered.

insert image description here

After clicking the Add button, http://localhost:8080/order/test_sentinela new current limiting rule will be added to the interface, as shown below:

insert image description here

Next, quickly refresh the interface on the browser http://localhost:8080/order/test_sentinel. When the refresh frequency exceeds 3 times per second, the following prompt message will appear:

insert image description here

2.1.3 Configure flow control mode

Click http://localhost:8080/order/test_sentinelthe edit button behind the interface flow control rule to open the edit flow control rule pop-up box (if this is the first configuration, it is the new flow control rule pop-up box). Click Advanced Options Configuration to display the following interface
insert image description here
:

insert image description here
As you can see, Sentinel mainly provides three flow control modes, namely direct, association and link :

  • Direct: The default flow control mode. When the interface reaches the current limiting condition, the current limiting function is directly enabled.
  • Association: When the associated resource reaches the current limiting condition, the current limiting function is enabled.
  • Link: When the resources requested from an interface reach the current limiting condition, the current limiting function is enabled.

2.1.3.1 Demonstrate direct flow control mode

Sentinel uses the direct flow control mode by default. What we have integrated in the order microservice before is Sentinel's direct flow control mode.

In the flow control rules of this article - configuring simple flow limiting , the direct flow control mode is also used, which will not be described again here.

2.1.3.2 Demonstrate associated flow control mode

1. Add an interface to the iOrderController class of the order microservice /test_sentinel2, as shown below:

2. Access the following interface on the browser http://localhost:8080/order/test_sentinel2so that Sentinel can detect this interface

3. Return to http://localhost:8080/order/test_sentinelthe page for configuring flow control rules for the interface, as shown below:

insert image description here

4. Select association in the flow control mode , and enter in the associated resource /test_sentinel2, as shown below

insert image description here
Click the Save button to save the configuration.

5. Open JMeter and http://localhost:8080/order/test_sentinel2test the interface so that the number of visits per second is greater than 3. The specific configuration of JMeter is as follows:

insert image description here
Configure 4 accesses per second in the thread group

insert image description here
6. Refresh the interface on the browser http://localhost:8080/order/test_sentineland find that the current limiting function of Sentinel has been triggered:

insert image description here
At this point, the demonstration of the associated flow control mode is completed.

2.1.3.3 Demonstration link flow control mode

1. Add an interface under the order microservice com.zzc.servicepackage . As follows:SentinelServiceSentinelServiceImpl

@Service
public class SentinelServiceImpl implements SentinelService {
    
    

    @Override
    @SentinelResource("sendMessage")
    public void sendMessage() {
    
    
        System.out.println("测试Sentinel的链路流控模式");
    }
}

We sendMessage()use @SentinelResourceannotations on methods. @SentinelResourceAnnotations will be introduced in subsequent articles and will not be repeated here.

2. Add it under the order microservice com.zzc.controllerpackage SentinelController. As follows:

@Slf4j
@RestController
public class SentinelController {
    
    

    @Autowired
    private SentinelService sentinelService;

    @GetMapping(value = "/request_sentinel1")
    public String requestSentinel1(){
    
    
        log.info("测试Sentinel1");
        sentinelService.sendMessage();
        return "sentinel1";
    }

    @GetMapping(value = "/request_sentinel2")
    public String requestSentinel2(){
    
    
        log.info("测试Sentinel2");
        sentinelService.sendMessage();
        return "sentinel2";
    }

}

3. Modify the pom.xml configuration of the shop-springcloud-alibaba parent project. Upgrade the dependency version of SpringCloud Alibaba to 2.2.7.RELEASE, upgrade the SpringCloud version to Hoxton.SR12, and add the management dependency of SpringBoot. The modified configuration file looks like this:

<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring-cloud.version>Hoxton.SR12</spring-cloud.version>
    <spring-cloud-alibaba.version>2.2.7.RELEASE</spring-cloud-alibaba.version>
    <spring.boot.version>2.3.12.RELEASE</spring.boot.version>
    <logback.version>1.1.7</logback.version>
    <slf4j.version>1.7.21</slf4j.version>
    <common.logging>1.2</common.logging>
    <fastjson.version>1.2.51</fastjson.version>
    <mybatis.version>3.4.6</mybatis.version>
    <mybatis.plus.version>3.4.1</mybatis.plus.version>
    <mysql.jdbc.version>8.0.19</mysql.jdbc.version>
    <druid.version>1.1.10</druid.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>${spring.boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>${spring-cloud-alibaba.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

4. Upgrade Nacos, upgrade the Nacos registration center from version 1.4.3 to version 2.1.0, and enter the bin directory of Nacos to start Nacos

5. Add link configuration in the application.yml file of the order microservice, as shown below:

spring:
  cloud:
    sentinel:
      web-context-unify: false

http://localhost:8080/order/request_sentinel17. Visit and respectively in the browser
http://localhost:8080/order/request_sentinel2to view the cluster point links in Sentinel, as shown below:

insert image description here
8. Click the flow control button behind sendMessage, as shown below:

insert image description here

9. In the pop-up new flow control rule edit box, select QPS for the threshold type, enter 3 for the single-machine threshold, select link for the flow control mode in the advanced options that opens, and enter the entrance resource, as shown below /request_sentinel1:

insert image description here

Click the Add button to save the configuration.

At this time, if the method is http://localhost:8080/order/request_sentinel1called through the interface com.zzc.service.SentinelService#sendMessage(), if the call frequency exceeds 3 times per second, Sentinel's current limiting operation will be triggered;

http://localhost:8080/order/request_sentinel2When calling methods through the interface
com.zzc.service.SentinelService#sendMessage(), there is no restriction

10. If you continue to access in the browser http://localhost:8080/order/request_sentinel1and the number of accesses per second exceeds 3, Sentinel's current limiting operation will be triggered, as shown below:

insert image description here

The access http://localhost:8080/order/request_sentinel2interface will not be limited.

At this point, the link flow control mode demonstration is completed.

Additional information

There are also three flow control effects in the advanced options of flow control rules, as shown below:

insert image description here
Next, a brief explanation of these three options:

  • Fast failure : It will fail directly and throw an exception, and no other processing operations will be done during the process.
  • Warm Up : There will be a buffer from the start threshold to the maximum QPS threshold. You can set a warm-up time. This option is more suitable for scenarios with sudden high concurrent traffic. It can convert sudden high concurrent traffic into even and slowly growing traffic. Scenes
  • Queuing and waiting : It can make requests pass evenly. The threshold of a single machine is the number of requests passed per second, and the remaining requests will be queued and waited. In addition, a timeout period will also be set. When the request exceeds the timeout period and is not processed, it will be discarded.

2.2 Circuit breaker rules

Generally speaking, downgrade rules refer to degrading services when certain conditions are met.

Sentinel mainly provides three circuit breaker strategies, namely: slow call ratio, exception ratio and number of exceptions.
insert image description here

2.2.1 Demo based on slow call ratio circuit breaker

1. First access it in the browser http://localhost:8080/order/request_sentinel2and find it in the cluster point link of Sentinel./request_sentinel2

insert image description here

2. Click the circuit breaker button to enter the circuit breaker rule configuration box and configure it as follows:

insert image description here

The above configuration indicates that the maximum response time is 1ms. When the proportion threshold reaches 0.1, the circuit breaker operation will be triggered, and the circuit breaker duration is 2s. The minimum number of requests is 5, and the statistical duration is 1000ms.

3. After clicking the Add button, keep refreshing in the browser http://localhost:8080/order/request_sentinel2, and you will find that the circuit breaker operation of Sentinel is triggered, as shown below:

insert image description here

2.2.2 Demonstration based on abnormal proportion of circuit breaker

com.zzc.controller.SentinelController1. Define a member variable count in the order microservice class to record the access count. At the same time, add a new requestSentinel4()method to test the circuit breaker based on the abnormal ratio, as shown below:

private int count;

@GetMapping(value = "/request_sentinel4")
@SentinelResource("request_sentinel4")
public String requestSentinel4(){
    
    
    log.info("测试Sentinel4");
    count++;
    //模拟异常,比例为50%
    if (count % 2 == 0){
    
    
        throw new RuntimeException("演示基于异常比例熔断");
    }
    return "sentinel4";
}

2. First access it in the browser http://localhost:8080/order/request_sentinel4and find it in the cluster point link of Sentinel./request_sentinel4

3. Click the circuit breaker button to enter the circuit breaker rule configuration box and configure it as follows:


In com.zzc.controller.SentinelController#requestSentinel4()the method, the exception ratio is set to 50%. The exception ratio set in Sentinel's ratio threshold is 0.3, which is 30%. Therefore, when accessing the interface, http://localhost:8080/order/request_sentinel4Sentinel's circuit breaker operation will be triggered.

4. After clicking the Add button, keep refreshing in the browser http://localhost:8080/order/request_sentinel4, and you will find that the circuit breaker operation of Sentinel is triggered, as shown below:

insert image description here

2.2.3 Demonstrate circuit breaker based on abnormal number

1. First access it in the browser http://localhost:8080/order/request_sentinel4and find it in the cluster point link of Sentinel./request_sentinel4

2. Click the circuit breaker button to enter the circuit breaker rule configuration box and configure it as follows:

insert image description here
The above configuration indicates that there are at least 2 requests within 1 second. When the number of exceptions is greater than 1, the circuit breaker operation will be triggered. The circuit breaker lasts for 5 seconds.

3. After clicking the Add button, keep refreshing in the browser http://localhost:8080/order/request_sentinel4, and you will find that the circuit breaker operation of Sentinel is triggered, as shown below:

insert image description here

code address

The code has been uploaded to Code Cloud, Code Cloud address

Among them, the database file is located dbunder the folder.

Guess you like

Origin blog.csdn.net/sco5282/article/details/131991413