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 15, 2024
1 parent d841de9 commit eaba723
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ private class ConsumerAttributeListener implements ConfigListener {

@Override
public void process(ConfigChangedEvent event) {
// 清除上次的赋值,并保留赋值过的key
for (String key : newValueMap.keySet()) {
newValueMap.put(key, "");
}
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
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 @@ -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.DYNAMIC_URL.getKey(), "apollo://127.0.0.1:8080?cluster=default");
ApplicationConfig clientApplication = new ApplicationConfig();
clientApplication.setAppName("demo");

Expand All @@ -64,13 +64,33 @@ public void testApolloDynamicConfig() throws Exception {
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.DYNAMIC_URL.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.DYNAMIC_URL.getKey(),
"nacos://127.0.0.1:8848?username=nacos&password=nacos");
ApplicationConfig clientApplication = new ApplicationConfig();
clientApplication.setAppName("demo");

Expand All @@ -61,12 +62,19 @@ public void testNacosDynamicConfig() throws Exception {
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.DYNAMIC_URL.getKey());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.alipay.sofa.rpc.dynamic.DynamicConfigManager;
import com.alipay.sofa.rpc.dynamic.DynamicConfigManagerFactory;
import com.alipay.sofa.rpc.dynamic.zk.ZookeeperDynamicConfigManager;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import com.alipay.sofa.rpc.test.HelloService;
import com.alipay.sofa.rpc.test.config.base.BaseZkTest;
import org.apache.curator.framework.CuratorFramework;
Expand All @@ -37,6 +39,8 @@
*/
public class ZookeeperDynamicConfigTest extends BaseZkTest {

Logger logger = LoggerFactory.getLogger(ZookeeperDynamicConfigTest.class);

@Test
public void testZookeeperDynamicConfig() throws Exception {
System.setProperty(DynamicConfigKeys.DYNAMIC_URL.getKey(), "zookeeper://127.0.0.1:2181");
Expand All @@ -57,7 +61,8 @@ public void testZookeeperDynamicConfig() throws Exception {
Field field = ZookeeperDynamicConfigManager.class.getDeclaredField("zkClient");
field.setAccessible(true);
CuratorFramework zkClient = (CuratorFramework) field.get(dynamicConfigManager);
// 创建或更新配置节点

// 新增或修改配置节点
if (zkClient.checkExists().forPath("/config/demo/com.alipay.sofa.rpc.test.HelloService") == null) {
zkClient.create()
.creatingParentsIfNeeded()
Expand All @@ -67,14 +72,25 @@ public void testZookeeperDynamicConfig() throws Exception {
zkClient.setData().forPath("/sofa-rpc/config/demo/com.alipay.sofa.rpc.test.HelloService",
"timeout=5000".getBytes());
}

try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
}
// 验证配置是否更新
Assert.assertEquals(5000, consumerConfig.getMethodTimeout("sayHello"));

//删除配置节点
zkClient.delete().forPath("/config/demo/com.alipay.sofa.rpc.test.HelloService");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
logger.error(e.getMessage(), e);
}
// 验证配置是否删除
Assert.assertEquals(-1, consumerConfig.getMethodTimeout("sayHello"));

System.clearProperty(DynamicConfigKeys.DYNAMIC_URL.getKey());

}
}

0 comments on commit eaba723

Please sign in to comment.