feat(launch): 添加 Nacos 配置导入控制功能
This commit is contained in:
@@ -46,6 +46,20 @@ import java.util.*;
|
|||||||
*/
|
*/
|
||||||
public class BladeApplication {
|
public class BladeApplication {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否注入 Blade 默认的 Nacos spring.config.import。
|
||||||
|
*/
|
||||||
|
public static final String NACOS_IMPORT_ENABLED_PROPERTY = "blade.launcher.nacos.import.enabled";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否注入 LauncherService 中的 Nacos 系统属性配置。
|
||||||
|
*/
|
||||||
|
public static final String NACOS_CONFIG_ENABLED_PROPERTY = "blade.launcher.nacos.config.enabled";
|
||||||
|
|
||||||
|
private static final String NACOS_IMPORT_ENABLED_ENV = "BLADE_LAUNCHER_NACOS_IMPORT_ENABLED";
|
||||||
|
|
||||||
|
private static final String NACOS_CONFIG_ENABLED_ENV = "BLADE_LAUNCHER_NACOS_CONFIG_ENABLED";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 启动SpringBoot应用,不使用自定义SpringApplicationBuilder
|
* 启动SpringBoot应用,不使用自定义SpringApplicationBuilder
|
||||||
*
|
*
|
||||||
@@ -120,6 +134,40 @@ public class BladeApplication {
|
|||||||
return StringUtils.hasText(osName) && !(AppConstant.OS_NAME_LINUX.equalsIgnoreCase(osName));
|
return StringUtils.hasText(osName) && !(AppConstant.OS_NAME_LINUX.equalsIgnoreCase(osName));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否启用 Blade 默认的 Nacos 配置导入。
|
||||||
|
*
|
||||||
|
* @return true 表示启用
|
||||||
|
*/
|
||||||
|
public static boolean isNacosImportEnabled() {
|
||||||
|
return getBoolean(NACOS_IMPORT_ENABLED_PROPERTY, NACOS_IMPORT_ENABLED_ENV, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否启用 LauncherService 中的 Nacos 系统属性配置。
|
||||||
|
*
|
||||||
|
* @return true 表示启用
|
||||||
|
*/
|
||||||
|
public static boolean isNacosConfigEnabled() {
|
||||||
|
return getBoolean(NACOS_CONFIG_ENABLED_PROPERTY, NACOS_CONFIG_ENABLED_ENV, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭 Blade 启动阶段的 Nacos 默认配置注入,改由应用自身配置文件声明。
|
||||||
|
*/
|
||||||
|
public static void disableNacosLaunchConfig() {
|
||||||
|
System.setProperty(NACOS_IMPORT_ENABLED_PROPERTY, Boolean.FALSE.toString());
|
||||||
|
System.setProperty(NACOS_CONFIG_ENABLED_PROPERTY, Boolean.FALSE.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean getBoolean(String propertyName, String envName, boolean defaultValue) {
|
||||||
|
String value = System.getProperty(propertyName);
|
||||||
|
if (!StringUtils.hasText(value)) {
|
||||||
|
value = System.getenv(envName);
|
||||||
|
}
|
||||||
|
return StringUtils.hasText(value) ? Boolean.parseBoolean(value) : defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 配置环境变量
|
* 配置环境变量
|
||||||
@@ -199,7 +247,9 @@ public class BladeApplication {
|
|||||||
defaultProperties.setProperty("spring.sleuth.sampler.percentage", "1.0");
|
defaultProperties.setProperty("spring.sleuth.sampler.percentage", "1.0");
|
||||||
defaultProperties.setProperty("spring.cloud.alibaba.seata.tx-service-group", appName.concat(NacosConstant.NACOS_GROUP_SUFFIX));
|
defaultProperties.setProperty("spring.cloud.alibaba.seata.tx-service-group", appName.concat(NacosConstant.NACOS_GROUP_SUFFIX));
|
||||||
defaultProperties.setProperty("nacos.logging.default.config.enabled", "false");
|
defaultProperties.setProperty("nacos.logging.default.config.enabled", "false");
|
||||||
|
if (isNacosImportEnabled()) {
|
||||||
defaultProperties.setProperty("spring.config.import", String.join(",", NacosConstant.dataId(), NacosConstant.dataId(profile), NacosConstant.dataId(appName, profile)));
|
defaultProperties.setProperty("spring.config.import", String.join(",", NacosConstant.dataId(), NacosConstant.dataId(profile), NacosConstant.dataId(appName, profile)));
|
||||||
|
}
|
||||||
return defaultProperties;
|
return defaultProperties;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user