使用nacos實(shí)現(xiàn)自定義文本配置的實(shí)時(shí)刷新
一、背景
我們都知道,使用Nacos時(shí),如果將Bean使用@RefreshScope標(biāo)注之后,這個(gè)Bean中的配置就會(huì)做到實(shí)時(shí)刷新。
但是,我們有時(shí)候使用nacos配置的并不是key-value形式的配置文件,而是txt形式的或者其他文本格式,就無法使用@RefreshScope進(jìn)行動(dòng)態(tài)實(shí)時(shí)刷新了。
所以,我這里自行擴(kuò)展了一個(gè)框架,可以稍微簡(jiǎn)化一些開發(fā)人員的工作。
二、編碼
1、spring.factories
在resources創(chuàng)建META-INF目錄,創(chuàng)建spring.factories文件
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.test.NacosLoaderConfiguration
2、NacosPropertiesLoader
/** * nacos配置加載器 */ public interface NacosPropertiesLoader { /** * 獲取dataId */ String getDataId(); /** * 配置刷新的回調(diào) */ void getConfigData(String configData); }
3、NacosConfigHandler
import com.alibaba.cloud.nacos.NacosConfigManager; import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.listener.AbstractListener; import com.alibaba.nacos.api.exception.NacosException; import org.springframework.beans.BeansException; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationListener; import java.util.List; import java.util.Map; import java.util.concurrent.CopyOnWriteArrayList; public class NacosConfigHandler implements ApplicationListener<ApplicationReadyEvent>, ApplicationContextAware { private final NacosConfigManager nacosConfigManager; List<NacosPropertiesLoader> nacosPropertiesLoaderList = new CopyOnWriteArrayList<>(); private String groupId; public NacosConfigHandler(NacosConfigManager nacosConfigManager) { this.nacosConfigManager = nacosConfigManager; } @Override public void onApplicationEvent(ApplicationReadyEvent event) { // 容器環(huán)境準(zhǔn)備完畢了,加載配置 ConfigService configService = nacosConfigManager.getConfigService(); try { // 加載所有的配置,并設(shè)置監(jiān)聽器 for (NacosPropertiesLoader nacosPropertiesLoader : nacosPropertiesLoaderList) { nacosPropertiesLoader.getConfigData( configService.getConfig(nacosPropertiesLoader.getDataId(), groupId, 3000) ); configService.addListener(nacosPropertiesLoader.getDataId(), groupId, new AbstractListener() { @Override public void receiveConfigInfo(String configInfo) { nacosPropertiesLoader.getConfigData(configInfo); } }); } } catch (NacosException e) { e.printStackTrace(); } } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { Map<String, NacosPropertiesLoader> nacosPropertiesLoaderBeans = applicationContext.getBeansOfType(NacosPropertiesLoader.class); if (nacosPropertiesLoaderBeans == null) { return; } for (NacosPropertiesLoader value : nacosPropertiesLoaderBeans.values()) { nacosPropertiesLoaderList.add(value); } // 從配置中讀取nacos.group nacos的groupId groupId = applicationContext.getEnvironment().getProperty("nacos.group"); } }
4、NacosLoaderConfiguration
import com.alibaba.cloud.nacos.NacosConfigManager; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.context.annotation.Bean; @ConditionalOnBean(NacosConfigManager.class) public class NacosLoaderConfiguration { @Bean public NacosConfigHandler nacosConfigHandler(NacosConfigManager nacosConfigManager) { return new NacosConfigHandler(nacosConfigManager); } }
5、測(cè)試類
import org.springframework.stereotype.Component; @Component public class TestConfigLoader1 implements NacosPropertiesLoader { @Override public String getDataId() { return "test"; } @Override public void getConfigData(String configData) { System.out.println("獲取了配置1:" + configData); } } import org.springframework.stereotype.Component; @Component public class TestConfigLoader2 implements NacosPropertiesLoader { @Override public String getDataId() { return "test1"; } @Override public void getConfigData(String configData) { System.out.println("獲取了配置2:" + configData); } }
我們?cè)趎acos創(chuàng)建test、test1命名的dataId,修改配置就會(huì)調(diào)用到getConfigData,實(shí)時(shí)獲取配置了。
6、擴(kuò)展
因?yàn)槲覀兊谝淮螐膎acos中獲取配置是基于ApplicationReadyEvent 事件,所以數(shù)據(jù)處理時(shí)間會(huì)晚于這個(gè)時(shí)間。
如果說要加載本地文件,本地文件加載時(shí)間要早于ApplicationReadyEvent 觸發(fā)的時(shí)間,可以考慮使用Bean初始化的生命周期。
以上就是使用nacos實(shí)現(xiàn)自定義文本配置的實(shí)時(shí)刷新的詳細(xì)內(nèi)容,更多關(guān)于nacos文本配置實(shí)時(shí)刷新的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
jtds1.1連接sqlserver2000測(cè)試示例
這篇文章主要介紹了jtds1.1連接sqlserver2000測(cè)試示例,需要的朋友可以參考下2014-02-02Java數(shù)據(jù)結(jié)構(gòu)之鏈表、棧、隊(duì)列、樹的實(shí)現(xiàn)方法示例
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)之鏈表、棧、隊(duì)列、樹的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Java數(shù)據(jù)結(jié)構(gòu)中鏈表、棧、隊(duì)列、樹的功能、定義及使用方法,需要的朋友可以參考下2019-03-03Java實(shí)現(xiàn)帶有權(quán)重隨機(jī)算法的示例詳解
這篇文章主要為大家詳細(xì)介紹了Java如何實(shí)現(xiàn)帶有權(quán)重隨機(jī)算法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10Java將不同的List集合復(fù)制到另一個(gè)集合常見的方法
在Java中,有時(shí)候我們需要將一個(gè)List對(duì)象的屬性值復(fù)制到另一個(gè)List對(duì)象中,使得兩個(gè)對(duì)象的屬性值相同,這篇文章主要介紹了Java將不同的List集合復(fù)制到另一個(gè)集合常見的方法,需要的朋友可以參考下2024-09-09