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( public NacosContextRefresher nacosContextRefresher(
NacosConfigManager nacosConfigManager, NacosConfigManager nacosConfigManager,
NacosRefreshHistory nacosRefreshHistory) { 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. // and use the new configuration if necessary.
return new NacosContextRefresher(nacosConfigManager, nacosRefreshHistory); return new NacosContextRefresher(nacosConfigManager, nacosRefreshHistory);
} }

View File

@ -17,7 +17,6 @@
package com.alibaba.cloud.nacos.client; package com.alibaba.cloud.nacos.client;
import java.util.Date; import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
@ -111,17 +110,7 @@ public class NacosPropertySourceBuilder {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private Map<String, Object> propertiesToMap(Properties properties) { private Map<String, Object> propertiesToMap(Properties properties) {
Map<String, Object> result = new HashMap<>(16); Map<String, Object> result = new HashMap<>(16);
Enumeration<String> keys = (Enumeration<String>) properties.propertyNames(); properties.forEach((k, v) -> result.put(String.valueOf(k), v));
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);
}
}
return result; return result;
} }

View File

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

View File

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

View File

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