Merge branch '2.7.2-release' of github.com:apache/dubbo into 2.7.2-release

This commit is contained in:
cvictory 2019-05-31 11:13:33 +08:00
commit 501e4dbdf9
5 changed files with 102 additions and 63 deletions

View File

@ -46,9 +46,6 @@ import static com.alibaba.nacos.api.PropertyKeyConst.NAMESPACE;
import static com.alibaba.nacos.api.PropertyKeyConst.SECRET_KEY;
import static com.alibaba.nacos.api.PropertyKeyConst.SERVER_ADDR;
import static com.alibaba.nacos.client.naming.utils.UtilAndComs.NACOS_NAMING_LOG_NAME;
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_CHAR_SEPERATOR;
import static org.apache.dubbo.common.constants.CommonConstants.PROPERTIES_CHAR_SEPERATOR;
import static org.apache.dubbo.configcenter.Constants.CONFIG_NAMESPACE_KEY;
import static org.apache.dubbo.common.constants.RemotingConstants.BACKUP_KEY;
/**
@ -57,11 +54,10 @@ import static org.apache.dubbo.common.constants.RemotingConstants.BACKUP_KEY;
public class NacosDynamicConfiguration implements DynamicConfiguration {
private final Logger logger = LoggerFactory.getLogger(getClass());
/**
* The final root path would be: /$NAME_SPACE/config
* the default timeout in millis to get config from nacos
*/
private String rootPath;
private static final long DEFAULT_TIMEOUT = 5000L;
/**
* The nacos configService
@ -75,7 +71,6 @@ public class NacosDynamicConfiguration implements DynamicConfiguration {
private final ConcurrentMap<String, NacosConfigListener> watchListenerMap;
NacosDynamicConfiguration(URL url) {
rootPath = url.getParameter(CONFIG_NAMESPACE_KEY, DEFAULT_GROUP) + "-config";
buildConfigService(url);
watchListenerMap = new ConcurrentHashMap<>();
}
@ -93,24 +88,6 @@ public class NacosDynamicConfiguration implements DynamicConfiguration {
return configService;
}
public void publishNacosConfig(String key, String value) {
try {
String[] keyAndGroup = getKeyAndGroup(key);
configService.publishConfig(keyAndGroup[0], keyAndGroup[1], value);
} catch (NacosException e) {
logger.error(e.getErrMsg());
}
}
private String[] getKeyAndGroup(String key) {
int i = key.lastIndexOf(GROUP_CHAR_SEPERATOR);
if (i < 0) {
return new String[]{key, null};
} else {
return new String[]{key.substring(0, i), key.substring(i+1)};
}
}
private Properties buildNacosProperties(URL url) {
Properties properties = new Properties();
setServerAddr(url, properties);
@ -162,43 +139,70 @@ public class NacosDynamicConfiguration implements DynamicConfiguration {
return configListener;
}
/**
* FIXME: 2019-05-30 to remove this function
* Nacos server does not support * as valid character of data-id.
* If a Dubbo service specifies group. For example:
*
* <dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService"
* version="1.0.0.test" group="test"/>
*
* The key passed to NacosDynamicConfiguration will be sth. like:
* test*org.apache.dubbo.demo.DemoService:1.0.0.test.configurators
*
* See logic in org.apache.dubbo.common.URL#getEncodedServiceKey()
*
* The purpose of this function is to convert the * into :, to keep align with
* the implementation in NacosRegistry.
*
* In the future this logic should be removed if Dubbo core can handle this.
* @param key
* @return
*/
private String normalizedKey(String key) {
return key.replaceFirst("\\*", ":");
}
@Override
public void addListener(String key, String group, ConfigurationListener listener) {
String[] keyAndGroup = getKeyAndGroup(key);
if (keyAndGroup[1] != null) {
group = keyAndGroup[1];
}
String finalGroup = group;
NacosConfigListener nacosConfigListener = watchListenerMap.computeIfAbsent(generateKey(key, group), k -> createTargetListener(key, finalGroup));
String keyInNacos = rootPath + PROPERTIES_CHAR_SEPERATOR + key;
String normalizedKey = normalizedKey(key);
NacosConfigListener nacosConfigListener = watchListenerMap.computeIfAbsent(normalizedKey, k -> createTargetListener(normalizedKey, group));
nacosConfigListener.addListener(listener);
try {
configService.addListener(keyInNacos, group, nacosConfigListener);
System.out.println("1");
configService.addListener(normalizedKey, group, nacosConfigListener);
} catch (NacosException e) {
logger.error(e.getMessage());
}
}
private String generateKey(String key, String group) {
if (StringUtils.isNotEmpty(group)) {
key = key + GROUP_CHAR_SEPERATOR + group;
}
return key;
}
@Override
public void removeListener(String key, String group, ConfigurationListener listener) {
NacosConfigListener eventListener = watchListenerMap.get(generateKey(key, group));
String normalizedKey = normalizedKey(key);
NacosConfigListener eventListener = watchListenerMap.get(normalizedKey);
if (eventListener != null) {
eventListener.removeListener(listener);
}
}
/**
* FIXME the model of Zookeeper and Nacos is inconsistent, need to remove this function in next release.
*/
@Override
public String getConfig(String key) {
return getConfig(key, DEFAULT_GROUP, -1L);
}
@Override
public String getConfig(String key, String group, long timeout) throws IllegalStateException {
key = generateKey(key, group);
return (String) getInternalProperty(rootPath + PROPERTIES_CHAR_SEPERATOR + key);
try {
String normalizedKey = normalizedKey(key);
long nacosTimeout = timeout < 0 ? DEFAULT_TIMEOUT : timeout;
return configService.getConfig(normalizedKey, group, nacosTimeout);
} catch (NacosException e) {
logger.error(e.getMessage());
}
return null;
}
@Override
@ -209,8 +213,8 @@ public class NacosDynamicConfiguration implements DynamicConfiguration {
@Override
public Object getInternalProperty(String key) {
try {
String[] keyAndGroup = getKeyAndGroup(key);
return configService.getConfig(keyAndGroup[0], keyAndGroup[1], 5000L);
String normalizedKey = normalizedKey(key);
return configService.getConfig(normalizedKey, DEFAULT_GROUP, DEFAULT_TIMEOUT);
} catch (NacosException e) {
logger.error(e.getMessage());
}

View File

@ -17,10 +17,14 @@
package org.apache.dubbo.configcenter.support.nacos;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.configcenter.ConfigChangeEvent;
import org.apache.dubbo.configcenter.ConfigurationListener;
import org.apache.dubbo.configcenter.DynamicConfiguration;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
@ -42,15 +46,22 @@ public class NacosDynamicConfigurationTest {
private static NacosDynamicConfiguration config;
/**
* A test client to put data to Nacos server for testing purpose
*/
private static ConfigService nacosClient;
@Test
public void testGetConfig() throws Exception {
put("dubbo-config-org.apache.dubbo.nacos.testService.configurators", "hello");
put("org.apache.dubbo.nacos.testService.configurators", "hello");
Thread.sleep(200);
put("dubbo-config-dubbo.properties:test", "aaa=bbb");
put("dubbo.properties", "test", "aaa=bbb");
Thread.sleep(200);
put("xxxx:org.apache.dubbo.demo.DemoService:1.0.0.test.configurators", "helloworld");
Thread.sleep(200);
Assertions.assertEquals("hello", config.getConfig("org.apache.dubbo.nacos.testService.configurators"));
Assertions.assertEquals("aaa=bbb", config.getConfig("dubbo.properties", "test"));
Assertions.assertEquals("helloworld", config.getConfig("xxxx*org.apache.dubbo.demo.DemoService:1.0.0.test.configurators"));
}
@Test
@ -67,19 +78,19 @@ public class NacosDynamicConfigurationTest {
config.addListener("testapp.tagrouters", listener3);
config.addListener("testapp.tagrouters", listener4);
put("dubbo-config-AService.configurators", "new value1");
put("AService.configurators", "new value1");
Thread.sleep(200);
put("dubbo-config-testapp.tagrouters", "new value2");
put("testapp.tagrouters", "new value2");
Thread.sleep(200);
put("dubbo-config-testapp", "new value3");
put("testapp", "new value3");
Thread.sleep(5000);
latch.await();
Assertions.assertEquals(1, listener1.getCount("dubbo-config-AService.configurators"));
Assertions.assertEquals(1, listener2.getCount("dubbo-config-AService.configurators"));
Assertions.assertEquals(1, listener3.getCount("dubbo-config-testapp.tagrouters"));
Assertions.assertEquals(1, listener4.getCount("dubbo-config-testapp.tagrouters"));
Assertions.assertEquals(1, listener1.getCount("AService.configurators"));
Assertions.assertEquals(1, listener2.getCount("AService.configurators"));
Assertions.assertEquals(1, listener3.getCount("testapp.tagrouters"));
Assertions.assertEquals(1, listener4.getCount("testapp.tagrouters"));
Assertions.assertEquals("new value1", listener1.getValue());
Assertions.assertEquals("new value1", listener2.getValue());
@ -89,8 +100,12 @@ public class NacosDynamicConfigurationTest {
}
private void put(String key, String value) {
put(key, DynamicConfiguration.DEFAULT_GROUP, value);
}
private void put(String key, String group, String value) {
try {
config.publishNacosConfig(key, value);
nacosClient.publishConfig(key, group, value);
} catch (Exception e) {
System.out.println("Error put value to nacos.");
}
@ -103,10 +118,18 @@ public class NacosDynamicConfigurationTest {
URL url = URL.valueOf(urlForDubbo)
.addParameter(SESSION_TIMEOUT_KEY, 15000);
config = new NacosDynamicConfiguration(url);
try {
nacosClient = NacosFactory.createConfigService("127.0.0.1:8848");
} catch (NacosException e) {
e.printStackTrace();
}
}
@AfterAll
public static void tearDown() {
}
private class TestListener implements ConfigurationListener {
@ -121,7 +144,7 @@ public class NacosDynamicConfigurationTest {
@Override
public void process(ConfigChangeEvent event) {
System.out.println(this + ": " + event);
Integer count = countMap.computeIfAbsent(event.getKey(), k -> new Integer(0));
Integer count = countMap.computeIfAbsent(event.getKey(), k -> 0);
countMap.put(event.getKey(), ++count);
value = event.getValue();
latch.countDown();

View File

@ -37,18 +37,28 @@ import static com.alibaba.nacos.api.PropertyKeyConst.ENDPOINT;
import static com.alibaba.nacos.api.PropertyKeyConst.NAMESPACE;
import static com.alibaba.nacos.api.PropertyKeyConst.CLUSTER_NAME;
import static com.alibaba.nacos.client.naming.utils.UtilAndComs.NACOS_NAMING_LOG_NAME;
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
import static org.apache.dubbo.common.constants.RemotingConstants.BACKUP_KEY;
/**
* metadata report impl for nacos
*/
public class NacosMetadataReport extends AbstractMetadataReport {
private static final Logger logger = LoggerFactory.getLogger(NacosMetadataReport.class);
private ConfigService configService;
/**
* The group used to store metadata in Nacos
*/
private String group;
public NacosMetadataReport(URL url) {
super(url);
this.configService = buildConfigService(url);
group = url.getParameter(GROUP_KEY, DEFAULT_ROOT);
}
public ConfigService buildConfigService(URL url) {
@ -113,7 +123,7 @@ public class NacosMetadataReport extends AbstractMetadataReport {
private void storeMetadata(MetadataIdentifier identifier, String value) {
try {
boolean publishResult = configService.publishConfig(identifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), identifier.getGroup(), value);
boolean publishResult = configService.publishConfig(identifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), group, value);
if (!publishResult) {
throw new RuntimeException("publish nacos metadata failed");
}

View File

@ -45,10 +45,12 @@ public class NacosMetadataReportTest {
private NacosMetadataReportFactory nacosMetadataReportFactory;
private ConfigService configService;
private static final String NACOS_GROUP = "zzz";
@BeforeEach
public void setUp() {
// timeout in 15 seconds.
URL url = URL.valueOf("nacos://127.0.0.1:8848")
URL url = URL.valueOf("nacos://127.0.0.1:8848?group=" + NACOS_GROUP)
.addParameter(SESSION_TIMEOUT_KEY, 15000);
nacosMetadataReportFactory = new NacosMetadataReportFactory();
this.nacosMetadataReport = (NacosMetadataReport) nacosMetadataReportFactory.createMetadataReport(url);
@ -66,7 +68,7 @@ public class NacosMetadataReportTest {
String application = "nacos-metdata-report-test";
MetadataIdentifier providerIdentifier =
storeProvider(nacosMetadataReport, TEST_SERVICE, version, group, application);
String serverContent = configService.getConfig(providerIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), group, 5000L);
String serverContent = configService.getConfig(providerIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), NACOS_GROUP, 5000L);
Assertions.assertNotNull(serverContent);
Gson gson = new Gson();
@ -81,7 +83,7 @@ public class NacosMetadataReportTest {
String application = "nacos-metadata-report-consumer-test";
MetadataIdentifier consumerIdentifier = storeConsumer(nacosMetadataReport, TEST_SERVICE, version, group, application);
String serverContent = configService.getConfig(consumerIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), group, 5000L);
String serverContent = configService.getConfig(consumerIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), NACOS_GROUP, 5000L);
Assertions.assertNotNull(serverContent);
Assertions.assertEquals(serverContent, "{\"paramConsumerTest\":\"nacosConsumer\"}");
}

View File

@ -383,7 +383,7 @@ public class NacosRegistry extends FailbackRegistry {
*/
private String[] getCategories(URL url) {
return ANY_VALUE.equals(url.getServiceInterface()) ?
ALL_SUPPORTED_CATEGORIES : of(DEFAULT_CATEGORY);
ALL_SUPPORTED_CATEGORIES : url.getParameter(CATEGORY_KEY,new String[]{DEFAULT_CATEGORY});
}
private URL buildURL(Instance instance) {