Spring Cloud Gateway integrates Alibaba Sentinel gateway to limit current

The article contents are as follows:

How does the gateway limit current?

Spring Cloud Gateway has its own current limiting implementation. The filter is RequestRateLimiterGatewayFilterFactory. However, those that can't be introduced will not be introduced. Those who are interested can be implemented.

Today's focus is to integrate Alibaba sentinel to realize gateway current limiting. If sentinel doesn't understand anything, you can read Chen's article: Alibaba's current limiting artifact sentinel lethal serial 17 q?

Starting from version 1.6.0, Sentinel provides the adaptation module of spring cloud gateway, which can provide flow restriction of two resource dimensions:

  • Route dimension: refers to the route entry configured in the configuration file. The resource name is the corresponding routeId. This is a coarse-grained flow restriction, which is generally used to restrict the flow of a micro service.
  • Custom API dimension: users can use the API provided by Sentinel to define some API groups. This is a fine-grained flow restriction. Matching flow restriction is carried out for a class of URIs, which can span multiple micro services.


sentinel official documents: https://github.com/alibaba/Sentinel/wiki/%E7%BD%91%E5%85%B3%E9%99%90%E6%B5%81


The implementation of Spring Cloud Gateway integration Sentinel is very simple, which is the charm of Ali. It provides simple and easy-to-use tools to let programmers focus on business.

New project

Create a new gateway-sentinel9026 module and add the following dependencies:

<!--nacos Registration Center-->

    <!--spring cloud gateway-->

    <!--    spring cloud gateway integration sentinel Dependence of-->

    <!--    sentinel Dependence of-->


Note: This is still a gateway service. Do not add WEB dependency


configuration file

The following three configurations are specified in the configuration file:

  • Address of nacos
  • sentinel console address
  • Gateway routing configuration

The configuration is as follows:

    ## Integrate sentinel and configure the address of sentinel console
        ## Specify the address of the console. The default port is 8080
        dashboard: localhost:8080
      ## Registry configuration
        # Service address of nacos, IP address in nacos server: port number
      ## route
        ## As long as the id is unique, the name is arbitrary
        - id: gateway-provider
          uri: lb://gateway-provider
          ## Configuration assertion
            ## Path Route Predicate Factory asserts that all paths satisfying the request of / gateway/provider / * * will be routed to http://localhost:9024 In this uri
            - Path=/gateway/provider/**

A routing gateway provider is set in the above configuration. As long as the request path meets / gateway/provider / * *, it will be routed to the gateway provider service.

Current limiting configuration

Sentinel has been integrated through the above two steps. At this time, access the following interface: http://localhost:9026/gateway/provider/port

On the sentinel console, you can see that the monitored route is a gateway provider, as shown in the following figure:

At this time, we can add a current limit of route dimension, as shown in the following figure:

In the figure above, the current of the gateway provider route is limited, and the QPS threshold is 1.

Quick access at this point: http://localhost:9026/gateway/provider/port , it can be seen that the current has been limited, as shown in the following figure:

The current limit of the above route dimension has been configured successfully. Small partners can try it according to the above steps.

API Group current limiting is also very simple. First, you need to define a group, API management - > Add API Group, as shown in the following figure:

The matching mode selects exact matching (also prefix matching and regular matching), so there is only this uri: http://xxxx/gateway/provider/port Will be current limited.

The second step is to add flow control rules to this group. Flow control rules - > Add gateway flow control, as shown in the following figure:

Select the corresponding group from the API name. After adding, the flow restriction rules will take effect.

Chen won't test any more. Let's test it by ourselves


Chen here is just a simple configuration. For some content about the persistence of flow restriction rules, please see Chen's Sentinel article, which will not be introduced too much here.


How to customize current limiting exception information?

From the above demonstration, we can see that the default exception return information is "Block..........", which is certainly unacceptable to the client, so you need to customize your own exception return information.

The following describes two different ways to customize exception return information. Choose one of them during development.

Direct customization in configuration file

Developers can directly modify the returned information in the configuration file. The configuration is as follows:

    ## Integrate sentinel and configure the address of sentinel console
      #After current limiting is configured, the response content
          ## There are two modes. One is response, which returns text prompt information,
          ## One is redirect, which redirects and jumps. Redirect (the URI of the jump) needs to be configured at the same time
          mode: response
          ## Status of response
          response-status: 200
          ## Responder
          response-body: '{"code": 200,"message": "The request failed. Please try again later!"}'

In the above configuration, the mode configuration is response. Once the current is limited, the JSON string will be returned.

    "code": 200,
    "message": "The request failed. Please try again later!"

The configuration of redirection is as follows:

    ## Integrate sentinel and configure the address of sentinel console
      #After current limiting is configured, the response content
          ## There are two modes: one is response, which returns a text prompt message, and the other is redirect, which redirects the jump. Redirect (the URI of the jump) needs to be configured at the same time
          mode: redirect
          ## URL to jump
          redirect: http://www.baidu.com

Once current is limited, it will jump directly to: http://www.baidu.com

Coding customization

This is not very flexible. Through hard coding, the complete code is as follows:

public class GatewayConfig {
     * Custom current limiting processor
    public void initBlockHandlers() {
        BlockRequestHandler blockHandler = (serverWebExchange, throwable) -> {
            Map map = new HashMap();
            map.put("message","The request failed. Please try again later!");
            return ServerResponse.status(HttpStatus.OK)

After introducing the two methods, choose the appropriate method according to the business needs. Of course, Chen prefers the first. Reason: agreement > configuration > coding.

If the gateway limits the current, is the service safe?

Many people think that as long as the flow is limited at the gateway level, the services hiding behind can rest easy. Do you also have this idea?

Obviously, this idea is wrong. In a complex microservice architecture, an independent service is not only called by one party, but often called by multiple parties, as shown in the following figure:

The commodity service is not only called by the gateway layer, but also by the internal order service. At this time, the flow is limited only at the gateway layer. Is the commodity service safe?

Once a large number of orders are requested for services, such as big promotion spike, the goods and services will be destroyed in an instant without flow restriction.

Therefore, you need to limit the flow of your own services according to the company's business scenarios. The most common scheme is: gateway layer cluster current limit + single machine current limit of internal services, so as to ensure that it will not be overwhelmed by traffic.


This paper introduces the integration of Sentinel in Spring Cloud Gateway to limit the current in the gateway layer, as well as some thoughts on current limiting. If there are any mistakes, please leave a message for correction.

Tags: Java Back-end Spring Cloud Microservices gateway

Posted on Thu, 04 Nov 2021 14:45:54 -0400 by TJMAudio