Skip to content

Commit

Permalink
Modify config update process
Browse files Browse the repository at this point in the history
  • Loading branch information
Narzisss committed Oct 31, 2024
1 parent 08a0a85 commit 0f613cf
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -459,13 +459,13 @@ public void updateAllProviders(List<ProviderGroup> groups) {
private class ConsumerAttributeListener implements ConfigListener {

// 可以动态配置的选项
private final Set<String> dynamicConfigKeys = new HashSet<>();
private Map<String, String> newValueMap = new HashMap<>();
private final Set<String> supportDynamicConfigKeys = new HashSet<>();
private final Map<String, String> newValueMap = new HashMap<>();

ConsumerAttributeListener() {
dynamicConfigKeys.add(RpcConstants.CONFIG_KEY_TIMEOUT);
dynamicConfigKeys.add(RpcConstants.CONFIG_KEY_RETRIES);
dynamicConfigKeys.add(RpcConstants.CONFIG_KEY_LOADBALANCER);
supportDynamicConfigKeys.add(RpcConstants.CONFIG_KEY_TIMEOUT);
supportDynamicConfigKeys.add(RpcConstants.CONFIG_KEY_RETRIES);
supportDynamicConfigKeys.add(RpcConstants.CONFIG_KEY_LOADBALANCER);
}

@Override
Expand All @@ -474,16 +474,23 @@ public void process(ConfigChangedEvent event) {
consumerConfig.getDynamicConfigValueCache().clear();
// 获取对应配置项的默认值
for (String key : newValueMap.keySet()) {
newValueMap.put(key, String.valueOf(consumerConfig.getConfigValueCache().get(key)));
if (consumerConfig.getConfigValueCache().get(key) != null) {
newValueMap.put(key, String.valueOf(consumerConfig.getConfigValueCache().get(key)));
} else {
newValueMap.put(key, null);
}
}
if (!event.getChangeType().equals(ConfigChangeType.DELETED)) {
// ADDED or MODIFIED
Map<String, String> dynamicValueMap = event.getDynamicValueMap();
for (String key : dynamicValueMap.keySet()) {
String tempKey = key.lastIndexOf(".") == -1 ? key : key.substring(key.lastIndexOf(".") + 1);
if (dynamicConfigKeys.contains(tempKey)) {
consumerConfig.getDynamicConfigValueCache().put(key, dynamicValueMap.get(key));
newValueMap.put(key, dynamicValueMap.get(key));
if (supportDynamicConfigKeys.contains(tempKey)) {
String value = dynamicValueMap.get(key);
if (StringUtils.isNotBlank(value)) {
consumerConfig.getDynamicConfigValueCache().put(key, value);
newValueMap.put(key, value);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ public class ApolloDynamicConfigManager extends DynamicConfigManager {
private static final String APOLLO_PARAM_NAMESPACE_KEY = "namespace";

private static final String APOLLO_PROTOCOL_PREFIX = "http://";

private final ConcurrentMap<String, ApolloListener> watchListenerMap = new ConcurrentHashMap<>();
private Config config;

private final Config config;

protected ApolloDynamicConfigManager(String appName) {
super(appName, SofaConfigs.getOrCustomDefault(DynamicConfigKeys.CONFIG_CENTER_ADDRESS, ""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@

public class ApolloDynamicConfigManagerTest {

private final static Logger logger = LoggerFactory
.getLogger(ApolloDynamicConfigManagerTest.class);
private final static Logger logger = LoggerFactory
.getLogger(ApolloDynamicConfigManagerTest.class);

private DynamicConfigManager apolloDynamicConfigManager = DynamicConfigManagerFactory.getDynamicManager("test",
"apollo");
"apollo");

@Test
public void getProviderServiceProperty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.Executor;

/**
* @author Narziss
* @version NaocsDynamicConfigManager.java, v 0.1 2024年07月26日 09:37 Narziss
Expand All @@ -60,18 +58,12 @@ public class NacosDynamicConfigManager extends DynamicConfigManager {

private ConfigService configService;

private Properties nacosConfig = new Properties();

protected NacosDynamicConfigManager(String appName) {
super(appName, SofaConfigs.getOrCustomDefault(DynamicConfigKeys.CONFIG_CENTER_ADDRESS, "nacos://127.0.0.1:8848"));
group = appName;
Properties nacosConfig = new Properties();
nacosConfig.put(PropertyKeyConst.SERVER_ADDR, getDynamicUrl().getAddress());
if (StringUtils.isNotBlank(getDynamicUrl().getParam(PropertyKeyConst.USERNAME))) {
nacosConfig.put(PropertyKeyConst.USERNAME, getDynamicUrl().getParam(PropertyKeyConst.USERNAME));
}
if (StringUtils.isNotBlank(getDynamicUrl().getParam(PropertyKeyConst.PASSWORD))) {
nacosConfig.put(PropertyKeyConst.PASSWORD, getDynamicUrl().getParam(PropertyKeyConst.PASSWORD));
}
nacosConfig.putAll(getDynamicUrl().getParams());
try {
configService = ConfigFactory.createConfigService(nacosConfig);
} catch (Exception e) {
Expand Down Expand Up @@ -168,18 +160,14 @@ private NacosConfigListener createTargetListener(String key) {
return configListener;
}

public class NacosConfigListener extends AbstractSharedListener {
public static class NacosConfigListener extends AbstractSharedListener {

private final Set<ConfigListener> listeners = new CopyOnWriteArraySet<>();

/**
* cache data to store old value
*/
private Map<String, String> cacheData = new ConcurrentHashMap<>();

@Override
public Executor getExecutor() {
return null;
}
private final Map<String, String> cacheData = new ConcurrentHashMap<>();

/**
* receive config change event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ private ZookeeperConfigListener createTargetListener(String pathKey) {

public class ZookeeperConfigListener implements NodeCacheListener {

private String pathKey;
private Set<ConfigListener> listeners = new CopyOnWriteArraySet<>();
private NodeCache nodeCache;
private final String pathKey;
private final Set<ConfigListener> listeners = new CopyOnWriteArraySet<>();
private final NodeCache nodeCache;

public ZookeeperConfigListener(String pathKey) {
this.pathKey = pathKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ public boolean updateAttribute(String property, String newValueStr, boolean over
Class propertyClazz = getMethod.getReturnType(); // 旧值的类型
// 拿到旧的值
Object oldValue = null;
Object newValue = newValueStr == "null" ? null : CompatibleTypeUtils.convert(newValueStr, propertyClazz);
Object newValue = CompatibleTypeUtils.convert(newValueStr, propertyClazz);
if (dynamicConfigValueCache.containsKey(property)) {
dynamicConfigValueCache.put(property, newValue);
}
Expand Down Expand Up @@ -988,7 +988,7 @@ public boolean updateAttribute(String property, String newValueStr, boolean over
Class propertyClazz = getMethod.getReturnType(); // 旧值的类型
// 拿到旧的值
Object oldValue = BeanUtils.getProperty(this, property, propertyClazz);
Object newValue = newValueStr == "null" ? null : CompatibleTypeUtils.convert(newValueStr, propertyClazz);
Object newValue = CompatibleTypeUtils.convert(newValueStr, propertyClazz);
if (dynamicConfigValueCache.containsKey(property)) {
dynamicConfigValueCache.put(property, newValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.sofa.rpc.dynamic;

import com.alipay.sofa.rpc.common.utils.StringUtils;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;

Expand All @@ -29,6 +30,7 @@
*/

public class ConfigChangedEvent extends EventObject {

private final static Logger LOGGER = LoggerFactory.getLogger(ConfigChangedEvent.class);

private final String key;
Expand All @@ -37,9 +39,7 @@ public class ConfigChangedEvent extends EventObject {

private final ConfigChangeType changeType;


private Map<String, String> dynamicValueMap = new HashMap<>();
;
private final Map<String, String> dynamicValueMap = new HashMap<>();

public ConfigChangedEvent(String key, String content) {
this(key, content, ConfigChangeType.MODIFIED);
Expand All @@ -50,13 +50,13 @@ public ConfigChangedEvent(String key, String content, ConfigChangeType changeTyp
this.key = key;
this.content = content;
this.changeType = changeType;
if (content != null) {
if (StringUtils.isNotBlank(content)) {
parseConfigurationLines(content);
}
}

private void parseConfigurationLines(String content) {
String[] lines = content.split("\n");
String[] lines = content.split(System.lineSeparator());
for (String line : lines) {
String[] keyValue = line.split("=", 2);
if (keyValue.length == 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void testApolloDynamicConfig() throws Exception {
.getInterfaceId());

// 测试配置新增
String configValue = "timeout=5000\n";
String configValue = "timeout=5000"+System.lineSeparator();
ConfigChange configChange = new ConfigChange("application", consumerConfig.getInterfaceId(), null, configValue, PropertyChangeType.ADDED);
Map<String, ConfigChange> changes = new HashMap<>();
changes.put(configChange.getPropertyName(), configChange);
Expand All @@ -76,7 +76,7 @@ public void testApolloDynamicConfig() throws Exception {

// 测试配置修改
String oldValue = configValue;
configValue = "timeout=5000\n.sayHello.timeout=6000";
configValue = "timeout=5000"+System.lineSeparator()+".sayHello.timeout=6000";
configChange = new ConfigChange("application", consumerConfig.getInterfaceId(), oldValue, configValue, PropertyChangeType.MODIFIED);
changes = new HashMap<>();
changes.put(configChange.getPropertyName(), configChange);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class NacosDynamicConfigTest {
public void testNacosDynamicConfig() throws Exception {
System.setProperty(DynamicConfigKeys.DYNAMIC_REFRESH_ENABLE.getKey(), "true");
System.setProperty(DynamicConfigKeys.CONFIG_CENTER_ADDRESS.getKey(),
"nacos://127.0.0.1:8848?username=nacos&password=nacos");
"nacos://127.0.0.1:8848/sofa-rpc-config?username=nacos&password=nacos");
ApplicationConfig clientApplication = new ApplicationConfig();
clientApplication.setAppName("demo");

Expand Down Expand Up @@ -68,7 +68,7 @@ public void testNacosDynamicConfig() throws Exception {
nacosConfigListener.innerReceive(consumerConfig.getInterfaceId(), consumerConfig.getAppName(), configValue);
Assert.assertEquals(5000, consumerConfig.getMethodTimeout("sayHello"));
// 测试配置修改
configValue = "timeout=5000\n.sayHello.timeout=6000";
configValue = "timeout=5000" + System.lineSeparator() + ".sayHello.timeout=6000";
nacosConfigListener.innerReceive(consumerConfig.getInterfaceId(), consumerConfig.getAppName(), configValue);
Assert.assertEquals(6000, consumerConfig.getMethodTimeout("sayHello"));
// 测试配置删除
Expand Down

0 comments on commit 0f613cf

Please sign in to comment.