Detailed explanation of Sentinel usage

Component introduction

SentinelIt is a comprehensive solution for service fault tolerance developed by Alibaba. It uses traffic as the entry point to protect the stability of services from multiple dimensions such as flow control, circuit breaker degradation, and system load protection. Sentinel has taken over the core scenarios of Alibaba's Double Eleven traffic promotion in the past 10 years, such as flash sales, message peak shaving, cluster traffic control, real-time circuit breaker for unavailable downstream applications, etc.

Sentinel core is divided into two parts:

  1. Core library (Java client): It can run in all Java runtime environments, and also has good support for frameworks such as Dubbo/SpringCloud.
  2. Dashboard: Developed based on SpringBoot, it can be run directly after packaging.

The purpose of current limiting is to prevent malicious request traffic, malicious attacks, or prevent traffic from exceeding system peaks.

Insert image description here

scenes to be used

SentinelIt is Alibaba's open source distributed system traffic guard. It is mainly used for service fault tolerance. It uses traffic as the entry point to help users solve the stability problems of distributed systems from multiple dimensions such as flow control, circuit breaker degradation, and system protection. .

Common uses and usage scenarios of Sentinel include:

  1. Flow control: Sentinel can control the access rate of traffic to prevent the system from being overwhelmed when the traffic is too large. It can limit traffic according to preset rules to ensure that the system can automatically shrink when the traffic is too large, protecting the stability and availability of the system.
  2. Circuit breaker and downgrade: When a service fails or the response is too slow, Sentinel can automatically cut off the call to the service through the fuse downgrade mechanism to prevent the fault from spreading to the entire system. . At the same time, Sentinel also supports custom downgrade logic, which can perform flexible downgrade processing according to actual conditions.
  3. System protection: Sentinel can monitor system resource usage in real time, such as CPU, memory, network bandwidth, etc. When system resources reach the preset threshold, the protection mechanism can be automatically triggered. , such as automatic capacity expansion, automatic current limiting, etc., to ensure that the system can operate normally under high load conditions.
  4. Real-time monitoring: Sentinel provides real-time monitoring functions. You can view second-level data of a single machine connected to the application through the console, and even summarize the clusters of less than 500 machines. Operation status. At the same time, Sentinel also supports integration with third-party monitoring systems, such as Prometheus, Grafana, etc., to facilitate users to conduct unified monitoring and management.
  5. Open source ecosystem: Sentinel provides a rich open source ecosystem and has good integration capabilities with microservice frameworks and libraries such as Spring Cloud, Dubbo, and gRPC. Users only need to introduce the corresponding dependencies and perform simple configuration to quickly access Sentinel. At the same time, Sentinel also provides native implementations in multiple languages ​​such as Java/Go/C++ to facilitate users’ cross-language development.

Sentinel is a powerful distributed system traffic guard that can help users solve the stability problems of distributed systems and improve the availability and reliability of the system.

Component architecture

Insert image description here
Sentinel's architecture mainly includes the following parts:

  1. Sentinel Console: This is Sentinel’s management console, used to configure and manage Sentinel rules.
  2. Sentinel core library: This is the core part of Sentinel, used to implement functions such as flow control, circuit breaker degradation, etc.
  3. Sentinel plug-in: Sentinel supports a variety of plug-ins, such as Dubbo plug-in, Spring Cloud plug-in, etc. These plug-ins can help Sentinel better integrate into different frameworks and environments.

In Sentinel's architecture, flow control is one of the core functions. Sentinel controls traffic through current limiting, traffic shaping, circuit breaker degradation and other means to ensure that the system can operate stably. At the same time, Sentinel also provides functions such as system load protection and hotspot protection, further enhancing the stability of the system.

console

The Sentinel console is a lightweight console provided by Sentinel. It provides functions such as machine discovery, real-time monitoring of single-machine resources, and rule management. Sentinel console is a project developed based on SpringBoot, and you can directly use the jar command to start the project. By accessing the URL provided by the console, users can manage Sentinel's rules and configurations, monitor system resource usage in real time, and perform other related management operations.

In the Sentinel console, users can define and modify current limiting rules, circuit breaker degradation rules, etc. to ensure system stability and availability. At the same time, the console also provides real-time system resource monitoring functions. Users can easily check the usage of the system's CPU, memory, network and other resources to find and solve problems in a timely manner.

The Sentinel console is an important part of Sentinel. It provides users with convenient management and monitoring functions, helping users better manage and maintain the stability and availability of the system.

How to use the Sentinel console mainly includes the following steps:

  1. Download and start the Sentinel console: You can download the jar package of the Sentinel console from Sentinel's official website or GitHub repository, and then start it through the command line. You can configure the behavior of the console by specifying different parameters when starting, such as specifying the console port, configuring rules, etc.
  2. Configuration rules: In the Sentinel console, you can define and modify current limiting rules, circuit breaker degradation rules, etc. These rules can be configured according to actual needs, such as setting thresholds, trigger conditions, etc.
  3. Monitor system resources: The Sentinel console provides real-time system resource monitoring function, which can check the usage of the system's CPU, memory, network and other resources. This data can help users discover and solve problems in a timely manner.
  4. Management machine list: The Sentinel console can collect heartbeat packets sent by the Sentinel client to determine whether the machine is online. At the same time, you can also manage the machine list through the console, including adding, deleting, and modifying machine information.
  5. Other management functions: In addition to the above functions, the Sentinel console also provides other management functions, such as rule management and push, authentication, etc. These functions can be configured and used according to actual needs.

It should be noted that using the Sentinel console requires certain basic knowledge of Java and SpringBoot, as well as a certain understanding of Sentinel and microservice architecture. At the same time, you need to pay attention to safety and stability issues during use to ensure the normal operation of the system.

core library

Sentinel's core library is a lightweight open source library that does not rely on any framework or library and can run in the runtime environment of Java 8 and above. At the same time, it provides good support for microservice frameworks such as Spring Cloud, Dubbo, and gRPC.

The core skeleton of Sentinel strings together different Slots in order (chain of responsibility model), thereby combining different functions (current limiting, downgrading, system protection). Specifically, Slot Chain can be divided into statistical data construction part (statistic) and judgment part (rule checking).

Sentinel's core library supports a wide range of open source components, including but not limited to the following:

  1. Spring Cloud: Sentinel has good integration with Spring Cloud and can be easily integrated into the Spring Cloud environment.
  2. Dubbo:Dubbo is a high-performance, lightweight distributed service framework. Sentinel has good support for Dubbo.
  3. gRPC:gRPC is a high-performance, open source general RPC framework. Sentinel also supports integration with gRPC.

In addition, Sentinel also provides native implementations in multiple languages ​​such as Java/Go/C++, and provides a complete SPI extension mechanism to facilitate users to customize extensions.

plug-in

Sentinel plug-ins are part of the Sentinel ecosystem and allow users to integrate Sentinel into different frameworks and environments. The purpose of the Sentinel plug-in is to provide integration with various frameworks and libraries so that users can use Sentinel's functions more conveniently.

Sentinel plug-ins usually include adaptation code to specific frameworks or libraries so that Sentinel can integrate seamlessly with these frameworks or libraries. For example, Sentinel provides plug-ins with microservice frameworks such as Spring Cloud, Dubbo, and gRPC. These plug-ins can help users easily apply Sentinel's current limiting, circuit breaker and other functions in a microservice environment.

By using Sentinel plug-ins, users can more easily integrate Sentinel into their projects and take advantage of the various features provided by Sentinel to protect the stability and availability of services. At the same time, the Sentinel plug-in also provides expansion and customization capabilities to meet the specific needs of different users.

The specific steps for installing Sentinel plug-ins may vary depending on the plug-in type and target environment. Generally speaking, you can install it by following these steps:

  1. Download plugins: First, download the required plugins from Sentinel’s official website or repository. Usually, plug-ins are provided in the form of jar packages.
  2. Configure dependencies: Add the plug-in jar package to your project dependencies. If you are using a build tool such as Maven or Gradle, you can add the plugin's dependencies in your project's build configuration file.
  3. Configure rules: Configure Sentinel rules according to your needs. This may include current limiting rules, circuit breaker degradation rules, etc. You can configure these rules in the plug-in's configuration file.
  4. Launch the app: Launch your app and make sure the Sentinel plugin is loaded and running correctly.

Sentinel’s main plug-ins include Sentinel console plug-in and Sentinel Spring Cloud plug-in.

  • Sentinel console plug-in: This is a lightweight console provided by Sentinel. It provides functions such as machine discovery, real-time monitoring of single-machine resources, and rule management.
  • Sentinel Spring Cloud plug-in: This plug-in supports the Spring Cloud microservice framework, allowing users to conveniently use Sentinel's current limiting, circuit breaker and other functions in the Spring Cloud environment.

Limiting

The principle of current limiting

The principle of current limiting is mainly to ensure that the number of requests received by the system is within the normal range through flow control. Since the throughput of any system has an upper limit, reasonable limits must be set to avoid traffic peaks from bringing down the entire system. Commonly used current limiting algorithms include counter algorithm (fixed window current limiting + sliding window current limiting), token bucket algorithm, leaky bucket algorithm, etc. The main purpose of these algorithms is to ensure that the system operates efficiently and stably while ensuring that the system is not overwhelmed by traffic.

Current limiting rule configuration

Sentinel's current limiting rule configuration can be performed through the following steps:

  1. Add current limiting rules: In the Sentinel console, you can add current limiting rules. Select the service or interface that needs to be throttled, and set the throttling threshold and throttling mode. For example, you can set a service or interface that can only be accessed 10 times per second, and any excess access will be limited.
  2. Configure rule priority: You can set the priority of rules according to your business needs. For example, you can set some high-priority rules to ensure that critical business is not affected.
  3. Configure the rule triggering method: You can select the rule triggering method according to actual needs. For example, you can set the rule to trigger at a fixed window time or a sliding window time.
  4. Configure rule thresholds: You can set rule thresholds according to actual needs. For example, you can set a threshold that triggers a rule when traffic to a service reaches that threshold.
  5. Configure rules to associate resources: You can associate one or more resources with a rule. For example, you can associate multiple traffic limiting rules with a service or interface.
  6. Save and publish rules: After configuring the rules, you need to save and publish the rules so that Sentinel can limit traffic according to the rules.

Current limiting rule type

Sentinel’s current limiting rule types mainly include the following:

  1. Counting current limiting: Counter-based current limiting, which records the number of requests through the counter. When the number of requests exceeds the preset threshold, current limiting is triggered.
  2. Time window current limiting: Time window-based current limiting divides time into fixed-length time windows. The number of requests in each time window is limited. When the number of requests exceeds the limit, current limiting is triggered.
  3. Link current limiting: Limit the traffic on the link to ensure that the requests on the link will not exceed the preset threshold, thereby protecting the link from the impact of excessive traffic.

Sentinel installation

The installation of Sentinel depends on your usage scenario and target environment. The following are two common installation methods:

  1. Source code installation: If you want to install from source code, you can visit Sentinel's official GitHub repository, download the source code and follow the instructions to install. Specific installation steps may vary depending on version and target environment, please make sure you follow the correct instructions.
  2. Download the Jar package for installation: If you want to directly download the Sentinel Jar package for installation, you can visit Sentinel's official website or GitHub repository to find the appropriate version and download it. Then, run the Jar package in the command line to start Sentinel. By default, Sentinel uses port 8080. If this port is already in use, you can specify another port. For example, a command using port 8858 might be as follows:java -Dserver.port=8858 -jar sentinel-dashboard-1.8.0.jar

No matter which installation method you choose, make sure to follow the correct instructions and configure it appropriately for your needs. At the same time, pay attention to handling any possible security issues to ensure the safe operation of Sentinel.

Sentinel integrated with Nacos

The integration of Sentinel and Nacos is mainly achieved through Sentinel's data source extension.

First, you need to introduce the dependencies of Spring Cloud Alibaba's Sentinel module and Nacos storage extension into the pom.xml of the Spring Cloud application.

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

Then, add configuration information in the Spring Cloud application. For example:

spring:
  application:
    name: sentinel-server
  cloud:
    sentinel:
      transport:
        dashboard: 127.0.0.1:8080
      datasource:
        nacos:
          server-addr: 127.0.0.1:8848
          data-id: ${
    
    spring.application.name}-nacos

These configurations will specify the service address of Sentinel Dashboard and the connection information to Nacos. Nacos is used as a data source to store Sentinel rules and statistics.

After completing the above configuration, Sentinel will be able to integrate with Nacos and use Nacos as its data storage backend. You can manage rules in the Sentinel console, and these rules will be automatically synchronized to Nacos and take effect in the Sentinel client. At the same time, the Sentinel client will also obtain statistics and other related information from Nacos.

Springboot and Sentinel integration example

  1. Add dependencies: Add Sentinel dependencies in the pom.xml file of the Spring Boot project, for example:
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-spring-boot-starter</artifactId>
    <version>最新版本</version>
</dependency>
  1. Configure rules: Configure rules in the Sentinel console or programmatically in code. For example, configure flow control rules in code:
@Configuration
public class SentinelConfig {
    
    
    @Bean
    public BlockHandler blockHandler() {
    
    
        return new BlockHandler() {
    
    
            @Override
            public void blockHandler(BlockHandlerRequest request, BlockHandlerResponse response) {
    
    
                // 流量控制处理逻辑
            }
        };
    }
}
  1. Start the application: Start the Spring Boot application, and Sentinel will automatically integrate and monitor the traffic of the microservice. When the traffic exceeds the set threshold or an exception occurs, Sentinel will automatically trigger the circuit breaker and degradation mechanism to protect the stability and availability of microservices.
  2. Monitor and protect microservices: Through Sentinel's monitoring function, you can view the resource usage, request volume, response time and other indicators of microservices in real time. When the traffic exceeds the set threshold or an exception occurs, Sentinel will automatically trigger the circuit breaker and degradation mechanism to protect the stability and availability of microservices.

It should be noted that the specific usage of Sentinel may vary depending on the version and environment.

Prometheus monitoring Sentinel

Prometheus can be used to monitor Sentinel. The following is a common configuration method:

  1. In Sentinel's configuration file, expose Sentinel's monitoring data as an HTTP service. For example, you can configure the following:
csp.sentinel.api.port: 8719
csp.sentinel.dashboard.server.port: 8080
csp.sentinel.dashboard.server.threadnum: 10
  1. In the Prometheus configuration file, add a target to monitor Sentinel's HTTP service. For example:
scrape_configs:
- job_name: sentinel
  static_configs:
  - targets: ['localhost:8719']
  1. Start the Prometheus and Sentinel services. Prometheus will automatically start collecting Sentinel monitoring data and store it in a time series database.
  2. In the Prometheus web interface, you can view Sentinel monitoring data, including traffic control, circuit breaker degradation and other indicators.

Component comparison

Sentinel Hystrix resilience4j
Isolation strategy Semaphore isolation (concurrency control) Thread pool isolation/semaphore isolation Semaphore isolation
circuit breaker downgrade strategy Based on slow call ratio, exception ratio, and number of exceptions Based on abnormal proportion Based on abnormal ratio and response time
Real-time statistics implementation Sliding window (LeapArray) Sliding window (based on RxJava) Ring Bit Buffer
Dynamic rule configuration Support nearly ten kinds of dynamic data sources Support multiple data sources Limited support
Scalability multiple extension points Plug-in form interface form
Annotation-based support support support support
Single machine current limit Based on QPS, supports current limiting based on calling relationships limited support Rate Limiter
Cluster flow control support not support not support
Traffic shaping Supports preheating mode and uniform queuing control effects not support Simple Rate Limiter mode
System adaptive protection support not support not support
Hotspot identification/protection support not support not support
Multi-language support Java/Go/C++ Java Java
Service mesh support Support Envoy/Istio not support not support
console Provides an out-of-the-box console that can configure rules, real-time monitoring, machine discovery, etc. Simple monitoring and viewing No console is provided, but can be connected to other monitoring systems

Insert image description here

Sentinel official website

Core class analysis

Dubbo protocol introduction


Guess you like

Origin blog.csdn.net/zhangzehai2234/article/details/134985345