Bläddra i källkod

优化Nacos配置中心处理逻辑

woody 1 år sedan
förälder
incheckning
dad04b3300

+ 16 - 35
framework-nacos/src/main/java/com/chelvc/framework/nacos/config/NacosConfigConfigurer.java

@@ -1,15 +1,14 @@
 package com.chelvc.framework.nacos.config;
 
-import java.util.Properties;
+import java.util.Collections;
 
-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.config.ConfigService;
-import com.alibaba.nacos.api.exception.NacosException;
 import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
-import com.chelvc.framework.common.util.StringUtils;
-import org.springframework.context.annotation.Bean;
+import com.alibaba.nacos.spring.context.event.config.NacosConfigReceivedEvent;
+import lombok.RequiredArgsConstructor;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationListener;
 import org.springframework.context.annotation.Configuration;
 
 /**
@@ -19,32 +18,14 @@ import org.springframework.context.annotation.Configuration;
  * @date 2024/1/30
  */
 @Configuration
-@NacosPropertySource(dataId = "${nacos.config.id:${spring.application.name}}", autoRefreshed = true)
-public class NacosConfigConfigurer {
-    @Bean
-    public ConfigService configService(NacosConfigProperties configuration) throws NacosException {
-        Properties properties = new Properties();
-        if (StringUtils.notEmpty(configuration.getServerAddr())) {
-            properties.put(PropertyKeyConst.SERVER_ADDR, configuration.getServerAddr());
-        }
-        if (StringUtils.notEmpty(configuration.getContextPath())) {
-            properties.put(PropertyKeyConst.CONTEXT_PATH, configuration.getContextPath());
-        }
-        if (StringUtils.notEmpty(configuration.getEncode())) {
-            properties.put(PropertyKeyConst.ENCODE, configuration.getEncode());
-        }
-        if (StringUtils.notEmpty(configuration.getEndpoint())) {
-            properties.put(PropertyKeyConst.ENDPOINT, configuration.getEndpoint());
-        }
-        if (StringUtils.notEmpty(configuration.getNamespace())) {
-            properties.put(PropertyKeyConst.NAMESPACE, configuration.getNamespace());
-        }
-        if (StringUtils.notEmpty(configuration.getAccessKey())) {
-            properties.put(PropertyKeyConst.ACCESS_KEY, configuration.getAccessKey());
-        }
-        if (StringUtils.notEmpty(configuration.getSecretKey())) {
-            properties.put(PropertyKeyConst.SECRET_KEY, configuration.getSecretKey());
-        }
-        return NacosFactory.createConfigService(properties);
+@RequiredArgsConstructor(onConstructor = @__(@Autowired))
+@NacosPropertySource(dataId = "${nacos.config.id:${spring.application.name}}",
+        groupId = "${nacos.config.group:DEFAULT_GROUP}", autoRefreshed = true)
+public class NacosConfigConfigurer implements ApplicationListener<NacosConfigReceivedEvent> {
+    private final ApplicationContext applicationContext;
+
+    @Override
+    public void onApplicationEvent(NacosConfigReceivedEvent event) {
+        this.applicationContext.publishEvent(new EnvironmentChangeEvent(Collections.emptySet()));
     }
 }

+ 46 - 41
framework-nacos/src/main/java/com/chelvc/framework/nacos/context/NacosContextHolder.java

@@ -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);
     }
 
     /**

+ 0 - 4
framework-security/pom.xml

@@ -36,9 +36,5 @@
             <groupId>org.springframework.security</groupId>
             <artifactId>spring-security-config</artifactId>
         </dependency>
-        <dependency>
-            <groupId>com.alibaba.boot</groupId>
-            <artifactId>nacos-config-spring-boot-autoconfigure</artifactId>
-        </dependency>
     </dependencies>
 </project>