SpringCloudAlibaba - integrate Sentinel to realize service flow restriction


Sentinel is a lightweight flow control component for distributed architecture. It mainly takes flow as the starting point to ensure the stability of micro services from multiple dimensions such as flow restriction, flow shaping, service degradation and system load protection. Sentinel provides very flexible and powerful flow restriction capabilities and supports various current restriction gestures

Composition of Sentinel

  • Core library (Java client): it does not depend on any framework / library and can run in all Java runtime environments
  • Dashboard: it is developed based on SpringBoot and can be run directly after packaging


Spring Cloud Hoxton.SR9 + Spring Cloud Alibaba 2.2.6.RELEASE + Sentinel 1.8.1

Integrate Sentinel

  • pom.xml
<!-- sentinel -->
<!-- actuator -->
  • application.yml
# Actuator
  endpoint.health.show-details: always # health
        include: "*"
  • View sentinel information through the actor / sentinel endpoint, http://localhost:8010/actuator/sentinel

Set up Sentinel console

  • The console starts the jar package (specify port 8083), and the default port of sentinel is 8080
java -jar sentinel-dashboard-1.8.1.jar --server.port=8083

  • Access Sentinel console http://localhost:8083

  • Default account password: sentinel/sentinel

Integrate Sentinel console

  • application.yml
    # Service name
    name: content-center
        # Specifies the address of the sentinel console
        dashboard: localhost:8083
  • Sentinel is lazily loaded. Only by accessing the interface of the next microservice can you see the real-time monitoring details

  • Real time monitoring shows the fluctuation of QPS (query rate per second) in recent minutes

Sentinel and console

Communication principle between Sentinel and console

Sentinel implements a set of service discovery mechanism. The micro service needs to integrate the sentinel transport simple HTTP module. After integrating the module, it will register itself with the sentinel dashboard (console) and send heartbeat to the console regularly. After registering with the console, the console will know the address of each micro service and can call the API on the micro service to monitor health status and push rules

  • You can view sentinel information through the actor / sentinel endpoint, http://localhost:8010/actuator/sentinel

  • Through clientIp:clientPort/api, you can view the API of sentinel console communicating with microservices

Application side connection console configuration item

  # Specify console address
  dashboard: localhost:8083
  # Specify the communication IP. If it is not configured, IP registration will be automatically selected
  # Specify the communication port. The default value is 8719. If it is not configured, the unoccupied port will be found from 8719 + 1
  port: 8719
  # Heartbeat sending cycle, null by default
  heartbeat-interval-ms: 10000

Console configuration item

  • Console configuration item
Configuration itemDefault valuedescribe
server.port8080Specify port
csp.sentinel.dashboard.serverlocalhost:8080Specify address
project.name-Specifies the name of the program
sentinel.dashboard.auth.username[1.6]sentinelDashboard login account
sentinel.dashboard.auth.password[1.6]sentinelDashboard login password
server.servlet.session.timeout[1.6]30 MinutesLogin Session expiration time
If it is configured as 7200, it means 7200 seconds
The configuration is 60m, which means 60 minutes
  • For example, change the user name and password
java -jar -Dsentinel.dashboard.auth.username=xxx -Dsentinel.dashboard.auth.password=xxx sentinel-dashboard-1.8.1.jar

Flow control rules

  • Click the cluster link to see the path that has been accessed

  • The flow control button can set flow control rules for this path

Direct flow control mode

  • Add a direct flow control rule. The threshold type is QPS and the threshold is 1

  • As the name implies, the QPS of the current API exceeds the threshold time limit stream

Associated flow control mode

When the associated resource reaches the threshold, it limits itself

  • When the QPS of the / test endpoint reaches the threshold, the current is limited / test4

  • Make the access of / test endpoint above the threshold through cyclic call
public static void main(String[] args) throws InterruptedException {
    RestTemplate restTemplate = new RestTemplate();
    for (int i = 0; i < 1000; i ++) {
        String url = "http://localhost:8010/test";
        restTemplate.getForObject(url, String.class);
  • When the QPS of the associated resource / test is higher than the threshold, / test4 is always in the current limiting state

Link flow control mode

Only the traffic on the specified link is recorded. If the traffic of the specified resource coming in from the entrance resource reaches the threshold, the flow restriction is enabled

Sentinel 1.8.1 link flow control mode failure handling

<!-- sentinel 1.8.1 Link flow control mode failure -->
  • Close the CommonFilter instantiation of sentinel

Link flow control mode test

  • TestController.java
public class TestController {

    private TestService testService;

    public String test_a() {
        return "test_a";

    public String test_b() {
        return "test_b";
  • TestService.java
public class TestService {

	// @SentinelResource annotation is used to define resources
    public void common() {

  • After accessing the two interfaces, there is a cluster point link

  • Configure the flow control rule of / test-a - > common as link

  • Frequent access, when the entry / test_ When the QPS of a exceeds the threshold, it is current limited

  • And / test_b) normal access

Difference between link flow control mode and associated flow control mode

Link flow control mode is a fine-grained source specific mode API Level, and associated flow control mode micro service level

Flow control effect

Rapid failure

  • Direct failure throw exception

Warm Up

  • Warm Up can make the allowable flow increase slowly, and then reach the set QPS threshold after preheating for a long time

Queue up

  • Queue at a uniform speed to allow requests to pass at a uniform speed. The threshold type must be set to QPS, otherwise it is invalid

  • As described above, only one request is allowed in 1s, and the remaining requests are queued after the timeout is exceeded and the request is discarded

- End - ﹀ ﹀ ﹀ White whoring is risky Point zanga collection

Tags: Spring Cloud sentinel

Posted on Fri, 24 Sep 2021 09:57:20 -0400 by eco