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
|
# Environment
|
||||||
.env
|
.env
|
||||||
.env.local
|
.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
|
@RestController
|
||||||
public class ConfigController {
|
public class ConfigController {
|
||||||
|
|
||||||
@Value("${app.name:unknown}")
|
@Value("${database.host:unknown}")
|
||||||
private String appName;
|
private String host;
|
||||||
|
|
||||||
@Value("${app.version:unknown}")
|
@Value("${database.port:unknown}")
|
||||||
private String appVersion;
|
private String port;
|
||||||
|
|
||||||
@Value("${db.url:unknown}")
|
@Value("${database.name:unknown}")
|
||||||
private String dbUrl;
|
private String name;
|
||||||
|
|
||||||
@Value("${db.username:unknown}")
|
@Value("${database.user:unknown}")
|
||||||
private String dbUsername;
|
private String user;
|
||||||
|
|
||||||
@Value("${custom.config1:unknown}")
|
@Value("${database.password:unknown}")
|
||||||
private String customConfig1;
|
private String password;
|
||||||
|
|
||||||
@GetMapping("/")
|
@GetMapping("/")
|
||||||
public Map<String, Object> index() {
|
public Map<String, Object> index() {
|
||||||
Map<String, Object> result = new HashMap<>();
|
Map<String, Object> result = new HashMap<>();
|
||||||
result.put("appName", appName);
|
result.put("host", host);
|
||||||
result.put("appVersion", appVersion);
|
result.put("port", port);
|
||||||
result.put("dbUrl", dbUrl);
|
result.put("name", name);
|
||||||
result.put("dbUsername", dbUsername);
|
result.put("user", user);
|
||||||
result.put("customConfig1", customConfig1);
|
result.put("password", password);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,13 +7,15 @@ app.version=1.0.0
|
|||||||
|
|
||||||
# Kenaito Config Center
|
# Kenaito Config Center
|
||||||
kenaito.config.baseUrl=http://localhost:8080
|
kenaito.config.baseUrl=http://localhost:8080
|
||||||
kenaito.config.appName=demo
|
kenaito.config.appName=kenaito-springboot-demo
|
||||||
kenaito.config.profile=dev
|
kenaito.config.profile=daily
|
||||||
kenaito.config.version=latest
|
kenaito.config.version=latest
|
||||||
kenaito.config.token=
|
kenaito.config.token=
|
||||||
kenaito.config.sync-interval-seconds=30
|
kenaito.config.sync-interval-seconds=30
|
||||||
|
|
||||||
# Local configs (will be overridden by remote if exists)
|
# Local configs (will be overridden by remote if exists)
|
||||||
db.url=jdbc:mysql://localhost:3306/local_db
|
database.host=192.168.1.1
|
||||||
db.username=local_user
|
database.port=8888
|
||||||
custom.config1=local_value
|
database.name=xxxx
|
||||||
|
database.user=xxxx
|
||||||
|
database.password=xxxxxxxx
|
||||||
|
|||||||
+2
-2
@@ -245,7 +245,7 @@ public class ConfigcenterContext {
|
|||||||
env = getProperty("kenaito.config.profile", "dev");
|
env = getProperty("kenaito.config.profile", "dev");
|
||||||
Validate.notBlank(env, "[kenaito.config.profile] is required");
|
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");
|
version = getProperty("kenaito.config.version", "latest");
|
||||||
ignoreGlobal = getBoolean("kenaito.config.global-ignore", false);
|
ignoreGlobal = getBoolean("kenaito.config.global-ignore", false);
|
||||||
@@ -269,7 +269,7 @@ public class ConfigcenterContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setApiBaseUrl(String apiBaseUrl) {
|
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(",|;");
|
String[] urls = apiBaseUrl.split(",|;");
|
||||||
this.apiBaseUrls = new String[urls.length];
|
this.apiBaseUrls = new String[urls.length];
|
||||||
for (int i = 0; i < urls.length; i++) {
|
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) {
|
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
|
||||||
if (ccContext.isRemoteEnabled() && !ccContext.isProcessed()) {
|
if (ccContext.isRemoteEnabled() && !ccContext.isProcessed()) {
|
||||||
Properties properties = new Properties();
|
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.init(properties, true);
|
||||||
ccContext.mergeRemoteProperties(properties);
|
ccContext.mergeRemoteProperties(properties);
|
||||||
PropertiesPropertySource propertySource = new PropertiesPropertySource(ConfigcenterContext.MANAGER_PROPERTY_SOURCE, properties);
|
PropertiesPropertySource propertySource = new PropertiesPropertySource(ConfigcenterContext.MANAGER_PROPERTY_SOURCE, properties);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
# Server
|
# Server
|
||||||
server.port=8082
|
server.port=8081
|
||||||
|
|
||||||
# Application
|
# Application
|
||||||
app.name=kenaito-conf-springboot3-client-demo
|
app.name=kenaito-conf-springboot3-client-demo
|
||||||
@@ -7,13 +7,15 @@ app.version=1.0.0
|
|||||||
|
|
||||||
# Kenaito Config Center
|
# Kenaito Config Center
|
||||||
kenaito.config.baseUrl=http://localhost:8080
|
kenaito.config.baseUrl=http://localhost:8080
|
||||||
kenaito.config.appName=demo
|
kenaito.config.appName=kenaito-springboot-demo
|
||||||
kenaito.config.profile=dev
|
kenaito.config.profile=daily
|
||||||
kenaito.config.version=latest
|
kenaito.config.version=latest
|
||||||
kenaito.config.token=
|
kenaito.config.token=
|
||||||
kenaito.config.sync-interval-seconds=30
|
kenaito.config.sync-interval-seconds=30
|
||||||
|
|
||||||
# Local configs (will be overridden by remote if exists)
|
# Local configs (will be overridden by remote if exists)
|
||||||
db.url=jdbc:mysql://localhost:3306/local_db
|
database.host=192.168.1.1
|
||||||
db.username=local_user
|
database.port=8888
|
||||||
custom.config1=local_value
|
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");
|
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");
|
version = getProperty("kenaito.config.version", "latest");
|
||||||
ignoreGlobal = Boolean.parseBoolean(getProperty("kenaito.config.global-ignore", "false"));
|
ignoreGlobal = Boolean.parseBoolean(getProperty("kenaito.config.global-ignore", "false"));
|
||||||
@@ -145,7 +145,7 @@ public class ConfigcenterContext {
|
|||||||
|
|
||||||
public void setApiBaseUrl(String apiBaseUrl) {
|
public void setApiBaseUrl(String apiBaseUrl) {
|
||||||
if (apiBaseUrl == null || apiBaseUrl.isBlank()) {
|
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(",|;");
|
String[] urls = apiBaseUrl.split(",|;");
|
||||||
|
|||||||
+13
@@ -27,6 +27,19 @@ public class KenaitoEnvironmentInitializer implements EnvironmentPostProcessor {
|
|||||||
if (ccContext.isRemoteEnabled() && !ccContext.isProcessed()) {
|
if (ccContext.isRemoteEnabled() && !ccContext.isProcessed()) {
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
ccContext.setEnvironment((StandardEnvironment) environment);
|
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.init(properties, true);
|
||||||
ccContext.mergeRemoteProperties(properties);
|
ccContext.mergeRemoteProperties(properties);
|
||||||
PropertiesPropertySource propertySource = new PropertiesPropertySource(ConfigcenterContext.MANAGER_PROPERTY_SOURCE, properties);
|
PropertiesPropertySource propertySource = new PropertiesPropertySource(ConfigcenterContext.MANAGER_PROPERTY_SOURCE, properties);
|
||||||
|
|||||||
Reference in New Issue
Block a user