|
@@ -2,7 +2,6 @@ package com.chelvc.framework.base.config;
|
|
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
-import java.util.Properties;
|
|
|
|
import java.util.function.Predicate;
|
|
import java.util.function.Predicate;
|
|
|
|
|
|
import com.chelvc.framework.base.context.ApplicationContextHolder;
|
|
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.ObjectUtils;
|
|
import com.chelvc.framework.common.util.StringUtils;
|
|
import com.chelvc.framework.common.util.StringUtils;
|
|
import com.google.common.collect.Maps;
|
|
import com.google.common.collect.Maps;
|
|
-import lombok.NonNull;
|
|
|
|
import org.springframework.boot.SpringApplication;
|
|
import org.springframework.boot.SpringApplication;
|
|
|
|
+import org.springframework.boot.SpringApplicationRunListener;
|
|
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
|
|
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
|
|
-import org.springframework.boot.env.EnvironmentPostProcessor;
|
|
|
|
import org.springframework.context.annotation.Import;
|
|
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.core.io.Resource;
|
|
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
|
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
|
|
|
|
|
@@ -27,26 +22,47 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
|
|
* @author Woody
|
|
* @author Woody
|
|
* @date 2024/4/3
|
|
* @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
|
|
@Override
|
|
@@ -63,32 +79,4 @@ public class MultipleServerConfigurer implements WebMvcRegistrations, Environmen
|
|
mapping.setPathPrefixes(prefixes);
|
|
mapping.setPathPrefixes(prefixes);
|
|
return mapping;
|
|
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);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
}
|
|
}
|