Sentinel通过Nacos持久化规则

1、在nacos上新增json规则配置; 

data-id = system-service-sentinel 

配置格式 = JSON

[
    {
        "resource": "/index",
        "limitApp": "default",
        "grade": "1",
        "count": "5",
        "strategy": "0",
        "controlBehavior": "0",
        "clusterMode": false
    }
]

1.1、配置说明

resource:资源名,即限流规则的作用对象
count:限流阈值
grade:限流阈值类型(QPS 或并发线程数)
limitApp:流控针对的调用来源,若为 default 则不区分调用来源
strategy:调用关系限流策略
controlBehavior:流量控制效果(直接拒绝、Warm Up、匀速排队)
clusterMode:是否集群

2、在项目里引入相关依赖

<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>

3、在yml里配置sentinel

spring:
    cloud:
        # sentinel 配置
        sentinel:
          # 是否立即加载,true 是
          eager: true
          # 配置 dashboard 页面地址 和 交互端口
          transport:
            dashboard: http://127.0.0.1:7777
            port: 8719
          # 配置规则储存地方;默认存内存,重启就会丢失
          datasource:
            ds1:
              # 将规则存储到 nacos
              nacos:
                serverAddr: localhost:8848
                dataId: system-service-sentinel
                namespace: 1edf4e3c-56e7-4f4b-230-d333b1907f536
                groupId: DEFAULT_GROUP
                dataType: json
                ruleType: flow


同类文章