2018-12-24 11:58:45 +08:00
|
|
|
|
/**
|
|
|
|
|
* Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
|
|
|
|
|
* <p>
|
2018-12-29 18:46:01 +08:00
|
|
|
|
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
|
2018-12-24 11:58:45 +08:00
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
* <p>
|
|
|
|
|
* http://www.gnu.org/licenses/lgpl.html
|
|
|
|
|
* <p>
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
package org.springblade.core.launch;
|
|
|
|
|
|
|
|
|
|
import org.springblade.core.launch.constant.AppConstant;
|
2019-02-05 22:25:11 +08:00
|
|
|
|
import org.springblade.core.launch.constant.NacosConstant;
|
2019-02-10 19:06:17 +08:00
|
|
|
|
import org.springblade.core.launch.constant.SentinelConstant;
|
2018-12-24 11:58:45 +08:00
|
|
|
|
import org.springblade.core.launch.service.LauncherService;
|
|
|
|
|
import org.springframework.boot.builder.SpringApplicationBuilder;
|
|
|
|
|
import org.springframework.context.ConfigurableApplicationContext;
|
|
|
|
|
import org.springframework.core.env.*;
|
|
|
|
|
import org.springframework.util.Assert;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.function.Function;
|
2019-06-17 00:15:24 +08:00
|
|
|
|
import java.util.stream.Collectors;
|
2018-12-24 11:58:45 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 项目启动器,搞定环境变量问题
|
2018-12-27 13:29:11 +08:00
|
|
|
|
*
|
2018-12-29 18:43:11 +08:00
|
|
|
|
* @author Chill
|
2018-12-24 11:58:45 +08:00
|
|
|
|
*/
|
|
|
|
|
public class BladeApplication {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create an application context
|
2018-12-29 00:52:46 +08:00
|
|
|
|
* java -jar app.jar --spring.profiles.active=prod --server.port=2333
|
2018-12-24 11:58:45 +08:00
|
|
|
|
*
|
|
|
|
|
* @param appName application name
|
|
|
|
|
* @param source The sources
|
|
|
|
|
* @return an application context created from the current state
|
|
|
|
|
*/
|
|
|
|
|
public static ConfigurableApplicationContext run(String appName, Class source, String... args) {
|
|
|
|
|
SpringApplicationBuilder builder = createSpringApplicationBuilder(appName, source, args);
|
|
|
|
|
return builder.run(args);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-04 09:34:52 +08:00
|
|
|
|
public static SpringApplicationBuilder createSpringApplicationBuilder(String appName, Class source, String... args) {
|
2024-03-13 10:34:54 +08:00
|
|
|
|
return createSpringApplicationBuilder(null,appName, source, args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 兼容tomcat等外部容器启动,使用此方法时请按照1进行工程改造
|
|
|
|
|
* <pre>1-https://sns.bladex.cn/q-6813.html</pre>
|
|
|
|
|
* <pre>2-解决https://sns.bladex.cn/q-66.html问题</pre>
|
|
|
|
|
* @param builder builder
|
|
|
|
|
* @param appName appName
|
|
|
|
|
* @param source source
|
|
|
|
|
* @param args args
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static SpringApplicationBuilder createSpringApplicationBuilder(SpringApplicationBuilder builder,String appName, Class source, String... args){
|
2018-12-24 11:58:45 +08:00
|
|
|
|
Assert.hasText(appName, "[appName]服务名不能为空");
|
|
|
|
|
// 读取环境变量,使用spring boot的规则
|
|
|
|
|
ConfigurableEnvironment environment = new StandardEnvironment();
|
|
|
|
|
MutablePropertySources propertySources = environment.getPropertySources();
|
|
|
|
|
propertySources.addFirst(new SimpleCommandLinePropertySource(args));
|
|
|
|
|
propertySources.addLast(new MapPropertySource(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, environment.getSystemProperties()));
|
|
|
|
|
propertySources.addLast(new SystemEnvironmentPropertySource(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, environment.getSystemEnvironment()));
|
|
|
|
|
// 获取配置的环境变量
|
|
|
|
|
String[] activeProfiles = environment.getActiveProfiles();
|
|
|
|
|
// 判断环境:dev、test、prod
|
|
|
|
|
List<String> profiles = Arrays.asList(activeProfiles);
|
|
|
|
|
// 预设的环境
|
2019-05-04 22:15:31 +08:00
|
|
|
|
List<String> presetProfiles = new ArrayList<>(Arrays.asList(AppConstant.DEV_CODE, AppConstant.TEST_CODE, AppConstant.PROD_CODE));
|
2018-12-24 11:58:45 +08:00
|
|
|
|
// 交集
|
|
|
|
|
presetProfiles.retainAll(profiles);
|
|
|
|
|
// 当前使用
|
|
|
|
|
List<String> activeProfileList = new ArrayList<>(profiles);
|
|
|
|
|
Function<Object[], String> joinFun = StringUtils::arrayToCommaDelimitedString;
|
2024-03-13 10:34:54 +08:00
|
|
|
|
if(builder==null){
|
|
|
|
|
// 如果builder为空 创建 此操作将启动内嵌web容器 以jar模式启动
|
|
|
|
|
builder = new SpringApplicationBuilder(source);
|
|
|
|
|
}
|
2018-12-24 11:58:45 +08:00
|
|
|
|
String profile;
|
|
|
|
|
if (activeProfileList.isEmpty()) {
|
|
|
|
|
// 默认dev开发
|
2019-05-04 22:15:31 +08:00
|
|
|
|
profile = AppConstant.DEV_CODE;
|
2018-12-24 11:58:45 +08:00
|
|
|
|
activeProfileList.add(profile);
|
|
|
|
|
builder.profiles(profile);
|
|
|
|
|
} else if (activeProfileList.size() == 1) {
|
|
|
|
|
profile = activeProfileList.get(0);
|
|
|
|
|
} else {
|
|
|
|
|
// 同时存在dev、test、prod环境时
|
|
|
|
|
throw new RuntimeException("同时存在环境变量:[" + StringUtils.arrayToCommaDelimitedString(activeProfiles) + "]");
|
|
|
|
|
}
|
|
|
|
|
String startJarPath = BladeApplication.class.getResource("/").getPath().split("!")[0];
|
|
|
|
|
String activePros = joinFun.apply(activeProfileList.toArray());
|
|
|
|
|
System.out.println(String.format("----启动中,读取到的环境变量:[%s],jar地址:[%s]----", activePros, startJarPath));
|
|
|
|
|
Properties props = System.getProperties();
|
|
|
|
|
props.setProperty("spring.application.name", appName);
|
|
|
|
|
props.setProperty("spring.profiles.active", profile);
|
|
|
|
|
props.setProperty("info.version", AppConstant.APPLICATION_VERSION);
|
|
|
|
|
props.setProperty("info.desc", appName);
|
|
|
|
|
props.setProperty("blade.env", profile);
|
|
|
|
|
props.setProperty("blade.name", appName);
|
|
|
|
|
props.setProperty("blade.is-local", String.valueOf(isLocalDev()));
|
|
|
|
|
props.setProperty("blade.dev-mode", profile.equals(AppConstant.PROD_CODE) ? "false" : "true");
|
|
|
|
|
props.setProperty("blade.service.version", AppConstant.APPLICATION_VERSION);
|
2019-03-31 22:18:59 +08:00
|
|
|
|
props.setProperty("spring.main.allow-bean-definition-overriding", "true");
|
2019-02-05 22:25:11 +08:00
|
|
|
|
props.setProperty("spring.cloud.nacos.config.prefix", NacosConstant.NACOS_CONFIG_PREFIX);
|
|
|
|
|
props.setProperty("spring.cloud.nacos.config.file-extension", NacosConstant.NACOS_CONFIG_FORMAT);
|
2019-02-10 19:06:17 +08:00
|
|
|
|
props.setProperty("spring.cloud.sentinel.transport.dashboard", SentinelConstant.SENTINEL_ADDR);
|
2019-09-23 12:51:47 +08:00
|
|
|
|
props.setProperty("spring.cloud.alibaba.seata.tx-service-group", appName.concat(NacosConstant.NACOS_GROUP_SUFFIX));
|
2018-12-24 11:58:45 +08:00
|
|
|
|
// 加载自定义组件
|
2019-06-17 00:15:24 +08:00
|
|
|
|
List<LauncherService> launcherList = new ArrayList<>();
|
|
|
|
|
ServiceLoader.load(LauncherService.class).forEach(launcherList::add);
|
2024-03-13 10:34:54 +08:00
|
|
|
|
SpringApplicationBuilder finalBuilder = builder;
|
2019-06-17 00:15:24 +08:00
|
|
|
|
launcherList.stream().sorted(Comparator.comparing(LauncherService::getOrder)).collect(Collectors.toList())
|
2024-03-13 10:34:54 +08:00
|
|
|
|
.forEach(launcherService -> launcherService.launcher(finalBuilder, appName, profile));
|
|
|
|
|
return finalBuilder;
|
2018-12-24 11:58:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 判断是否为本地开发环境
|
|
|
|
|
*
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
2019-01-04 09:34:52 +08:00
|
|
|
|
public static boolean isLocalDev() {
|
2018-12-24 11:58:45 +08:00
|
|
|
|
String osName = System.getProperty("os.name");
|
|
|
|
|
return StringUtils.hasText(osName) && !(AppConstant.OS_NAME_LINUX.equals(osName.toUpperCase()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|