Nacos-SpringBoot 配置無法自動刷新問題排查

背景

Nacos SpringBoot版本中,提供了@NacosValue注解,支持控制臺修改值時,自動刷新,但是今天遇見了無法自動刷新的問題。

環境

SpringBoot 2.2.x
nacos-client:2.1.0
nacos-config-spring-boot-starter:0.2.12

問題排查

首先確認,nacos的配置信息:

nacos:config:bootstrap:enable: truelog-enable: falseserver-addr: xxxtype: yamlauto-refresh: truedata-ids: my-config.yml

確認auto-refresh配置為true,Nacos提供了注解@NacosConfigListener可以監聽配置修改的信息,排查nacos與client的長連接通道是否正常。

@NacosConfigListener(dataId = "${nacos.config.data-ids}", timeout = 5000)
public void onConfigChange(String newConfig) {log.info("Nacos配置更新完成, config data={}", newConfig);
}

在控制臺修改配置,發現onConfigChange()可以正常觸發,說明長連通道沒有問題,看來只能追查源碼。

com.alibaba.nacos.client.config.impl.CacheData#safeNotifyListener

private void safeNotifyListener(final String dataId, final String group, final String content, final String type,final String md5, final String encryptedDataKey, final ManagerListenerWrap listenerWrap) {final Listener listener = listenerWrap.listener;if (listenerWrap.inNotifying) {LOGGER.warn("[{}] [notify-currentSkip] dataId={}, group={}, md5={}, listener={}, listener is not finish yet,will try next time.",name, dataId, group, md5, listener);return;}Runnable job = () -> {long start = System.currentTimeMillis();ClassLoader myClassLoader = Thread.currentThread().getContextClassLoader();ClassLoader appClassLoader = listener.getClass().getClassLoader();try {if (listener instanceof AbstractSharedListener) {AbstractSharedListener adapter = (AbstractSharedListener) listener;adapter.fillContext(dataId, group);LOGGER.info("[{}] [notify-context] dataId={}, group={}, md5={}", name, dataId, group, md5);}// Before executing the callback, set the thread classloader to the classloader of// the specific webapp to avoid exceptions or misuses when calling the spi interface in// the callback method (this problem occurs only in multi-application deployment).Thread.currentThread().setContextClassLoader(appClassLoader);ConfigResponse cr = new ConfigResponse();cr.setDataId(dataId);cr.setGroup(group);cr.setContent(content);cr.setEncryptedDataKey(encryptedDataKey);configFilterChainManager.doFilter(null, cr);String contentTmp = cr.getContent();listenerWrap.inNotifying = true;// 重點實現方法,將Nacos Server的配置寫入Spring容器中listener.receiveConfigInfo(contentTmp);// compare lastContent and contentif (listener instanceof AbstractConfigChangeListener) {Map data = ConfigChangeHandler.getInstance().parseChangeData(listenerWrap.lastContent, content, type);ConfigChangeEvent event = new ConfigChangeEvent(data);((AbstractConfigChangeListener) listener).receiveConfigChange(event);listenerWrap.lastContent = content;}.....省略部分代碼}

進入receiveConfigInfo()實現:
com.alibaba.nacos.spring.context.event.config.DelegatingEventPublishingListener#receiveConfigInfo

@Override
public void receiveConfigInfo(String content) {// 刷新Spring容器配置onReceived(content);// 發布變更事件publishEvent(content);
}

com.alibaba.nacos.spring.core.env.NacosPropertySourcePostProcessor#addListenerIfAutoRefreshed

public static void addListenerIfAutoRefreshed(final NacosPropertySource nacosPropertySource, final Properties properties,final ConfigurableEnvironment environment) {if (!nacosPropertySource.isAutoRefreshed()) { // Disable Auto-Refreshedreturn;}final String dataId = nacosPropertySource.getDataId();final String groupId = nacosPropertySource.getGroupId();final String type = nacosPropertySource.getType();final NacosServiceFactory nacosServiceFactory = getNacosServiceFactoryBean(beanFactory);try {ConfigService configService = nacosServiceFactory.createConfigService(properties);Listener listener = new AbstractListener() {@Overridepublic void receiveConfigInfo(String config) {String name = nacosPropertySource.getName();NacosPropertySource newNacosPropertySource = new NacosPropertySource(dataId, groupId, name, config, type);newNacosPropertySource.copy(nacosPropertySource);MutablePropertySources propertySources = environment.getPropertySources();// replace NacosPropertySource// 核心實現,將Nacos的配置值刷新Spring容器中的配置值propertySources.replace(name, newNacosPropertySource);}};....省略部分代碼}

點擊replace的實現,發現一點問題:
疑點1
這里面有兩個實現,其中一個是jasypt的實現,這個三方類庫是常用于對代碼中的數據庫配置信息進行加密的,難道說,是因為它?同時在控制臺也有一條日志值得注意:

[notify-error] dataId=xxx  …… placeholder 'project.version' in value "${project.version}

這里是一個疑點,我們先繼續往下看:
com.alibaba.nacos.spring.context.event.config.DelegatingEventPublishingListener#receiveConfigInfo

@Override
public void receiveConfigInfo(String content) {onReceived(content);publishEvent(content);
}private void publishEvent(String content) {NacosConfigReceivedEvent event = new NacosConfigReceivedEvent(configService,dataId, groupId, content, configType);// 發布變更事件applicationEventPublisher.publishEvent(event);
}

org.springframework.context.support.AbstractApplicationContext#publishEvent

/*** Publish the given event to all listeners.* @param event the event to publish (may be an {@link ApplicationEvent}* or a payload object to be turned into a {@link PayloadApplicationEvent})* @param eventType the resolved event type, if known* @since 4.2*/protected void publishEvent(Object event, @Nullable ResolvableType eventType) {Assert.notNull(event, "Event must not be null");// Decorate event as an ApplicationEvent if necessaryApplicationEvent applicationEvent;if (event instanceof ApplicationEvent) {applicationEvent = (ApplicationEvent) event;}else {applicationEvent = new PayloadApplicationEvent<>(this, event);if (eventType == null) {eventType = ((PayloadApplicationEvent<?>) applicationEvent).getResolvableType();}}// Multicast right now if possible - or lazily once the multicaster is initializedif (this.earlyApplicationEvents != null) {this.earlyApplicationEvents.add(applicationEvent);}else {// 重點關注,發布廣播事件,通知全部監聽器getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);}// Publish event via parent context as well...if (this.parent != null) {if (this.parent instanceof AbstractApplicationContext) {((AbstractApplicationContext) this.parent).publishEvent(event, eventType);}else {this.parent.publishEvent(event);}}}

下面就是核心的實現部分,真正刷新值的實現邏輯:
com.alibaba.nacos.spring.context.annotation.config.NacosValueAnnotationBeanPostProcessor#onApplicationEvent

@Override
public void onApplicationEvent(NacosConfigReceivedEvent event) {// In to this event receiver, the environment has been updated the// latest configuration information, pull directly from the environment// fix issue #142for (Map.Entry<String, List<NacosValueTarget>> entry : placeholderNacosValueTargetMap.entrySet()) {String key = environment.resolvePlaceholders(entry.getKey());// 從Spring容器中獲取變更后的新值,通過反射的方式,更新數據String newValue = environment.getProperty(key);if (newValue == null) {continue;}List<NacosValueTarget> beanPropertyList = entry.getValue();for (NacosValueTarget target : beanPropertyList) {String md5String = MD5Utils.md5Hex(newValue, "UTF-8");boolean isUpdate = !target.lastMD5.equals(md5String);if (isUpdate) {target.updateLastMD5(md5String);Object evaluatedValue = resolveNotifyValue(target.nacosValueExpr, key, newValue);if (target.method == null) {setField(target, evaluatedValue);}else {setMethod(target, evaluatedValue);}}}}
}

OK,看到這里,基本已經明朗,了解了Nacos配置刷新的全流程,非常可疑的一點,就是三方類庫jasypt,為了驗證才想,我們將jasypt的類庫移除,再次進行嘗試,奇跡出現了!Nacos可以順利刷新配置值,終于破案,是因為jasypt的加密導致的該問題,搜了一下可能導致的原因:
1、屬性源優先級沖突:jasypt 的 PropertySource(如 EncryptablePropertySource)可能覆蓋或干擾 Nacos 的動態屬性源,導致解密后的值無法被 Nacos 更新邏輯捕獲。
2、解密邏輯未觸發:Nacos 配置更新時,新的加密值未被 jasypt 及時解密,導致 Environment 中仍是舊值。

查看一下jasypt使用的版本:

<dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId><version>2.1.1</version>
</dependency>

猜想是否是2.1.1版本的BUG導致了該問題,于是升級至最新版本3.0.5,再次進行測試,發現Nacos可以順利更新,問題解決。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/80725.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/80725.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/80725.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

JAVA | 聚焦 OutOfMemoryError 異常

個人主頁 文章專欄 在正文開始前&#xff0c;我想多說幾句&#xff0c;也就是吐苦水吧…最近這段時間一直想寫點東西&#xff0c;停下來反思思考一下。 心中萬言&#xff0c;真正執筆時又不知先寫些什么。通常這個時候&#xff0c;我都會隨便寫寫&#xff0c;文風極像散文&…

基于開源技術體系的品牌賽道力重構:AI智能名片與S2B2C商城小程序源碼驅動的品類創新機制研究

摘要&#xff1a;在數字經濟與實體經濟深度融合的背景下&#xff0c;品牌競爭已從單一產品力競爭轉向生態化、技術化的賽道力競爭。本文以開源AI大模型、AI智能名片及S2B2C商城小程序源碼為核心技術載體&#xff0c;構建"技術賦能-場景貫通-生態協同"三維分析框架&am…

【vue3】購物車實戰:從狀態管理到用戶體驗的全流程實現

在電商項目中&#xff0c;購物車是核心功能之一&#xff0c;需要兼顧數據一致性、用戶體驗和邏輯復雜度。 本文結合 Vue3 Pinia 技術棧&#xff0c;詳細講解如何實現一個高效且易用的購物車系統&#xff0c;重點剖析 添加購物車 和 頭部購物車預覽 的核心邏輯與實現細節。 一…

卡洛詩西餐廳,以“中式西餐”為核心戰略

在餐飲市場的激烈競爭中&#xff0c;“本土化”是許多國際餐飲品牌難以跨越的鴻溝——要么因水土不服黯然退場&#xff0c;要么因過度妥協失去特色。然而&#xff0c;卡洛詩以“中式西餐”為核心戰略&#xff0c;將西餐與國內飲食文化深度融合&#xff0c;不僅破解了西餐本土化…

28-29【動手學深度學習】批量歸一化 + ResNet

1. 批量歸一化 1.1 原理 當神經網絡比較深的時候會發現&#xff1a;數據在下面&#xff0c;損失函數在上面&#xff0c;這樣會出現什么問題&#xff1f; 正向傳遞的時候&#xff0c;數據是從下往上一步一步往上傳遞反向傳遞的時候&#xff0c;數據是從上面往下傳遞&#xff0…

【Linux網絡】Http服務優化 - 增加請求后綴、狀態碼描述、重定向、自動跳轉及注冊多功能服務

&#x1f4e2;博客主頁&#xff1a;https://blog.csdn.net/2301_779549673 &#x1f4e2;博客倉庫&#xff1a;https://gitee.com/JohnKingW/linux_test/tree/master/lesson &#x1f4e2;歡迎點贊 &#x1f44d; 收藏 ?留言 &#x1f4dd; 如有錯誤敬請指正&#xff01; &…

AIGC(生成式AI)試用 32 -- AI做軟件程序測試 3

總結之前的AI做程序測試過程&#xff0c;試圖優化提問方式&#xff0c;整合完成的AI程序測試提問&#xff0c;探索更多可能的AI測試 AIGC&#xff08;生成式AI&#xff09;試用 30 -- AI做軟件程序測試 1 AIGC&#xff08;生成式AI&#xff09;試用 31 -- AI做軟件程序…

C語言實現迪杰斯特拉算法進行路徑規劃

使用C語言實現迪杰斯特拉算法進行路徑規劃 迪杰斯特拉算法是一種用于尋找加權圖中最短路徑的經典算法。它特別適合用于計算從一個起點到其他所有節點的最短路徑&#xff0c;前提是圖中的邊權重為非負數。 一、迪杰斯特拉算法的基本原理 迪杰斯特拉算法的核心思想是“貪心法”…

引領印尼 Web3 變革:Mandala Chain 如何助力 1 億用戶邁向數字未來?

當前 Web3 的發展正處于關鍵轉折點&#xff0c;行業亟需吸引新用戶以推動 Web3 的真正大規模采用。然而&#xff0c;大規模采用面臨著核心挑戰&#xff1a;數據泄露風險、集中存儲的安全漏洞、跨系統互操作性障礙&#xff0c;以及低效的服務訪問等問題。如何才能真正突破這些瓶…

WebSocket是h5定義的,雙向通信,節省資源,更好的及時通信

瀏覽器和服務器之間的通信更便利&#xff0c;比http的輪詢等效率提高很多&#xff0c; WebSocket并不是權限的協議&#xff0c;而是利用http協議來建立連接 websocket必須由瀏覽器發起請求&#xff0c;協議是一個標準的http請求&#xff0c;格式如下 GET ws://example.com:3…

Kaamel白皮書:IoT設備安全隱私評估實踐

1. IoT安全與隱私領域的現狀與挑戰 隨著物聯網技術的快速發展&#xff0c;IoT設備在全球范圍內呈現爆發式增長。然而&#xff0c;IoT設備帶來便捷的同時&#xff0c;也引發了嚴峻的安全與隱私問題。根據NSF&#xff08;美國國家科學基金會&#xff09;的研究表明&#xff0c;I…

php安裝swoole擴展

PHP安裝swoole擴展 Swoole官網 安裝準備 安裝前必須保證系統已經安裝了下列軟件 4.8 版本需要 PHP-7.2 或更高版本5.0 版本需要 PHP-8.0 或更高版本6.0 版本需要 PHP-8.1 或更高版本gcc-4.8 或更高版本makeautoconf 安裝Swool擴展 安裝官方文檔安裝后需要再php.ini中增加…

服務器傳輸數據存儲數據建議 傳輸慢的原因

一、JSON存儲的局限性 1. 性能瓶頸 全量讀寫&#xff1a;JSON文件通常需要整體加載到內存中才能操作&#xff0c;當數據量大時&#xff08;如幾百MB&#xff09;&#xff0c;I/O延遲和內存占用會顯著增加。 無索引機制&#xff1a;查找數據需要遍歷所有條目&#xff08;時間復…

Android四大核心組件

目錄 一、為什么需要四大組件&#xff1f; 二、Activity&#xff1a;看得見的界面 核心功能 生命周期圖解 代碼示例 三、Service&#xff1a;看不見的勞動者 兩大類型 生命周期對比 注意陷阱 四、BroadcastReceiver&#xff1a;消息傳遞專員 兩種注冊方式 廣播類型 …

「Mac暢玩AIGC與多模態01」架構篇01 - 展示層到硬件層的架構總覽

一、概述 AIGC&#xff08;AI Generated Content&#xff09;系統由多個結構層級組成&#xff0c;自上而下涵蓋交互界面、API 通信、模型推理、計算框架、底層驅動與硬件支持。本篇梳理 AIGC 應用的六層體系結構&#xff0c;明確各組件在系統中的職責與上下游關系&#xff0c;…

[MERN 項目實戰] MERN Multi-Vendor 電商平臺開發筆記(v2.0 從 bug 到結構優化的工程記錄)

[MERN 項目實戰] MERN Multi-Vendor 電商平臺開發筆記&#xff08;v2.0 從 bug 到結構優化的工程記錄&#xff09; 其實之前沒想著這么快就能把 2.0 的筆記寫出來的&#xff0c;之前的預期是&#xff0c;下一個階段會一直維持到將 MERN 項目寫完&#xff0c;畢竟后期很多東西都…

互斥量函數組

頭文件 #include <pthread.h> pthread_mutex_init 函數原型&#xff1a; int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr); 函數參數&#xff1a; mutex&#xff1a;指向要初始化的互斥量的指針。 attr&#xf…

互聯網的下一代脈搏:深入理解 QUIC 協議

互聯網的下一代脈搏&#xff1a;深入理解 QUIC 協議 互聯網是現代社會的基石&#xff0c;而數據在其中高效、安全地傳輸是其運轉的關鍵。長期以來&#xff0c;傳輸層的 TCP&#xff08;傳輸控制協議&#xff09;一直是互聯網的主力軍。然而&#xff0c;隨著互聯網應用場景的日…

全球城市范圍30米分辨率土地覆蓋數據(1985-2020)

Global urban area 30 meter resolution land cover data (1985-2020) 時間分辨率年空間分辨率10m - 100m共享方式保護期 277 天 5 時 42 分 9 秒數據大小&#xff1a;8.98 GB數據時間范圍&#xff1a;1985-2020元數據更新時間2024-01-11 數據集摘要 1985~2020全球城市土地覆…

【Vue】單元測試(Jest/Vue Test Utils)

個人主頁&#xff1a;Guiat 歸屬專欄&#xff1a;Vue 文章目錄 1. Vue 單元測試簡介1.1 為什么需要單元測試1.2 測試工具介紹 2. 環境搭建2.1 安裝依賴2.2 配置 Jest 3. 編寫第一個測試3.1 組件示例3.2 編寫測試用例3.3 運行測試 4. Vue Test Utils 核心 API4.1 掛載組件4.2 常…