-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#1365] add router config to config-center can not be load when consu…
- Loading branch information
1 parent
f7e4f0e
commit e5d3926
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...s-config/src/main/java/com/huaweicloud/nacos/config/client/LabelRouterConfigListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |