preface

Article content output source: Pull hook education Java high salary training camp;

Spring Cloud is a one-stop microservices solution. Many companies are using Spring Cloud components. If we want to learn about the Spring Cloud microservices architecture, we need to learn about their components. These include: registry, load balancing, fuse processing, procedure call, gateway services, configuration center, message bus, call link, data monitoring, and more.

This article takes you through the use of Sentinel, a microservice component launched by Sentinel SCA, as a service degradation service, service limiting and service monitoring. Also produced by Ali, it works well with Nacos.

You will learn:

  1. Sentinel introduces
  2. Sentinel deployment
  3. Sentinel key Concepts
  4. Sentinel flow rule
  5. Sentinel degradation rule
  6. Nacos implements Sentinel rule persistence

Sentinel introduces

Sentinel is a flow control, fuse downgrading component for cloud native microservices. Replace Hystrix for problems: service avalanche, service degradation, service fuse, service limiting

Hystrix:

Service consumer (auto-post microservice) – > Invoke service provider (resume microservice)

Hystrix was introduced on the caller — > a separate Dashboard project — >Turbine

1. Build a monitoring platform dashboard by myself

2. Did not provide UI interface for service fusing, service degradation and other configurations (but wrote codes and invaded our source program environment)

Sentinel:

1. Independently deployable Dashboard/ console components

2, reduce code development, through the UI interface configuration can complete fine-grained control (automatic delivery of micro-services)

Sentinel is divided into two parts:

  • Core libraries: Java clients are independent of any framework/library, can run in all Java runtime environments, and have good support for frameworks such as Dubbo /Spring Cloud.

  • Console :(Dashboard) based on Spring Boot, packaged can run directly, no additional application containers such as Tomcat.

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, cluster flow control, real-time fusing of unavailable downstream 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 SpringCloud and Dubbo. You can quickly access Sentinel by introducing the appropriate dependencies and simple configuration.

  • Sophisticated SPI extension points: Sentinel provides an easy-to-use, sophisticated SPI extension interface. You can quickly customize the logic by implementing an extension interface. For example, customize rule management and adapt dynamic data sources.

Key features of Sentinel:

image-20200825085052087

Sentinel’s open source ecology:

image-20200825085211525

Sentinel deployment

As mentioned, Sentinel is divided into core libraries and dashboards. The core library is to introduce dependencies into our services. The dashboard can be used for monitoring and management.

The dashboard

Let’s start by deploying the dashboard. The website provides a direct running JAR

Website:

https://github.com/alibaba/Sentinel/releases
Copy the code

I chose 1.7.2 here

image-20200825090229099

Once downloaded, run.

Java - jar - Dfile. Encoding = utf-8 sentinel - dashboard - 1.7.2. JarCopy the code

After the startup is complete, the browser type:

http://localhost:8080/#/login
Copy the code

Username/password: sentinel/sentinel

image-20200825092721702

Core library

The dashboard is set up, but there’s no data, because we haven’t introduced the core library into the service and connected it to the dashboard. So let’s implement them as well.

Introducing dependencies:

<! -- Sentinel Core Environment Dependence --><dependency>
 <groupId>com.alibaba.cloud</groupId>
 <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
Copy the code

Application. Yml changes:

spring: 
   cloud: 
 sentinel:
      transport:
Dashboard: 127.0.0.1:8080# sentinel Dashboard /console address
 port: 8719 # sentinel will start the HTTP server on this port, so that some of the rules defined by the console can be sent and passed. If port 8719 is occupied, it will be +1 Copy the code

test

Now, let’s test it out. Requesting an interface

http://localhost/api/user/login/[email protected]/123
Copy the code
image-20200826142850259

You can see that you can monitor it now. That means our deployment was successful.

Sentinel key Concepts

Resources:

It can be anything in a Java application, for example, a service provided by the application, or another application invoked by the application, or even a piece of code. The API interface we request is the resource

Rules:

The rules set around the real-time status of resources can include flow control rules, fuse degrade rules, and system protection rules. All rules can be dynamically adjusted in real time

Sentinel traffic rule module

The concurrent capacity of the system is limited. For example, system A supports one QPS. If too many requests come in, THEN A should control the flow

image-20200826140555343

Resource name: the default request path

For source: Sentinel can limit traffic for the caller. Enter the microservice name and default (regardless of source) threshold type/single-machine threshold

QPS :(number of requests per second) traffic limiting occurs when the QPS for invoking this resource reaches the threshold

Threads: when the number of threads to invoke the resources carried out while the threshold current limiting (thread to handle the request, if the business logic execution time is very long, flood peak flow comes, will cost a lot of thread resources, these thread resources accumulation, could eventually cause the service is not available, further upstream service is not available, could eventually service avalanche)

Cluster or not: Specifies whether to restrict traffic in a cluster

image-20200826140939045

Flow control mode:

Direct: When the resource invocation reaches the flow limiting condition, the resource is directly restricted

Association: The associated resource call limits itself when it reaches the threshold

Copy the code

Link: Only traffic on a specified link is recorded

Flow control effect:

Fast failure: Fails directly and throws an exception

Warm Up: Based on the value of the cold loading factor (default 3), the QPS threshold is reached after the preheating period from the threshold to the cold loading factor

Queue waiting: queue at a uniform speed to allow the request to pass at a uniform speed. The threshold type must be set to QPS, otherwise it is invalid

Associated limiting of flow control mode:

When the number of associated resource calls reaches the threshold, traffic limits itself. For example, the user registration interface needs to invoke the ID verification interface (usually the ID verification interface). If the id verification interface request reaches the threshold, traffic limits the user registration interface using association.

Link current limiting in flow control mode:

In link mode, the system controls the traffic of the invocation link where the resource resides. You need to configure the entry resource in the rule, which is the context name of the link entry for this invocation.

A typical call tree is shown below :(provided by aliyun)

image-20200825101501933

The requests from Entrance1 and Entrance2 in the figure above both invoke the resource NodeA. Sentinel allows limiting the flow of resources based only on the statistics of a call entry. For example, in link mode, setting the entry resource to Entrance1 means that only calls from Entrance1 are recorded in NodeA’s traffic limiting statistics, regardless of calls coming through Entrance2.

Warm up of flow control effect:

When the system is idle for a long time, when the traffic suddenly increases, directly pulling the system to the high water level may instantly overwhelm the system, such as the second kill module of the e-commerce website.

Through the Warm Up mode, the traffic slowly increases and reaches the set value of the request processing rate after the preset warmup time.

By default, the Warm Up mode starts at 1/3 of the set QPS threshold and slowly increases to the QPS set value.

Queuing for flow control effect:

In queuing mode, the interval for passing requests is strictly controlled. That is, the requests are passed at a uniform speed and part of the requests are queued. This mode is usually applied to scenarios such as peak clipping and valley filling in message queues. You need to set the timeout period. If the waiting time exceeds the timeout period, the request is rejected.

A lot of traffic is coming, not directly reject the request, but the request is queued, a uniform speed through (processing), the request can wait to be processed, can not wait (waiting time > timeout time) will be rejected.

For example, if QPS is set to 5, a request is processed every 200 ms, and additional requests are queued. The timeout period represents the maximum queuing time. Requests exceeding the maximum queuing time will be rejected. In queuing mode, the QPS value should not exceed 1000 (request interval: 1 ms).

Sentinel degradation rule module

Flow control is to control the large flow from the outside, and the perspective of fuse downgrading is to deal with the internal problems.

Sentinel degradation will restrict the invocation of a resource in the call link when it is in unstable state (such as call timeout or abnormal proportion increases), so that the request can fail quickly and avoid cascading errors caused by affecting other resources. When a resource is degraded, all calls to the resource are automatically disabled within the next degraded window. The downgrade was actually a circuit breaker in Hystrix, remember how Hystrix worked at the time


strategy

Sentinel does not miss a request like Hystrix does and try to repair itself, which is that the fuse is triggered and the request is rejected within the time window and then restored after the time window.

  • RT (Average response time)

    If the average response time exceeds the threshold (in ms), all calls to this method will automatically fuse (throw a DegradeException) within the next time window (in s). Pay attention to the Sentinel default statistical RT limit is 4900 ms, is beyond the threshold will be classified as 4900 ms, if need to change this limit by startup configuration items – Dcsp. Sentinel. Statistic. Max. RT = XXX to configure.

    image-20200825103415604
  • Abnormal proportion

    When requests per second for the resource >= 5 and the ratio of total exceptions per second to passes exceeds the threshold, the resource enters the degraded state, that is, within the next time window (in s), all calls to this method are automatically returned. The threshold range for the abnormal ratio is [0.0, 1.0], representing 0-100%.

    image-20200825103510765
  • Number of abnormal

    When the number of resource anomalies in the last 1 minute exceeds the threshold, the system fuses. Notice the statistical timeWindow is minute. If the timeWindow is less than 60 seconds, the circuit breaker may enter the circuit breaker state again after the circuit breaker state ends. Time window >= 60s

    image-20200825103601454

Nacos implements Sentinel rule persistence

Currently, the rule data added to Sentinel Dashboard is stored in memory, and the rule data disappears when the microservice is stopped, which is not appropriate in a production environment. We can persist Sentinel rule data to the Nacos configuration center and let microservices get rule data from Nacos.

image-20200826143214750

Rely on

First we need to add dependencies to the project

<! -- Sentinel supports the use of Nacos as a rule configuration data source.<dependency>
 <groupId>com.alibaba.csp</groupId>
 <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
Copy the code

Increase the configuration

    sentinel:
      transport:
Dashboard: 127.0.0.1:8080        port: 8719
      # Sentinel Nacos data source configuration, the rules in Nacos will be automatically synchronized to Sentinel flow control rules
 datasource:  # Custom flow control rule data source name  flow:  nacos:  server-addr: ${spring.cloud.nacos.discovery.server-addr}  data-id: ${spring.application.name}-flow-rules  groupId: DEFAULT_GROUP  data-type: json  rule-type: flow The # type comes from the RuleType class  # Custom reversion rule data source name  degrade:  nacos:  server-addr: ${spring.cloud.nacos.discovery.server-addr}  data-id: ${spring.application.name}-degrade-rules  groupId: DEFAULT_GROUP  data-type: json  rule-type: degrade The # type comes from the RuleType class  Copy the code

Nacos Server

Next we need to add the corresponding rule configuration set in Nacos Server (public namespace – >DEFAULT_GROUP)

Configure flow control rules

[{
 "resource":"/user/login/[email protected]/123". "limitApp":"default". "grade": 1, "count": 1, "strategy": 0. "controlBehavior": 0. "clusterMode":false }] Copy the code
image-20200826145959712

All attributes come from the source FlowRule class

  • Resource: indicates the name of the resource

  • LimitApp: Source app

  • Grade: Threshold Type 0 Number of threads 1 QPS

  • Count: single-machine threshold

  • Strategy: In flow control mode, 0 directly associates 1 with 2 links

  • ControlBehavior: Flow control effect, 0 fast failure 1 Warm Up 2 Queue waiting

  • ClusterMode: True /false Indicates whether to cluster

Configuring degradation Rules

[{
 "resource":"/user/login/[email protected]/123". "grade": 2. "count": 1, "timeWindow": 5}] Copy the code

All properties come from the Source Anyhow ule class

  • Resource: indicates the name of the resource

  • Grade: downgrade policy 0 RT 1 Abnormal ratio 2 Abnormal number

  • Count: the threshold

  • TimeWindow: indicates the timeWindow

Note:

1. A resource can have multiple limiting and degrading rules at the same time, so the configuration set is a JSON array

2. The modification of rules in Sentinel console only takes effect in memory and does not modify the configuration value in Nacos. The original value is restored after restart. The modification of rules in the Nacos console takes effect not only in memory, but also in persistent rules in Nacos, and the rules remain after the restart

conclusion

Sentinel is a flow control, fuse downgrading component for cloud native microservices. In the course I learned in the pull hook training camp, the teacher just talked about this part of the content, I followed the teacher’s practice, their influence is more profound. If you are interested, get your hands on it

This article is formatted using MDNICE