Spring Cloud集成Sentinel之持久化规则

Laughing
2021-07-28 / 0 评论 / 995 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2024年03月17日,已超过343天没有更新,若内容或图片失效,请留言反馈。

在使用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
}]

需要注意:dataIdgroupIddata-type属性,Nacos配置中心需要与配置文件的保持一致。
此时,我们再次1s内多次刷新http://localhost:8401/testQPSA,看到会被限流

1

评论 (0)

取消