fix(config): 统一配置键名并修复 EnvironmentInitializer 属性读取
- 统一使用 kenaito.config.baseUrl 替代 kenaito.config.base.url - 更新示例配置参数(appName、profile、数据库配置键名) - EnvironmentInitializer 添加从 Spring Environment 提取 kenaito.config.* 属性的逻辑 - .gitignore 添加 target 目录忽略规则
This commit is contained in:
@@ -37,3 +37,5 @@ bin/
|
||||
# Environment
|
||||
.env
|
||||
.env.local
|
||||
/kenaito-conf-springboot2-client-demo/target/classes/META-INF/spring.factories
|
||||
/kenaito-conf-springboot2-client-demo/target/classes/application.properties
|
||||
|
||||
+15
-15
@@ -10,29 +10,29 @@ import java.util.Map;
|
||||
@RestController
|
||||
public class ConfigController {
|
||||
|
||||
@Value("${app.name:unknown}")
|
||||
private String appName;
|
||||
@Value("${database.host:unknown}")
|
||||
private String host;
|
||||
|
||||
@Value("${app.version:unknown}")
|
||||
private String appVersion;
|
||||
@Value("${database.port:unknown}")
|
||||
private String port;
|
||||
|
||||
@Value("${db.url:unknown}")
|
||||
private String dbUrl;
|
||||
@Value("${database.name:unknown}")
|
||||
private String name;
|
||||
|
||||
@Value("${db.username:unknown}")
|
||||
private String dbUsername;
|
||||
@Value("${database.user:unknown}")
|
||||
private String user;
|
||||
|
||||
@Value("${custom.config1:unknown}")
|
||||
private String customConfig1;
|
||||
@Value("${database.password:unknown}")
|
||||
private String password;
|
||||
|
||||
@GetMapping("/")
|
||||
public Map<String, Object> index() {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("appName", appName);
|
||||
result.put("appVersion", appVersion);
|
||||
result.put("dbUrl", dbUrl);
|
||||
result.put("dbUsername", dbUsername);
|
||||
result.put("customConfig1", customConfig1);
|
||||
result.put("host", host);
|
||||
result.put("port", port);
|
||||
result.put("name", name);
|
||||
result.put("user", user);
|
||||
result.put("password", password);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,13 +7,15 @@ app.version=1.0.0
|
||||
|
||||
# Kenaito Config Center
|
||||
kenaito.config.baseUrl=http://localhost:8080
|
||||
kenaito.config.appName=demo
|
||||
kenaito.config.profile=dev
|
||||
kenaito.config.appName=kenaito-springboot-demo
|
||||
kenaito.config.profile=daily
|
||||
kenaito.config.version=latest
|
||||
kenaito.config.token=
|
||||
kenaito.config.sync-interval-seconds=30
|
||||
|
||||
# Local configs (will be overridden by remote if exists)
|
||||
db.url=jdbc:mysql://localhost:3306/local_db
|
||||
db.username=local_user
|
||||
custom.config1=local_value
|
||||
database.host=192.168.1.1
|
||||
database.port=8888
|
||||
database.name=xxxx
|
||||
database.user=xxxx
|
||||
database.password=xxxxxxxx
|
||||
|
||||
+2
-2
@@ -245,7 +245,7 @@ public class ConfigcenterContext {
|
||||
env = getProperty("kenaito.config.profile", "dev");
|
||||
Validate.notBlank(env, "[kenaito.config.profile] is required");
|
||||
|
||||
setApiBaseUrl(getAnyProperty("kenaito.config.baseUrl", "kenaito.config.base.url"));
|
||||
setApiBaseUrl(getAnyProperty("kenaito.config.baseUrl", "kenaito.config.baseUrl"));
|
||||
|
||||
version = getProperty("kenaito.config.version", "latest");
|
||||
ignoreGlobal = getBoolean("kenaito.config.global-ignore", false);
|
||||
@@ -269,7 +269,7 @@ public class ConfigcenterContext {
|
||||
}
|
||||
|
||||
public void setApiBaseUrl(String apiBaseUrl) {
|
||||
Validate.notBlank(apiBaseUrl, "[kenaito.config.base.url] is required");
|
||||
Validate.notBlank(apiBaseUrl, "[kenaito.config.baseUrl] is required");
|
||||
String[] urls = apiBaseUrl.split(",|;");
|
||||
this.apiBaseUrls = new String[urls.length];
|
||||
for (int i = 0; i < urls.length; i++) {
|
||||
|
||||
+13
@@ -26,6 +26,19 @@ public class KenaitoEnvironmentInitializer implements EnvironmentPostProcessor {
|
||||
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
|
||||
if (ccContext.isRemoteEnabled() && !ccContext.isProcessed()) {
|
||||
Properties properties = new Properties();
|
||||
// Extract kenaito.config.* properties from Spring Environment
|
||||
environment.getPropertySources().forEach(ps -> {
|
||||
if (ps instanceof org.springframework.core.env.EnumerablePropertySource) {
|
||||
for (String key : ((org.springframework.core.env.EnumerablePropertySource<?>) ps).getPropertyNames()) {
|
||||
if (key.startsWith("kenaito.config.") || key.equals("spring.application.name")) {
|
||||
String val = environment.getProperty(key);
|
||||
if (val != null) {
|
||||
properties.setProperty(key, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
ccContext.init(properties, true);
|
||||
ccContext.mergeRemoteProperties(properties);
|
||||
PropertiesPropertySource propertySource = new PropertiesPropertySource(ConfigcenterContext.MANAGER_PROPERTY_SOURCE, properties);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# Server
|
||||
server.port=8082
|
||||
server.port=8081
|
||||
|
||||
# Application
|
||||
app.name=kenaito-conf-springboot3-client-demo
|
||||
@@ -7,13 +7,15 @@ app.version=1.0.0
|
||||
|
||||
# Kenaito Config Center
|
||||
kenaito.config.baseUrl=http://localhost:8080
|
||||
kenaito.config.appName=demo
|
||||
kenaito.config.profile=dev
|
||||
kenaito.config.appName=kenaito-springboot-demo
|
||||
kenaito.config.profile=daily
|
||||
kenaito.config.version=latest
|
||||
kenaito.config.token=
|
||||
kenaito.config.sync-interval-seconds=30
|
||||
|
||||
# Local configs (will be overridden by remote if exists)
|
||||
db.url=jdbc:mysql://localhost:3306/local_db
|
||||
db.username=local_user
|
||||
custom.config1=local_value
|
||||
database.host=192.168.1.1
|
||||
database.port=8888
|
||||
database.name=xxxx
|
||||
database.user=xxxx
|
||||
database.password=xxxxxxxx
|
||||
|
||||
+2
-2
@@ -116,7 +116,7 @@ public class ConfigcenterContext {
|
||||
throw new IllegalArgumentException("[kenaito.config.profile] is required");
|
||||
}
|
||||
|
||||
setApiBaseUrl(getProperty("kenaito.config.baseUrl", getProperty("kenaito.config.base.url", "")));
|
||||
setApiBaseUrl(getProperty("kenaito.config.baseUrl", getProperty("kenaito.config.baseUrl", "")));
|
||||
|
||||
version = getProperty("kenaito.config.version", "latest");
|
||||
ignoreGlobal = Boolean.parseBoolean(getProperty("kenaito.config.global-ignore", "false"));
|
||||
@@ -145,7 +145,7 @@ public class ConfigcenterContext {
|
||||
|
||||
public void setApiBaseUrl(String apiBaseUrl) {
|
||||
if (apiBaseUrl == null || apiBaseUrl.isBlank()) {
|
||||
throw new IllegalArgumentException("[kenaito.config.base.url] is required");
|
||||
throw new IllegalArgumentException("[kenaito.config.baseUrl] is required");
|
||||
}
|
||||
|
||||
String[] urls = apiBaseUrl.split(",|;");
|
||||
|
||||
+13
@@ -27,6 +27,19 @@ public class KenaitoEnvironmentInitializer implements EnvironmentPostProcessor {
|
||||
if (ccContext.isRemoteEnabled() && !ccContext.isProcessed()) {
|
||||
Properties properties = new Properties();
|
||||
ccContext.setEnvironment((StandardEnvironment) environment);
|
||||
// Extract kenaito.config.* properties from Spring Environment
|
||||
environment.getPropertySources().forEach(ps -> {
|
||||
if (ps instanceof org.springframework.core.env.EnumerablePropertySource) {
|
||||
for (String key : ((org.springframework.core.env.EnumerablePropertySource<?>) ps).getPropertyNames()) {
|
||||
if (key.startsWith("kenaito.config.") || key.equals("spring.application.name")) {
|
||||
String val = environment.getProperty(key);
|
||||
if (val != null) {
|
||||
properties.setProperty(key, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
ccContext.init(properties, true);
|
||||
ccContext.mergeRemoteProperties(properties);
|
||||
PropertiesPropertySource propertySource = new PropertiesPropertySource(ConfigcenterContext.MANAGER_PROPERTY_SOURCE, properties);
|
||||
|
||||
Reference in New Issue
Block a user