diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/pom.xml b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/pom.xml
index 0a36d2c8..7629648d 100644
--- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/pom.xml
+++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/pom.xml
@@ -105,7 +105,6 @@
2.0.0
test
-
diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySource.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySource.java
index 2d83fa5d..8e1b6fe2 100644
--- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySource.java
+++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySource.java
@@ -16,12 +16,16 @@
package com.alibaba.cloud.nacos.client;
+import java.util.Collections;
import java.util.Date;
+import java.util.List;
import java.util.Map;
import com.alibaba.cloud.nacos.NacosConfigProperties;
import org.springframework.core.env.MapPropertySource;
+import org.springframework.core.env.PropertySource;
+import org.springframework.util.CollectionUtils;
/**
* @author xiaojing
@@ -50,7 +54,7 @@ public class NacosPropertySource extends MapPropertySource {
private final boolean isRefreshable;
NacosPropertySource(String group, String dataId, Map source,
- Date timestamp, boolean isRefreshable) {
+ Date timestamp, boolean isRefreshable) {
super(String.join(NacosConfigProperties.COMMAS, dataId, group), source);
this.group = group;
this.dataId = dataId;
@@ -58,6 +62,32 @@ public class NacosPropertySource extends MapPropertySource {
this.isRefreshable = isRefreshable;
}
+ NacosPropertySource(List> propertySources, String group,
+ String dataId, Date timestamp, boolean isRefreshable) {
+ this(group, dataId, getSourceMap(group, dataId, propertySources), timestamp,
+ isRefreshable);
+ }
+
+ private static Map getSourceMap(String group, String dataId,
+ List> propertySources) {
+ if (CollectionUtils.isEmpty(propertySources)) {
+ return Collections.emptyMap();
+ }
+ // If only one, return the internal element, otherwise wrap it.
+ if (propertySources.size() == 1) {
+ PropertySource propertySource = propertySources.get(0);
+ if (propertySource != null && propertySource.getSource() instanceof Map) {
+ return (Map) propertySource.getSource();
+ }
+ }
+ // If it is multiple, it will be returned as it is, and the internal elements
+ // cannot be directly retrieved, so the user needs to implement the retrieval
+ // logic by himself
+ return Collections.singletonMap(
+ String.join(NacosConfigProperties.COMMAS, dataId, group),
+ propertySources);
+ }
+
public String getGroup() {
return this.group;
}
diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceBuilder.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceBuilder.java
index ed5ac5d9..762b9b91 100644
--- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceBuilder.java
+++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceBuilder.java
@@ -16,17 +16,18 @@
package com.alibaba.cloud.nacos.client;
-import java.util.Date;
-import java.util.LinkedHashMap;
-import java.util.Map;
-
import com.alibaba.cloud.nacos.NacosPropertySourceRepository;
import com.alibaba.cloud.nacos.parser.NacosDataParserHandler;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
+
+import java.util.Collections;
+import java.util.Date;
+import java.util.List;
+
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
+import org.springframework.core.env.PropertySource;
import org.springframework.util.StringUtils;
/**
@@ -38,8 +39,6 @@ public class NacosPropertySourceBuilder {
private static final Logger log = LoggerFactory
.getLogger(NacosPropertySourceBuilder.class);
- private static final Map EMPTY_MAP = new LinkedHashMap();
-
private ConfigService configService;
private long timeout;
@@ -71,14 +70,15 @@ public class NacosPropertySourceBuilder {
*/
NacosPropertySource build(String dataId, String group, String fileExtension,
boolean isRefreshable) {
- Map p = loadNacosData(dataId, group, fileExtension);
- NacosPropertySource nacosPropertySource = new NacosPropertySource(group, dataId,
- p, new Date(), isRefreshable);
+ List> propertySources = loadNacosData(dataId, group,
+ fileExtension);
+ NacosPropertySource nacosPropertySource = new NacosPropertySource(propertySources,
+ group, dataId, new Date(), isRefreshable);
NacosPropertySourceRepository.collectNacosPropertySource(nacosPropertySource);
return nacosPropertySource;
}
- private Map loadNacosData(String dataId, String group,
+ private List> loadNacosData(String dataId, String group,
String fileExtension) {
String data = null;
try {
@@ -87,24 +87,23 @@ public class NacosPropertySourceBuilder {
log.warn(
"Ignore the empty nacos configuration and get it based on dataId[{}] & group[{}]",
dataId, group);
- return EMPTY_MAP;
+ return Collections.emptyList();
}
if (log.isDebugEnabled()) {
log.debug(String.format(
"Loading nacos data, dataId: '%s', group: '%s', data: %s", dataId,
group, data));
}
- Map dataMap = NacosDataParserHandler.getInstance()
- .parseNacosData( data, fileExtension);
- return dataMap == null ? EMPTY_MAP : dataMap;
+ return NacosDataParserHandler.getInstance().parseNacosData(dataId, data,
+ fileExtension);
}
catch (NacosException e) {
- log.error("get data from Nacos error,dataId:{}, ", dataId, e);
+ log.error("get data from Nacos error,dataId:{} ", dataId, e);
}
catch (Exception e) {
- log.error("parse data from Nacos error,dataId:{},data:{},", dataId, data, e);
+ log.error("parse data from Nacos error,dataId:{},data:{}", dataId, data, e);
}
- return EMPTY_MAP;
+ return Collections.emptyList();
}
}
diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/parser/AbstractNacosDataParser.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/parser/AbstractNacosDataParser.java
deleted file mode 100644
index 6f94cb6c..00000000
--- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/parser/AbstractNacosDataParser.java
+++ /dev/null
@@ -1,175 +0,0 @@
-/*
- * Copyright 2013-2018 the original author or authors.
- *
- * 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
- *
- * https://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.alibaba.cloud.nacos.parser;
-
-import java.io.IOException;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.Map;
-import java.util.Set;
-
-import org.springframework.util.StringUtils;
-
-/**
- * @author zkz
- */
-public abstract class AbstractNacosDataParser {
-
- protected static final String DOT = ".";
-
- protected static final String VALUE = "value";
-
- protected static final String EMPTY_STRING = "";
-
- private String extension;
-
- private AbstractNacosDataParser nextParser;
-
- protected AbstractNacosDataParser(String extension) {
- if (StringUtils.isEmpty(extension)) {
- throw new IllegalArgumentException("extension cannot be empty");
- }
- this.extension = extension.toLowerCase();
- }
-
- /**
- * Verify dataId extensions.
- * @param extension file extension. json or xml or yml or yaml or properties
- * @return valid or not
- */
- public final boolean checkFileExtension(String extension) {
- if (this.isLegal(extension.toLowerCase())) {
- return true;
- }
- if (this.nextParser == null) {
- return false;
- }
- return this.nextParser.checkFileExtension(extension);
-
- }
-
- /**
- * Parsing nacos configuration content.
- * @param data config data from Nacos
- * @param extension file extension. json or xml or yml or yaml or properties
- * @return result of Properties
- * @throws IOException thrown if there is a problem parsing config.
- */
- public final Map parseNacosData(String data, String extension)
- throws IOException {
- if (extension == null || extension.length() < 1) {
- throw new IllegalStateException("The file extension cannot be empty");
- }
- if (this.isLegal(extension.toLowerCase())) {
- return this.doParse(data);
- }
- if (this.nextParser == null) {
- throw new IllegalStateException(getTips(extension));
- }
- return this.nextParser.parseNacosData(data, extension);
- }
-
- /**
- * Core logic for parsing.
- * @param data config from Nacos
- * @return result of Properties
- * @throws IOException thrown if there is a problem parsing config.
- */
- protected abstract Map doParse(String data) throws IOException;
-
- protected AbstractNacosDataParser setNextParser(AbstractNacosDataParser nextParser) {
- this.nextParser = nextParser;
- return this;
- }
-
- public AbstractNacosDataParser addNextParser(AbstractNacosDataParser nextParser) {
- if (this.nextParser == null) {
- this.nextParser = nextParser;
- }
- else {
- this.nextParser.addNextParser(nextParser);
- }
- return this;
- }
-
- protected boolean isLegal(String extension) {
- return this.extension.equalsIgnoreCase(extension)
- || this.extension.contains(extension);
- }
-
- protected void flattenedMap(Map result, Map dataMap,
- String parentKey) {
- Set> entries = dataMap.entrySet();
- for (Iterator> iterator = entries.iterator(); iterator
- .hasNext();) {
- Map.Entry entry = iterator.next();
- String key = entry.getKey();
- Object value = entry.getValue();
-
- String fullKey = StringUtils.isEmpty(parentKey) ? key : key.startsWith("[")
- ? parentKey.concat(key) : parentKey.concat(DOT).concat(key);
-
- if (value instanceof Map) {
- Map map = (Map) value;
- flattenedMap(result, map, fullKey);
- continue;
- }
- else if (value instanceof Collection) {
- int count = 0;
- Collection