Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#1365] add router config to config-center can not be load when consumer service already started #1366

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()).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,69 @@
/*

* 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.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;

public LabelRouterConfigListener(NacosContextRefresher contextRefresher, Set<String> listenersKey) {
this.listenersKey = new HashSet<>(listenersKey);
this.contextRefresher = contextRefresher;
this.taskScheduler = buildTaskScheduler();
}

public void schedulerCheckLabelRouterConfig() {
taskScheduler.scheduleWithFixedDelay(this::checkLabelRouterConfig, Duration.ofMillis(15000));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改成一个配置项,默认值调大一点,改成30s,减少服务端压力

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

}

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;
}
}
Loading