|
@@ -2,9 +2,14 @@ package com.chelvc.framework.nacos.context;
|
|
|
|
|
|
import java.util.Collections;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Properties;
|
|
|
import java.util.StringTokenizer;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
+import com.alibaba.boot.nacos.config.properties.NacosConfigProperties;
|
|
|
+import com.alibaba.nacos.api.NacosFactory;
|
|
|
+import com.alibaba.nacos.api.PropertyKeyConst;
|
|
|
+import com.alibaba.nacos.api.common.Constants;
|
|
|
import com.alibaba.nacos.api.config.ConfigService;
|
|
|
import com.alibaba.nacos.api.exception.NacosException;
|
|
|
import com.chelvc.framework.base.context.ApplicationContextHolder;
|
|
@@ -13,7 +18,6 @@ import com.chelvc.framework.common.util.StringUtils;
|
|
|
import com.google.common.collect.ImmutableMap;
|
|
|
import com.google.common.collect.Maps;
|
|
|
import lombok.NonNull;
|
|
|
-import org.springframework.core.env.Environment;
|
|
|
|
|
|
/**
|
|
|
* Nacos上下文工具类
|
|
@@ -22,26 +26,11 @@ import org.springframework.core.env.Environment;
|
|
|
* @date 2024/1/30
|
|
|
*/
|
|
|
public final class NacosContextHolder {
|
|
|
- /**
|
|
|
- * Nacos配置ID
|
|
|
- */
|
|
|
- private static String ID;
|
|
|
-
|
|
|
- /**
|
|
|
- * Nacos配置组
|
|
|
- */
|
|
|
- private static String GROUP;
|
|
|
-
|
|
|
/**
|
|
|
* 配置中心业务处理对象实例
|
|
|
*/
|
|
|
private static ConfigService CONFIG_SERVICE;
|
|
|
|
|
|
- /**
|
|
|
- * Nacos默认分组
|
|
|
- */
|
|
|
- public static final String DEFAULT_GROUP = "DEFAULT_GROUP";
|
|
|
-
|
|
|
private NacosContextHolder() {
|
|
|
}
|
|
|
|
|
@@ -50,16 +39,12 @@ public final class NacosContextHolder {
|
|
|
*
|
|
|
* @return 配置ID
|
|
|
*/
|
|
|
- public static String getId() {
|
|
|
- if (ID == null) {
|
|
|
- synchronized (NacosContextHolder.class) {
|
|
|
- Environment environment = ApplicationContextHolder.getEnvironment();
|
|
|
- if (StringUtils.isEmpty(ID = environment.getProperty("nacos.config.id"))) {
|
|
|
- ID = environment.getRequiredProperty("spring.application.name");
|
|
|
- }
|
|
|
- }
|
|
|
+ public static String getConfigId() {
|
|
|
+ String id = ApplicationContextHolder.getProperty("nacos.config.id");
|
|
|
+ if (StringUtils.isEmpty(id)) {
|
|
|
+ return ApplicationContextHolder.getRequiredProperty("spring.application.name");
|
|
|
}
|
|
|
- return ID;
|
|
|
+ return id;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -67,15 +52,8 @@ public final class NacosContextHolder {
|
|
|
*
|
|
|
* @return 配置分组
|
|
|
*/
|
|
|
- public static String getGroup() {
|
|
|
- if (GROUP == null) {
|
|
|
- synchronized (NacosContextHolder.class) {
|
|
|
- if (GROUP == null) {
|
|
|
- GROUP = ApplicationContextHolder.getProperty("spring.cloud.nacos.config.group", DEFAULT_GROUP);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return GROUP;
|
|
|
+ public static String getConfigGroup() {
|
|
|
+ return ApplicationContextHolder.getProperty("nacos.config.group", Constants.DEFAULT_GROUP);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -87,7 +65,34 @@ public final class NacosContextHolder {
|
|
|
if (CONFIG_SERVICE == null) {
|
|
|
synchronized (ConfigService.class) {
|
|
|
if (CONFIG_SERVICE == null) {
|
|
|
- CONFIG_SERVICE = ApplicationContextHolder.getBean(ConfigService.class);
|
|
|
+ NacosConfigProperties properties = ApplicationContextHolder.getBean(NacosConfigProperties.class);
|
|
|
+ Properties configuration = new Properties();
|
|
|
+ if (StringUtils.notEmpty(properties.getServerAddr())) {
|
|
|
+ configuration.put(PropertyKeyConst.SERVER_ADDR, properties.getServerAddr());
|
|
|
+ }
|
|
|
+ if (StringUtils.notEmpty(properties.getContextPath())) {
|
|
|
+ configuration.put(PropertyKeyConst.CONTEXT_PATH, properties.getContextPath());
|
|
|
+ }
|
|
|
+ if (StringUtils.notEmpty(properties.getEncode())) {
|
|
|
+ configuration.put(PropertyKeyConst.ENCODE, properties.getEncode());
|
|
|
+ }
|
|
|
+ if (StringUtils.notEmpty(properties.getEndpoint())) {
|
|
|
+ configuration.put(PropertyKeyConst.ENDPOINT, properties.getEndpoint());
|
|
|
+ }
|
|
|
+ if (StringUtils.notEmpty(properties.getNamespace())) {
|
|
|
+ configuration.put(PropertyKeyConst.NAMESPACE, properties.getNamespace());
|
|
|
+ }
|
|
|
+ if (StringUtils.notEmpty(properties.getAccessKey())) {
|
|
|
+ configuration.put(PropertyKeyConst.ACCESS_KEY, properties.getAccessKey());
|
|
|
+ }
|
|
|
+ if (StringUtils.notEmpty(properties.getSecretKey())) {
|
|
|
+ configuration.put(PropertyKeyConst.SECRET_KEY, properties.getSecretKey());
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ CONFIG_SERVICE = NacosFactory.createConfigService(configuration);
|
|
|
+ } catch (NacosException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -100,7 +105,7 @@ public final class NacosContextHolder {
|
|
|
* @return 配置键/值映射表
|
|
|
*/
|
|
|
public static Map<String, String> getConfiguration() {
|
|
|
- return getConfiguration(getId());
|
|
|
+ return getConfiguration(getConfigId());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -110,7 +115,7 @@ public final class NacosContextHolder {
|
|
|
* @return 配置键/值映射表
|
|
|
*/
|
|
|
public static Map<String, String> getConfiguration(@NonNull String id) {
|
|
|
- return getConfiguration(id, getGroup());
|
|
|
+ return getConfiguration(id, getConfigGroup());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -151,7 +156,7 @@ public final class NacosContextHolder {
|
|
|
* @param value 配置值
|
|
|
*/
|
|
|
public static void initializeConfiguration(@NonNull String key, @NonNull String value) {
|
|
|
- initializeConfiguration(getId(), key, value);
|
|
|
+ initializeConfiguration(getConfigId(), key, value);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -162,7 +167,7 @@ public final class NacosContextHolder {
|
|
|
* @param value 配置值
|
|
|
*/
|
|
|
public static void initializeConfiguration(@NonNull String id, @NonNull String key, @NonNull String value) {
|
|
|
- initializeConfiguration(id, getGroup(), key, value);
|
|
|
+ initializeConfiguration(id, getConfigGroup(), key, value);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -184,7 +189,7 @@ public final class NacosContextHolder {
|
|
|
* @param configuration 配置信息键/值映射表
|
|
|
*/
|
|
|
public static void initializeConfiguration(@NonNull Map<String, String> configuration) {
|
|
|
- initializeConfiguration(getId(), configuration);
|
|
|
+ initializeConfiguration(getConfigId(), configuration);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -194,7 +199,7 @@ public final class NacosContextHolder {
|
|
|
* @param configuration 配置信息键/值映射表
|
|
|
*/
|
|
|
public static void initializeConfiguration(@NonNull String id, @NonNull Map<String, String> configuration) {
|
|
|
- initializeConfiguration(id, getGroup(), configuration);
|
|
|
+ initializeConfiguration(id, getConfigGroup(), configuration);
|
|
|
}
|
|
|
|
|
|
/**
|