diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/config/strategy/LoadYamlStrategy.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/config/strategy/LoadYamlStrategy.java index f41df38f2e..8d1b50c9f7 100644 --- a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/config/strategy/LoadYamlStrategy.java +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/config/strategy/LoadYamlStrategy.java @@ -126,9 +126,15 @@ private Map fixEntry(Map typeMap, Class> cls) { Map fixedTypeMap = fixEntry(typeMap, cls.getSuperclass()); for (Field field : cls.getDeclaredFields()) { final ConfigFieldKey configFieldKey = field.getAnnotation(ConfigFieldKey.class); - final String fieldKey = configFieldKey == null ? field.getName() : configFieldKey.value(); - final Object subTypeVal = - configFieldKey == null ? fixedTypeMap.get(fieldKey) : fixedTypeMap.remove(fieldKey); + final String fieldKey; + final Object subTypeVal; + if (configFieldKey != null && fixedTypeMap.get(configFieldKey.value()) != null) { + subTypeVal = fixedTypeMap.remove(configFieldKey.value()); + fieldKey = configFieldKey.value(); + } else { + subTypeVal = fixedTypeMap.get(field.getName()); + fieldKey = field.getName(); + } if (subTypeVal == null) { continue; } diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/config/utils/ConfigValueUtil.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/config/utils/ConfigValueUtil.java index 0ee9801dc6..c5f089e562 100644 --- a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/config/utils/ConfigValueUtil.java +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/config/utils/ConfigValueUtil.java @@ -33,6 +33,8 @@ import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * 对配置中参数值进行处理的工具 @@ -62,6 +64,11 @@ public class ConfigValueUtil { */ private static final int MAP_KV_LEN = 2; + /** + * 驼峰匹配pattern + */ + private static final Pattern PATTERN = Pattern.compile("[A-Z]"); + /** * 配置键格式化器, 针对不同环境变量格式读取 *
若读取环境变量 service.meta.applicationName, 则会尝试从下面的变量进行读取, 否则取默认值
@@ -87,6 +94,20 @@ public class ConfigValueUtil { key -> key.toLowerCase(Locale.ROOT).replace('.', '-'), }; + /** + * 配置参考值获取表达式 + *优先级: 启动配置 > 环境变量 > 启动参数 > 配置文件
+ */ + private static final List优先级: 启动配置 > 环境变量 > 启动参数 > 配置文件
@@ -296,7 +317,7 @@ public static String fixValue(String configKey, String configVal, Map修正不同配置格式获取值, 含'-','_','.',大写以及小写
+ * + * @param key 键 + * @param configVal 配置值 + * @return 最终配置参考值 + */ + private static String getValByFixedKey(String key, String configVal, Map若需读取环境变量,service.meta.applicationName先处理为service.meta.application.name再 + * 由KeyFormatter处理后读取环境变量
+ * + * @param key 需处理的键 + * @return 处理后的键 + */ + private static String transFromCamel(String key) { + Matcher matcher = PATTERN.matcher(key); + StringBuffer sb = new StringBuffer(); + while (matcher.find()) { + matcher.appendReplacement(sb, "." + matcher.group(0).toLowerCase(Locale.ROOT)); + } + matcher.appendTail(sb); + return sb.toString(); + } + + /** + * 配置键中划线格式化 + *若需读取环境变量 service.meta.application-name, 则先处理为service.meta.application.name再 + * 由KeyFormatter处理后读取环境变量
+ * + * @param key 需处理的键 + * @return 处理后的键 + */ + private static String transFromMiddleLine(String key) { + return key.replace("-", "."); + } + /** * 通过环境变量或者系统变量获取 环境变量 > 系统变量 * @@ -361,6 +462,25 @@ interface ValueFixFunction添加@ConfigFieldKey("int-field")后,config.yaml中配置demo.test.intField或demo.test.int-field皆可
+ *如果不添加注解,config.yaml中只能配置为demo.test.intField
*/ + @ConfigFieldKey("int-field") private int intField; /**