1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

enhance load config data from Nacos

This commit is contained in:
yuhuangbin 2020-01-10 20:11:26 +08:00
parent 9b45147d4e
commit 0b50a47a02
5 changed files with 52 additions and 54 deletions

View File

@ -64,7 +64,8 @@ public class NacosConfigAutoConfiguration {
public NacosContextRefresher nacosContextRefresher(
NacosConfigManager nacosConfigManager,
NacosRefreshHistory nacosRefreshHistory) {
// Consider that it is not necessary to be compatible with the previous configuration
// Consider that it is not necessary to be compatible with the previous
// configuration
// and use the new configuration if necessary.
return new NacosContextRefresher(nacosConfigManager, nacosRefreshHistory);
}

View File

@ -17,7 +17,6 @@
package com.alibaba.cloud.nacos.client;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
@ -111,17 +110,7 @@ public class NacosPropertySourceBuilder {
@SuppressWarnings("unchecked")
private Map<String, Object> propertiesToMap(Properties properties) {
Map<String, Object> result = new HashMap<>(16);
Enumeration<String> keys = (Enumeration<String>) properties.propertyNames();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
String value = properties.getProperty(key);
if (value != null) {
result.put(key, value.trim());
}
else {
result.put(key, null);
}
}
properties.forEach((k, v) -> result.put(String.valueOf(k), v));
return result;
}

View File

@ -111,12 +111,12 @@ public abstract class AbstractNacosDataParser {
/**
* Generate key-value pairs from the map.
*/
protected Properties generateProperties(Map<String, String> map) {
protected Properties generateProperties(Map<String, Object> map) {
if (null == map || map.isEmpty()) {
return null;
}
Properties properties = new Properties();
for (Map.Entry<String, String> entry : map.entrySet()) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
if (StringUtils.isEmpty(key)) {
continue;
@ -130,12 +130,12 @@ public abstract class AbstractNacosDataParser {
/**
* Reload the key ending in `value` if need.
*/
protected Map<String, String> reloadMap(Map<String, String> map) {
protected Map<String, Object> reloadMap(Map<String, Object> map) {
if (map == null || map.isEmpty()) {
return null;
}
Map<String, String> result = new HashMap<>(map);
for (Map.Entry<String, String> entry : map.entrySet()) {
Map<String, Object> result = new HashMap<>(map);
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
if (key.contains(DOT)) {
int idx = key.lastIndexOf(DOT);

View File

@ -17,18 +17,22 @@
package com.alibaba.cloud.nacos.parser;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
/**
* @author zkz
* @author yuhuangbin
*/
public class NacosDataJsonParser extends AbstractNacosDataParser {
@ -41,7 +45,7 @@ public class NacosDataJsonParser extends AbstractNacosDataParser {
if (StringUtils.isEmpty(data)) {
return null;
}
Map<String, String> map = parseJSON2Map(data);
Map<String, Object> map = parseJSON2Map(data);
return this.generateProperties(this.reloadMap(map));
}
@ -51,44 +55,48 @@ public class NacosDataJsonParser extends AbstractNacosDataParser {
* @return the map convert by json string
* @throws IOException thrown if there is a problem parsing config.
*/
public static Map<String, String> parseJSON2Map(String json) throws IOException {
Map<String, String> map = new HashMap<>(32);
public static Map<String, Object> parseJSON2Map(String json) throws IOException {
Map<String, Object> result = new HashMap<>(32);
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(json);
if (null == jsonNode) {
return map;
Map<String, Object> nacosDataMap = mapper.readValue(json, Map.class);
if (CollectionUtils.isEmpty(nacosDataMap)) {
return result;
}
parseJsonNode(map, jsonNode, "");
return map;
parseNacosDataMap(result, nacosDataMap, "");
return result;
}
private static void parseJsonNode(Map<String, String> jsonMap, JsonNode jsonNode,
String parentKey) {
Iterator<String> fieldNames = jsonNode.fieldNames();
while (fieldNames.hasNext()) {
String name = fieldNames.next();
String fullKey = StringUtils.isEmpty(parentKey) ? name
: parentKey + DOT + name;
JsonNode resultValue = jsonNode.findValue(name);
if (null == resultValue) {
private static void parseNacosDataMap(Map<String, Object> result,
Map<String, Object> dataMap, String parentKey) {
Set<Map.Entry<String, Object>> entries = dataMap.entrySet();
for (Iterator<Map.Entry<String, Object>> iterator = entries.iterator(); iterator
.hasNext();) {
Map.Entry<String, Object> 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<String, Object> map = (Map<String, Object>) value;
parseNacosDataMap(result, map, fullKey);
continue;
}
if (resultValue.isArray()) {
Iterator<JsonNode> iterator = resultValue.elements();
while (iterator != null && iterator.hasNext()) {
JsonNode next = iterator.next();
if (null == next) {
continue;
}
parseJsonNode(jsonMap, next, fullKey);
else if (value instanceof Collection) {
int count = 0;
Collection<Object> collection = (Collection<Object>) value;
for (Object object : collection) {
parseNacosDataMap(result,
Collections.singletonMap("[" + (count++) + "]", object),
fullKey);
}
continue;
}
if (resultValue.isObject()) {
parseJsonNode(jsonMap, resultValue, fullKey);
continue;
}
jsonMap.put(fullKey, resultValue.asText());
result.put(fullKey, value);
}
}

View File

@ -50,13 +50,13 @@ public class NacosDataXmlParser extends AbstractNacosDataParser {
if (StringUtils.isEmpty(data)) {
return null;
}
Map<String, String> map = parseXml2Map(data);
Map<String, Object> map = parseXml2Map(data);
return this.generateProperties(this.reloadMap(map));
}
private Map<String, String> parseXml2Map(String xml) throws IOException {
private Map<String, Object> parseXml2Map(String xml) throws IOException {
xml = xml.replaceAll("\\r", "").replaceAll("\\n", "").replaceAll("\\t", "");
Map<String, String> map = new HashMap<>(32);
Map<String, Object> map = new HashMap<>(32);
try {
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
@ -73,7 +73,7 @@ public class NacosDataXmlParser extends AbstractNacosDataParser {
return map;
}
private void parseNodeList(NodeList nodeList, Map<String, String> map,
private void parseNodeList(NodeList nodeList, Map<String, Object> map,
String parentKey) {
if (nodeList == null || nodeList.getLength() < 1) {
return;
@ -104,7 +104,7 @@ public class NacosDataXmlParser extends AbstractNacosDataParser {
}
}
private void parseNodeAttr(NamedNodeMap nodeMap, Map<String, String> map,
private void parseNodeAttr(NamedNodeMap nodeMap, Map<String, Object> map,
String parentKey) {
if (null == nodeMap || nodeMap.getLength() < 1) {
return;