Skip to content

Commit

Permalink
Merge pull request #1614 from AYue-94/fix/nacos_reconnect
Browse files Browse the repository at this point in the history
fix: Backend Use Nacos cause OutOfMemoryError
  • Loading branch information
Sherlockhan authored Sep 18, 2024
2 parents 03c241a + 5bbaf6e commit acda541
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@
* @since 2023-08-17
*/
public class NacosBufferedClient implements Closeable {
/**
* configService connection status type
*/
public static final String KEY_CONNECTED = "UP";

/**
* logger
Expand Down Expand Up @@ -233,44 +229,16 @@ private Properties createProperties(String connectString, int sessionTimeout, St
* @throws NacosInitException Connect to Nacos failed
*/
private void createNacosClient(String connectString, Properties properties) {
ClassLoader tempClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
try {
if (!connect(properties)) {
LOGGER.log(Level.SEVERE, "Nacos connection reaches the maximum number of retries");
throw new NacosInitException(connectString);
}
nacosClient = new NacosClient(properties);
} catch (NacosException e) {
LOGGER.log(Level.SEVERE, "Nacos connection exception, msg is: {0}", e.getMessage());
throw new NacosInitException(connectString);
}
}

/**
* Connect to Nacos
*
* @param properties Nacos Client connection configuration
* @return Nacos connection status
* @throws NacosException Nacos initialization exception
*/
private boolean connect(Properties properties) throws NacosException {
int tryNum = 0;
while (tryNum++ <= CONFIG.getConnectRetryTimes()) {
// Nacos client initialization gets the current thread's classloader, which needs to be changed and then
// changed back to the original classloader
ClassLoader tempClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
nacosClient = new NacosClient(properties);
} finally {
Thread.currentThread().setContextClassLoader(tempClassLoader);
if (KEY_CONNECTED.equals(nacosClient.getServerStatus())) {
return true;
}
try {
Thread.sleep(CONFIG.getConnectTimeout());
LOGGER.log(Level.INFO, "The {0} times to retry to connect to nacos", tryNum);
} catch (InterruptedException e) {
LOGGER.log(Level.SEVERE, "Nacos connection sleep exception, msg is: {0}", e.getMessage());
}
}
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,6 @@ public void reConnection() {
if (ConfigCenterType.ZOOKEEPER.name().equals(dynamicConfig.getDynamicConfigType())
&& !configClient.isConnect()) {
configClient = new ZooKeeperClient(dynamicConfig.getServerAddress(), dynamicConfig.getTimeout(), watcher);
return;
}
if (ConfigCenterType.NACOS.name().equals(dynamicConfig.getDynamicConfigType()) && !configClient.isConnect()) {
for (Map.Entry<String, ConfigClient> entry : CONFIG_CLIENT_MAP.entrySet()) {
createNacosClient(entry.getKey());
}
}
}
}

0 comments on commit acda541

Please sign in to comment.