Let’s talk about Sentinel flow control

2023.10.09

Let’s talk about Sentinel flow control


Although there are four solutions to the avalanche problem (flow control, circuit breaker degradation, thread isolation, and timeout processing), flow control is to prevent services from malfunctioning due to sudden traffic and is the prevention of avalanche problems in microservices, so we first learn about Sentinel Flow control or current limiting.

Today's goal

  • Install Sentinel
  • Project integration Sentinel

1. Flow control

Although there are four solutions to the avalanche problem (flow control, circuit breaker degradation, thread isolation, and timeout processing), flow control is to prevent services from malfunctioning due to sudden traffic and is the prevention of avalanche problems in microservices, so we first learn about Sentinel Flow control or current limiting.

How does Sentinel achieve flow control?

Sentinel monitors each endpoint (Endpoint) of SpringMVC, so each endpoint (Endpoint) of SpringMVC is a resource in the call link. When a request enters a microservice, it accesses DispatcherServlet, and then enters Controller, Service, and Mapper. Such a call chain is called a cluster point link. Each interface monitored in a cluster point link is a resource.

For example, the endpoint in the OrderController in the order-service we just visited: /order/{Id}

picturepicture

In the future, our flow control and circuit breaker will be initiated for the resources in the cluster point link, so we can click the button behind the corresponding resource to set the rules:

  • Flow control: flow control
  • Downgrade: downgrade circuit breaker
  • Hotspot: Hotspot parameter current limiting is a type of current limiting.
  • Authorization: Requested permission control

2. Get started quickly

Click the flow control button of edit resource/order/{id}

picturepicture

You can fill in the current limiting rules in the form, as follows:

picturepicture

Fill in the single-machine threshold: 2, which means: limit the QPS of resource /order/{id} to 2, which means that only 2 requests are allowed per second. Requests exceeding the limit will be intercepted and an error will be reported.

2.1.Testing

When you quickly refresh the browser, you will find the following error message

picturepicture

But generally speaking, the traffic produced is more. Here we will use a stress testing tool: Jmeter to do a more comprehensive stress test.

2.2. Test using Jmeter

download

You can download it from the Apache Jmeter official website, address: https://jmeter.apache.org/

picturepicture

Unzip

After downloading Jmeter, unzip it and use it. The directory structure is as follows:

picturepicture

The bin directory is the executed script, which contains the startup script:

picturepicture

run

Double-click jmeter.bat to run:

picturepicture

Jmeter test

Test QPS < 2 using Jmeter

picturepicture

This rule is as follows: 10 requests are executed within 1 second. The current limit rule that takes effect at this time is 2 (up to 2 requests can pass per second).

picturepicture

start up

picturepicture

After running and starting, you will find that up to 2 requests can be passed per second (the following display may be out of order, subject to the request time):

picturepicture

At this point, we have completed the introductory effect of flow control, which is also the direct mode of one of the flow control modes.

If you think this article is good, please follow, like, collect and support it. Your attention is the motivation for me to persist!

3. Sentinel flow control mode

When adding flow limiting rules in Sentinel, click Advanced Options to choose from three flow control modes:

  • Direct: counts requests for current resources, and directly limits current resources when the threshold is triggered. This is also the default mode.
  • Association: Statistics on another resource related to the current resource. When the threshold is triggered, the current resource is limited.
  • Link: Counts requests to access this resource from the specified link. When the threshold is triggered, the specified link will be flow-limited.

picturepicture

Quick Start is the direct mode, which is also the default mode.

3.1 Sentinel flow control-direct mode

What is Sentinel flow control direct mode

Direct mode: For a request for the current resource, when the threshold is triggered, the current resource is directly limited. This is also the default mode.

Direct mode applicable scenarios

Direct mode can be used for any special scenarios with no obvious differences, which is simpler and easier to use.

3.2 Sentinel flow control-association mode

What is association mode

Association mode: Counts another resource related to the current resource. When the threshold is triggered, the current resource is limited.

Applicable scenarios for association mode

In a mall system, users need to modify the order status when paying, and at the same time, users need to query the order. Query and modification operations will compete for database locks (too high a reading speed will affect writing, and an excessive writing speed will affect reading speed). The overhead caused by the competition itself will reduce the overall throughput. At this time, we need to give priority to ensuring the function of modifying orders and limiting the flow of inquiry and order business. That is, when A triggers the condition, the associated resource B will have current limit.

picturepicture

Association mode implementation association rules are established as follows:

picturepicture

Rules explained

When the number of accesses to the /write interface exceeds the threshold (we have our own flow control rules for /write), the /read interface will be flow-limited to avoid the impact on: /write resources.

Association rules in action

need

  • Create two new endpoints (http interface) in OrderController: /order/query and /order/update
  • Configure associated flow control rules: When the /update interface access exceeds QPS>3, limit the flow of /query

[Step 1]: Define the /order/query interface

@GetMapping("/query")
public String queryOrder() {
    return "查询order成功";
}
  • 1.
  • 2.
  • 3.
  • 4.

[Step 2]: Define the /order/update interface

@GetMapping("/update")
public String updateOrder() {
    return "更新order成功";
}
  • 1.
  • 2.
  • 3.
  • 4.

[Step 3] Restart the service and check the cluster point link of the sentinel console

After restarting the SentinelOrderApplication service, the browser accesses the endpoints: http://localhost:9092/order/query, http://localhost:9092/order/update, accesses the Sentinel console, and views the new endpoint information:

picturepicture

[Step 4] Configure associated flow control rules

An example is to limit the current flow of the /order/query interface, then click the corresponding button behind it.

picturepicture

Configure flow control rules as follows:

Note: For whom we are limiting the flow, just click the flow control button of the corresponding resource. Here we are targeting: /order/query

picturepicture

[Step 5] Jemter test configuration associated flow control

Jmeter stress test verification, lasting 10s, qps=10, the purpose is to continuously cause a window period exceeding the configured threshold and give users time to access verification.

100 users can be seen for 10 seconds, so the QPS is 10, which exceeds the threshold we set: 3

View HTTP requests

picturepicture

The target of the request is /order/update, so that the endpoint /order/update will trigger the threshold.

Note: The target of current limiting is /order/query. When we access it in the browser, we can find:

picturepicture

3.3. Sentinel flow control-link mode

What is link mode

Link mode: Make statistics on requests to access this resource from the specified link to determine whether the threshold is exceeded.

Link mode applicable scenarios

As business developers, we have an order query business, and the output is displayed on the page. The access link is: own system Controller-->query order interface. At this time, the data team needs to rely on our interface for cockpit information, because we expose the query order interface to other teams, that is, add a new access link: Data Team Controller-->Query Order Interface. We have reasonably evaluated the QPS of our own system based on stress testing, but we have not considered the data team's request. Assuming tens of thousands of requests come in, our own system performance will also be significantly affected. At this time we need to limit the calls from the data team. as follows

picturepicture

The link mode implementation has two request links:

  • /user --> /userinfo
  • /login --> /userinfo

If you only want to count requests from /login to /userinfo, you can configure it like this:

picturepicture

Link rules in action

need

There are query order (/query) and create order (/createOrder) businesses, both of which require querying goods (service layer method queryGoods). Collect statistics on requests from order inquiry to product inquiry, and set current limits.

[Step 1]: Define the /order/query interface

@GetMapping("/query")
public String queryOrder() {
    orderService.queryGoods();;
    return "查询order成功";
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

[Step 2]: Define the /order/create interface

@GetMapping("/create")
public String createOrder() {
    // 查询商品
    orderService.queryGoods();
    // 查询订单
    System.out.println("新增订单");
    return "新增order成功";
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

[Step 3]: In the order-service service, add a queryGoods method to the OrderService interface:

public interface OrderService {

    /**
     * 创建订单
     */
    Long create(Order order);

    void queryGoods();
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

[Step 4]: In the order-service service, add a queryGoods method to the OrderService interface that implements OrderServiceImpl:

@Override
@SentinelResource("goods")
public void queryGoods(){
    System.out.println("查询商品");
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

Notice:

By default, the methods in OrderService are not monitored by Sentinel. We need to mark the methods to be monitored through annotations.

Add the @SentinelResource annotation to the queryGoods method of OrderServiceImpl

[Step 5]: Modify application.ym

In link mode, two links from different sources are monitored. However, sentinel will set the same root resource for all requests entering SpringMVC by default, which will cause the link mode to fail.

We need to turn off this resource aggregation of SpringMVC and modify the application.yml file of the order-service service

spring:
  cloud:
    sentinel:
      web-context-unify: false # 关闭context整合
  • 1.
  • 2.
  • 3.
  • 4.

[Step 6]: Set current limiting rules for queryGoods. The method of entering queryGoods from /order/query limits QPS to less than 2.

Note: Before setting the rules, you also need to access the cluster point link to generate the rules, so you need to restart the service + access the new interface.

picturepicture

picturepicture

Only resources entering /goods from /order/query are counted. The QPS threshold is 2. If it exceeds the QPS threshold, the traffic will be limited.

[Step 7] Jemter test configuration link flow control

Start the Jmeter stress test. It is expected that /order/create will not be affected, and /order/query can allow up to 2 traffic.

/order/create

picturepicture

picturepicture

/order/query

picturepicture

picturepicture

Summarize

What are the flow control modes?

  • Direct: limit current resources
  • Association: trigger threshold for high-priority resources and limit flow for low-priority resources.
  • Link: During threshold statistics, only requests entering the current resource from the specified resource are counted, which is a current limit for the request source.