From e5d392676121c78eb6fb97437c95af18d6db6adc Mon Sep 17 00:00:00 2001 From: Alex <97039406+chengyouling@users.noreply.github.com> Date: Fri, 16 Aug 2024 11:15:54 +0800 Subject: [PATCH] [#1365] add router config to config-center can not be load when consumer service already started (#1366) (#1373) --- .../nacos/refresh/NacosContextRefresher.java | 12 +++ .../client/LabelRouterConfigListener.java | 75 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-config/src/main/java/com/huaweicloud/nacos/config/client/LabelRouterConfigListener.java diff --git a/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-config/src/main/java/com/alibaba/cloud/nacos/refresh/NacosContextRefresher.java b/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-config/src/main/java/com/alibaba/cloud/nacos/refresh/NacosContextRefresher.java index 482c091b8..739a3b0e3 100644 --- a/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-config/src/main/java/com/alibaba/cloud/nacos/refresh/NacosContextRefresher.java +++ b/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-config/src/main/java/com/alibaba/cloud/nacos/refresh/NacosContextRefresher.java @@ -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; @@ -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); } @@ -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); + } + } } diff --git a/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-config/src/main/java/com/huaweicloud/nacos/config/client/LabelRouterConfigListener.java b/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-config/src/main/java/com/huaweicloud/nacos/config/client/LabelRouterConfigListener.java new file mode 100644 index 000000000..6e0a9c15a --- /dev/null +++ b/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-config/src/main/java/com/huaweicloud/nacos/config/client/LabelRouterConfigListener.java @@ -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 listenersKey; + + private final NacosContextRefresher contextRefresher; + + private final Environment env; + + public LabelRouterConfigListener(NacosContextRefresher contextRefresher, Set 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 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; + } +}