public void init() throws NacosException {
String baseUrl = "http://nacos地址:8848/nacos/v1/cs/configs"
try {
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baseUrl)
.queryParam("dataId", dataId)
.queryParam("group", group)
.queryParam("tenant", namespace)
String result = restTemplate.getForEntity(builder.toUriString(), String.class).getBody()
Map<String, Object> configMap = parseConfigString(result)
System.out.println(configMap)
} catch (Exception e) {
e.printStackTrace()
}
}
public static Map<String, Object> parseConfigString(String configStr) {
String[] lines = configStr.split("\r?\n")
Map<String, Object> resultMap = new HashMap<>()
Map<String, Object> currentMap = resultMap
for (String line : lines) {
String trimmedLine = line.trim()
if (trimmedLine.isEmpty() || trimmedLine.startsWith("
continue
}
if (trimmedLine.endsWith(":")) {
// Key line
String key = trimmedLine.substring(0, trimmedLine.length() - 1)
currentMap.put(key, new HashMap<>())
currentMap = (Map<String, Object>) currentMap.get(key)
} else {
// Value line
int colonIndex = trimmedLine.indexOf(":")
String key = trimmedLine.substring(0, colonIndex).trim()
String value = trimmedLine.substring(colonIndex + 1).trim()
currentMap.put(key, value)
}
}
return resultMap
}