修复nacos问题

This commit is contained in:
2026-08-18 13:00:23 +08:00
parent cb425028c0
commit 6c64b32a4a
2 changed files with 40 additions and 5 deletions
@@ -54,6 +54,12 @@ public interface LauncherConstant {
*/ */
String NACOS_PROD_ADDR = "172.16.203.228:8848"; String NACOS_PROD_ADDR = "172.16.203.228:8848";
String NACOS_PROD_HOST = "172.16.203.228:8848";
String NACOS_PROD_USERNAME = "nacos";
String NACOS_PROD_PASSWORD = "nacosTMS";
/** /**
* nacos test 地址 * nacos test 地址
*/ */
@@ -156,13 +162,42 @@ public interface LauncherConstant {
* @return addr * @return addr
*/ */
static String nacosAddr(String profile) { static String nacosAddr(String profile) {
String profileKey = profile == null ? "" : profile.toUpperCase();
String profileAddr = env("NACOS_" + profileKey + "_HOST");
if (profileAddr != null) return profileAddr;
String configuredAddr = env("NACOS_HOST");
if (configuredAddr != null) return configuredAddr;
return switch (profile) { return switch (profile) {
case (AppConstant.PROD_CODE) -> NACOS_PROD_ADDR; case (AppConstant.PROD_CODE) -> NACOS_PROD_HOST;
case (AppConstant.TEST_CODE) -> NACOS_TEST_ADDR; case (AppConstant.TEST_CODE) -> NACOS_TEST_ADDR;
default -> NACOS_DEV_ADDR; default -> NACOS_DEV_ADDR;
}; };
} }
static String nacosUsername(String profile) {
String profileKey = profile == null ? "" : profile.toUpperCase();
String value = env("NACOS_" + profileKey + "_USERNAME");
if (value != null) return value;
value = env("NACOS_USERNAME");
if (value != null) return value;
return AppConstant.PROD_CODE.equals(profile) ? NACOS_PROD_USERNAME : NACOS_USERNAME;
}
static String nacosPassword(String profile) {
String profileKey = profile == null ? "" : profile.toUpperCase();
String value = env("NACOS_" + profileKey + "_PASSWORD");
if (value != null) return value;
value = env("NACOS_PASSWORD");
if (value != null) return value;
return AppConstant.PROD_CODE.equals(profile) ? NACOS_PROD_PASSWORD : NACOS_PASSWORD;
}
static String env(String name) {
String value = System.getProperty(name);
if (value == null || value.isBlank()) value = System.getenv(name);
return value == null || value.isBlank() ? null : value.trim();
}
/** /**
* 动态获取sentinel地址 * 动态获取sentinel地址
* *
@@ -48,12 +48,12 @@ public class LauncherServiceImpl implements LauncherService {
if (BladeApplication.isNacosConfigEnabled()) { if (BladeApplication.isNacosConfigEnabled()) {
// nacos注册中心配置 // nacos注册中心配置
PropsUtil.setProperty(props, "spring.cloud.nacos.discovery.username", LauncherConstant.NACOS_USERNAME); PropsUtil.setProperty(props, "spring.cloud.nacos.discovery.username", LauncherConstant.nacosUsername(profile));
PropsUtil.setProperty(props, "spring.cloud.nacos.discovery.password", LauncherConstant.NACOS_PASSWORD); PropsUtil.setProperty(props, "spring.cloud.nacos.discovery.password", LauncherConstant.nacosPassword(profile));
PropsUtil.setProperty(props, "spring.cloud.nacos.discovery.server-addr", LauncherConstant.nacosAddr(profile)); PropsUtil.setProperty(props, "spring.cloud.nacos.discovery.server-addr", LauncherConstant.nacosAddr(profile));
// nacos配置中心配置 // nacos配置中心配置
PropsUtil.setProperty(props, "spring.cloud.nacos.config.username", LauncherConstant.NACOS_USERNAME); PropsUtil.setProperty(props, "spring.cloud.nacos.config.username", LauncherConstant.nacosUsername(profile));
PropsUtil.setProperty(props, "spring.cloud.nacos.config.password", LauncherConstant.NACOS_PASSWORD); PropsUtil.setProperty(props, "spring.cloud.nacos.config.password", LauncherConstant.nacosPassword(profile));
PropsUtil.setProperty(props, "spring.cloud.nacos.config.server-addr", LauncherConstant.nacosAddr(profile)); PropsUtil.setProperty(props, "spring.cloud.nacos.config.server-addr", LauncherConstant.nacosAddr(profile));
} }