Merge pull request #2346 from helloichen/feature/issue2328_nacos

[Enhancement] Support for obtaining configuration data with delimiters from Nacos
pull/2361/head
余黄彬 3 years ago committed by GitHub
commit e5149643d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -16,8 +16,10 @@
package com.alibaba.cloud.nacos.client;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@ -63,7 +65,7 @@ public class NacosPropertySource extends MapPropertySource {
}
NacosPropertySource(List<PropertySource<?>> propertySources, String group,
String dataId, Date timestamp, boolean isRefreshable) {
String dataId, Date timestamp, boolean isRefreshable) {
this(group, dataId, getSourceMap(group, dataId, propertySources), timestamp,
isRefreshable);
}
@ -80,12 +82,32 @@ public class NacosPropertySource extends MapPropertySource {
return (Map<String, Object>) 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);
Map<String, Object> sourceMap = new LinkedHashMap<>();
List<PropertySource<?>> otherTypePropertySources = new ArrayList<>();
for (PropertySource<?> propertySource : propertySources) {
if (propertySource == null) {
continue;
}
if (propertySource instanceof MapPropertySource) {
// If the Nacos configuration file uses "---" to separate property name,
// propertySources will be multiple documents, and every document is a map.
// see org.springframework.boot.env.YamlPropertySourceLoader#load
MapPropertySource mapPropertySource = (MapPropertySource) propertySource;
Map<String, Object> source = mapPropertySource.getSource();
sourceMap.putAll(source);
} else {
otherTypePropertySources.add(propertySource);
}
}
// Other property sources which is not instanceof MapPropertySource will be put as it is,
// and the internal elements cannot be directly retrieved,
// so the user needs to implement the retrieval logic by himself
if (!otherTypePropertySources.isEmpty()) {
sourceMap.put(String.join(NacosConfigProperties.COMMAS, dataId, group), otherTypePropertySources);
}
return sourceMap;
}
public String getGroup() {

@ -67,7 +67,7 @@ public class NacosFileExtensionTest {
throws Throwable {
if ("test-name.yaml".equals(args[0])
&& "DEFAULT_GROUP".equals(args[1])) {
return "user:\n name: hello\n age: 12";
return "user:\n name: hello\n age: 12\n---\nuser:\n gender: male";
}
return "";
}
@ -88,6 +88,7 @@ public class NacosFileExtensionTest {
Assert.assertEquals(environment.getProperty("user.name"), "hello");
Assert.assertEquals(environment.getProperty("user.age"), "12");
Assert.assertEquals(environment.getProperty("user.gender"), "male");
}
@Configuration

Loading…
Cancel
Save