zuul integrates Sentinel's latest gateway flow control components

Keywords: Java Spring

1. Description

Sentinel Gateway Flow Control supports flow control for different routes and custom API groupings, and for request attributes such as URL parameters, Client IP, Header, etc.Sentinel 1.6.3 introduces support for the Gateway Flow Control Console, which allows users to view API Gateway real-time route and custom API grouping monitoring directly on the entinel console, manage gateway rules, and API grouping configuration.

 

2. Functional Access

1. Gateway adds sentinel-related jar dependencies

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>

 

2. sentinel configuration for gateway zuul

spring:
  # sentinel dynamic configuration rule
  cloud:
    sentinel:
      zuul:
        enabled: true
        order:
          pre: 2000
          post: 500
          error: -100
      filter:
        enabled: false
      datasource:
        # Current Limiting
        ds1:
          nacos:
            server-addr: ${zlt.nacos.server-addr}
            dataId: ${spring.application.name}-sentinel-gw-flow
            groupId: DEFAULT_GROUP
            rule-type: gw-flow
        # api grouping
        ds2:
          nacos:
            server-addr: ${zlt.nacos.server-addr}
            dataId: ${spring.application.name}-sentinel-gw-api-group
            groupId: DEFAULT_GROUP
            rule-type: gw-api-group

The rule data source for binding gw-flow (flow limiting) and gw-api-group(api grouping) is nacos
And specify the corresponding dataId and groupId on nacos

 

3. nacos rule configuration

3.1. Current Limiting Configuration gw-flow

  • Data ID: api-gateway-sentinel-gw-flow
  • Group: DEFAULT_GROUP
  • Configuration:

    [
      {
        "resource": "user",
        "count": 0,
        "paramItem": {
          "parseStrategy": 3,
          "fieldName": "name"
        }
      },
      {
        "resource": "uaa_api",
        "count": 0
      }
    ]

    Rule 1: All user requests are intercepted as long as the parameter has a name (qps=0), and user is routeId on the zuul routing configuration
    Rule 2: All requests grouped into uaa_api by API are blocked (qps=0)

 

3.2. api grouping configuration gw-api-group

  • Data ID: api-gateway-sentinel-gw-api-group
  • Group: DEFAULT_GROUP
  • Configuration:

    [
      {
        "apiName": "uaa_api",
        "predicateItems": [
          {
            "pattern": "/user/login"
          },
          {
            "pattern": "/api-uaa/oauth/**",
            "matchStrategy": 1
          }
        ]
      }
    ]

    The above configuration means that all APIs meeting the rules are grouped together into uaa_api
    Grouping Rule 1: Precision Matching/user/login
    Grouping Rule 2: Prefix Matching/api-uaa/oauth/**

 

4. Gateway zuul startup parameters

You need to add -Dcsp.sentinel.app.type=1 to the original startup parameters on the access side to mark your service as an API Gateway. When you access the console, your service is automatically registered as a gateway type. Then you can configure the gateway rules and API grouping on the console, for example:

java -Dcsp.sentinel.app.type=1 -jar zuul-gateway.jar

 

3. sentinel console management

API Management (Grouping)

Gateway Flow Control Rules

 

4. Test current limiting api

1. Test current limiting rule 1

All user requests are intercepted as long as the parameter has a name (qps=0)

  • Access to api without name parameter

  • The request was intercepted by appending the name parameter

 

2. Test current limiting rule 2

All requests grouped into uaa_api by API are blocked (qps=0)

  • Prefix Matching/api-uaa/oauth/**

  • Precision Matching/user/login

Posted by mclordgt on Sun, 18 Aug 2019 20:23:53 -0700