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

发布于:2025-08-15 ⋅ 阅读:(13) ⋅ 点赞:(0)

前言:简单记录下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-service
  cloud:
    nacos:
      discovery:
        server-addr: 19xxxxxxxxx9:8848
        namespace: test_ruoyi
        group: ljh_ruoyi-group
      config:
        server-addr: 19xxxxxxxxx9:8848
        namespace: test_ruoyi
        group: ljh_ruoyi-group
        file-extension: yml
    compatibility-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 的兼容性验证开关,在配置文件把它关闭就行了。


网站公告

今日签到

点亮在社区的每一天
去签到