From 904b8a1c9d73da9b58426aaeeb137b0e2b62ef39 Mon Sep 17 00:00:00 2001 From: rztao Date: Sun, 17 Nov 2024 20:18:56 +0800 Subject: [PATCH] Fix nacos reconnection failed issue because PayloadRegistry without initializing due to incorrect classloader when creating nacos client. Signed-off-by: rztao --- .../dubbo/registry/utils/NamingServiceUtils.java | 4 ++++ .../service/register/NacosServiceManager.java | 16 ++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/sermant-plugins/sermant-service-registry/dubbo-registry-service/src/main/java/io/sermant/dubbo/registry/utils/NamingServiceUtils.java b/sermant-plugins/sermant-service-registry/dubbo-registry-service/src/main/java/io/sermant/dubbo/registry/utils/NamingServiceUtils.java index 87a9f6d4b9..767ebbbe92 100644 --- a/sermant-plugins/sermant-service-registry/dubbo-registry-service/src/main/java/io/sermant/dubbo/registry/utils/NamingServiceUtils.java +++ b/sermant-plugins/sermant-service-registry/dubbo-registry-service/src/main/java/io/sermant/dubbo/registry/utils/NamingServiceUtils.java @@ -57,12 +57,16 @@ private NamingServiceUtils() { public static NamingService buildNamingService(Map parameters, NacosRegisterConfig registerConfig, RegisterServiceCommonConfig commonConfig) { Properties nacosProperties = buildNacosProperties(parameters, registerConfig, commonConfig); + ClassLoader tempClassLoader = Thread.currentThread().getContextClassLoader(); + Thread.currentThread().setContextClassLoader(NamingServiceUtils.class.getClassLoader()); try { return NacosFactory.createNamingService(nacosProperties); } catch (NacosException e) { LOGGER.log(Level.SEVERE, String.format(Locale.ENGLISH, "create namingService failed: {%s}", e.getErrMsg()), e); throw new IllegalStateException(e); + } finally { + Thread.currentThread().setContextClassLoader(tempClassLoader); } } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/io/sermant/registry/service/register/NacosServiceManager.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/io/sermant/registry/service/register/NacosServiceManager.java index 792d4182db..fdb6414d02 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/io/sermant/registry/service/register/NacosServiceManager.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/io/sermant/registry/service/register/NacosServiceManager.java @@ -104,11 +104,23 @@ private void buildNamingService(Properties properties) throws NacosException { } private NamingService createNewNamingService(Properties properties) throws NacosException { - return new NacosNamingService(properties); + ClassLoader tempClassLoader = Thread.currentThread().getContextClassLoader(); + Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader()); + try { + return new NacosNamingService(properties); + } finally { + Thread.currentThread().setContextClassLoader(tempClassLoader); + } } private NamingMaintainService createNamingMaintainService(Properties properties) throws NacosException { - return new NacosNamingMaintainService(properties); + ClassLoader tempClassLoader = Thread.currentThread().getContextClassLoader(); + Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader()); + try { + return new NacosNamingMaintainService(properties); + } finally { + Thread.currentThread().setContextClassLoader(tempClassLoader); + } } /**