Skip to content

Commit

Permalink
Modify dynamic config test
Browse files Browse the repository at this point in the history
  • Loading branch information
Narzisss committed Oct 25, 2024
1 parent d841de9 commit 7a354b3
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@
import com.alipay.sofa.rpc.registry.Registry;
import com.alipay.sofa.rpc.registry.RegistryFactory;

import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -167,7 +172,7 @@ public T refer() {
}

//接口级别动态配置参数
final String dynamicUrl = getOrDefault(DynamicConfigKeys.DYNAMIC_URL);
final String dynamicUrl = getOrDefault(DynamicConfigKeys.CENTER_ADDRESS);
if ( StringUtils.isNotBlank(dynamicUrl)) {
//启用接口级别动态配置
final DynamicConfigManager dynamicManager = DynamicConfigManagerFactory.getDynamicManagerWithUrl(
Expand Down Expand Up @@ -453,38 +458,40 @@ private class ConsumerAttributeListener implements ConfigListener {
private Map<String, String> newValueMap = new HashMap<>();

// 动态配置项
private List<String> dynamicConfigKeys = Arrays.asList(RpcConstants.CONFIG_KEY_TIMEOUT,
RpcConstants.CONFIG_KEY_RETRIES, RpcConstants.CONFIG_KEY_LOADBALANCER);
private final Set<String> dynamicConfigKeys = new HashSet<>();

ConsumerAttributeListener() {

dynamicConfigKeys.add(RpcConstants.CONFIG_KEY_TIMEOUT);
dynamicConfigKeys.add(RpcConstants.CONFIG_KEY_RETRIES);
dynamicConfigKeys.add(RpcConstants.CONFIG_KEY_LOADBALANCER);
}

@Override
public void process(ConfigChangedEvent event) {
for (String key : newValueMap.keySet()) {
newValueMap.put(key, "");
}
// 清除上次的赋值,并保留赋值过的key
newValueMap.replaceAll((k, v) -> "");
if (!event.getChangeType().equals(ConfigChangeType.DELETED)) {
// ADDED or MODIFIED
String[] lines = event.getContent().split("\n");
for (String line : lines) {
String[] keyValue = line.split("=", 2);
if (keyValue.length == 2) {
String key = keyValue[0].trim();
String value = keyValue[1].trim();
for (String dynamicConfigKey : dynamicConfigKeys) {
if (key.equals(dynamicConfigKey) || key.endsWith("." + dynamicConfigKey)) {
newValueMap.put(key, value);
break;
}
}
} else {
LOGGER.warn("Malformed configuration line: {}", line);
parseConfigurationLines(event.getContent());
}
attrUpdated(newValueMap);
}

private void parseConfigurationLines(String content) {
String[] lines = content.split("\n");
for (String line : lines) {
String[] keyValue = line.split("=", 2);
if (keyValue.length == 2) {
String key = keyValue[0].trim();
String value = keyValue[1].trim();
String tempKey = key.lastIndexOf(".") == -1 ? key : key.substring(key.lastIndexOf(".") + 1);
if (dynamicConfigKeys.contains(tempKey)) {
newValueMap.put(key, value);
}
} else {
LOGGER.warn("Malformed configuration line: {}", line);
}
}
attrUpdated(newValueMap);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.ctrip.framework.apollo.model.ConfigChange;

import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
Expand Down Expand Up @@ -71,16 +72,10 @@ protected ApolloDynamicConfigManager(String appName, String remainUrl) {
super(appName, remainUrl);
System.setProperty(APOLLO_APPID_KEY, appName);
System.setProperty(APOLLO_ADDR_KEY, APOLLO_PROTOCOL_PREFIX + getAddress());
String params[] = getParams();
if (params!= null && params.length > 0){
for (String param : params) {
String[] keyValue = param.split("=");
if (keyValue.length == 2) {
if ("cluster".equals(keyValue[0])) {
System.setProperty(APOLLO_CLUSTER_KEY, keyValue[1]);
}
}
}
Map params = getParams();
if (params != null && params.containsKey("cluster")) {
String clusterValue = (String)params.get("cluster");
System.setProperty(APOLLO_CLUSTER_KEY, clusterValue);
}
config = ConfigService.getAppConfig();
}
Expand Down Expand Up @@ -158,6 +153,9 @@ private ConfigChangeType getChangeType(ConfigChange change) {
if (change.getChangeType() == PropertyChangeType.DELETED) {
return ConfigChangeType.DELETED;
}
if (change.getChangeType() == PropertyChangeType.ADDED) {
return ConfigChangeType.ADDED;
}
return ConfigChangeType.MODIFIED;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,15 @@ protected NacosDynamicConfigManager(String appName, String remainUrl) {
super(appName, remainUrl);
group = appName;
nacosConfig.put(PropertyKeyConst.SERVER_ADDR, getAddress());
String params[] = getParams();
if (params != null && params.length > 0) {
for (String param : params) {
String[] keyValue = param.split("=");
if (keyValue.length == 2) {
if ("username".equals(keyValue[0])) {
nacosConfig.put(PropertyKeyConst.USERNAME, keyValue[1]);
} else if ("password".equals(keyValue[0])) {
nacosConfig.put(PropertyKeyConst.PASSWORD, keyValue[1]);
}
}
Map params = getParams();
if (params != null) {
if( params.containsKey("username")) {
String username = (String)params.get("username");
nacosConfig.put(PropertyKeyConst.USERNAME, username);
}
if( params.containsKey("password")) {
String password = (String) params.get("password");
nacosConfig.put(PropertyKeyConst.PASSWORD, password);
}
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,35 @@ public class DynamicConfigKeys {

public static final String DEFAULT_NAMESPACE = "sofa-rpc";

public static ConfigKey<String> DYNAMIC_URL = ConfigKey
public static ConfigKey<String> CENTER_ADDRESS = ConfigKey
.build(
"dynamicUrl",
"sofa.rpc.config.center.address",
" ",
false,
"The url of the dynamic configuration.",
new String[] { "dynamicUrl" });
new String[] { "sofa_rpc_config_center_address" });

public static ConfigKey<String> ZK_ADDRESS = ConfigKey
.build(
"sofa.rpc.config.center.zookeeper.address",
"127.0.0.1:2181",
false,
"The address of Zookeeper configuration center.",
new String[] { "zookeeper_address" });
new String[] { "sofa_rpc_config_center_zookeeper_address" });

public static ConfigKey<String> NACOS_ADDRESS = ConfigKey
.build(
"sofa.rpc.config.center.nacos.address",
"127.0.0.1:8848",
false,
"The address of Nacos configuration center.",
new String[] { "nacos_address" });
new String[] { "sofa_rpc_config_center_nacos_address" });
public static ConfigKey<String> APOLLO_ADDRESS = ConfigKey
.build(
"sofa.rpc.config.center.apollo.address",
"127.0.0.1:8080",
false,
"The address of Apollo configuration center.",
new String[] { "apollo_address" });
new String[] { "sofa_rpc_config_center_apollo_address" });

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import com.alipay.sofa.rpc.ext.Extensible;
import com.alipay.sofa.rpc.listener.ConfigListener;

import java.util.HashMap;
import java.util.Map;

/**
*
* @author bystander
Expand All @@ -32,15 +35,25 @@ public abstract class DynamicConfigManager {

private String address;

private String params[];
private Map<String, String> params = new HashMap<>();

protected DynamicConfigManager(String appName, String remainUrl) {
this.appName = appName;
parseRemainUrl(remainUrl);
}

protected void parseRemainUrl(String remainUrl) {
int queryIndex = remainUrl.indexOf("?");
this.address = (queryIndex > -1) ? remainUrl.substring(0, queryIndex) : remainUrl;
if (queryIndex > -1 && queryIndex < remainUrl.length() - 1) {
String query = remainUrl.substring(queryIndex + 1);
this.params = query.split("&");
String[] paramPairs = query.split("&");
for (String paramPair : paramPairs) {
String[] keyValue = paramPair.split("=");
if (keyValue.length == 2) {
this.params.put(keyValue[0], keyValue[1]);
}
}
}
}

Expand All @@ -52,7 +65,7 @@ protected String getAddress() {
return address;
}

protected String[] getParams() {
protected Map getParams() {
return params;
}

Expand Down
4 changes: 2 additions & 2 deletions test/test-integration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,13 @@
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-rpc-config-apollo</artifactId>
<version>5.13.1-SNAPSHOT</version>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-rpc-config-zk</artifactId>
<version>5.13.1-SNAPSHOT</version>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class ApolloDynamicConfigTest {

@Test
public void testApolloDynamicConfig() throws Exception {
System.setProperty(DynamicConfigKeys.DYNAMIC_URL.getKey(), "apollo://127.0.0.1:8080");
System.setProperty(DynamicConfigKeys.CENTER_ADDRESS.getKey(), "apollo://127.0.0.1:8080?cluster=default");
ApplicationConfig clientApplication = new ApplicationConfig();
clientApplication.setAppName("demo");

Expand All @@ -56,21 +56,41 @@ public void testApolloDynamicConfig() throws Exception {

// 获取接口对应的动态配置监听器
DynamicConfigManager dynamicConfigManager = DynamicConfigManagerFactory.getDynamicManagerWithUrl
(clientApplication.getAppName(), System.getProperty(DynamicConfigKeys.DYNAMIC_URL.getKey()));
(clientApplication.getAppName(), System.getProperty(DynamicConfigKeys.CENTER_ADDRESS.getKey()));
Field field = ApolloDynamicConfigManager.class.getDeclaredField("watchListenerMap");
field.setAccessible(true);
Map<String, ApolloDynamicConfigManager.ApolloListener> watchListenerMap = (Map<String, ApolloDynamicConfigManager.ApolloListener>) field
.get(dynamicConfigManager);
ApolloDynamicConfigManager.ApolloListener apolloConfigListener = watchListenerMap.get(consumerConfig
.getInterfaceId());

// 测试配置更新
String configValue = "timeout=5000\n.sayHello.timeout=6000";
// 测试配置新增
String configValue = "timeout=5000\n";
ConfigChange configChange = new ConfigChange("application", consumerConfig.getInterfaceId(), null, configValue, PropertyChangeType.ADDED);
Map<String, ConfigChange> changes= new HashMap<>();
changes.put(configChange.getPropertyName(), configChange);
ConfigChangeEvent event = new ConfigChangeEvent("application",changes);
apolloConfigListener.onChange(event);
Assert.assertEquals(5000, consumerConfig.getMethodTimeout("sayHello"));

// 测试配置修改
String oldValue = configValue;
configValue = "timeout=5000\n.sayHello.timeout=6000";
configChange = new ConfigChange("application", consumerConfig.getInterfaceId(), oldValue, configValue, PropertyChangeType.MODIFIED);
changes= new HashMap<>();
changes.put(configChange.getPropertyName(), configChange);
event = new ConfigChangeEvent("application",changes);
apolloConfigListener.onChange(event);
Assert.assertEquals(6000, consumerConfig.getMethodTimeout("sayHello"));

// 测试配置删除
configChange = new ConfigChange("application", consumerConfig.getInterfaceId(), configValue, null, PropertyChangeType.DELETED);
changes= new HashMap<>();
changes.put(configChange.getPropertyName(), configChange);
event = new ConfigChangeEvent("application",changes);
apolloConfigListener.onChange(event);
Assert.assertEquals(-1, consumerConfig.getMethodTimeout("sayHello"));

System.clearProperty(DynamicConfigKeys.CENTER_ADDRESS.getKey());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public class NacosDynamicConfigTest {

@Test
public void testNacosDynamicConfig() throws Exception {
System.setProperty(DynamicConfigKeys.DYNAMIC_URL.getKey(), "nacos://127.0.0.1:8848");
System.setProperty(DynamicConfigKeys.CENTER_ADDRESS.getKey(),
"nacos://127.0.0.1:8848?username=nacos&password=nacos");
ApplicationConfig clientApplication = new ApplicationConfig();
clientApplication.setAppName("demo");

Expand All @@ -53,20 +54,27 @@ public void testNacosDynamicConfig() throws Exception {

// 获取接口对应的动态配置监听器
DynamicConfigManager dynamicConfigManager = DynamicConfigManagerFactory.getDynamicManagerWithUrl
(clientApplication.getAppName(), System.getProperty(DynamicConfigKeys.DYNAMIC_URL.getKey()));
(clientApplication.getAppName(), System.getProperty(DynamicConfigKeys.CENTER_ADDRESS.getKey()));
Field field = NacosDynamicConfigManager.class.getDeclaredField("watchListenerMap");
field.setAccessible(true);
Map<String, NacosDynamicConfigManager.NacosConfigListener> watchListenerMap = (Map<String, NacosDynamicConfigManager.NacosConfigListener>) field
.get(dynamicConfigManager);
NacosDynamicConfigManager.NacosConfigListener nacosConfigListener = watchListenerMap.get(consumerConfig
.getInterfaceId());

// 测试配置更新
// 测试配置新增
String configValue = "timeout=5000";
nacosConfigListener.innerReceive(consumerConfig.getInterfaceId(), consumerConfig.getAppName(), configValue);
Assert.assertEquals(5000, consumerConfig.getMethodTimeout("sayHello"));
// 测试配置修改
configValue = "timeout=5000\n.sayHello.timeout=6000";
nacosConfigListener.innerReceive(consumerConfig.getInterfaceId(), consumerConfig.getAppName(), configValue);
Assert.assertEquals(6000, consumerConfig.getMethodTimeout("sayHello"));
// 测试配置删除
configValue = "";
nacosConfigListener.innerReceive(consumerConfig.getInterfaceId(), consumerConfig.getAppName(), configValue);
Assert.assertEquals(-1, consumerConfig.getMethodTimeout("sayHello"));

System.clearProperty(DynamicConfigKeys.CENTER_ADDRESS.getKey());
}
}
Loading

0 comments on commit 7a354b3

Please sign in to comment.