SpringCloud Alibaba之Nacos动态刷新配置

前言

在开发的时候,我们经常会修改nacos的配置,如果每一次都重启服务的话就很麻烦了,这时候我们就可以使用Naocs的动态刷新配置来实现无需重启更新配置。

Nacos实现动态刷新配置的方法有两种,下面进行介绍

1、使用@RefreshScope

在我们注入配置的类上添加@RefreshScope注解

java 复制代码
@Slf4j
@RestController
@RefreshScope
public class MybatisTest {

    @Value("test_key")
    private String testValue;

    @GetMapping("getValue")
    public String testNacos(){
        return testValue;
    }
}

添加了@RefreshScope注解之后,如果我们类中配置变量有修改的话,就会进行刷新

2、使用@ConfigurationProperties注解

使用@ConfigurationProperties注解可以将 Nacos 配置直接注入到 Java 对象的属性中,并实现配置的自动刷新

java 复制代码
@Data
@Component
@ConfigurationProperties(prefix = "my")
public class MyConfig {

    private String key;

}

使用

java 复制代码
 	@Autowired
    private MyConfig myConfig;

	@GetMapping("getValue")
    public String testNacos(){
        return myConfig.getKey();
    }
相关推荐
苍何4 分钟前
一手实测 Claude Fable 5,手搓了个 Obsidian 的 Codex 插件
后端
zfoo-framework8 分钟前
[修改代码使用]codex官方app中使用中转(不需要cc-switch) 1.config.toml 2.sk方式登录
java
逍遥德27 分钟前
MQTT教程详解-05.SpringBoot集成mqtt client 性能分析
java·spring boot·spring·mt
云烟成雨TD32 分钟前
Spring AI 1.x 系列【54】Retry 机制分析
java·人工智能·spring
weixin_5231853235 分钟前
Collections.unmodifiableMap详解:真的不可修改吗?
java·linux·前端
点燃大海36 分钟前
SpringAI构建智能体
java·spring boot·spring·springai智能体
xier_ran38 分钟前
【infra之路】02_RadixAttention与KV_Cache管理
java·spring boot·spring
swipe1 小时前
做多轮对话 Agent,为什么我建议把短期记忆放到 Redis
后端·面试·llm
黑马师兄1 小时前
RAG混合检索深度解析:让AI真正找到你要的内容
java·人工智能·ai·agent·rag·ai-native
码客日记1 小时前
Spring Boot 配置文件敏感信息加密(Jasypt 企业级完整方案)
java·spring boot·git