|
@@ -1,19 +1,15 @@
|
|
|
package com.zd.common.core.launch;
|
|
package com.zd.common.core.launch;
|
|
|
|
|
|
|
|
import com.zd.common.core.launch.constant.LauncherConstant;
|
|
import com.zd.common.core.launch.constant.LauncherConstant;
|
|
|
-import com.zd.common.core.launch.service.LauncherService;
|
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.boot.builder.SpringApplicationBuilder;
|
|
import org.springframework.boot.builder.SpringApplicationBuilder;
|
|
|
import org.springframework.context.ConfigurableApplicationContext;
|
|
import org.springframework.context.ConfigurableApplicationContext;
|
|
|
-import org.springframework.context.annotation.PropertySource;
|
|
|
|
|
import org.springframework.core.env.*;
|
|
import org.springframework.core.env.*;
|
|
|
import org.springframework.util.Assert;
|
|
import org.springframework.util.Assert;
|
|
|
import org.springframework.util.StringUtils;
|
|
import org.springframework.util.StringUtils;
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
import java.util.function.Function;
|
|
import java.util.function.Function;
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* <p>启动类</p>
|
|
* <p>启动类</p>
|
|
@@ -26,12 +22,31 @@ public class ZdStartApplication {
|
|
|
private static final Logger log = LoggerFactory.getLogger(ZdStartApplication.class);
|
|
private static final Logger log = LoggerFactory.getLogger(ZdStartApplication.class);
|
|
|
|
|
|
|
|
public ZdStartApplication() {}
|
|
public ZdStartApplication() {}
|
|
|
- public static ConfigurableApplicationContext run(String appName, Class<?> source, String... args) {
|
|
|
|
|
- SpringApplicationBuilder builder = createSpringApplicationBuilder(appName, source, args);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 运行服务
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param appName -- 应用名
|
|
|
|
|
+ * @param isShareDataSource 是否共用数据库,ture-是,fasle-否
|
|
|
|
|
+ * @param source
|
|
|
|
|
+ * @param args
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static ConfigurableApplicationContext run(String appName, Boolean isShareDataSource, Class<?> source, String... args) {
|
|
|
|
|
+ SpringApplicationBuilder builder = createSpringApplicationBuilder(appName, isShareDataSource, source, args);
|
|
|
return builder.run(args);
|
|
return builder.run(args);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static SpringApplicationBuilder createSpringApplicationBuilder(String appName, Class<?> source, String... args) {
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 创建ApplicationBuilder
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param appName -- 应用名
|
|
|
|
|
+ * @param isShareDataSource 是否共用数据库,ture-是,fasle-否
|
|
|
|
|
+ * @param source
|
|
|
|
|
+ * @param args
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static SpringApplicationBuilder createSpringApplicationBuilder(String appName, Boolean isShareDataSource, Class<?> source, String... args) {
|
|
|
Assert.hasText(appName, "[appName]服务名不能为空");
|
|
Assert.hasText(appName, "[appName]服务名不能为空");
|
|
|
ConfigurableEnvironment environment = new StandardEnvironment();
|
|
ConfigurableEnvironment environment = new StandardEnvironment();
|
|
|
MutablePropertySources propertySources = environment.getPropertySources();
|
|
MutablePropertySources propertySources = environment.getPropertySources();
|
|
@@ -58,7 +73,7 @@ public class ZdStartApplication {
|
|
|
|
|
|
|
|
String startJarPath = Objects.requireNonNull(ZdStartApplication.class.getResource("/")).getPath().split("!")[0];
|
|
String startJarPath = Objects.requireNonNull(ZdStartApplication.class.getResource("/")).getPath().split("!")[0];
|
|
|
String activePros = joinFun.apply(activeProfileList.toArray());
|
|
String activePros = joinFun.apply(activeProfileList.toArray());
|
|
|
- System.out.printf("----启动中,读取到的环境变量:[%s],jar地址:[%s]----%n", activePros, startJarPath);
|
|
|
|
|
|
|
+ System.out.printf("============ 启动中,读取到的环境变量:[%s],jar地址:[%s]============ %n", activePros, startJarPath);
|
|
|
Properties props = System.getProperties();
|
|
Properties props = System.getProperties();
|
|
|
props.setProperty("spring.application.name", appName);
|
|
props.setProperty("spring.application.name", appName);
|
|
|
props.setProperty("spring.profiles.active", profile);
|
|
props.setProperty("spring.profiles.active", profile);
|
|
@@ -68,29 +83,24 @@ public class ZdStartApplication {
|
|
|
props.setProperty("service.is-local", String.valueOf(isLocalDev()));
|
|
props.setProperty("service.is-local", String.valueOf(isLocalDev()));
|
|
|
props.setProperty("service.dev-mode", profile.equals("pro") ? "false" : "true");
|
|
props.setProperty("service.dev-mode", profile.equals("pro") ? "false" : "true");
|
|
|
props.setProperty("spring.main.allow-bean-definition-overriding", "true");
|
|
props.setProperty("spring.main.allow-bean-definition-overriding", "true");
|
|
|
|
|
+ //获取nacos服务地址
|
|
|
String addr = LauncherConstant.nacosAddr(profile);
|
|
String addr = LauncherConstant.nacosAddr(profile);
|
|
|
props.setProperty("spring.cloud.nacos.discovery.server-addr", addr);
|
|
props.setProperty("spring.cloud.nacos.discovery.server-addr", addr);
|
|
|
props.setProperty("spring.cloud.nacos.config.server-addr", addr);
|
|
props.setProperty("spring.cloud.nacos.config.server-addr", addr);
|
|
|
- /* props.setProperty("spring.cloud.nacos.discovery.server-addr", "127.0.0.1:8848");
|
|
|
|
|
- props.setProperty("spring.cloud.nacos.config.server-addr", "127.0.0.1:8848");*/
|
|
|
|
|
-
|
|
|
|
|
|
|
+ //获取nacos命名空间
|
|
|
String nameSpace = LauncherConstant.nacosNameSpace(profile);
|
|
String nameSpace = LauncherConstant.nacosNameSpace(profile);
|
|
|
if (nameSpace != null && !"".equals(nameSpace)) {
|
|
if (nameSpace != null && !"".equals(nameSpace)) {
|
|
|
props.setProperty("spring.cloud.nacos.discovery.namespace", nameSpace);
|
|
props.setProperty("spring.cloud.nacos.discovery.namespace", nameSpace);
|
|
|
props.setProperty("spring.cloud.nacos.config.namespace", nameSpace);
|
|
props.setProperty("spring.cloud.nacos.config.namespace", nameSpace);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- String prefixName = LauncherConstant.DATA_IS_SINGLE?LauncherConstant.NACOS_CONFIG_NAME:appName;
|
|
|
|
|
|
|
+ String prefixName = (isShareDataSource == null || isShareDataSource)?LauncherConstant.NACOS_CONFIG_NAME:appName;
|
|
|
props.setProperty("spring.cloud.nacos.config.prefix", prefixName);
|
|
props.setProperty("spring.cloud.nacos.config.prefix", prefixName);
|
|
|
props.setProperty("spring.cloud.nacos.config.file-extension", "yml");
|
|
props.setProperty("spring.cloud.nacos.config.file-extension", "yml");
|
|
|
props.setProperty("spring.cloud.nacos.config.shared-configs", "application.yml");
|
|
props.setProperty("spring.cloud.nacos.config.shared-configs", "application.yml");
|
|
|
//props.setProperty("spring.cloud.nacos.config.extension-configs[0].data-id", "application.yml");
|
|
//props.setProperty("spring.cloud.nacos.config.extension-configs[0].data-id", "application.yml");
|
|
|
|
|
|
|
|
- log.info("------ nacos配置信息 addr:[{}]---namespace:[{}],prefixName:[{}]---", new Object[]{addr, nameSpace, prefixName});
|
|
|
|
|
- List<LauncherService> launcherList = new ArrayList<>();
|
|
|
|
|
- ServiceLoader.load(LauncherService.class).forEach(launcherList::add);
|
|
|
|
|
- (launcherList.stream().sorted(Comparator.comparing(LauncherService::getOrder)).collect(Collectors.toList()))
|
|
|
|
|
- .forEach((launcherService) -> launcherService.launcher(builder, appName, profile));
|
|
|
|
|
|
|
+ log.info("============ nacos配置信息 addr:[{}], namespace:[{}],prefixName:[{}] ===========", new Object[]{addr, nameSpace, prefixName});
|
|
|
return builder;
|
|
return builder;
|
|
|
}
|
|
}
|
|
|
|
|
|