修复生产Nacos配置解析并重发Core
This commit is contained in:
+78
@@ -0,0 +1,78 @@
|
||||
package com.xiaowang.jayucloud.gateway.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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
org.springframework.context.ApplicationContextInitializer=\
|
||||
com.xiaowang.jayucloud.gateway.config.MiddlewareEnvironmentPostProcessor
|
||||
@@ -34,15 +34,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
|
||||
|
||||
+2
-4
@@ -56,10 +56,8 @@ class AuthGlobalFilterWhitelistTest {
|
||||
|
||||
assertThat(yaml)
|
||||
.contains("name: gateway")
|
||||
.contains("data-id: datasource.yaml")
|
||||
.contains("data-id: infra.yaml")
|
||||
.contains("data-id: secret.yaml")
|
||||
.contains("data-id: logging.yaml");
|
||||
.contains("data-id: common.yaml")
|
||||
.contains("data-id: middleware.yaml");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user