当前位置: 首页 > news >正文

Nacos 配置热更新:Spring Boot Bean 自动获取最新配置

Nacos 配置热更新:Spring Boot Bean 自动获取最新配置

  • nacos 配置相关
    • 命名空间
    • 配置列表
  • 代码
    • demo
    • 配置文件 bootstrap.yml
  • 测试
  • 注释事项
    • 版本兼容问题
    • 问题解决方法:
      • 依赖的版本
      • 关闭兼容错误提示

前言:简单记录下nacos的动态刷新获取以及版本兼容问题

nacos 配置相关

命名空间

在这里插入图片描述

配置列表

一些简单的配置

在这里插入图片描述

在这里插入图片描述

代码

demo

在这里插入图片描述

package com.ruoyi.web.mytest.nacos;import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** 实现功能测试:nacos的配置文件修改了数据,这个bean就会动态的刷新获取。** 涉及到的代码:添加了一个 bootstrap.yml 的配置类,然后nacos再添加一个配置来测试--命名空间和配置管理那里** @author lujinhong* @since 2025-08-14*/
@RefreshScope //nacos 配置刷新
@RestController
@RequestMapping("/nacos")
public class NacosTest {/*** 问题:为什么 ${user.name} 能拿到nacos服务端的配置数据。** Spring Cloud Alibaba Nacos Config 会在Spring Boot 启动的最早阶段,在加载到bootstrap.yml时,* 去 Nacos 服务器加载对应 Data ID 的配置文件,然后把这些配置项放进 Spring Environment 里,* 而且优先级比本地 application.yml 高。** @Value("${user.name}") 能拿到 Nacos 的值,* 是因为 Nacos 配置会在启动时注入到 Spring Environment 里,* 而 @Value 从 Environment 取值,不管值来自本地文件、远程配置还是系统变量,只要名字匹配就能取到*/@Value("${user.name}")private String userName;@GetMapping("/user")public String getUserName(){System.err.println("获取nacos中的user.name的值:" + userName);return "获取nacos中的user.name的值:" + userName;}}

配置文件 bootstrap.yml

在这里插入图片描述


# bootstrap.yml 加载时机比 application.yml 快
spring:application:name: demo-servicecloud:nacos:discovery:server-addr: 19xxxxxxxxx9:8848namespace: test_ruoyigroup: ljh_ruoyi-groupconfig:server-addr: 19xxxxxxxxx9:8848namespace: test_ruoyigroup: ljh_ruoyi-groupfile-extension: ymlcompatibility-verifier:enabled: false

测试

在这里插入图片描述

注释事项

版本兼容问题

我这个是使用若依框架来测试的,框架版本和cloud版本不一样,因为使用 @RefreshScope 这个动态获取数据,需要用到【org.springframework.cloud.context.config.annotation.RefreshScope;】这个依赖。

【@RefreshScope 是 Spring Cloud 提供的注解,用来让 Spring 管理的 Bean 支持动态刷新配置】

Description: Your project setup is incompatible with our requirements due to following reasons:- Spring Boot [2.5.15] is not compatible with this Spring Cloud release train Action: - Consider applying the following actions:

在这里插入图片描述

问题解决方法:

主要记录的就是这个版本兼容问题,用的是这些依赖

依赖的版本

        <!-- nacos 配置中心--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId><version>2021.1</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId><version>3.0.1</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><version>2021.1</version></dependency>

关闭兼容错误提示

如果在启动的时候一直说版本不一样,如:

Spring Cloud 在启动时,会默认 检查 Spring Boot 和 Spring Cloud 版本是否兼容,如果不兼容就会抛出错误
(比如之前的 “Spring Boot [2.5.15] is not compatible with this Spring Cloud release train”)

spring.cloud.compatibility-verifier.enabled: false 是 Spring Cloud 的兼容性验证开关,在配置文件把它关闭就行了。

http://www.lryc.cn/news/620728.html

相关文章:

  • flutter3.7.12版本设置TextField的contextMenuBuilder的文字颜色
  • MixOne在macOS上安装碰到的问题
  • 解决SQL Server连接失败:Connection refused: connect
  • 苹果正计划大举进军人工智能硬件领域
  • 从0开始跟小甲鱼C语言视频使用linux一步步学习C语言(持续更新)8.14
  • 2025 电赛 C 题 发挥3 带数字编号的正方形识别 边长测量
  • [特殊字符]走进华为,解锁商业传奇密码
  • 通过网页调用身份证阅读器http websocket方法-湖南步联科技美萍MP999A电子————仙盟创梦IDE
  • 从根源到生态:Apache Doris 与 StarRocks 的深度对比 —— 论开源基因与长期价值的优越性
  • 审批流程系统设计与实现:状态驱动、灵活扩展的企业级解决方案
  • 实战指南|消防管理系统搭建全流程解析
  • OpenCV ------图像基础处理(一)
  • 【P81 10-7】OpenCV Python【实战项目】——车辆识别、车流统计(图像/视频加载、图像运算与处理、形态学、轮廓查找、车辆统计及显示)
  • 【OpenCV】Mat详解
  • 入门基础人工智能理论
  • 计算机视觉(opencv)实战二——图像边界扩展cv2.copyMakeBorder()
  • 论,物联网日志系统架构如何设计?
  • AI增强SEO关键词表现
  • Postman 平替 技术解析:架构优势与实战指南
  • 考研408《计算机组成原理》复习笔记,第五章(2)——CPU指令执行过程
  • 使用 Docker 部署 PostgreSQL
  • 考研408《计算机组成原理》复习笔记,第四章(3)——指令集、汇编语言
  • Java设计模式之《策略模式》
  • Effective C++ 条款41:理解隐式接口和编译期多态
  • 应用系统连达梦数据库报“服务器模式不匹配”的根源与修复方案
  • 使用colmap自制3DGaussian_Splatting数据集
  • BotCash:GPT-5发布观察 工程优化的进步,还是技术突破的瓶颈?
  • GoLand 项目从 0 到 1:第八天 ——GORM 命名策略陷阱与 Go 项目启动慢问题攻坚
  • 通过机器学习框架实现Android手写识别输入功能
  • Spring Boot 3中JWT密钥安全存储方案