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

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

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

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

整个文件内容为:

bash 复制代码
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配置:


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

4、启动feingnconsumer服务:

控制台输出获取nacos的配置


5、sentinel控制台:

获取到nacos的配置信息

7、问题

sentinel若获取不到nacos配置,检查配置文件是否存在问题,若无其他问题大概率原因就是版本不匹配。

相关推荐
程序员阿鹏29 分钟前
实现SpringBoot底层机制【Tomcat启动分析+Spring容器初始化+Tomcat 如何关联 Spring容器】
java·spring boot·后端·spring·docker·tomcat·intellij-idea
小可爱的大笨蛋3 小时前
Spring AI - Redis缓存对话
java·spring·spring ai
等什么君!3 小时前
springmvc入门案例
后端·spring
hello_ejb33 小时前
聊聊Spring AI Alibaba的ObsidianDocumentReader
java·人工智能·spring
bing_1584 小时前
一个 HTTP 请求进入 Spring MVC 应用后,大致经历了哪些主要步骤?
spring·http·mvc
暮乘白帝过重山4 小时前
为什么Spring中@Bean注解默认创建单例Bean
java·spring
24k小善5 小时前
ApacheJmeter使用权威指南
java·大数据·spring·云计算
东阳马生架构5 小时前
Sentinel源码—9.限流算法的实现对比二
算法·sentinel
东阳马生架构5 小时前
Sentinel源码—9.限流算法的实现对比一
算法·sentinel
敖云岚6 小时前
【LangChain4j】AI 第一弹:LangChain4j 的理解
java·人工智能·spring boot·后端·spring