Skip to content

Commit

Permalink
[#1428] fixed LabelRouterConfigListener scheduler task multiple initi…
Browse files Browse the repository at this point in the history
…alizations in dual-engine Scenarios (#1433) (#1434)
  • Loading branch information
chengyouling authored Jan 18, 2025
1 parent ddc6bf8 commit d343f72
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
Expand Down Expand Up @@ -117,12 +118,19 @@ public void onApplicationEvent(ApplicationReadyEvent event) {
// many Spring context
if (this.ready.compareAndSet(false, true) && isRefreshEnabled()) {
this.registerNacosListenersForApplications();
this.registerLabelRouterConfigListener();
if (nacosConfigProperties.isMasterStandbyEnabled()) {
startSchedulerTask();
}
}
}

private void registerLabelRouterConfigListener() {
if (env.getProperty(NacosConfigConst.ROUTER_CONFIG_DEFAULT_LOAD_ENABLED, boolean.class, false)) {
new LabelRouterConfigListener(this, env).schedulerCheckLabelRouterConfig();
}
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
Expand Down Expand Up @@ -196,9 +204,6 @@ private void registerNacosListenersForApplications() {
registerNacosListener(propertySource.getGroup(), propertySource.getDataId(), currentConfigServiceManager);
}
}
if (env.getProperty(NacosConfigConst.ROUTER_CONFIG_DEFAULT_LOAD_ENABLED, boolean.class, false)) {
new LabelRouterConfigListener(this, listenerMap.keySet(), env).schedulerCheckLabelRouterConfig();
}
} catch (NacosException e) {
log.error("add nacos config listener error, serverAddr=[{}]", currentConfigServiceManager.getServerAddr(), e);
}
Expand Down Expand Up @@ -275,4 +280,8 @@ public void registerAddRouterConfigListener(String dataId, String group) {
log.error("add nacos config listener error, serverAddr=[{}]", currentConfigServiceManager.getServerAddr(), e);
}
}

public Set<String> getListenerKeys() {
return listenerMap.keySet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
package com.huaweicloud.nacos.config.client;

import java.time.Duration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.core.env.Environment;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
Expand All @@ -32,14 +30,11 @@
public class LabelRouterConfigListener {
private final ThreadPoolTaskScheduler taskScheduler;

private final Set<String> listenersKey;

private final NacosContextRefresher contextRefresher;

private final Environment env;

public LabelRouterConfigListener(NacosContextRefresher contextRefresher, Set<String> listenersKey, Environment env) {
this.listenersKey = new HashSet<>(listenersKey);
public LabelRouterConfigListener(NacosContextRefresher contextRefresher, Environment env) {
this.contextRefresher = contextRefresher;
this.env = env;
this.taskScheduler = buildTaskScheduler();
Expand All @@ -59,16 +54,15 @@ private void checkLabelRouterConfig() {
}
for (PropertyConfigItem configItem : routerProperties) {
String key = NacosPropertySourceRepository.getMapKey(configItem.getDataId(), configItem.getGroup());
if (!listenersKey.contains(key)) {
if (!contextRefresher.getListenerKeys().contains(key)) {
contextRefresher.registerAddRouterConfigListener(configItem.getDataId(), configItem.getGroup());
listenersKey.add(key);
}
}
}

private ThreadPoolTaskScheduler buildTaskScheduler() {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setBeanName("Nacos-Router-Config-Listener-Scheduler");
taskScheduler.setBeanName("Router-Config-Listener-Task");
taskScheduler.initialize();
return taskScheduler;
}
Expand Down

0 comments on commit d343f72

Please sign in to comment.