在使用Sentinel我们发现,只要重新启动Sentinel的Java 客户端服务,Sentinel控制台配置的限流规则,就清空不存在了,下面介绍怎么持久化Sentinel规则。
我们继续修改前面的代码。
增加依赖
<!-- sentinel-datasource-nacos -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
修改配置增加
增加Nacos数据源
server:
port: 8401
spring:
application:
name: cloudalibaba-sentinel-service
cloud:
nacos:
discovery:
server-addr: 192.168.120.180:1111
sentinel:
transport:
dashboard: 192.168.120.180:9000
# 默认8719端口,假如端口被占用,依次+1,直到找到未被占用端口
port: 8719
datasource:
ds1:
nacos:
server-addr: 192.168.120.180:1111
dataId: nacos-consumer-order
groupId: DEFAULT_GROUP
data-type: json
rule-type: flow
management:
endpoints:
web:
exposure:
include: "*"
在Nacos配置中心,增加配置
[{
"resource": "/testQPSA",
"limitApp": "default",
"grade": 1,
"count": 1,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
}]
需要注意:dataId
、groupId
、data-type
属性,Nacos配置中心需要与配置文件的保持一致。
此时,我们再次1s内多次刷新http://localhost:8401/testQPSA
,看到会被限流
评论 (0)