You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by hu...@apache.org on 2019/05/30 04:40:34 UTC

[dubbo] branch dubbo-4200 created (now 02171c3)

This is an automated email from the ASF dual-hosted git repository.

huxing pushed a change to branch dubbo-4200
in repository https://gitbox.apache.org/repos/asf/dubbo.git.


      at 02171c3  [Dubbo-4200] Nacos group should be not resolved from key.

This branch includes the following new commits:

     new 02171c3  [Dubbo-4200] Nacos group should be not resolved from key.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[dubbo] 01/01: [Dubbo-4200] Nacos group should be not resolved from key.

Posted by hu...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

huxing pushed a commit to branch dubbo-4200
in repository https://gitbox.apache.org/repos/asf/dubbo.git

commit 02171c39c40a0e9570c63fb33d0d27b7dab9e84a
Author: Huxing Zhang <hu...@gmail.com>
AuthorDate: Thu May 30 12:39:57 2019 +0800

    [Dubbo-4200] Nacos group should be not resolved from key.
---
 .../support/nacos/NacosDynamicConfiguration.java   | 48 ++++++++--------------
 .../nacos/NacosDynamicConfigurationTest.java       | 10 +++--
 .../metadata/store/nacos/NacosMetadataReport.java  | 12 +++++-
 .../store/nacos/NacosMetadataReportTest.java       |  8 ++--
 4 files changed, 40 insertions(+), 38 deletions(-)

diff --git a/dubbo-configcenter/dubbo-configcenter-nacos/src/main/java/org/apache/dubbo/configcenter/support/nacos/NacosDynamicConfiguration.java b/dubbo-configcenter/dubbo-configcenter-nacos/src/main/java/org/apache/dubbo/configcenter/support/nacos/NacosDynamicConfiguration.java
index 96bec25..9fdb8eb 100644
--- a/dubbo-configcenter/dubbo-configcenter-nacos/src/main/java/org/apache/dubbo/configcenter/support/nacos/NacosDynamicConfiguration.java
+++ b/dubbo-configcenter/dubbo-configcenter-nacos/src/main/java/org/apache/dubbo/configcenter/support/nacos/NacosDynamicConfiguration.java
@@ -57,6 +57,10 @@ import static org.apache.dubbo.common.constants.RemotingConstants.BACKUP_KEY;
 public class NacosDynamicConfiguration implements DynamicConfiguration {
 
     private final Logger logger = LoggerFactory.getLogger(getClass());
+    /**
+     * the default timeout in millis to get config from nacos
+     */
+    private static final long DEFAULT_TIMEOUT = 5000L;
 
     /**
      * The final root path would be: /$NAME_SPACE/config
@@ -93,24 +97,14 @@ public class NacosDynamicConfiguration implements DynamicConfiguration {
         return configService;
     }
 
-    public void publishNacosConfig(String key, String value) {
+    public void publishNacosConfig(String key, String group, String value) {
         try {
-            String[] keyAndGroup = getKeyAndGroup(key);
-            configService.publishConfig(keyAndGroup[0], keyAndGroup[1], value);
+            configService.publishConfig(key, group, 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);
@@ -164,32 +158,19 @@ public class NacosDynamicConfiguration implements DynamicConfiguration {
 
     @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;
+        NacosConfigListener nacosConfigListener = watchListenerMap.computeIfAbsent(key, k -> createTargetListener(keyInNacos, group));
         nacosConfigListener.addListener(listener);
         try {
             configService.addListener(keyInNacos, group, nacosConfigListener);
-            System.out.println("1");
         } 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));
+        NacosConfigListener eventListener = watchListenerMap.get(key);
         if (eventListener != null) {
             eventListener.removeListener(listener);
         }
@@ -197,8 +178,14 @@ public class NacosDynamicConfiguration implements DynamicConfiguration {
 
     @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 keyInNacos = rootPath + PROPERTIES_CHAR_SEPERATOR + key;
+            long nacosTimeout = timeout < 0 ?  DEFAULT_TIMEOUT : timeout;
+            return configService.getConfig(keyInNacos, group, nacosTimeout);
+        } catch (NacosException e) {
+            logger.error(e.getMessage());
+        }
+        return null;
     }
 
     @Override
@@ -209,8 +196,7 @@ public class NacosDynamicConfiguration implements DynamicConfiguration {
     @Override
     public Object getInternalProperty(String key) {
         try {
-            String[] keyAndGroup = getKeyAndGroup(key);
-            return configService.getConfig(keyAndGroup[0], keyAndGroup[1], 5000L);
+            return configService.getConfig(key, null, 5000L);
         } catch (NacosException e) {
             logger.error(e.getMessage());
         }
diff --git a/dubbo-configcenter/dubbo-configcenter-nacos/src/test/java/org/apache/dubbo/configcenter/support/nacos/NacosDynamicConfigurationTest.java b/dubbo-configcenter/dubbo-configcenter-nacos/src/test/java/org/apache/dubbo/configcenter/support/nacos/NacosDynamicConfigurationTest.java
index ff785b9..df65c63 100644
--- a/dubbo-configcenter/dubbo-configcenter-nacos/src/test/java/org/apache/dubbo/configcenter/support/nacos/NacosDynamicConfigurationTest.java
+++ b/dubbo-configcenter/dubbo-configcenter-nacos/src/test/java/org/apache/dubbo/configcenter/support/nacos/NacosDynamicConfigurationTest.java
@@ -47,7 +47,7 @@ public class NacosDynamicConfigurationTest {
 
         put("dubbo-config-org.apache.dubbo.nacos.testService.configurators", "hello");
         Thread.sleep(200);
-        put("dubbo-config-dubbo.properties:test", "aaa=bbb");
+        put("dubbo-config-dubbo.properties", "test", "aaa=bbb");
         Thread.sleep(200);
         Assertions.assertEquals("hello", config.getConfig("org.apache.dubbo.nacos.testService.configurators"));
         Assertions.assertEquals("aaa=bbb", config.getConfig("dubbo.properties", "test"));
@@ -89,8 +89,12 @@ public class NacosDynamicConfigurationTest {
     }
 
     private void put(String key, String value) {
+        put(key, "", value);
+    }
+
+    private void put(String key, String group, String value) {
         try {
-            config.publishNacosConfig(key, value);
+            config.publishNacosConfig(key, "dubbo", value);
         } catch (Exception e) {
             System.out.println("Error put value to nacos.");
         }
@@ -121,7 +125,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();
diff --git a/dubbo-metadata-report/dubbo-metadata-report-nacos/src/main/java/org/apache/dubbo/metadata/store/nacos/NacosMetadataReport.java b/dubbo-metadata-report/dubbo-metadata-report-nacos/src/main/java/org/apache/dubbo/metadata/store/nacos/NacosMetadataReport.java
index 03bdeb8..b12e4f0 100644
--- a/dubbo-metadata-report/dubbo-metadata-report-nacos/src/main/java/org/apache/dubbo/metadata/store/nacos/NacosMetadataReport.java
+++ b/dubbo-metadata-report/dubbo-metadata-report-nacos/src/main/java/org/apache/dubbo/metadata/store/nacos/NacosMetadataReport.java
@@ -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");
             }
diff --git a/dubbo-metadata-report/dubbo-metadata-report-nacos/src/test/java/org/apache/dubbo/metadata/store/nacos/NacosMetadataReportTest.java b/dubbo-metadata-report/dubbo-metadata-report-nacos/src/test/java/org/apache/dubbo/metadata/store/nacos/NacosMetadataReportTest.java
index 07d6f8b..2f6b9e5 100644
--- a/dubbo-metadata-report/dubbo-metadata-report-nacos/src/test/java/org/apache/dubbo/metadata/store/nacos/NacosMetadataReportTest.java
+++ b/dubbo-metadata-report/dubbo-metadata-report-nacos/src/test/java/org/apache/dubbo/metadata/store/nacos/NacosMetadataReportTest.java
@@ -45,10 +45,12 @@ public class NacosMetadataReportTest {
     private NacosMetadataReportFactory nacosMetadataReportFactory;
     private ConfigService configService;
 
+    private static final String 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=" + 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), "zzz", 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), "zzz", 5000L);
         Assertions.assertNotNull(serverContent);
         Assertions.assertEquals(serverContent, "{\"paramConsumerTest\":\"nacosConsumer\"}");
     }