这里使用SpringCloud
。
依赖
父类依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency>
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
|
基础依赖
1 2 3 4 5
| <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency>
|
sentinel
使用 nacos
做配置中心
1 2 3 4 5
| <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency>
|
sentinel
与 gateway
依赖
1 2 3 4 5
| <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId> </dependency>
|
配置
配置文件配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| spring: cloud: gateway: discovery: locator: lowerCaseServiceId: true enabled: true routes: - id: auth uri: lb://auth predicates: - Path=/auth/** filters: - StripPrefix=1 nacos: discovery: server-addr: 127.0.0.1:8848 config: server-addr: 127.0.0.1:8848 file-extension: yml shared-configs: - application-dev.${spring.cloud.nacos.config.file-extension} sentinel: eager: true transport: port: 8719 dashboard: 127.0.0.1:48080 datasource: ds1: nacos: server-addr: 127.0.0.1:8848 dataId: sentinel-gateway groupId: DEFAULT_GROUP data-type: json rule-type: flow
|
规则的种类(ruleType
):
- flow: 流量控制
- degrade: 熔断降级
- system:系统保护
- authority:访问控制
- param-flow:热点
官方规则种类配置说明
flow
的配置
nacos
配置中心就可以添加网关流量控制规则:
1 2 3 4 5 6 7 8 9 10
| [ { "resource": "auth", "count": 500, "grade": 1, "limitApp": "default", "strategy": 0, "controlBehavior": 0 } ]
|
网关限流规则:GatewayFlowRule
本文地址: https://github.com/maxzhao-it/blog/post/8a4e05d0/