微服務(wù)Nacos配置動態(tài)刷新方式(簡易版)附配置
實(shí)現(xiàn)方法
環(huán)境:Nacos、Java、SpringBoot等
主要是在boostrap.yaml中的data-id屬性下配置refresh:true來實(shí)現(xiàn)動態(tài)更新
配置依賴 + yaml
具體的版本參考官方的說明:官方版本說明
<!--讀取bootstrap文件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<!--統(tǒng)一配置管理-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--nacos 服務(wù)注冊發(fā)現(xiàn)-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>boostrap.yaml 配置
spring:
application:
name: xxx-service # 微服務(wù)名稱
profiles:
active: dev
cloud:
nacos:
server-addr: xxx.xxx.xxx.xxx:8848 # Nacos 地址
config:
namespace: xxxxxxxxxxxxxxxxxxx # 命名空間
file-extension: yaml
shared-configs:
- data-id: shared-common.yaml
refresh: true # 要開啟刷新配置,下面的同理
- data-id: shared-redis.yaml
refresh: true
- data-id: shared-jdbc.yaml
refresh: true
- data-id: shared-log.yaml
refresh: true
- data-id: shared-swagger.yaml
refresh: true
- data-id: shared-sentinel.yaml
refresh: true
discovery:
namespace: xxxxxxxxxxxxxxxx # 命名空間驗(yàn)證效果
配置監(jiān)聽,更新時(shí)打印配置到控制臺
package com.lingchuangdao.creator.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import java.util.Set;
/**
* 監(jiān)聽配置變更事件,并打印所有變更的屬性。
*/
@Component
@Slf4j
public class ConfigRefreshListener {
private final Environment environment;
public ConfigRefreshListener(Environment environment) {
this.environment = environment;
}
@EventListener
public void onEnvironmentChangeEvent(EnvironmentChangeEvent event) {
Set<String> keys = event.getKeys();
log.info("配置已更新,變更的屬性數(shù)量: {}", keys.size());
// 打印所有變更的配置項(xiàng)
for (String key : keys) {
log.info("配置變更 - 屬性: {}, 新值: {}", key, environment.getProperty(key));
}
}
@EventListener
public void onRefreshScopeRefreshed(RefreshScopeRefreshedEvent event) {
log.info("RefreshScope已刷新,Bean: {}", event.getName());
}
}控制臺輸出:

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Security+MyBatis實(shí)現(xiàn)從數(shù)據(jù)庫動態(tài)查詢權(quán)限的完整實(shí)現(xiàn)方案
文章詳細(xì)介紹了SpringSecurity從數(shù)據(jù)庫動態(tài)查詢權(quán)限的實(shí)現(xiàn)方案,包括核心接口、數(shù)據(jù)庫表設(shè)計(jì)、MyBatis查詢權(quán)限、UserDetailsService實(shí)現(xiàn)、SpringSecurity配置、Controller使用及常見問題優(yōu)化等內(nèi)容,該方案生產(chǎn)可用,靈活且遵循SpringSecurity接口設(shè)計(jì)標(biāo)準(zhǔn)2026-04-04
Maven中plugins和pluginManagement區(qū)別小結(jié)
pluginManagement是表示插件聲明,plugins就是直接引入一個(gè)plugin,本文主要介紹了Maven中plugins和pluginManagement區(qū)別小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06
Spring Boot集成Druid出現(xiàn)異常報(bào)錯(cuò)的原因及解決
Druid 可以很好的監(jiān)控 DB 池連接和 SQL 的執(zhí)行情況,天生就是針對監(jiān)控而生的 DB 連接池。本文講述了Spring Boot集成Druid項(xiàng)目中discard long time none received connection異常的解決方法,出現(xiàn)此問題的同學(xué)可以參考下2021-05-05
解決Kibana報(bào)錯(cuò)[security_exception] current license&nbs
ElasticSearch 5.1.1因安全許可證過期報(bào)錯(cuò),解決方法:官網(wǎng)申請新許可證,下載JSON文件并上傳至服務(wù)器,執(zhí)行代碼時(shí)切換root權(quán)限,配置host為服務(wù)器IP,端口9200,替換原license文件2025-07-07
Springboot整合Shiro使用yml文件管理Urls方式
這篇文章主要介紹了Springboot整合Shiro使用yml文件管理Urls方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2026-03-03
解決Intellij IDEA運(yùn)行報(bào)Command line is too long的問題
這篇文章主要介紹了解決Intellij IDEA運(yùn)行報(bào)Command line is too long的問題,本文通過兩種方案給大家詳細(xì)介紹,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
Java多線程Callable接口實(shí)現(xiàn)代碼示例
相信大家對Java編程中如何創(chuàng)建線程已經(jīng)不陌生了,這篇文章就向朋友們介紹實(shí)現(xiàn)callable接口,具體實(shí)例詳見正文。2017-10-10
用Maven插件生成Mybatis代碼的實(shí)現(xiàn)方法
本文主要介紹 Maven插件生成Mybatis代碼,現(xiàn)在做開發(fā)的朋友有好多用Maven 來管理代碼,這里給大家舉個(gè)例子,有需要的同學(xué)可以看下2016-07-07

