Skip to content

Commit

Permalink
[#1365] add router config to config-center can not be load when consu…
Browse files Browse the repository at this point in the history
…mer service already started (#1366) (#1373)
  • Loading branch information
chengyouling authored Aug 16, 2024
1 parent f7e4f0e commit e5d3926
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.cloud.nacos.NacosPropertySourceRepository;
import com.alibaba.cloud.nacos.client.NacosPropertySource;
import com.huaweicloud.nacos.config.client.LabelRouterConfigListener;
import com.huaweicloud.nacos.config.manager.ConfigServiceManagerUtils;
import com.huaweicloud.nacos.config.manager.NacosConfigManager;
import com.alibaba.nacos.api.config.ConfigService;
Expand Down Expand Up @@ -195,6 +196,9 @@ 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 @@ -263,4 +267,12 @@ private ThreadPoolTaskScheduler buildTaskScheduler() {
taskScheduler.initialize();
return taskScheduler;
}

public void registerAddRouterConfigListener(String dataId, String group) {
try {
registerNacosListener(group, dataId, currentConfigServiceManager);
} catch (NacosException e) {
log.error("add nacos config listener error, serverAddr=[{}]", currentConfigServiceManager.getServerAddr(), e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (C) 2020-2024 Huawei Technologies Co., Ltd. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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;
import org.springframework.util.CollectionUtils;

import com.alibaba.cloud.nacos.NacosPropertySourceRepository;
import com.alibaba.cloud.nacos.refresh.NacosContextRefresher;

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);
this.contextRefresher = contextRefresher;
this.env = env;
this.taskScheduler = buildTaskScheduler();
}

public void schedulerCheckLabelRouterConfig() {
String checkRouterConfigDelayTime = "spring.cloud.nacos.check.router.config.delay.time";
taskScheduler.scheduleWithFixedDelay(this::checkLabelRouterConfig, Duration.ofMillis(env.getProperty(
checkRouterConfigDelayTime, long.class, 30000L)));
}

private void checkLabelRouterConfig() {
NacosPropertiesFuzzyQueryService blurQueryService = NacosPropertiesFuzzyQueryService.getInstance();
List<PropertyConfigItem> routerProperties = blurQueryService.loadRouterProperties();
if (CollectionUtils.isEmpty(routerProperties)) {
return;
}
for (PropertyConfigItem configItem : routerProperties) {
String key = NacosPropertySourceRepository.getMapKey(configItem.getDataId(), configItem.getGroup());
if (!listenersKey.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.initialize();
return taskScheduler;
}
}

0 comments on commit e5d3926

Please sign in to comment.