Sentinel 整合Nacos实现动态规则配置持久化

前言

提示:这里可以添加本文要记录的大概内容:

默认情况下Sentinel配置的规则是储存的内存中,在重新Sentinel服务后,配置会显示,我们通过整合第三方中间件实现,配置的持久化,比如使用NacosMysql


我们以流控规则为例,演示一个数据同步持久化的操作;

一、导入依赖

		<!-- sentinel数据源持久化进nacos -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

二、改写pom

spring:
  application:
    name: cloud-alibaba-sentinel
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.137.134
    sentinel:
      transport:
        dashboard: 192.168.137.134:8080 # 该微服务的仪表板地址,即【sentinel前台展示服务】
        port: 8179 # sentinel后台监控服务,它监控8858【微服务】的同时,还与8080【sentinel前台展示服务】交互数据,将监控到的数据在dashboard上展现。
      datasource: # 添加数据源配置
        ds1:
          nacos:
            server-addr: 192.168.137.134
            dataId: ${
    
    spring.application.name}
            groupId: DEFAULT_GROUP
            data-type: json
            rule-type: flow

三、nacos中添加配置

在这里插入图片描述

[
    {
    
    
        "resource": "/byResource",
        "limitApp": "default",
        "grade": 1,
        "count": 1,
        "strategy": 0,
        "controlBehavior": 0,
        "clusterMode":  false
    }
]
ps:配置中不能加注释,不然不起效。

四、启动服务后访问该资源

在这里插入图片描述

在这里插入图片描述

启动服务并访问后可以查看到该资源已经被加上了流控规则。

猜你喜欢

转载自blog.csdn.net/RuserTion/article/details/128261106