Selaa lähdekoodia

修复多服务部署配置合并导致登录异常问题

Woody 5 päivää sitten
vanhempi
commit
a50d43abb2

+ 36 - 48
framework-base/src/main/java/com/chelvc/framework/base/config/MultipleServerConfigurer.java

@@ -2,7 +2,6 @@ package com.chelvc.framework.base.config;
 
 import java.util.List;
 import java.util.Map;
-import java.util.Properties;
 import java.util.function.Predicate;
 
 import com.chelvc.framework.base.context.ApplicationContextHolder;
@@ -10,14 +9,10 @@ import com.chelvc.framework.base.util.SpringUtils;
 import com.chelvc.framework.common.util.ObjectUtils;
 import com.chelvc.framework.common.util.StringUtils;
 import com.google.common.collect.Maps;
-import lombok.NonNull;
 import org.springframework.boot.SpringApplication;
+import org.springframework.boot.SpringApplicationRunListener;
 import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
-import org.springframework.boot.env.EnvironmentPostProcessor;
 import org.springframework.context.annotation.Import;
-import org.springframework.core.env.ConfigurableEnvironment;
-import org.springframework.core.env.PropertiesPropertySource;
-import org.springframework.core.env.PropertySource;
 import org.springframework.core.io.Resource;
 import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
 
@@ -27,26 +22,47 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
  * @author Woody
  * @date 2024/4/3
  */
-public class MultipleServerConfigurer implements WebMvcRegistrations, EnvironmentPostProcessor {
+public class MultipleServerConfigurer implements WebMvcRegistrations {
     /**
-     * 判断是否启用多服务配置
-     *
-     * @param application Spring应用实例
-     * @return true/false
+     * 多服务部署应用监听类
      */
-    private boolean isEnabled(@NonNull SpringApplication application) {
-        Class<?> main = application.getMainApplicationClass();
-        if (main != null) {
-            Class<?>[] imports = ObjectUtils.ifNull(main.getAnnotation(Import.class), Import::value);
-            if (ObjectUtils.notEmpty(imports)) {
-                for (Class<?> clazz : imports) {
-                    if (clazz == MultipleServerConfigurer.class) {
-                        return true;
+    public static class Listener implements SpringApplicationRunListener {
+        /**
+         * 是否已初始化
+         */
+        private static volatile boolean INITIALIZED;
+
+        public Listener(SpringApplication application, String[] args) {
+            if (!INITIALIZED) {
+                synchronized (Listener.class) {
+                    if (!INITIALIZED) {
+                        INITIALIZED = true;
+                        SpringUtils.setMultipleServer(this.isMultipleServer(application));
                     }
                 }
             }
         }
-        return false;
+
+        /**
+         * 判断是否是多服务配置
+         *
+         * @param application Spring应用实例
+         * @return true/false
+         */
+        private boolean isMultipleServer(SpringApplication application) {
+            Class<?> main = application.getMainApplicationClass();
+            if (main != null) {
+                Class<?>[] imports = ObjectUtils.ifNull(main.getAnnotation(Import.class), Import::value);
+                if (ObjectUtils.notEmpty(imports)) {
+                    for (Class<?> clazz : imports) {
+                        if (clazz == MultipleServerConfigurer.class) {
+                            return true;
+                        }
+                    }
+                }
+            }
+            return false;
+        }
     }
 
     @Override
@@ -63,32 +79,4 @@ public class MultipleServerConfigurer implements WebMvcRegistrations, Environmen
         mapping.setPathPrefixes(prefixes);
         return mapping;
     }
-
-    @Override
-    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
-        if (!this.isEnabled(application)) {
-            return;
-        }
-
-        Properties properties = null;
-        Class<?> main = application.getMainApplicationClass();
-        List<Resource> resources = SpringUtils.getPropertyResources(environment);
-        for (Resource resource : resources) {
-            // 排除当前应用配置
-            if (main != null && SpringUtils.isResourceClass(resource, main)) {
-                continue;
-            }
-
-            // 合并其他服务配置
-            if (properties == null) {
-                properties = SpringUtils.getResourceProperties(resource);
-            } else {
-                properties.putAll(SpringUtils.getResourceProperties(resource));
-            }
-        }
-        if (ObjectUtils.notEmpty(properties)) {
-            PropertySource<?> source = new PropertiesPropertySource("multipleServerProperties", properties);
-            environment.getPropertySources().addLast(source);
-        }
-    }
 }

+ 0 - 21
framework-base/src/main/java/com/chelvc/framework/base/context/ApplicationContextHolder.java

@@ -15,7 +15,6 @@ import java.util.Set;
 import java.util.function.Consumer;
 import java.util.function.Function;
 
-import com.chelvc.framework.base.config.MultipleServerConfigurer;
 import com.chelvc.framework.base.util.SpringUtils;
 import com.chelvc.framework.common.model.Paging;
 import com.chelvc.framework.common.model.Period;
@@ -137,26 +136,6 @@ public class ApplicationContextHolder implements EnvironmentAware, ApplicationCo
         return Boolean.TRUE.equals(bool) || Objects.equals(getProfile(), "prod");
     }
 
-    /**
-     * 判断是否是多服务部署
-     *
-     * @return true/false
-     */
-    public static boolean isMultipleServer() {
-        ApplicationContext applicationContext = getApplicationContext(false);
-        return applicationContext != null && isMultipleServer(applicationContext);
-    }
-
-    /**
-     * 判断是否是多服务部署
-     *
-     * @param applicationContext 应用上下文实例
-     * @return true/false
-     */
-    public static boolean isMultipleServer(@NonNull ApplicationContext applicationContext) {
-        return applicationContext.containsBean(MultipleServerConfigurer.class.getName());
-    }
-
     /**
      * 获取应用名称
      *

+ 8 - 6
framework-base/src/main/java/com/chelvc/framework/base/interceptor/ControllerAccessInterceptor.java

@@ -65,13 +65,15 @@ public class ControllerAccessInterceptor {
             "|| @within(org.springframework.web.bind.annotation.RestController)")
     public Object logging(ProceedingJoinPoint point) throws Throwable {
         Object value = point.proceed();
-        try {
-            Logger logger = this.getLogger(point);
-            if (logger != null) {
-                LoggingContextHolder.debug(logger, SessionContextHolder.getRequest(), value);
+        if (!(point.getTarget() instanceof ErrorController)) {
+            try {
+                Logger logger = this.getLogger(point);
+                if (logger != null) {
+                    LoggingContextHolder.debug(logger, SessionContextHolder.getRequest(), value);
+                }
+            } catch (Exception e) {
+                log.error("Controller access logging failed", e);
             }
-        } catch (Exception e) {
-            log.error("Controller access logging failed", e);
         }
         return value;
     }

+ 26 - 0
framework-base/src/main/java/com/chelvc/framework/base/util/SpringUtils.java

@@ -69,6 +69,11 @@ public final class SpringUtils {
      */
     public static final String ANT_PATH_MARK = "/*";
 
+    /**
+     * 是否是多服务部署
+     */
+    private static Boolean MULTIPLE_SERVER;
+
     /**
      * 资源路径匹配器
      */
@@ -77,6 +82,27 @@ public final class SpringUtils {
     private SpringUtils() {
     }
 
+    /**
+     * 判断是否是多服务部署
+     *
+     * @return true/false
+     */
+    public static boolean isMultipleServer() {
+        return Boolean.TRUE.equals(MULTIPLE_SERVER);
+    }
+
+    /**
+     * 设置是否是多服务部署
+     *
+     * @param multiple 是否多服务部署
+     */
+    public static void setMultipleServer(boolean multiple) {
+        if (MULTIPLE_SERVER != null) {
+            throw new IllegalStateException("'MULTIPLE_SERVER' has been initialized");
+        }
+        MULTIPLE_SERVER = multiple;
+    }
+
     /**
      * 判断资源路径是否符合指定匹配模式
      *

+ 1 - 1
framework-base/src/main/resources/META-INF/spring.factories

@@ -1 +1 @@
-org.springframework.boot.env.EnvironmentPostProcessor=com.chelvc.framework.base.config.MultipleServerConfigurer
+org.springframework.boot.SpringApplicationRunListener=com.chelvc.framework.base.config.MultipleServerConfigurer.Listener

+ 7 - 0
framework-feign-circuitbreaker/src/main/java/com/chelvc/framework/feign/circuitbreaker/FeignCircuitBreakerInitializer.java

@@ -37,7 +37,14 @@ import org.springframework.util.ClassUtils;
  * @date 2025/4/14
  */
 public class FeignCircuitBreakerInitializer implements SpringApplicationRunListener {
+    /**
+     * 是否已初始化
+     */
     private static volatile boolean INITIALIZED;
+
+    /**
+     * 服务名称集合
+     */
     private static volatile Set<String> SERVERS;
 
     private final boolean enabled;

+ 2 - 2
framework-feign/src/main/java/com/chelvc/framework/feign/interceptor/FeignInvokeInterceptor.java

@@ -8,11 +8,11 @@ import java.util.Objects;
 import java.util.Set;
 import java.util.function.Predicate;
 
-import com.chelvc.framework.base.context.ApplicationContextHolder;
 import com.chelvc.framework.base.context.Session;
 import com.chelvc.framework.base.context.SessionContextHolder;
 import com.chelvc.framework.base.context.Using;
 import com.chelvc.framework.base.util.HttpUtils;
+import com.chelvc.framework.base.util.SpringUtils;
 import com.chelvc.framework.common.model.Platform;
 import com.chelvc.framework.common.model.Terminal;
 import com.chelvc.framework.common.util.ObjectUtils;
@@ -51,7 +51,7 @@ public class FeignInvokeInterceptor implements RequestInterceptor {
         // 初始化请求转发配置
         FeignProperties properties = applicationContext.getBean(FeignProperties.class);
         List<FeignProperties.Forward> forwards = Lists.newArrayList(properties.getForwards());
-        if (ApplicationContextHolder.isMultipleServer(applicationContext)
+        if (SpringUtils.isMultipleServer()
                 && forwards.stream().noneMatch(forward -> Objects.equals(forward.getSource(), "*"))) {
             FeignProperties.Forward forward = new FeignProperties.Forward();
             forward.setSource("*");

+ 1 - 1
framework-security/src/main/java/com/chelvc/framework/security/context/SecurityContextHolder.java

@@ -187,7 +187,7 @@ public final class SecurityContextHolder {
      */
     public static Set<String> getAuthorizeIgnores(@NonNull ApplicationContext applicationContext) {
         Set<String> ignores = Sets.newHashSet();
-        boolean multiple = ApplicationContextHolder.isMultipleServer(applicationContext);
+        boolean multiple = SpringUtils.isMultipleServer();
         List<Resource> resources = multiple ?
                 SpringUtils.getPropertyResources(applicationContext.getEnvironment()) : Collections.emptyList();
         SpringUtils.getControllers(applicationContext).forEach(controller -> {