Skip to content

Commit

Permalink
Fix nacos reconnection failed issue because PayloadRegistry without i…
Browse files Browse the repository at this point in the history
…nitializing due to incorrect classloader when creating nacos client.

Signed-off-by: rztao <[email protected]>
  • Loading branch information
rztao committed Nov 17, 2024
1 parent f6f66b0 commit 904b8a1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,16 @@ private NamingServiceUtils() {
public static NamingService buildNamingService(Map<String, String> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand Down

0 comments on commit 904b8a1

Please sign in to comment.