feat: 实现配置删除通知机制(deletedKeys)
- 服务端 fetchChangedConfigs 接口新增 deletedKeys 字段,返回已删除配置的 key 列表 - Go/Node.js/Python/Spring Boot 客户端支持解析 deletedKeys 并删除本地配置 - Spring Boot 客户端新增 removeConfigs 方法,同步更新 Spring Environment
This commit is contained in:
+36
@@ -415,6 +415,42 @@ public class ConfigcenterContext {
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void removeConfigs(List<String> keys) {
|
||||
for (String key : keys) {
|
||||
localProps.remove(key);
|
||||
logger.info("Config [{}] removed", key);
|
||||
}
|
||||
// Update Spring Environment property source
|
||||
if (applicationContext != null) {
|
||||
StandardEnvironment environment = (StandardEnvironment) applicationContext.getEnvironment();
|
||||
MutablePropertySources propertySources = environment.getPropertySources();
|
||||
synchronized (propertySources) {
|
||||
if (propertySources.contains(MANAGER_PROPERTY_SOURCE)) {
|
||||
Properties properties = new Properties();
|
||||
Object source = propertySources.get(MANAGER_PROPERTY_SOURCE);
|
||||
if (source instanceof MapPropertySource) {
|
||||
Map<String, Object> map = ((MapPropertySource) source).getSource();
|
||||
for (Entry<String, Object> entry : map.entrySet()) {
|
||||
if (!keys.contains(entry.getKey())) {
|
||||
properties.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
} else if (source instanceof PropertiesPropertySource) {
|
||||
Map<String, Object> src = ((PropertiesPropertySource) source).getSource();
|
||||
for (Entry<String, Object> entry : src.entrySet()) {
|
||||
if (!keys.contains(entry.getKey())) {
|
||||
properties.put(entry.getKey(), entry.getValue().toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
propertySources.replace(MANAGER_PROPERTY_SOURCE, new PropertiesPropertySource(MANAGER_PROPERTY_SOURCE, properties));
|
||||
}
|
||||
}
|
||||
}
|
||||
// Refresh @Value and @ConfigurationProperties fields
|
||||
refreshDynamicConfigFields();
|
||||
}
|
||||
|
||||
private void refreshDynamicConfigFields() {
|
||||
if (applicationContext != null) {
|
||||
try {
|
||||
|
||||
+9
@@ -63,6 +63,15 @@ public class InternalConfigChangeListener {
|
||||
if (data.containsKey(LATEST_FETCH_TIMESTAMP)) {
|
||||
params.put("lastTime", Long.parseLong(data.remove(LATEST_FETCH_TIMESTAMP).toString()));
|
||||
}
|
||||
// 处理删除的配置
|
||||
if (data.containsKey("deletedKeys")) {
|
||||
String deletedKeysStr = (String) data.remove("deletedKeys");
|
||||
if (deletedKeysStr != null && !deletedKeysStr.isEmpty()) {
|
||||
String[] deletedKeys = deletedKeysStr.split(",");
|
||||
context.removeConfigs(java.util.Arrays.asList(deletedKeys));
|
||||
logger.info("[ConfigClient] {} configs deleted", deletedKeys.length);
|
||||
}
|
||||
}
|
||||
if (!data.isEmpty()) context.updateConfig(data);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user