diff --git a/bootstrap/bootstrap-api/src/main/java/com/alipay/sofa/rpc/bootstrap/DefaultConsumerBootstrap.java b/bootstrap/bootstrap-api/src/main/java/com/alipay/sofa/rpc/bootstrap/DefaultConsumerBootstrap.java index 54d16d264..7e559d89b 100644 --- a/bootstrap/bootstrap-api/src/main/java/com/alipay/sofa/rpc/bootstrap/DefaultConsumerBootstrap.java +++ b/bootstrap/bootstrap-api/src/main/java/com/alipay/sofa/rpc/bootstrap/DefaultConsumerBootstrap.java @@ -459,13 +459,13 @@ public void updateAllProviders(List groups) { private class ConsumerAttributeListener implements ConfigListener { // 可以动态配置的选项 - private final Set dynamicConfigKeys = new HashSet<>(); - private Map newValueMap = new HashMap<>(); + private final Set supportDynamicConfigKeys = new HashSet<>(); + private final Map 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 @@ -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 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); + } } } } diff --git a/config/config-apollo/src/main/java/com/alipay/sofa/rpc/dynamic/apollo/ApolloDynamicConfigManager.java b/config/config-apollo/src/main/java/com/alipay/sofa/rpc/dynamic/apollo/ApolloDynamicConfigManager.java index 0b4156a88..d7aec05c5 100644 --- a/config/config-apollo/src/main/java/com/alipay/sofa/rpc/dynamic/apollo/ApolloDynamicConfigManager.java +++ b/config/config-apollo/src/main/java/com/alipay/sofa/rpc/dynamic/apollo/ApolloDynamicConfigManager.java @@ -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 watchListenerMap = new ConcurrentHashMap<>(); - private Config config; + + private final Config config; protected ApolloDynamicConfigManager(String appName) { super(appName, SofaConfigs.getOrCustomDefault(DynamicConfigKeys.CONFIG_CENTER_ADDRESS, "")); diff --git a/config/config-apollo/src/test/java/com/alipay/sofa/rpc/dynamic/apollo/ApolloDynamicConfigManagerTest.java b/config/config-apollo/src/test/java/com/alipay/sofa/rpc/dynamic/apollo/ApolloDynamicConfigManagerTest.java index 2b08e90d3..1109a4d06 100644 --- a/config/config-apollo/src/test/java/com/alipay/sofa/rpc/dynamic/apollo/ApolloDynamicConfigManagerTest.java +++ b/config/config-apollo/src/test/java/com/alipay/sofa/rpc/dynamic/apollo/ApolloDynamicConfigManagerTest.java @@ -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() { diff --git a/config/config-nacos/src/main/java/com/alipay/sofa/rpc/dynamic/nacos/NacosDynamicConfigManager.java b/config/config-nacos/src/main/java/com/alipay/sofa/rpc/dynamic/nacos/NacosDynamicConfigManager.java index c0e958a1f..366681927 100644 --- a/config/config-nacos/src/main/java/com/alipay/sofa/rpc/dynamic/nacos/NacosDynamicConfigManager.java +++ b/config/config-nacos/src/main/java/com/alipay/sofa/rpc/dynamic/nacos/NacosDynamicConfigManager.java @@ -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 @@ -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) { @@ -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 listeners = new CopyOnWriteArraySet<>(); + /** * cache data to store old value */ - private Map cacheData = new ConcurrentHashMap<>(); - - @Override - public Executor getExecutor() { - return null; - } + private final Map cacheData = new ConcurrentHashMap<>(); /** * receive config change event diff --git a/config/config-zk/src/main/java/com/alipay/sofa/rpc/dynamic/zk/ZookeeperDynamicConfigManager.java b/config/config-zk/src/main/java/com/alipay/sofa/rpc/dynamic/zk/ZookeeperDynamicConfigManager.java index bb13595aa..b1539eeb0 100644 --- a/config/config-zk/src/main/java/com/alipay/sofa/rpc/dynamic/zk/ZookeeperDynamicConfigManager.java +++ b/config/config-zk/src/main/java/com/alipay/sofa/rpc/dynamic/zk/ZookeeperDynamicConfigManager.java @@ -187,9 +187,9 @@ private ZookeeperConfigListener createTargetListener(String pathKey) { public class ZookeeperConfigListener implements NodeCacheListener { - private String pathKey; - private Set listeners = new CopyOnWriteArraySet<>(); - private NodeCache nodeCache; + private final String pathKey; + private final Set listeners = new CopyOnWriteArraySet<>(); + private final NodeCache nodeCache; public ZookeeperConfigListener(String pathKey) { this.pathKey = pathKey; diff --git a/core/api/src/main/java/com/alipay/sofa/rpc/config/AbstractInterfaceConfig.java b/core/api/src/main/java/com/alipay/sofa/rpc/config/AbstractInterfaceConfig.java index 5cea34345..f3a236e09 100644 --- a/core/api/src/main/java/com/alipay/sofa/rpc/config/AbstractInterfaceConfig.java +++ b/core/api/src/main/java/com/alipay/sofa/rpc/config/AbstractInterfaceConfig.java @@ -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); } @@ -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); } diff --git a/core/api/src/main/java/com/alipay/sofa/rpc/dynamic/ConfigChangedEvent.java b/core/api/src/main/java/com/alipay/sofa/rpc/dynamic/ConfigChangedEvent.java index 3f133db37..220f2889d 100644 --- a/core/api/src/main/java/com/alipay/sofa/rpc/dynamic/ConfigChangedEvent.java +++ b/core/api/src/main/java/com/alipay/sofa/rpc/dynamic/ConfigChangedEvent.java @@ -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; @@ -29,6 +30,7 @@ */ public class ConfigChangedEvent extends EventObject { + private final static Logger LOGGER = LoggerFactory.getLogger(ConfigChangedEvent.class); private final String key; @@ -37,9 +39,7 @@ public class ConfigChangedEvent extends EventObject { private final ConfigChangeType changeType; - - private Map dynamicValueMap = new HashMap<>(); - ; + private final Map dynamicValueMap = new HashMap<>(); public ConfigChangedEvent(String key, String content) { this(key, content, ConfigChangeType.MODIFIED); @@ -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) { diff --git a/test/test-integration/src/test/java/com/alipay/sofa/rpc/test/config/ApolloDynamicConfigTest.java b/test/test-integration/src/test/java/com/alipay/sofa/rpc/test/config/ApolloDynamicConfigTest.java index 860b19eb7..4a9bbf20f 100644 --- a/test/test-integration/src/test/java/com/alipay/sofa/rpc/test/config/ApolloDynamicConfigTest.java +++ b/test/test-integration/src/test/java/com/alipay/sofa/rpc/test/config/ApolloDynamicConfigTest.java @@ -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 changes = new HashMap<>(); changes.put(configChange.getPropertyName(), configChange); @@ -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); diff --git a/test/test-integration/src/test/java/com/alipay/sofa/rpc/test/config/NacosDynamicConfigTest.java b/test/test-integration/src/test/java/com/alipay/sofa/rpc/test/config/NacosDynamicConfigTest.java index 150a6a03d..4a2b281fc 100644 --- a/test/test-integration/src/test/java/com/alipay/sofa/rpc/test/config/NacosDynamicConfigTest.java +++ b/test/test-integration/src/test/java/com/alipay/sofa/rpc/test/config/NacosDynamicConfigTest.java @@ -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"); @@ -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")); // 测试配置删除