Skip to content

Commit

Permalink
Merge pull request #1676 from rztao/fix-nacos-reconnection-failed-iss…
Browse files Browse the repository at this point in the history
…ue-due-to-incorrect-init-classloader

Fix Nacos reconnection failed continuously issue with message "com.alibaba.nacos.common.remote.exception.RemoteException: errCode: 500, errMsg: Unknown payload type:ServerCheckResponse" but actually the connection is already established.
  • Loading branch information
Sherlockhan authored Nov 20, 2024
2 parents bc4649f + 904b8a1 commit 76b68a4
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 76b68a4

Please sign in to comment.