Skip to content

Commit

Permalink
[#1319] fixed nacos configuration item cannot be listened when master…
Browse files Browse the repository at this point in the history
…/standby server is unavailable (#1320) (#1324)
  • Loading branch information
chengyouling authored Jul 16, 2024
1 parent e06090d commit 60eb736
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import java.util.stream.Collectors;

import com.alibaba.cloud.nacos.NacosPropertySourceRepository;
import com.huaweicloud.nacos.config.manager.ConfigServiceManagerUtils;
import com.huaweicloud.nacos.config.manager.NacosConfigManager;
import com.alibaba.cloud.nacos.parser.NacosDataParserHandler;
import com.alibaba.cloud.nacos.refresh.NacosSnapshotConfigManager;
import com.alibaba.nacos.api.exception.NacosException;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -54,7 +54,7 @@ public class NacosPropertySourceBuilder {
private long timeout;

public NacosPropertySourceBuilder(List<NacosConfigManager> nacosConfigManagers, long timeout) {
this.configServiceManagers = nacosConfigManagers.stream().filter(NacosConfigManager::checkServerConnect)
this.configServiceManagers = nacosConfigManagers.stream()
.sorted(Comparator.comparingInt(NacosConfigManager::getOrder)).collect(Collectors.toList());
this.timeout = timeout;
}
Expand Down Expand Up @@ -92,7 +92,8 @@ private List<PropertySource<?>> loadNacosData(String dataId, String group,
String configSnapshot = NacosSnapshotConfigManager.getAndRemoveConfigSnapshot(dataId, group);
if (StringUtils.isEmpty(configSnapshot)) {
log.debug("get config from nacos, dataId: {}, group: {}", dataId, group);
data = getConfigDataFromNacos(dataId, group);
data = ConfigServiceManagerUtils.chooseConfigManager(configServiceManagers)
.getConfigService().getConfig(dataId, group, timeout);
} else {
log.debug("get config from memory snapshot, dataId: {}, group: {}",
dataId, group);
Expand All @@ -116,16 +117,4 @@ private List<PropertySource<?>> loadNacosData(String dataId, String group,
}
return Collections.emptyList();
}

private String getConfigDataFromNacos(String dataId, String group) {
for (NacosConfigManager configManager : configServiceManagers) {
try {
return configManager.getConfigService().getConfig(dataId, group, timeout);
} catch (NacosException e) {
log.error("get config from nacos server error, serverAddr=[{}], dataId=[{}], group=[{}]",
configManager.getServerAddr(), dataId, group, e);
}
}
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ private ConfigService getConfigService(ConfigDataLoaderContext context) {
if (configServiceStandby != null && configServiceStandby.checkServerConnect()) {
return configServiceStandby.getConfigService();
}
if (configServiceMaster != null) {
return configServiceMaster.getConfigService();
}
if (configServiceStandby != null) {
return configServiceStandby.getConfigService();
}
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.List;
import java.util.stream.Collectors;

import com.huaweicloud.nacos.config.manager.ConfigServiceManagerUtils;
import com.huaweicloud.nacos.config.manager.NacosConfigManager;

import org.springframework.boot.actuate.health.AbstractHealthIndicator;
Expand Down Expand Up @@ -56,12 +57,8 @@ public NacosConfigHealthIndicator(List<NacosConfigManager> nacosConfigManagers)
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
// Just return "UP" or "DOWN"
String status = STATUS_DOWN;
for (NacosConfigManager configManager : nacosConfigManagers) {
if (configManager.checkServerConnect()) {
status = configManager.getConfigService().getServerStatus();
}
}
String status = ConfigServiceManagerUtils.chooseConfigManager(nacosConfigManagers)
.getConfigService().getServerStatus();

// Set the status to Builder
builder.status(status);
Expand Down
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.manager.ConfigServiceManagerUtils;
import com.huaweicloud.nacos.config.manager.NacosConfigManager;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.AbstractSharedListener;
Expand Down Expand Up @@ -187,25 +188,15 @@ public static void shutDownConfigService(NacosConfigManager configServiceManager
* register Nacos Listeners.
*/
private void registerNacosListenersForApplications() {
int idx = 0;
while (idx < nacosConfigManagers.size()) {
NacosConfigManager configManager = nacosConfigManagers.get(idx);
if (!configManager.checkServerConnect()) {
idx++;
continue;
}
this.currentConfigServiceManager = configManager;
try {
for (NacosPropertySource propertySource : NacosPropertySourceRepository.getAll()) {
if (propertySource.isRefreshable()) {
registerNacosListener(propertySource.getGroup(), propertySource.getDataId(), configManager);
}
this.currentConfigServiceManager = ConfigServiceManagerUtils.chooseConfigManager(nacosConfigManagers);
try {
for (NacosPropertySource propertySource : NacosPropertySourceRepository.getAll()) {
if (propertySource.isRefreshable()) {
registerNacosListener(propertySource.getGroup(), propertySource.getDataId(), currentConfigServiceManager);
}
return;
} catch (NacosException e) {
log.error("add nacos config listener error, serverAddr=[{}]", configManager.getServerAddr(), e);
}
idx++;
} catch (NacosException e) {
log.error("add nacos config listener error, serverAddr=[{}]", currentConfigServiceManager.getServerAddr(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.net.Socket;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
Expand All @@ -48,9 +49,25 @@ public static boolean checkServerConnect(String serverAddress) {

private static URI parseIpPortFromURI(String uri) {
try {
return new URI(uri);
String realUri = uri.startsWith("http") ? uri : "http://" + uri;
return new URI(realUri);
} catch (URISyntaxException e) {
return null;
}
}

public static NacosConfigManager chooseConfigManager(List<NacosConfigManager> nacosConfigManagers) {
int idx = 0;
while (idx < nacosConfigManagers.size()) {
if (checkServerConnect(nacosConfigManagers.get(idx).getServerAddr())) {
return nacosConfigManagers.get(idx);
}
LOGGER.warn("nacos server [{}] unavailable, choose others.", nacosConfigManagers.get(idx).getServerAddr());
idx++;
}
LOGGER.warn("all nacos server unavailable, use master server.");

// if all server unavailable, return master server, ensure listening configuration when service is available again.
return nacosConfigManagers.get(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<file>${user.home}/logs/nacos/nacos-remote.log</file>

<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>nacos-remote.%d{yyyy-MM-dd}.log</fileNamePattern>
<fileNamePattern>${user.home}/logs/nacos/nacos-remote.%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>

<encoder>
Expand All @@ -50,7 +50,7 @@
<file>${user.home}/logs/nacos/nacos-naming.log</file>

<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>nacos-naming.%d{yyyy-MM-dd}.log</fileNamePattern>
<fileNamePattern>${user.home}/logs/nacos/nacos-naming.%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>

<encoder>
Expand Down

0 comments on commit 60eb736

Please sign in to comment.