This is the sixth day of my participation in the November Gwen Challenge. See details: The Last Gwen Challenge 2021.

The use of Sentinel can be divided into two parts:

  • Core Library (Java client) : A runtime environment that is independent of any framework/library, can run on Java 7 and above, and has good support for frameworks such as Dubbo/Spring Cloud (see mainstream framework adaptation).
  • Console (Dashboard) : The Dashboard is mainly responsible for managing push rules, monitoring, and managing machine information.

Start Sentinel console

An overview of the

Sentinel provides a lightweight open source console that provides machine discovery as well as health management, monitoring (single and cluster), rule management and push capabilities. Here, we’ll detail how these features can be used in simple steps.

Next, we’ll take a step-by-step look at how to integrate the Sentinel core library and Dashboard to get the most out of it. At the same time, we also offer the enterprise Sentinel service on Aliyun: AHAS Sentinel Console. With just a few simple steps, you can see directly how the console realizes these functions, and experience the diversified monitoring and fully managed cluster flow control capability.

The Sentinel console contains the following features:

  • View the machine list and health: Collect heartbeat packets sent by the Sentinel client to determine whether the machine is online.
  • Monitoring (single machine and cluster aggregation) : The monitoring API exposed by Sentinel client is used to periodically pull and aggregate application monitoring information, and finally achieve real-time monitoring at the second level.
  • Rule management and push: Manages push rules in a unified manner.
  • Authentication: Authentication is important in the production environment. Each developer needs to customize this for his or her own situation.

Note: Sentinel console currently supports standalone deployment only. Sentinel Console project provides a complete sample of Sentinel functions. It is not used as a production environment console out of the box. If you want to use it in production environment, please customize and modify it according to the documentation.

Get Sentinel console

You can download the latest version of the console JAR package from the Release page.

You can also build the Sentinel console yourself from the latest version of the source code:

  • Download Console Engineering
  • Package the code into a fat JAR using the following command:mvn clean package

Start the

Note: JDK version 1.8 or later is required to start the Sentinel console.

Use the following command to start the console:

java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
Copy the code

-dserver. port=8080 specifies the Sentinel console port as 8080.

Starting with Sentinel 1.6.0, the Sentinel console introduced basic login functionality, with the default user name and password being Sentinel. For details about how to configure the user name and password, see the authentication module documentation.

Note: If your application is a Spring Boot or Spring Cloud application, you can specify the configuration through the Spring configuration file, please refer to the Spring Cloud Alibaba Sentinel documentation for details.

After successful startup, you can see the default application: Sentinel-Dashboard

The client accesses the console

Take the SpringBoot project as an example:

throughpom.xmlThe introduction of the JAR package

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    <version>2.2.1. RELEASE</version>
</dependency>
Copy the code

Setting Startup Parameters

Add the configuration to the application.yml or application.properties file

Spring: Application: name: rameo security: user: # Dashboard: 127.0.0.1:8080 # Sentinel console request addressCopy the code

Define the resources

Resource is one of the core concepts in Sentinel. The most common resources are Java methods in our code. Of course, you can define your resources more flexibly, for example, by enclosing code that needs to control traffic with Sentinel API sphu.entry (“HelloWorld”) and entry.exit(). In the following example, we call system.out.println (” Hello world”); As a resource (protected logic), wrapped in an API. The reference code is as follows:

public static void main(String[] args) {
    // Configure rules.
    initFlowRules();

    while (true) {
        // As of version 1.5.0, try-with-resources is available directly
        try (Entry entry = SphU.entry("HelloWorld")) {
            // Protected logic
            System.out.println("hello world");
    } catch (BlockException ex) {
            // Handle flow-controlled logic
        System.out.println("blocked!"); }}}Copy the code

After completing the above two steps, the transformation on the code side is complete.

You can also define our resources through our annotation support module, similar to the following code:

@SentinelResource("HelloWorld")
public void helloWorld(a) {
    // The logic in the resource
    System.out.println("hello world");
}
Copy the code

Thus, the helloWorld() method becomes one of our resources. Note that the annotation support module needs to be used with Spring AOP or AspectJ.

Define the rules

Next, use flow control rules to specify the number of requests that the resource is allowed to pass. For example, the following code defines a maximum of 20 requests per second for the resource HelloWorld.

private static void initFlowRules(a){
    List<FlowRule> rules = new ArrayList<>();
    FlowRule rule = new FlowRule();
    rule.setResource("HelloWorld");
    rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
    // Set limit QPS to 20.
    rule.setCount(20);
    rules.add(rule);
    FlowRuleManager.loadRules(rules);
}
Copy the code

After completing the above three steps, Sentinel is ready to work. For more information, please refer to the usage documentation.

Start the project

By default Sentinel is initialized (lazy loading) the first time the client calls it and starts sending heartbeat packets to the console.

You need to access the interface before you can see it on the console.

Difference between Sentinel and Hystrix

function Sentinel Hystrix
Isolation strategy Semaphore isolation (concurrent thread flow limiting) Thread pool isolation/semaphore isolation
Fuse downgrading strategy Based on response time, exception ratio, number of exceptions Based on abnormal ratio
Real-time statistical implementation Sliding Windows (LeapArray) Sliding Windows (based on RxJava)
Dynamic Rule Configuration Support for multiple data sources Support for multiple data sources
scalability Multiple extension points Plug-in form
Annotation-based support support support
Current limiting Based on QPS, traffic limiting based on call relationships is supported Limited support
Traffic shaping Support preheating mode, constant speed mode, preheating queuing mode (can be configured in flow rules) Does not support
System adaptive protection support Does not support
The console Provides out-of-the-box console for configuring rules, viewing second-level monitoring, machine discovery, and more Simple monitoring view