Please state the source of the article. Welcome to add Echo wechat (wechat id: T2421499075) for exchange and learning.


Sentinel, a brief introduction to

Speaking of limiting streaming, many of you are probably familiar with Hystrix, but unfortunately Netflix has announced that it will no longer update Hystrix. Sentinel is a flow limiting component with the official title: Flow Defense of distributed System. It is a high-availability flow control and protection component oriented to cloud native micro-services. It mainly takes flow as the entry point and helps users to guarantee the stability of micro-services from multiple dimensions such as flow control, fuse degradation and system adaptive protection. It is similar to Hystrix in that Sentinel is more comprehensive and has become a mainstream component for limiting traffic since Hystrix has stopped updating.

Does Sentinel only do current limiting? No, we’re going to move on to other Sentinel features, but we’re going to start with stream limiting

Sentinel has the following characteristics

  • Rich application scenarios: Sentinel has undertaken the core scenarios of Alibaba’s double Eleven traffic drive in the past 10 years, such as SEC killing (i.e., burst traffic control within the range of system capacity), message peaking and valley filling, real-time fusing of downstream unavailable applications, etc.
  • Complete real-time monitoring: Sentinel also provides real-time monitoring capabilities. From the console, you can see a summary of the performance of a single machine-by-second data, or even a cluster of less than 500 machines, for accessing the application.
  • Extensive Open source ecosystem: Sentinel provides out-of-the-box integration modules with other open source frameworks/libraries, such as Spring Cloud, Dubbo, and gRPC. You can quickly access Sentinel by introducing the appropriate dependencies and simple configuration.
  • Sophisticated SPI extension points: Sentinel provides easy-to-use, sophisticated SPI extension points. You can quickly customize logic by implementing extension points. For example, customize rule management and adapt data sources.

Use Sentinel to implement traffic limiting on the interface

Sentinel is divided into two parts, the server and the client. The server has a visual interface, and the client can communicate with the server after importing the JAR package, which is similar to the structure used in our nacOS. To use Sentinel, download the server jar package and start it

  • Download address of the server:https://github.com/alibaba/Sentinel/releases

The latest CloudAlibaba uses the latest sentinel version

  • Start command:
java -jar sentinel-dashboard-1.8. 0Jar - You can also use the following command to do some Settings, especially for ports, many of which are likely to be required for microservices java-dserver.port =10083 -Dcsp.sentinel.dashboard.server=localhost:10083 -Dproject.name=sentinel-dashboard-1.8. 0.jar sentinel-dashboard.jar
Copy the code

If the following log appears, the startup is successful and we can access localhost:10083

The access interface is as follows:

  • The default login password is sentinel sentinel

The interface is as follows:

At this point our server preparation is complete and we can start integrating our Sentinel into our project

Here we need to create a client and integrate sentinel. Just add spring-cloud-starter-Alibaba-sentinel to the POM file, but there are many version problems. If you don’t know which version is compatible, just use my XML configuration below


      
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4. RELEASE</version>
        <relativePath/> <! -- lookup parent from repository -->
    </parent>
    <groupId>com.echo</groupId>
    <artifactId>sentinel</artifactId>
    <version>0.0.1 - the SNAPSHOT</version>
    <name>sentinel</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud-alibaba.version>2.2.3. RELEASE</spring-cloud-alibaba.version>
        <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <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>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
Copy the code
  • At this point the integration is complete and we add a test interface to the class that added the dependency

Note: The console does not display the project directly after the successful startup. You need to access the interface of the project we built, and then you can see that the sentinel console has the project we created

The interface we see is the real-time monitoring interface, and this interface displays our access data

Set flow control to complete flow limiting

Directly open cluster point link, then find our test interface and click flow control. Then we can add a flow control rule

Here we have a flow control with a QPS value of 1. We can access the interface continuously to see what happens to our interface return

Obviously, the test interface for flow control is set up, and if the QPS exceeds the threshold, access will be denied directly. And real-time surveillance shows us the same picture

Here we not only count the total QPS, but also divide us into approved QPS and rejected QPS

conclusion

  • Sentinel integration is simple
  • The key is to pay attention to version compatibility
  • The console needs to access the interface to see the rules interface of flow control