table of Contents
1. Introduction to Sentinel
Sentinel
It is a highly available flow control protection component for cloud-native microservices. With the popularity of microservices, the stability between services and services becomes more and more important. Sentinel
Taking the flow as the starting point, the stability of the service is protected from multiple dimensions such as flow control, fuse degradation, and system load protection. Simply put, it is an upgraded version of Hystrix that I have learned before.
The official definition: Sentinel is the traffic guard of distributed systems. It is conceivable that Sentinel is important to distributed systems.
2.Sentinel features
Rich application scenarios : Sentinel has undertaken the core scenarios of Alibaba’s double eleven major traffic promotion in the past 10 years, such as spike (that is, burst traffic control within the range of the system capacity), message peak reduction and valley filling, and cluster flow control , Real-time fusing downstream unavailable applications, etc.
Complete real-time monitoring : Sentinel also provides real-time monitoring functions. You can see the second-level data of a single machine connected to the application in the console, and even the summary operation status of a cluster of less than 500 units.
Extensive open source ecology : 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 a simple configuration to quickly access Sentinel.
Complete SPI extension point : Sentinel provides a simple, easy-to-use and complete SPI extension interface. You can quickly customize the logic by implementing an extended interface. For example, custom rule management, adaptation of dynamic data sources, etc.
Sentinel's Open Source Ecological Map
As you can see from the above figure, Sentinel provides out-of-the-box integration modules with other open source frameworks/libraries.
Sentinel official website address: https://github.com/alibaba/Sentinel
3.Hystrix VS Sentinel
Hystrix | Sentinel |
---|---|
Need programmers to build monitoring platform manually | A single component can be independent |
There is no set of web interfaces that can provide programmers with more fine-grained configuration, such as flow control, rate control, service fusing, and service degradation | Fine-grained unified configuration with direct interface |
In general, Sentinel is more convenient and quick to use, does not require programmers to manually build a monitoring platform, and the degree of coupling is reduced.
4. Sentinel download, install and run
[A] Download address : https://github.com/alibaba/Sentinel/releases
Here we choose to download: sentinel-dashboard-1.7.0.jar
as the Sentinel version used in this demonstration.
[B] Install Sentinel console
Sentinel is divided into two parts:
-
The core library (Java client) does not depend on any framework/library, can run in all Java runtime environments, and also has good support for Dubbo/Spring Cloud and other frameworks.
-
The console (Dashboard) is developed based on SpringBoot and can be run directly after packaging, without additional application containers such as Tomcat.
[C] Run command
Operation prerequisite: Java8 JDK environment is required at least OK, and port 8080 cannot be occupied. We all know that port 8080 is the default port of Tomcat. I don't know why, Sentinel chose 8080 as the port number.
Open the CMD command line window in the Sentinel Jar package directory you downloaded, and enter the following command to start:
java -jar sentinel-dashboard-1.7.0.jar
After the startup is complete, you can see that the port that Sentinel monitors is 8080.
[D] Visit the Sentinel management interface
After the startup is successful, our browser visits: http://localhost:8080/#/dashboard/home
We use the default user name and password (both sentinel
) to log in, after the login is successful, as shown below:
if you can see this The interface indicates that our Sentinel download and installation configuration is successful.
5. Sentinel initialization monitoring
Next, we use an example to introduce how to integrate our microservices with the Sentinel
monitoring platform to implement operations such as monitoring and current limiting of microservice applications.
We create a new module【springcloudalibaba-sentinel-service8401
】, and note that the service registration function needs to be combined with nacos.
[A] Add sentinel dependency to pom.xml : spring-cloud-starter-alibaba-sentinel
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud-alibaba-nacos</artifactId>
<groupId>com.bruce.springcloud-alibaba-nacos</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<dependencies>
<!-- Alibaba-nacos服务发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<exclusion>
<artifactId>nacos-client</artifactId>
<groupId>com.alibaba.nacos</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- nacos-client -->
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.2.0</version>
</dependency>
<!-- hystrix断路器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- openfeign客户端 ,默认集成并开启了ribbon负载均衡-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!--排除tomcat依赖 -->
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!--undertow容器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<!-- lombok插件 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--SpringCloud ailibaba sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--SpringCloud ailibaba sentinel-datasource-nacos 后续做持久化用到-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>6</source>
<target>6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
[B] aplication.yml : Configure the address of the sentinel monitoring platform
server:
port: 8401
spring:
application:
name: springcloudalibaba-sentinel-service
cloud:
nacos:
discovery:
server-addr: localhost:8848 #指定nacos服务器地址
sentinel:
transport:
dashboard: localhost:8080 #指定sentinel控制台的地址
port: 8719 #sentinel api端口, 默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
management:
endpoints:
web:
exposure:
include: '*' #配置对外暴露端口
[C] Startup
package com.bruce;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class SentinelServiceApplicaiton8401 {
public static void main(String[] args) {
SpringApplication.run(SentinelServiceApplicaiton8401.class, args);
}
}
[D] Test Controller
package com.bruce.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SentinelController {
@GetMapping("/sentinel")
public String sentinel() {
return "hello, sentinel dashboard....";
}
}
[E] Test
Step1
: Start Nacos server ;:
Step2
Start Sentinel Dashboard server
Step3
;: Run [springcloudalibaba-sentinel-service8401] startup class, port 8401;:
Step4
Then visit http://localhost:8848/nacos, find the list of services under service management, you can see Go to an application named "springcloudalibaba-sentinel-service" and register to the Nacos service management page, as shown in the following figure
Step5
:: Then access the http://localhost:8080
request, switch to the Sentinel login page and enter the Dashboard home page. At this time, the Sentinel console is not monitoring any microservices.
The reason is that Sentinel uses a lazy loading mechanism. The microservice [springcloudalibaba-sentinel-service8401] currently has no traffic information to monitor. We need to manually trigger a request: http://localhost:8401/sentine
l:
Then go back to the Sentinel Dashboard interface again, and you can see [ springcloudalibaba-sentinel-service8401] service monitoring traffic information, etc., as shown in the following figure:
through real-time monitoring, you can see the flow effect of the peak traffic, the green line represents pass, and the blue line represents rejection.
At this time, the Sentinel console is monitoring the microservice [springcloudalibaba-sentinel-service8401], which means that Sentinel Dashboard replaces the Hystrix Dashboard function.
6. Summary
This article mainly summarizes the main features of Sentinel and uses examples to introduce how to use Sentinel as a traffic monitoring and current limiting platform for our microservices. In general, compared to the Hystrix that we learned before, it simplifies the way of use, at least we don't need to build the Hystrix Dashboard manually.