SpringCould Microservice Protection 01 – Download and use the Sentinel component

1. First introduction to Sentinel

1.1. Avalanche problems and solutions

1.1.1. Avalanche problem

In microservices, the calling relationships between services are complex, and one microservice often depends on multiple other microservices.
As shown in the figure, if service provider I fails, part of the current application’s business will also be blocked because it depends on service I. At this time, other businesses that do not depend on service I appear to be unaffected.
However, the business requests that rely on service I are blocked and the user will not get a response, so the tomcat thread will not be released, so more and more user requests come, and more and more threads will be blocked:

The number of threads and concurrency supported by the server is limited, and the request is always blocked, which will cause the server resources to be exhausted, resulting in all other services being unavailable, and the current service will also be unavailable.

Then, other services that depend on the current service will eventually become unavailable over time, forming a cascade of failures, and an avalanche occurs:

1.1.2. Timeout processing

There are four common ways to solve avalanche problems:

?Timeout processing: Set the timeout period. If the request exceeds a certain time and there is no response, an error message will be returned. There will be no endless waiting. ” >

1.1.3. Warehouse wall mode

Option 2: Warehouse wall mode

The warehouse wall pattern comes from the design of the cabin:
The cabins will be separated into multiple independent spaces by partitions. When the hull is damaged, only part of the space will be allowed to enter. The failure can be controlled within a certain range to prevent the entire hull from being submerged.

Similar to this, we can limit the number of threads that each business can use to avoid exhausting the entire tomcat resources, so it is also called thread isolation.

1.1.4. Circuit breaker

Circuit breaker mode: The circuit breaker counts the abnormal proportion of business execution. If the threshold is exceeded, the business will be circuited and all requests to access the business will be intercepted.

The circuit breaker counts the number of requests to access a certain service, and the abnormal ratio is:


When it is found that the abnormal proportion of requests to access service D is too high, it is believed that service D has the risk of causing an avalanche, and all requests to access service D will be intercepted to form a circuit breaker:

1.1.5. Current limiting

Traffic control: QPS that limits business access to avoid service failures due to sudden increases in traffic.

1.1.6. Summary

What is an avalanche problem?

  • Microservices call each other. Because one service in the call chain fails, the entire link becomes inaccessible.

It can be considered:

Current limiting is the protection of services to avoid service failures caused by instantaneous high concurrent traffic, thereby avoiding avalanches. It is apreventivemeasure.

Timeout processing, thread isolation, and downgrade circuit breaker are used to control failures within a certain range and avoid avalanches when some services fail. It is aremedial measure.

1.2. Comparison of service protection technologies

SpringCloud supports multiple service protection technologies:

  • Netfix Hystrix
  • Sentinel
  • Resilience4J

The Hystrix framework was more popular in the early days, but currently the most widely used framework in China is Alibaba’s Sentinel framework. Here we make a comparison:

Sentinel Hystrix
Isolation strategy Semaphore isolation Thread pool isolation/semaphore isolation
Circuit breaker downgrade strategy Based on slow call ratio or abnormal ratio Based on failure ratio
Real-time indicator implementation Sliding window Sliding window (based on RxJava)
Rule configuration Support multiple data sources Support multiple data sources
Extensibility Multiple extension points Plug-in form
Annotation-based support Support Support
Current limiting Based on QPS, supports current limiting based on call relationships Limited support
Traffic shaping Supports slow start, Uniform queuing mode Not supported
System adaptive protection Supported Not supported
Console Out of the box, you can configure rules, view second-level monitoring, machine discovery, etc. Imperfect
Adaptation of common frameworks Servlet, Spring Cloud, Dubbo, gRPC, etc. Servlet, Spring Cloud Netflix

1.3.Sentinel introduction and installation

1.3.1. First introduction to Sentinel

Sentinel is a microservice flow control component open sourced by Alibaba. Official website address: https://sentinelguard.io/zh-cn/index.html

Sentinel has the following characteristics:

?Rich application scenarios: Sentinel has undertaken the core scenarios of Alibaba’s Double Eleven traffic promotion in the past 10 years, such as flash sales (that is, burst traffic is controlled within the range that the system capacity can bear), messaging Peak shaving and valley filling, cluster flow control, real-time fusing of 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 clusters with less than 500 machines.

?Extensive open source ecosystem: 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 simple configuration to quickly connect to Sentinel.

?Complete SPI Extension points: Sentinel provides an easy-to-use, complete SPI extension interface. You can quickly customize logic by implementing extension interfaces. For example, customized rule management, adapting dynamic data sources, etc.

1.3.2. Install Sentinel

1) Download

Sentinel officially provides a UI console to facilitate us to set current limit on the system. You can download it on GitHub.

2) Run

Place the jar package in any non-Chinese directory and execute the command:

java -jar sentinel-dashboard-1.8.1.jar

If you want to modify Sentinel’s default port, account, and password, you can configure it through the following:

Configuration items Default value Description
server.port 8080 Service port
sentinel.dashboard.auth.username sentinel Default username
sentinel.dashboard. auth.password sentinel Default password

For example, modify the port:

java -Dserver.port=8090 -jar sentinel-dashboard-1.8.1.jar

3) Visit

Visit the http://localhost:8080 page and you can see the sentinel console:

1.4. Microservice integration with Sentinel

We integrate sentinel in order-service and connect to sentinel’s console. The steps are as follows:

1) Introduce sentinel dependency

<!--sentinel-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

2) Configure the console

Modify the application.yaml file and add the following content:

server:
  port: 8088
spring:
  cloud:
    sentinel:
      transport:
        dashboard: localhost:8080

3) Access any endpoint of order-service

Open the browser and visit http://localhost:8088/order/101 to trigger sentinel monitoring.

Then visit the sentinel console to check the effect: