通过Java修改consul配置(保留注释、保留缩进)
直接上代码了,找了很久也没找到保留注释的三方包,snakeyaml 的缩进一直也有问题,就自己写了个正则方式的
consul也没有相关接口,只接受整个的
传key和value,替换相应value值,
大佬有更好的方法希望能告诉我
<dependency>
<groupId>com.orbitz.consul</groupId>
<artifactId>consul-client</artifactId>
<version>1.5.3</version>
</dependency>
package com.qpaas.migration;
import com.orbitz.consul.Consul;
import com.orbitz.consul.KeyValueClient;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main3 {
public static void main(String[] args) {
String consulUrl = "xx";
String consulKey = "xx";
// String propertyKey = "server.port";
// String newValue = "8081";
// String yamlContent = "server:\n port: 28082 #xxxx\nspring:\n mail:\n host: 3333\n username: 1111\n password: 222";
try {
// 构建Consul客户端
Consul consul = Consul.builder().withUrl(consulUrl).build();
KeyValueClient kvClient = consul.keyValueClient();
// 读取当前的YAML值
String currentYaml = kvClient.getValueAsString(consulKey)
.orElseThrow(() -> new RuntimeException("无法找到配置项:" + consulKey));
// 示例1: 修改server.port的值为9091
String keyToFind1 = "server.port";
String newValue1 = "9091";
currentYaml = updateYml(keyToFind1, newValue1, currentYaml);
String keyToFind2 = "spring.mail.host";
String newValue2 = "xxxxx";
currentYaml = updateYml(keyToFind2, newValue2, currentYaml);
// 输出更新后的YAML内容
System.out.println(currentYaml);
// 更新Consul中的值
kvClient.putValue(consulKey, currentYaml);
System.out.println("Value updated successfully!");
} catch (Exception e) {
System.err.println("An error occurred while updating the value in Consul: " + e.getMessage());
}
}
public static String updateYml(String keyToFind, String newValue, String yamlContent) {
String pathRegex1 = keyToRegex(keyToFind);
Pattern pathPattern1 = Pattern.compile(pathRegex1, Pattern.MULTILINE);
Matcher pathMatcher1 = pathPattern1.matcher(yamlContent);
String pathRegex2 = buildRegexForKey(keyToFind);
Pattern pathPattern2 = Pattern.compile(pathRegex2, Pattern.MULTILINE);
Matcher pathMatcher2 = pathPattern2.matcher(yamlContent);
if (pathMatcher1.find() && pathMatcher2.find()) {
String replacement1 = pathMatcher2.group(0);
String replacement2 = pathMatcher1.group(0) + " " + newValue;
yamlContent = yamlContent.replaceFirst(replacement1, replacement2);
System.out.println("The YAML content has been updated. The value of " + keyToFind + " is now: " + newValue);
} else {
System.out.println("The key " + keyToFind + " was not found in the YAML content.");
}
return yamlContent;
}
public static String keyToRegex(String key) {
String[] parts = key.split("\\.");
StringBuilder regexBuilder = new StringBuilder();
for (String part : parts) {
regexBuilder.append("\\b").append(part).append("\\b\\s*:"); // Match the key and following colon
if (!part.equals(parts[parts.length - 1])) {
regexBuilder.append("\\s*\\n\\s*"); // Add newline and indentation for all parts except the last one
}
}
return regexBuilder.toString();
}
private static String buildRegexForKey(String key) {
String[] parts = key.split("\\.");
StringBuilder regexBuilder = new StringBuilder();
// 匹配每个部分的键和冒号,考虑到YAML的缩进
for (String part : parts) {
if (regexBuilder.length() > 0) {
// 如果不是第一个部分,添加缩进匹配(两个空格)
regexBuilder.append("\\n\\s*");
}
regexBuilder.append("\\b").append(part).append(":\\s*");
}
regexBuilder.append("[^\\s\\n]+"); // 匹配值直到下一个空白字符或换行符
return regexBuilder.toString();
}
}
package com.qpaas.migration; import com.orbitz.consul.Consul; import com.orbitz.consul.KeyValueClient; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main3 { public static void main(String[] args) { String consulUrl = "xx"; String consulKey = "xx"; // String propertyKey = "server.port"; // String newValue = "8081"; // String yamlContent = "server:\n port: 28082 #xxxx\nspring:\n mail:\n host: 3333\n username: 1111\n password: 222"; try { // 构建Consul客户端 Consul consul = Consul.builder().withUrl(consulUrl).build(); KeyValueClient kvClient = consul.keyValueClient(); // 读取当前的YAML值 String currentYaml = kvClient.getValueAsString(consulKey) .orElseThrow(() -> new RuntimeException("无法找到配置项:" + consulKey)); // 示例1: 修改server.port的值为9091 String keyToFind1 = "server.port"; String newValue1 = "9091"; currentYaml = updateYml(keyToFind1, newValue1, currentYaml); String keyToFind2 = "spring.mail.host"; String newValue2 = "xxxxx"; currentYaml = updateYml(keyToFind2, newValue2, currentYaml); // 输出更新后的YAML内容 System.out.println(currentYaml); // 更新Consul中的值 kvClient.putValue(consulKey, currentYaml); System.out.println("Value updated successfully!"); } catch (Exception e) { System.err.println("An error occurred while updating the value in Consul: " + e.getMessage()); } } public static String updateYml(String keyToFind, String newValue, String yamlContent) { String pathRegex1 = keyToRegex(keyToFind); Pattern pathPattern1 = Pattern.compile(pathRegex1, Pattern.MULTILINE); Matcher pathMatcher1 = pathPattern1.matcher(yamlContent); String pathRegex2 = buildRegexForKey(keyToFind); Pattern pathPattern2 = Pattern.compile(pathRegex2, Pattern.MULTILINE); Matcher pathMatcher2 = pathPattern2.matcher(yamlContent); if (pathMatcher1.find() && pathMatcher2.find()) { String replacement1 = pathMatcher2.group(0); String replacement2 = pathMatcher1.group(0) + " " + newValue; yamlContent = yamlContent.replaceFirst(replacement1, replacement2); System.out.println("The YAML content has been updated. The value of " + keyToFind + " is now: " + newValue); } else { System.out.println("The key " + keyToFind + " was not found in the YAML content."); } return yamlContent; } public static String keyToRegex(String key) { String[] parts = key.split("\\."); StringBuilder regexBuilder = new StringBuilder(); for (String part : parts) { regexBuilder.append("\\b").append(part).append("\\b\\s*:"); // Match the key and following colon if (!part.equals(parts[parts.length - 1])) { regexBuilder.append("\\s*\\n\\s*"); // Add newline and indentation for all parts except the last one } } return regexBuilder.toString(); } private static String buildRegexForKey(String key) { String[] parts = key.split("\\."); StringBuilder regexBuilder = new StringBuilder(); // 匹配每个部分的键和冒号,考虑到YAML的缩进 for (String part : parts) { if (regexBuilder.length() > 0) { // 如果不是第一个部分,添加缩进匹配(两个空格) regexBuilder.append("\\n\\s*"); } regexBuilder.append("\\b").append(part).append(":\\s*"); } regexBuilder.append("[^\\s\\n]+"); // 匹配值直到下一个空白字符或换行符 return regexBuilder.toString(); } }
By:努力向前的菜B