Sentinel fuse for microservice protection


foreword

        In the microservice system based on SpringCloud, the call link between services will become longer and longer as the system evolves, which will undoubtedly increase the unreliability of the entire system. In the case of relatively high concurrent traffic, due to a certain timeout period between network calls, the downtime of a certain service in the link will greatly increase the response time of the entire call link, and the instantaneous traffic peak will cause this. The available thread resources of all services on a link are fully utilized, resulting in the unavailability of the overall service, which is what we often call the "avalanche effect". In the process of microservice system design, in order to deal with such a bad situation, the most common method is to implement "flow control" and "fuse downgrade" for network service calls. Therefore, Sentinel was born out of luck.

        Sentinel is a lightweight traffic control component for distributed service architecture. It mainly uses traffic as the entry point to ensure service stability from multiple dimensions such as traffic control, fuse degradation, and system adaptive protection. The core idea is: Execute corresponding flow control/degradation/system protection policies for resources according to the rules of corresponding resource configuration


 

1. Solve the problem of microservice avalanche

There are four common ways to solve the avalanche problem:

Timeout processing: set the timeout period, the request will return an error message if there is no response after a certain period of time, and will not wait endlessly
Bulkhead mode: Limit the number of threads that each business can use to avoid exhausting the resources of the entire tomcat , so it is also called thread isolation.
Fuse degradation: The abnormal proportion of business execution is counted by the circuit breaker. If the threshold is exceeded, the business will be fused and all requests to access the business will be intercepted.
Flow control: QPS that restricts business access to avoid service failure due to sudden increase in traffic.

How to avoid service failure caused by instantaneous high concurrent traffic?

Flow control

How to avoid the avalanche problem caused by service failure?

Timeout handling
Thread isolation
Downgrade Fuse

 

Sentinel is a microservice traffic control component open sourced by Alibaba. Official website address: home | Sentinel

Sentinel has the following characteristics :

Rich application scenarios : Sentinel has undertaken the core scenarios of Alibaba's Double Eleven traffic promotion in the past 10 years, such as seckill (that is, the burst traffic is controlled within the range that the system capacity can bear), message peak-shaving and valley-filling, cluster traffic Control, real-time fusing of downstream unavailable applications, etc.
Complete real-time monitoring : Sentinel also provides real-time monitoring functions. In the console, you can see the second-level data of a single machine connected to the application, or even the aggregated running status of a cluster with a scale of less than 500 .
Extensive open source ecosystem : Sentinel provides out-of-the-box integration modules with other open source frameworks / libraries, such as integration with Spring Cloud , Dubbo , and gRPC . You only need to introduce the corresponding dependencies and perform simple configurations to quickly access Sentinel .

Perfect SPI extension point : Sentinel provides an easy-to-use and complete SPI extension interface. You can quickly customize the logic by implementing the extension interface. For example, custom rule management, adaptation of dynamic data sources, etc.

2. Use steps

First download the sentinel console jar package 

 Then open cmd in this directory 

Start the jar package

java -Dserver.port=8888 -jar sentinel-dashboard-1.8.6.jar

Microservice Integration Sentinel

We integrate Sentinel in order-service and connect to Sentinel 's console, the steps are as follows:

<!--sentinel-->
<dependency>
    <
groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</
dependency>

Then access in the configuration file

spring:
 
cloud:
    sentinel:
     
transport:
       
dashboard: localhost:8888

 

In this way, the access fuse of a microservice is completed, and other similar 

3. Use of fuses

3.1 Current Limiting Rules

 

 

3.1.1 Flow control mode

 

When adding a flow-limiting rule, click Advanced Options to choose from three flow-control modes:

Direct: Statistical requests for current resources, and directly limit the flow of current resources when the threshold is triggered, which is also the default mode
Association: Statistics of another resource related to the current resource, when the threshold is triggered, the current resource is limited
Links: Count the requests to access this resource from the specified link, and limit the flow of the specified link when the threshold is triggered

 

 

When using the link, you need to add identification annotations to allow the fuse to identify the underlying method

@SentinelResource ( "goods" )
public void queryGoods() {     System.err
.println ( " Query Goods " );
}

Sentinel will integrate the Controller method into the context by default , resulting in the failure of the flow control in the link mode. You need to modify the application.yml and add the configuration:

spring :
 
cloud :
    sentinel :
     
web-context- unify : false #Close context integration

 

What are the flow control modes?

Direct: limit current resources
Association: High-priority resources trigger thresholds, and low-priority resources are limited.
Link: When counting thresholds, only the requests entering the current resource from the specified resource are counted, which is the current limit to the source of the request

 

 3.1.2 Flow control effect

The flow control effect refers to the measures that should be taken when the request reaches the flow control threshold, including three types:

Fail Fast: Once the threshold is reached, new requests are rejected immediately and a FlowException is thrown . is the default processing method.
warm up : Warm-up mode, requests that exceed the threshold are also rejected and an exception is thrown. But this mode threshold will change dynamically, gradually increasing from a small value to a maximum threshold.
Waiting in queue: Let all requests be queued for execution in order of priority, and the interval between two requests cannot be less than the specified time

 

 

For example, the effect of 1s 10 qps queue waiting is that at a certain moment, all of them enter the queue and wait for each request to be processed for 200ms. At this time, the queue is full. If the first request has not been processed, then the total waiting time of the queue will be exceeded later. The request will be directly rejected, but with the processing, when the subsequent request comes again, it may just enter the queue and wait because it has already been processed. At this time, the request will not be restricted 

What are the flow control effects?

Fail Fast: Reject new requests when QPS exceeds threshold
warm up : When the QPS exceeds the threshold, new requests are rejected; the QPS threshold is gradually increased to avoid service downtime caused by high concurrency during cold start.
Waiting in queue: the request will enter the queue, and the request will be executed sequentially according to the time interval allowed by the threshold; if the expected waiting time of the request is longer than the timeout time, it will be rejected directly

 

When limiting the current of hot parameters, you need to add @SentinelResource ( "hot" )  to the control layer method of the current limiting to make the current limiting effective

3.2  Isolation and downgrade

3.2.1 Isolation

 

 

 Feign integrates Sentinel

 

In Spring Cloud , microservice calls are all implemented through Feign , so Feign and Sentinel must be integrated for client protection .

1. Modify the application.yml file of OrderService and enable Feign 's Sentinel function

Enable this feature in the configuration file

feign :
  sentinel :
   
enabled : true #Enable Feign 's Sentinel function _

2. Write downgrade logic for FeignClient after failure
Method 1: FallbackClass , unable to handle exceptions of remote calls

Method 2: FallbackFactory , which can handle the exception of remote calls, we choose this

 

First write a downgraded processing class 

@Component
public class ProductFeignFactory implements FallbackFactory<ProductFegin> {
    @Override
    public ProductFegin create(Throwable throwable) {
        return new ProductFegin() {
            @Override
            public Product findById(Integer pid) {
                Product product = new Product();
                product.setPid(1);
                product.setPname("系统繁忙");


                return product;
            }
        };
    }
}

then turn on

/**
 * @author :Student王心
 * @date :Created in 2023/2/11 15:30
 * @description:
 * fallbackFactory 远程调用微服务出现故障 则执行降级的业务代码
 * @modified By:
 * @version:
 */
//value里面表示服务名称,openfeign用来远程调用的,这个接口主要说明,需要调用那个微服务名称
@FeignClient(value = "xin-spring-cloud-product",fallbackFactory = ProductFeignFactory.class)
public interface ProductFegin {

    //该路径还有提交方式必须跟提供者的路径和提交方式保持一致
    @GetMapping("/product/getById/{pid}")
    public Product findById(@PathVariable Integer pid);
}

This completes the exception handling of the remote call

 

 

 

 

 

 

What are the two means of thread isolation?

Semaphore isolation
Thread pool isolation

What are the characteristics of semaphore isolation?

Based on the counter mode, simple and low overhead

What are the characteristics of thread pool isolation?

Based on the thread pool mode, there is additional overhead, but the isolation control is stronger

3.2.2 Downgrade 

 

 

What are the strategies for Sentinel circuit breaker downgrade?

Slow call ratio: Calls that exceed the specified duration are slow calls, and the ratio of slow calls within a unit of time is counted. If it exceeds the threshold, it will be blown
Abnormal ratio: the ratio of abnormal calls within the statistical unit time, if it exceeds the threshold, it will be blown
Number of exceptions: Count the number of abnormal calls within a unit time period, and if the threshold is exceeded, the fuse will be disconnected

 

 4. Persistence of sentinel rules

 

 

java -Dserver.port=8888 -Dnacos.serverAddr=localhost:8848 -Dnacos.namespace=nacos的命名空间id -jar sentinel-dashboard.jar
# -Dserver.port 控制台端口号
# -Dnacos.serverAddr: nacos 地址
# -Dnacos.namespace: 你项目所在的 nacos 命名空间  如果命名空间就是public可以省略该参数

 Then enter the console and use all the operations with +2

Microservice access

<dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
            <version>1.8.1</version>
        </dependency>

Modify the configuration file of the microservice


spring.application.name=xin-spring-cloud-order
#配置中心
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
#额外的配置
spring.cloud.nacos.config.extension-configs[0].data-id=datasource.properties
spring.cloud.nacos.config.extension-configs[0].group=DEFAULT_GROUP
spring.cloud.nacos.config.extension-configs[0].refresh=true

#配置熔断器
spring.cloud.sentinel.transport.dashboard=localhost:8888
#控制面板修改规则后,可以通过该端口把规则发给微服务
spring.cloud.sentinel.transport.port=8719

#关闭引起链路模式的流控失败的上下文
spring.cloud.sentinel.web-context-unify=false

#开启feign的Sentinel功能
feign.sentinel.enabled=true



spring.cloud.nacos.server-addr=localhost:8848

spring.cloud.nacos.config.namespace=nacos命名空间id
spring.cloud.nacos.config.group=aaa

#配置持久化规则
spring.cloud.sentinel.datasource.aaa-flow.nacos.server-addr=localhost:8848
spring.cloud.sentinel.datasource.aaa-flow.nacos.namespace=${spring.cloud.nacos.config.namespace}
spring.cloud.sentinel.datasource.aaa-flow.nacos.group-id=SENTINEL_GROUP
spring.cloud.sentinel.datasource.aaa-flow.nacos.data-id=${spring.application.name}-flow-rules
spring.cloud.sentinel.datasource.aaa-flow.nacos.rule-type=flow
spring.cloud.sentinel.datasource.aaa-flow.nacos.data-type=json


 

 This completes the persistence

 

 

 

 

 

 

 

 

 

 


Summarize

To be added

Guess you like

Origin blog.csdn.net/qq_55648724/article/details/129139751