Spring Cloud Sentinel整合Nacos实现配置持久化

本文介绍了如何在SpringBoot项目中使用Sentinel整合Nacos,实现配置的持久化和实时通信。通过POM.xml引入sentinel-datasource-nacos依赖,配置Nacos数据源并设置Sentinel规则,但在服务重启后配置丢失。解决方法可能是检查版本兼容性或配置文件问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sentinel配置相关配置后无法持久化,服务重启之后就没了,所以整合nacos,在nacos服务持久化,sentinel实时与nacos通信获取相关配置。

使用上一章节Feign消费者服务实现整合。

版本信息:

nacos:1.4.1
Sentinel 控制台 1.7.2
spring-boot:2.3.3.RELEASE
spring.cloud.version:Hoxton.SR8
spring.cloud.alibaba.version:2.2.3.RELEASE

1、项目POM.xml 引入

 <!--sentinel配置数据源nacos-->
       <dependency>
           <groupId>com.alibaba.csp</groupId>
           <artifactId>sentinel-datasource-nacos</artifactId>
       </dependency>

2、bootstrap.yml追加Nacos数据源配置:

在这里插入图片描述

整个文件内容为:

server:
  port: 8083
spring:
  profiles:
    active: dev
  application:
    name: tt-sc-feign-consumer
  cloud:
    nacos:
      username: nacos
      password: nacos
      config:
        server-addr: 192.168.10.x:8848
        file-extension: yml
      discovery:
        server-addr: 192.168.10.x:8848
    #sentinel看板相关配置
    sentinel:
      transport:
        dashboard: 192.168.10.x:8080
        #SpringBoot项目与控制台做交互的端口,默认8719
        port: 8723
      eager: true
      #添加Nacos数据源配置
      datasource:
        #名字自定义即可
        ds1:
          nacos:
            server-addr: 192.168.10.x:8848
            username: nacos
            password: nacos
            #请求频率较高
            namespace: public
            dataId: tt-sc-feign-consume-sentinel
            groupId: DEFAULT_GROUP
            data-type: json
            rule-type: flow
#        ds2:
#          nacos:
#            server-addr: 192.168.10.107:8848
#            username: nacos
#            password: nacos
#            namespace: public
#            dataId: tt-public
#            groupId: DEFAULT_GROUP
#            data-type: json
#            rule-type: flow


3、Nacos配置:

在这里插入图片描述
在这里插入图片描述

[
	{
		"resource": "/user/getUserSentinel",
		"limitApp": "default",
		"grade": 1,
		"count": 2,
		"strategy": 0,
		"controlBehavior": 0,
		"clusterMode": false
	}
]

4、启动feingnconsumer服务:

控制台输出获取nacos的配置

在这里插入图片描述
5、sentinel控制台:

获取到nacos的配置信息

在这里插入图片描述

7、问题
sentinel若获取不到nacos配置,检查配置文件是否存在问题,若无其他问题大概率原因就是版本不匹配。
在这里插入图片描述
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值