|
@@ -15,9 +15,11 @@ import com.chelvc.framework.base.context.ApplicationContextHolder;
|
|
|
import com.chelvc.framework.common.util.AssertUtils;
|
|
|
import com.chelvc.framework.common.util.ObjectUtils;
|
|
|
import com.chelvc.framework.common.util.StringUtils;
|
|
|
+import com.chelvc.framework.common.util.ThreadUtils;
|
|
|
import com.google.common.collect.ImmutableMap;
|
|
|
import com.google.common.collect.Maps;
|
|
|
import lombok.NonNull;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
/**
|
|
|
* Nacos上下文工具类
|
|
@@ -25,6 +27,7 @@ import lombok.NonNull;
|
|
|
* @author Woody
|
|
|
* @date 2024/1/30
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
public final class NacosContextHolder {
|
|
|
/**
|
|
|
* 权限配置ID
|
|
@@ -108,16 +111,16 @@ public final class NacosContextHolder {
|
|
|
/**
|
|
|
* 解析配置信息
|
|
|
*
|
|
|
- * @param config 配置信息
|
|
|
+ * @param content 配置信息
|
|
|
* @return 键/值映射表
|
|
|
*/
|
|
|
- public static Map<String, String> analyseConfig(String config) {
|
|
|
- if (StringUtils.isEmpty(config)) {
|
|
|
+ public static Map<String, String> analyseConfig(String content) {
|
|
|
+ if (StringUtils.isEmpty(content)) {
|
|
|
return Collections.emptyMap();
|
|
|
}
|
|
|
char eq = StringUtils.EQUAL.charAt(0);
|
|
|
- String[] lines = config.split(StringUtils.ENTER);
|
|
|
- Map<String, String> configuration = Maps.newHashMapWithExpectedSize(lines.length);
|
|
|
+ String[] lines = content.split(StringUtils.ENTER);
|
|
|
+ Map<String, String> config = Maps.newHashMapWithExpectedSize(lines.length);
|
|
|
for (String line : lines) {
|
|
|
String key, value;
|
|
|
int index = line.indexOf(eq);
|
|
@@ -125,9 +128,9 @@ public final class NacosContextHolder {
|
|
|
|| StringUtils.isEmpty(value = line.substring(index + 1).trim())) {
|
|
|
continue;
|
|
|
}
|
|
|
- configuration.put(key, value);
|
|
|
+ config.put(key, value);
|
|
|
}
|
|
|
- return configuration;
|
|
|
+ return config;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -148,18 +151,8 @@ public final class NacosContextHolder {
|
|
|
*
|
|
|
* @return 配置键/值映射表
|
|
|
*/
|
|
|
- public static Map<String, String> getConfiguration() {
|
|
|
- return getConfiguration(getConfigId());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取配置
|
|
|
- *
|
|
|
- * @param id 配置ID
|
|
|
- * @return 配置键/值映射表
|
|
|
- */
|
|
|
- public static Map<String, String> getConfiguration(@NonNull String id) {
|
|
|
- return getConfiguration(id, getConfigGroup());
|
|
|
+ public static Map<String, String> getConfig() {
|
|
|
+ return getConfig(getConfigId(), getConfigGroup());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -169,7 +162,7 @@ public final class NacosContextHolder {
|
|
|
* @param group 配置分组
|
|
|
* @return 配置键/值映射表
|
|
|
*/
|
|
|
- public static Map<String, String> getConfiguration(@NonNull String id, @NonNull String group) {
|
|
|
+ public static Map<String, String> getConfig(@NonNull String id, @NonNull String group) {
|
|
|
String content;
|
|
|
ConfigService service = getConfigService();
|
|
|
try {
|
|
@@ -181,71 +174,90 @@ public final class NacosContextHolder {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 初始化配置信息
|
|
|
+ * 更新配置
|
|
|
*
|
|
|
* @param key 配置键
|
|
|
* @param value 配置值
|
|
|
*/
|
|
|
- public static void initializeConfiguration(@NonNull String key, @NonNull String value) {
|
|
|
- initializeConfiguration(getConfigId(), key, value);
|
|
|
+ public static void updateConfig(@NonNull String key, String value) {
|
|
|
+ updateConfig(getConfigId(), getConfigGroup(), key, value);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 初始化配置信息
|
|
|
+ * 更新配置
|
|
|
*
|
|
|
* @param id 配置ID
|
|
|
+ * @param group 配置分组
|
|
|
* @param key 配置键
|
|
|
* @param value 配置值
|
|
|
*/
|
|
|
- public static void initializeConfiguration(@NonNull String id, @NonNull String key, @NonNull String value) {
|
|
|
- initializeConfiguration(id, getConfigGroup(), key, value);
|
|
|
+ public static void updateConfig(@NonNull String id, @NonNull String group, @NonNull String key, String value) {
|
|
|
+ updateConfig(id, group, ImmutableMap.of(key, ObjectUtils.ifNull(value, StringUtils.EMPTY)));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 初始化配置信息
|
|
|
+ * 更新配置
|
|
|
*
|
|
|
- * @param id 配置ID
|
|
|
- * @param group 配置分组
|
|
|
- * @param key 配置键
|
|
|
- * @param value 配置值
|
|
|
+ * @param config 配置键/值映射表
|
|
|
*/
|
|
|
- public static void initializeConfiguration(@NonNull String id, @NonNull String group, @NonNull String key,
|
|
|
- @NonNull String value) {
|
|
|
- initializeConfiguration(id, group, ImmutableMap.of(key, value));
|
|
|
+ public static void updateConfig(@NonNull Map<String, String> config) {
|
|
|
+ updateConfig(getConfigId(), getConfigGroup(), config);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 初始化配置信息
|
|
|
+ * 更新配置
|
|
|
*
|
|
|
- * @param configuration 配置信息键/值映射表
|
|
|
+ * @param id 配置ID
|
|
|
+ * @param group 配置分组
|
|
|
+ * @param config 配置键/值映射表
|
|
|
*/
|
|
|
- public static void initializeConfiguration(@NonNull Map<String, String> configuration) {
|
|
|
- initializeConfiguration(getConfigId(), configuration);
|
|
|
+ public static void updateConfig(@NonNull String id, @NonNull String group, @NonNull Map<String, String> config) {
|
|
|
+ if (ObjectUtils.isEmpty(config)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ int retry = 2;
|
|
|
+ ConfigService service = getConfigService();
|
|
|
+ do {
|
|
|
+ try {
|
|
|
+ Map<String, String> original = analyseConfig(service.getConfig(id, group, 3000));
|
|
|
+ Map<String, String> update = Maps.newHashMapWithExpectedSize(original.size() + config.size());
|
|
|
+ update.putAll(original);
|
|
|
+ config.forEach((key, value) -> {
|
|
|
+ if (StringUtils.isEmpty(value)) {
|
|
|
+ update.remove(key);
|
|
|
+ } else {
|
|
|
+ update.put(key, value);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ service.publishConfig(id, group, analyseConfig(update));
|
|
|
+ } catch (NacosException e) {
|
|
|
+ if (retry == 0) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ log.warn("Nacos config publish failed: {}, {}", id, group, e);
|
|
|
+ ThreadUtils.sleep(200);
|
|
|
+ }
|
|
|
+ } while (retry-- > 0);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 初始化配置信息
|
|
|
- *
|
|
|
- * @param id 配置ID
|
|
|
- * @param configuration 配置信息键/值映射表
|
|
|
+ * 删除配置
|
|
|
*/
|
|
|
- public static void initializeConfiguration(@NonNull String id, @NonNull Map<String, String> configuration) {
|
|
|
- initializeConfiguration(id, getConfigGroup(), configuration);
|
|
|
+ public static void removeConfig() {
|
|
|
+ removeConfig(getConfigId(), getConfigGroup());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 初始化配置信息
|
|
|
+ * 删除配置
|
|
|
*
|
|
|
- * @param id 配置ID
|
|
|
- * @param group 配置分组
|
|
|
- * @param configuration 配置信息键/值映射表
|
|
|
+ * @param id 配置ID
|
|
|
+ * @param group 配置分组
|
|
|
*/
|
|
|
- public static void initializeConfiguration(@NonNull String id, @NonNull String group,
|
|
|
- @NonNull Map<String, String> configuration) {
|
|
|
+ public static void removeConfig(@NonNull String id, @NonNull String group) {
|
|
|
ConfigService service = getConfigService();
|
|
|
- String content = analyseConfig(configuration);
|
|
|
try {
|
|
|
- service.publishConfig(id, group, content);
|
|
|
+ service.removeConfig(id, group);
|
|
|
} catch (NacosException e) {
|
|
|
throw new RuntimeException(e);
|
|
|
}
|