修复生产Nacos配置解析并重发Core

This commit is contained in:
2026-07-24 02:03:09 +08:00
parent c8443ab892
commit 5590ffb84d
17 changed files with 275 additions and 25 deletions
@@ -0,0 +1,78 @@
package com.xiaowang.jayucloud.core.config;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
/**
* 将中间件顶层配置映射到 Spring Boot 自动配置使用的标准前缀。
*/
public final class MiddlewareEnvironmentPostProcessor
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
private static final String ALIAS_SOURCE_NAME = "jayuCloudMiddlewareAliases";
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
applyAliases(applicationContext.getEnvironment());
}
void applyAliases(ConfigurableEnvironment environment) {
Map<String, Object> aliases = new LinkedHashMap<>();
copyPrefix(environment, aliases, "mysql", "spring.datasource", Set.of("mysql.mybatis", "mysql.mybatis-plus"));
copyPrefix(environment, aliases, "mysql.mybatis", "mybatis", Set.of());
copyPrefix(environment, aliases, "mysql.mybatis-plus", "mybatis-plus", Set.of());
copyPrefix(environment, aliases, "redis", "spring.redis", Set.of());
copyPrefix(environment, aliases, "rabbitmq", "spring.rabbitmq", Set.of());
if (!aliases.isEmpty()) {
environment.getPropertySources().addFirst(new MapPropertySource(ALIAS_SOURCE_NAME, aliases));
}
}
private void copyPrefix(
ConfigurableEnvironment environment,
Map<String, Object> aliases,
String sourcePrefix,
String targetPrefix,
Set<String> excludedPrefixes
) {
for (PropertySource<?> propertySource : environment.getPropertySources()) {
if (ALIAS_SOURCE_NAME.equals(propertySource.getName())
|| !(propertySource instanceof EnumerablePropertySource<?> enumerablePropertySource)) {
continue;
}
for (String propertyName : enumerablePropertySource.getPropertyNames()) {
if (!propertyName.equals(sourcePrefix) && !propertyName.startsWith(sourcePrefix + ".")) {
continue;
}
if (isExcluded(propertyName, excludedPrefixes)) {
continue;
}
String suffix = propertyName.substring(sourcePrefix.length());
String targetName = targetPrefix + suffix;
if (!aliases.containsKey(targetName)) {
Object value = propertySource.getProperty(propertyName);
if (value != null) {
aliases.put(targetName, value);
}
}
}
}
}
private boolean isExcluded(String propertyName, Set<String> excludedPrefixes) {
for (String excludedPrefix : excludedPrefixes) {
if (propertyName.equals(excludedPrefix) || propertyName.startsWith(excludedPrefix + ".")) {
return true;
}
}
return false;
}
}
@@ -17,7 +17,7 @@ import javax.validation.constraints.NotNull;
@Data
@Configuration
@Validated
@ConfigurationProperties(prefix = "aliyun.oss")
@ConfigurationProperties(prefix = "object-storage.aliyun.oss")
public class AliyunOssConfig {
@NotNull
@@ -37,7 +37,7 @@ public class AliyunOssConfig {
private Integer signedUrlExpireMinutes;
@Bean(destroyMethod = "shutdown")
@ConditionalOnProperty(prefix = "aliyun.oss", name = "enabled", havingValue = "true")
@ConditionalOnProperty(prefix = "object-storage.aliyun.oss", name = "enabled", havingValue = "true")
public OSS aliyunOssClient() {
if (!StringUtils.hasText(endpoint)
|| !StringUtils.hasText(accessKeyId)
@@ -15,7 +15,7 @@ import javax.validation.constraints.NotNull;
@Data
@Configuration
@Validated
@ConfigurationProperties(prefix = "minio")
@ConfigurationProperties(prefix = "object-storage.minio")
public class MinioStorageConfig {
@NotNull
@@ -37,7 +37,7 @@ public class MinioStorageConfig {
private Integer signedUrlExpireMinutes;
@Bean
@ConditionalOnProperty(prefix = "minio", name = "enabled", havingValue = "true")
@ConditionalOnProperty(prefix = "object-storage.minio", name = "enabled", havingValue = "true")
public MinioClient minioClient() {
if (!StringUtils.hasText(endpoint)
|| !StringUtils.hasText(accessKey)
@@ -17,7 +17,7 @@ import javax.validation.constraints.NotNull;
@Data
@Configuration
@Validated
@ConfigurationProperties(prefix = "qiniu.kodo")
@ConfigurationProperties(prefix = "object-storage.qiniu.kodo")
public class QiniuKodoConfig {
@NotBlank
@@ -0,0 +1,2 @@
org.springframework.context.ApplicationContextInitializer=\
com.xiaowang.jayucloud.core.config.MiddlewareEnvironmentPostProcessor
+2 -8
View File
@@ -40,15 +40,9 @@ spring:
password: ${NACOS_PASSWORD:}
# Core 与 Gateway 共享的公共配置文件。
shared-configs:
- data-id: datasource.yaml
- data-id: common.yaml
group: ${NACOS_GROUP:DEFAULT_GROUP}
refresh: true
- data-id: infra.yaml
group: ${NACOS_GROUP:DEFAULT_GROUP}
refresh: true
- data-id: secret.yaml
group: ${NACOS_GROUP:DEFAULT_GROUP}
refresh: true
- data-id: logging.yaml
- data-id: middleware.yaml
group: ${NACOS_GROUP:DEFAULT_GROUP}
refresh: true