A, description,
Sentinel Gateway flow control supports flow control for different routes and user-defined API groups, as well as for request attributes such as URL parameters, Client IP, Header, etc. Sentinel 1.6.3 introduces support for the Gateway flow control console. Users can view real-time route and custom API group monitoring from the API Gateway directly on the Sentinel console, and manage Gateway rules and API group configuration.
Two, function access
1. The 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>
Copy the code
2. Sentinel configuration of gateway Zuul
Spring: # sentinel Dynamic configuration rule Cloud: sentinel: zuul: enabled: true order: pre: 2000 Post: 500 error: -100 filter: enabled: Ds1: nacos: server-addr: ${zlt.nacos.server-addr} dataId: ${spring.application.name}-sentinel-gw-flow groupId: DEFAULT_GROUP rule-type: gw-flow # API ds2: nacos: server-addr: ${zlt.nacos.server-addr} dataId: ${spring.application.name}-sentinel-gw-api-group groupId: DEFAULT_GROUP rule-type: gw-api-groupCopy the code
Binding gW-flow (flow limiting) and GW-API-Group (API grouping) rule data source is NACOS and specifying corresponding dataId and groupId on NACOS
3. Configure the NACOS rule
3.1. Configure GW-flow for traffic limiting
- Data ID:
api-gateway-sentinel-gw-flow
- Group:
DEFAULT_GROUP
- Configuration contents:
[ { "resource": "user". "count": 0. "paramItem": { "parseStrategy": 3. "fieldName": "name" } }, { "resource": "uaa_api". "count": 0 } ] Copy the code
Rule 2: Block all requests whose API group is uAA_API (QPS =0).
3.2. Configure gW-API-group for API groups
- Data ID:
api-gateway-sentinel-gw-api-group
- Group:
DEFAULT_GROUP
- Configuration contents:
[ { "apiName": "uaa_api". "predicateItems": [ { "pattern": "/user/login" }, { "pattern": "/api-uaa/oauth/**". "matchStrategy": 1 } ] } ] Copy the code
The apis that meet the rule are grouped as uAA_API group Rule 1: Exact match /user/login Group rule 2: prefix match/apI-uAA /oauth/**
4. Zuul startup parameters of the gateway
You need to add -dcsp.sentinel.app. type=1 startup to mark your service as API Gateway. When you access the console, your service will be automatically registered as Gateway type. Then you can configure Gateway rules and API grouping on the console, for example:
java -Dcsp.sentinel.app.type=1 -jar zuul-gateway.jar
Copy the code
Sentinel console management
API Management (Group)
Gateway flow control rules
Test the flow limiting API
1. Test traffic limiting rule 1
All user requests with name are intercepted (QPS =0)
- Without the name parameter,Access to the API
- Followed by the name parameter, requestBe intercepted
2. Test traffic limiting rule 2
All requests grouped into UAA_API are intercepted (QPS =0)
- Prefix matching
/api-uaa/oauth/**
- Precise matching
/user/login