修复生产Nacos配置解析并重发Core
This commit is contained in:
+78
@@ -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;
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -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)
|
||||
|
||||
+2
-2
@@ -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)
|
||||
|
||||
+1
-1
@@ -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
|
||||
@@ -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
|
||||
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.xiaowang.jayucloud.core.config;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class MiddlewareEnvironmentPostProcessorTest {
|
||||
|
||||
@Test
|
||||
void mapsMiddlewareBlocksToSpringBootPrefixes() {
|
||||
MockEnvironment environment = new MockEnvironment();
|
||||
environment.getPropertySources().addLast(new MapPropertySource(
|
||||
"middleware",
|
||||
Map.of(
|
||||
"mysql.url", "jdbc:mysql://127.0.0.1:3306/jayu_cloud",
|
||||
"mysql.druid.initial-size", "2",
|
||||
"mysql.mybatis.configuration.map-underscore-to-camel-case", "true",
|
||||
"mysql.mybatis-plus.mapper-locations", "classpath:mapper/*.xml",
|
||||
"redis.sentinel.master", "mymaster",
|
||||
"redis.sentinel.nodes[0]", "127.0.0.1:26379",
|
||||
"rabbitmq.addresses", "127.0.0.1:5672"
|
||||
)
|
||||
));
|
||||
|
||||
new MiddlewareEnvironmentPostProcessor().applyAliases(environment);
|
||||
|
||||
assertThat(environment.getProperty("spring.datasource.url"))
|
||||
.isEqualTo("jdbc:mysql://127.0.0.1:3306/jayu_cloud");
|
||||
assertThat(environment.getProperty("spring.datasource.druid.initial-size"))
|
||||
.isEqualTo("2");
|
||||
assertThat(environment.getProperty("mybatis.configuration.map-underscore-to-camel-case"))
|
||||
.isEqualTo("true");
|
||||
assertThat(environment.getProperty("mybatis-plus.mapper-locations"))
|
||||
.isEqualTo("classpath:mapper/*.xml");
|
||||
assertThat(environment.getProperty("spring.datasource.mybatis.configuration.map-underscore-to-camel-case"))
|
||||
.isNull();
|
||||
assertThat(environment.getProperty("spring.redis.sentinel.master"))
|
||||
.isEqualTo("mymaster");
|
||||
assertThat(environment.getProperty("spring.redis.sentinel.nodes[0]"))
|
||||
.isEqualTo("127.0.0.1:26379");
|
||||
assertThat(environment.getProperty("spring.rabbitmq.addresses"))
|
||||
.isEqualTo("127.0.0.1:5672");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user