You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2020/07/27 07:36:44 UTC

[dubbo] branch 3.0 updated (53f2322 -> c63b990)

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

liujun pushed a change to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git.


    from 53f2322  customize instance metadata.
     new 0cd52b9  unify registry-cluster key
     new f5b2ded  fix compilation error
     new c63b990  add demo

The 3 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.


Summary of changes:
 .../dubbo/common/constants/RegistryConstants.java  |  4 +++-
 .../apache/dubbo/demo/consumer/Application.java    | 19 +++++++++++++++
 .../src/main/resources/spring/dubbo-consumer.xml   |  3 +++
 .../dubbo/demo/provider/GreetingServiceImpl.java   | 15 +++++-------
 .../src/main/resources/spring/dubbo-provider.xml   |  7 ++++--
 .../org/apache/dubbo/metadata/MetadataInfo.java    | 28 +++++++++++++++++-----
 .../apache/dubbo/metadata/MetadataInfoTest.java    |  2 +-
 .../client/DefaultRegistryClusterIdentifier.java   |  6 ++---
 .../registry/client/DefaultServiceInstance.java    |  1 +
 .../dubbo/registry/client/InstanceAddressURL.java  | 12 +++++++++-
 .../registry/client/RegistryClusterIdentifier.java |  7 +++---
 .../registry/client/ServiceDiscoveryRegistry.java  | 25 +++++++++----------
 .../listener/ServiceInstancesChangedListener.java  |  5 ++--
 ...MetadataServiceURLParamsMetadataCustomizer.java |  2 +-
 .../metadata/ServiceInstanceMetadataUtils.java     |  4 ++--
 .../store/InMemoryWritableMetadataService.java     | 20 ++++++++--------
 .../metadata/store/RemoteMetadataServiceImpl.java  | 15 ++++++------
 17 files changed, 114 insertions(+), 61 deletions(-)
 copy dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/DemoServiceImpl.java => dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/GreetingServiceImpl.java (79%)


[dubbo] 02/03: fix compilation error

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

liujun pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git

commit f5b2ded03e22c55b3c998a51e1326e2bb223299d
Author: ken.lj <ke...@gmail.com>
AuthorDate: Mon Jul 27 15:35:38 2020 +0800

    fix compilation error
---
 .../client/metadata/store/InMemoryWritableMetadataService.java        | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/store/InMemoryWritableMetadataService.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/store/InMemoryWritableMetadataService.java
index f0281da..44c6bbc 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/store/InMemoryWritableMetadataService.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/store/InMemoryWritableMetadataService.java
@@ -125,7 +125,7 @@ public class InMemoryWritableMetadataService implements WritableMetadataService
 
     @Override
     public boolean exportURL(URL url) {
-        String registryCluster = RegistryClusterIdentifier.getExtension().providerKey(url);
+        String registryCluster = RegistryClusterIdentifier.getExtension(url).providerKey(url);
         String[] clusters = registryCluster.split(",");
         for (String cluster : clusters) {
             MetadataInfo metadataInfo = metadataInfos.computeIfAbsent(cluster, k -> {
@@ -139,7 +139,7 @@ public class InMemoryWritableMetadataService implements WritableMetadataService
 
     @Override
     public boolean unexportURL(URL url) {
-        String registryCluster = RegistryClusterIdentifier.getExtension().providerKey(url);
+        String registryCluster = RegistryClusterIdentifier.getExtension(url).providerKey(url);
         String[] clusters = registryCluster.split(",");
         for (String cluster : clusters) {
             MetadataInfo metadataInfo = metadataInfos.get(cluster);


[dubbo] 01/03: unify registry-cluster key

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

liujun pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git

commit 0cd52b940c28e7fa94376f4a1e8f83e5da5b8627
Author: ken.lj <ke...@gmail.com>
AuthorDate: Mon Jul 27 15:20:51 2020 +0800

    unify registry-cluster key
---
 .../dubbo/common/constants/RegistryConstants.java  |  4 +++-
 .../org/apache/dubbo/metadata/MetadataInfo.java    | 28 +++++++++++++++++-----
 .../apache/dubbo/metadata/MetadataInfoTest.java    |  2 +-
 .../client/DefaultRegistryClusterIdentifier.java   |  6 ++---
 .../registry/client/DefaultServiceInstance.java    |  1 +
 .../dubbo/registry/client/InstanceAddressURL.java  | 12 +++++++++-
 .../registry/client/RegistryClusterIdentifier.java |  7 +++---
 .../registry/client/ServiceDiscoveryRegistry.java  | 25 +++++++++----------
 .../listener/ServiceInstancesChangedListener.java  |  5 ++--
 ...MetadataServiceURLParamsMetadataCustomizer.java |  2 +-
 .../metadata/ServiceInstanceMetadataUtils.java     |  4 ++--
 .../store/InMemoryWritableMetadataService.java     | 20 ++++++++--------
 .../metadata/store/RemoteMetadataServiceImpl.java  | 15 ++++++------
 13 files changed, 81 insertions(+), 50 deletions(-)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/RegistryConstants.java b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/RegistryConstants.java
index 420a909..d25e9fc 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/RegistryConstants.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/RegistryConstants.java
@@ -21,7 +21,9 @@ public interface RegistryConstants {
 
     String REGISTRY_KEY = "registry";
 
-    String REGISTRY_CLUSTER = "REGISTRY_CLUSTER";
+    String REGISTRY_CLUSTER_KEY = "REGISTRY_CLUSTER";
+
+    String REGISTRY_CLUSTER_TYPE_KEY = "registry-cluster-type";
 
     String REGISTRY_PROTOCOL = "registry";
 
diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataInfo.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataInfo.java
index 315e56e..47e8705 100644
--- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataInfo.java
+++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataInfo.java
@@ -47,6 +47,7 @@ public class MetadataInfo implements Serializable {
     private String revision;
     private Map<String, ServiceInfo> services;
 
+    // used at runtime
     private transient Map<String, String> extendParams;
     private transient AtomicBoolean reported = new AtomicBoolean(false);
 
@@ -85,7 +86,7 @@ public class MetadataInfo implements Serializable {
         markChanged();
     }
 
-    public String getRevision() {
+    public String calAndGetRevision() {
         if (revision != null && hasReported()) {
             return revision;
         }
@@ -161,8 +162,11 @@ public class MetadataInfo implements Serializable {
 
     @Override
     public String toString() {
-        // FIXME
-        return super.toString();
+        return "metadata{" +
+                "app='" + app + "'," +
+                "revision='" + revision + "'," +
+                "services=" + services +
+                "}";
     }
 
     public static class ServiceInfo implements Serializable {
@@ -174,12 +178,17 @@ public class MetadataInfo implements Serializable {
         private String path; // most of the time, path is the same with the interface name.
         private Map<String, String> params;
 
+        // params configuried on consumer side,
         private transient Map<String, String> consumerParams;
+        // cached method params
         private transient Map<String, Map<String, String>> methodParams;
         private transient Map<String, Map<String, String>> consumerMethodParams;
-        private volatile transient Map<String, Number> numbers;
-        private volatile transient Map<String, Map<String, Number>> methodNumbers;
+        // cached numbers
+        private transient Map<String, Number> numbers;
+        private transient Map<String, Map<String, Number>> methodNumbers;
+        // service + group + version
         private transient String serviceKey;
+        // service + group + version + protocol
         private transient String matchKey;
 
         public ServiceInfo() {
@@ -420,7 +429,14 @@ public class MetadataInfo implements Serializable {
 
         @Override
         public String toString() {
-            return super.toString();
+            return "service{" +
+                    "name='" + name + "'," +
+                    "group='" + group + "'," +
+                    "version='" + version + "'," +
+                    "protocol='" + protocol + "'," +
+                    "params=" + params + "," +
+                    "consumerParams=" + consumerParams +
+                    "}";
         }
     }
 }
diff --git a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/MetadataInfoTest.java b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/MetadataInfoTest.java
index be8c3fe..fdb7021 100644
--- a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/MetadataInfoTest.java
+++ b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/MetadataInfoTest.java
@@ -31,6 +31,6 @@ public class MetadataInfoTest {
         metadataInfo.addService(serviceInfo);
 
         System.out.println(serviceInfo.toDescString());
-        System.out.println(metadataInfo.getRevision());
+        System.out.println(metadataInfo.calAndGetRevision());
     }
 }
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultRegistryClusterIdentifier.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultRegistryClusterIdentifier.java
index 6c88265..42cc10b 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultRegistryClusterIdentifier.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultRegistryClusterIdentifier.java
@@ -18,16 +18,16 @@ package org.apache.dubbo.registry.client;
 
 import org.apache.dubbo.common.URL;
 
-import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_CLUSTER;
+import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_CLUSTER_KEY;
 
 public class DefaultRegistryClusterIdentifier implements RegistryClusterIdentifier {
     @Override
     public String providerKey(URL url) {
-        return url.getParameter(REGISTRY_CLUSTER);
+        return url.getParameter(REGISTRY_CLUSTER_KEY);
     }
 
     @Override
     public String consumerKey(URL url) {
-        return url.getParameter(REGISTRY_CLUSTER);
+        return url.getParameter(REGISTRY_CLUSTER_KEY);
     }
 }
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultServiceInstance.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultServiceInstance.java
index 3c7204d..eb581f9 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultServiceInstance.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultServiceInstance.java
@@ -49,6 +49,7 @@ public class DefaultServiceInstance implements ServiceInstance {
 
     private transient String address;
     private transient MetadataInfo serviceMetadata;
+    // used at runtime
     private transient Map<String, String> extendParams = new HashMap<>();
 
     public DefaultServiceInstance() {
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/InstanceAddressURL.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/InstanceAddressURL.java
index ca8f9be..f4ec3fe 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/InstanceAddressURL.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/InstanceAddressURL.java
@@ -32,6 +32,8 @@ import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
 public class InstanceAddressURL extends URL {
     private ServiceInstance instance;
     private MetadataInfo metadataInfo;
+
+    // cached numbers
     private volatile transient Map<String, Number> numbers;
     private volatile transient Map<String, Map<String, Number>> methodNumbers;
 
@@ -353,8 +355,16 @@ public class InstanceAddressURL extends URL {
         return getInstance().hashCode();
     }
 
+    public String getServiceString(String service) {
+        MetadataInfo.ServiceInfo serviceInfo = metadataInfo.getServiceInfo(service);
+        if (serviceInfo == null) {
+            return instance.toString();
+        }
+        return instance.toString() + serviceInfo.toString();
+    }
+
     @Override
     public String toString() {
-        return super.toString();
+        return instance.toString() + metadataInfo.toString();
     }
 }
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/RegistryClusterIdentifier.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/RegistryClusterIdentifier.java
index 76d141a..9d0e4a0 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/RegistryClusterIdentifier.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/RegistryClusterIdentifier.java
@@ -20,16 +20,17 @@ import org.apache.dubbo.common.URL;
 import org.apache.dubbo.common.extension.ExtensionLoader;
 import org.apache.dubbo.common.extension.SPI;
 
+import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_CLUSTER_TYPE_KEY;
+
 @SPI
 public interface RegistryClusterIdentifier {
     String providerKey(URL url);
 
     String consumerKey(URL url);
 
-    static RegistryClusterIdentifier getExtension() {
+    static RegistryClusterIdentifier getExtension(URL url) {
         ExtensionLoader<RegistryClusterIdentifier> loader
                 = ExtensionLoader.getExtensionLoader(RegistryClusterIdentifier.class);
-//        return loader.getExtension(ConfigurationUtils.getProperty("dubbo.application.sd.type", "default"));
-        return loader.getExtension("default");
+        return loader.getExtension(url.getParameter(REGISTRY_CLUSTER_TYPE_KEY, "default"));
     }
 }
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java
index f3d41c5..efd3c57 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java
@@ -62,8 +62,7 @@ import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE;
 import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY;
 import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
 import static org.apache.dubbo.common.constants.RegistryConstants.PROVIDED_BY;
-import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_CLUSTER;
-import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_KEY;
+import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_CLUSTER_KEY;
 import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_TYPE_KEY;
 import static org.apache.dubbo.common.constants.RegistryConstants.SERVICE_REGISTRY_TYPE;
 import static org.apache.dubbo.common.constants.RegistryConstants.SUBSCRIBED_SERVICE_NAMES_KEY;
@@ -206,8 +205,8 @@ public class ServiceDiscoveryRegistry implements Registry {
 
     public void doRegister(URL url) {
         String registryCluster = serviceDiscovery.getUrl().getParameter(ID_KEY);
-        if (registryCluster != null) {
-            url = url.addParameter(REGISTRY_CLUSTER, registryCluster);
+        if (registryCluster != null && url.getParameter(REGISTRY_CLUSTER_KEY) == null) {
+            url = url.addParameter(REGISTRY_CLUSTER_KEY, registryCluster);
         }
         if (writableMetadataService.exportURL(url)) {
             if (logger.isInfoEnabled()) {
@@ -230,8 +229,8 @@ public class ServiceDiscoveryRegistry implements Registry {
 
     public void doUnregister(URL url) {
         String registryCluster = serviceDiscovery.getUrl().getParameter(ID_KEY);
-        if (registryCluster != null) {
-            url = url.addParameter(REGISTRY_CLUSTER, registryCluster);
+        if (registryCluster != null && url.getParameter(REGISTRY_CLUSTER_KEY) == null) {
+            url = url.addParameter(REGISTRY_CLUSTER_KEY, registryCluster);
         }
         if (writableMetadataService.unexportURL(url)) {
             if (logger.isInfoEnabled()) {
@@ -250,8 +249,8 @@ public class ServiceDiscoveryRegistry implements Registry {
             return;
         }
         String registryCluster = serviceDiscovery.getUrl().getParameter(ID_KEY);
-        if (registryCluster != null) {
-            url = url.addParameter(REGISTRY_KEY, registryCluster);
+        if (registryCluster != null && url.getParameter(REGISTRY_CLUSTER_KEY) == null) {
+            url = url.addParameter(REGISTRY_CLUSTER_KEY, registryCluster);
         }
         doSubscribe(url, listener);
     }
@@ -273,8 +272,8 @@ public class ServiceDiscoveryRegistry implements Registry {
             return;
         }
         String registryCluster = serviceDiscovery.getUrl().getParameter(ID_KEY);
-        if (registryCluster != null) {
-            url = url.addParameter(REGISTRY_KEY, registryCluster);
+        if (registryCluster != null && url.getParameter(REGISTRY_CLUSTER_KEY) == null) {
+            url = url.addParameter(REGISTRY_CLUSTER_KEY, registryCluster);
         }
         doUnsubscribe(url, listener);
     }
@@ -319,9 +318,11 @@ public class ServiceDiscoveryRegistry implements Registry {
             List<ServiceInstance> serviceInstances = serviceDiscovery.getInstances(serviceName);
             serviceListener.onEvent(new ServiceInstancesChangedEvent(serviceName, serviceInstances));
         });
-        listener.notify(serviceListener.getUrls(url.getServiceKey() + GROUP_CHAR_SEPARATOR + url.getParameter(PROTOCOL_KEY, DUBBO)));
+        String protocolServiceKey = url.getServiceKey() + GROUP_CHAR_SEPARATOR + url.getParameter(PROTOCOL_KEY, DUBBO);
 
-        serviceListener.addListener(url.getProtocolServiceKey(), listener);
+        listener.notify(serviceListener.getUrls(protocolServiceKey));
+
+        serviceListener.addListener(protocolServiceKey, listener);
         registerServiceInstancesChangedListener(url, serviceListener);
     }
 
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/listener/ServiceInstancesChangedListener.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/listener/ServiceInstancesChangedListener.java
index 37b6bf0..6ff7c2a 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/listener/ServiceInstancesChangedListener.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/listener/ServiceInstancesChangedListener.java
@@ -45,8 +45,8 @@ import java.util.Objects;
 import java.util.Set;
 import java.util.TreeSet;
 
-import static org.apache.dubbo.common.constants.CommonConstants.REGISTER_KEY;
 import static org.apache.dubbo.common.constants.CommonConstants.REMOTE_METADATA_STORAGE_TYPE;
+import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_CLUSTER_KEY;
 import static org.apache.dubbo.metadata.MetadataInfo.DEFAULT_REVISION;
 import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getExportedServicesRevision;
 
@@ -156,7 +156,8 @@ public class ServiceInstancesChangedListener implements ConditionalEventListener
 
     private MetadataInfo getMetadataInfo(ServiceInstance instance) {
         String metadataType = ServiceInstanceMetadataUtils.getMetadataStorageType(instance);
-        instance.getExtendParams().putIfAbsent(REGISTER_KEY, RegistryClusterIdentifier.getExtension().consumerKey(url));
+        // FIXME, check "REGISTRY_CLUSTER_KEY" must be set by every registry implementation.
+        instance.getExtendParams().putIfAbsent(REGISTRY_CLUSTER_KEY, RegistryClusterIdentifier.getExtension(url).consumerKey(url));
         MetadataInfo metadataInfo;
         try {
             if (REMOTE_METADATA_STORAGE_TYPE.equals(metadataType)) {
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataServiceURLParamsMetadataCustomizer.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataServiceURLParamsMetadataCustomizer.java
index 8b525c7..9cc28a7 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataServiceURLParamsMetadataCustomizer.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataServiceURLParamsMetadataCustomizer.java
@@ -41,7 +41,7 @@ public class MetadataServiceURLParamsMetadataCustomizer implements ServiceInstan
         String propertyValue = resolveMetadataPropertyValue(serviceInstance);
 
         if (!isBlank(propertyName) && !isBlank(propertyValue)) {
-            metadata.put(propertyName, metadata.get(propertyName));
+            metadata.put(propertyName, propertyValue);
         }
     }
 
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/ServiceInstanceMetadataUtils.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/ServiceInstanceMetadataUtils.java
index 5713256..0f0cf3c 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/ServiceInstanceMetadataUtils.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/ServiceInstanceMetadataUtils.java
@@ -244,8 +244,8 @@ public class ServiceInstanceMetadataUtils {
         MetadataInfo metadataInfo = WritableMetadataService.getDefaultExtension().getMetadataInfos().get(registryCluster);
         if (metadataInfo != null) {
             String existingInstanceRevision = instance.getMetadata().get(EXPORTED_SERVICES_REVISION_PROPERTY_NAME);
-            if (!metadataInfo.getRevision().equals(existingInstanceRevision)) {
-                instance.getMetadata().put(EXPORTED_SERVICES_REVISION_PROPERTY_NAME, metadataInfo.getRevision());
+            if (!metadataInfo.calAndGetRevision().equals(existingInstanceRevision)) {
+                instance.getMetadata().put(EXPORTED_SERVICES_REVISION_PROPERTY_NAME, metadataInfo.calAndGetRevision());
                 if (existingInstanceRevision != null) {// skip the first registration.
                     instance.getExtendParams().put(INSTANCE_REVISION_UPDATED_KEY, "true");
                 }
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/store/InMemoryWritableMetadataService.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/store/InMemoryWritableMetadataService.java
index 2b747de..f0281da 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/store/InMemoryWritableMetadataService.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/store/InMemoryWritableMetadataService.java
@@ -125,10 +125,10 @@ public class InMemoryWritableMetadataService implements WritableMetadataService
 
     @Override
     public boolean exportURL(URL url) {
-        String registryKey = RegistryClusterIdentifier.getExtension().providerKey(url);
-        String[] keys = registryKey.split(",");
-        for (String key : keys) {
-            MetadataInfo metadataInfo = metadataInfos.computeIfAbsent(key, k -> {
+        String registryCluster = RegistryClusterIdentifier.getExtension().providerKey(url);
+        String[] clusters = registryCluster.split(",");
+        for (String cluster : clusters) {
+            MetadataInfo metadataInfo = metadataInfos.computeIfAbsent(cluster, k -> {
                 return new MetadataInfo(ApplicationModel.getName());
             });
             metadataInfo.addService(new ServiceInfo(url));
@@ -139,13 +139,13 @@ public class InMemoryWritableMetadataService implements WritableMetadataService
 
     @Override
     public boolean unexportURL(URL url) {
-        String registryKey = RegistryClusterIdentifier.getExtension().providerKey(url);
-        String[] keys = registryKey.split(",");
-        for (String key : keys) {
-            MetadataInfo metadataInfo = metadataInfos.get(key);
+        String registryCluster = RegistryClusterIdentifier.getExtension().providerKey(url);
+        String[] clusters = registryCluster.split(",");
+        for (String cluster : clusters) {
+            MetadataInfo metadataInfo = metadataInfos.get(cluster);
             metadataInfo.removeService(url.getProtocolServiceKey());
             if (metadataInfo.getServices().isEmpty()) {
-                metadataInfos.remove(key);
+                metadataInfos.remove(cluster);
             }
         }
         metadataSemaphore.release();
@@ -199,7 +199,7 @@ public class InMemoryWritableMetadataService implements WritableMetadataService
         }
         for (Map.Entry<String, MetadataInfo> entry : metadataInfos.entrySet()) {
             MetadataInfo metadataInfo = entry.getValue();
-            if (revision.equals(metadataInfo.getRevision())) {
+            if (revision.equals(metadataInfo.calAndGetRevision())) {
                 return metadataInfo;
             }
         }
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/store/RemoteMetadataServiceImpl.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/store/RemoteMetadataServiceImpl.java
index 2d543e5..c4235f4 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/store/RemoteMetadataServiceImpl.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/store/RemoteMetadataServiceImpl.java
@@ -44,8 +44,7 @@ import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE;
 import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY;
 import static org.apache.dubbo.common.constants.CommonConstants.TIMESTAMP_KEY;
 import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
-import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_CLUSTER;
-import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_KEY;
+import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_CLUSTER_KEY;
 
 public class RemoteMetadataServiceImpl {
     protected final Logger logger = LoggerFactory.getLogger(getClass());
@@ -61,12 +60,12 @@ public class RemoteMetadataServiceImpl {
 
     public void publishMetadata(String serviceName) {
         Map<String, MetadataInfo> metadataInfos = localMetadataService.getMetadataInfos();
-        metadataInfos.forEach((registryKey, metadataInfo) -> {
+        metadataInfos.forEach((registryCluster, metadataInfo) -> {
             if (!metadataInfo.hasReported()) {
-                SubscriberMetadataIdentifier identifier = new SubscriberMetadataIdentifier(serviceName, metadataInfo.getRevision());
-                metadataInfo.getRevision();
-                metadataInfo.getExtendParams().put(REGISTRY_KEY, registryKey);
-                MetadataReport metadataReport = getMetadataReports().get(registryKey);
+                SubscriberMetadataIdentifier identifier = new SubscriberMetadataIdentifier(serviceName, metadataInfo.calAndGetRevision());
+                metadataInfo.calAndGetRevision();
+                metadataInfo.getExtendParams().put(REGISTRY_CLUSTER_KEY, registryCluster);
+                MetadataReport metadataReport = getMetadataReports().get(registryCluster);
                 if (metadataReport == null) {
                     metadataReport = getMetadataReports().entrySet().iterator().next().getValue();
                 }
@@ -80,7 +79,7 @@ public class RemoteMetadataServiceImpl {
         SubscriberMetadataIdentifier identifier = new SubscriberMetadataIdentifier(instance.getServiceName(),
                 ServiceInstanceMetadataUtils.getExportedServicesRevision(instance));
 
-        String registryCluster = instance.getExtendParams().get(REGISTRY_CLUSTER);
+        String registryCluster = instance.getExtendParams().get(REGISTRY_CLUSTER_KEY);
 
         MetadataReport metadataReport = getMetadataReports().get(registryCluster);
         if (metadataReport == null) {


[dubbo] 03/03: add demo

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

liujun pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git

commit c63b990193d5ab9ce0eaa52d359eb5192f555bdd
Author: ken.lj <ke...@gmail.com>
AuthorDate: Mon Jul 27 15:35:49 2020 +0800

    add demo
---
 .../apache/dubbo/demo/consumer/Application.java    | 19 ++++++++++++++
 .../src/main/resources/spring/dubbo-consumer.xml   |  3 +++
 .../dubbo/demo/provider/GreetingServiceImpl.java   | 29 ++++++++++++++++++++++
 .../src/main/resources/spring/dubbo-provider.xml   |  7 ++++--
 4 files changed, 56 insertions(+), 2 deletions(-)

diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/main/java/org/apache/dubbo/demo/consumer/Application.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/main/java/org/apache/dubbo/demo/consumer/Application.java
index f637eb2..fba18bc 100644
--- a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/main/java/org/apache/dubbo/demo/consumer/Application.java
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/main/java/org/apache/dubbo/demo/consumer/Application.java
@@ -17,6 +17,7 @@
 package org.apache.dubbo.demo.consumer;
 
 import org.apache.dubbo.demo.DemoService;
+import org.apache.dubbo.demo.GreetingService;
 
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
@@ -31,9 +32,27 @@ public class Application {
         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer.xml");
         context.start();
         DemoService demoService = context.getBean("demoService", DemoService.class);
+        GreetingService greetingService = context.getBean("greetingService", GreetingService.class);
+
+        new Thread(() -> {
+            while (true) {
+                String greetings = greetingService.hello();
+                System.out.println(greetings + " from separated thread.");
+                try {
+                    Thread.sleep(100);
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                }
+            }
+        }).start();
+
         while (true) {
             CompletableFuture<String> hello = demoService.sayHelloAsync("world");
             System.out.println("result: " + hello.get());
+
+            String greetings = greetingService.hello();
+            System.out.println("result: " + greetings);
+
             Thread.sleep(500);
         }
     }
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/main/resources/spring/dubbo-consumer.xml b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/main/resources/spring/dubbo-consumer.xml
index bec81fe..44e8712 100644
--- a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/main/resources/spring/dubbo-consumer.xml
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/main/resources/spring/dubbo-consumer.xml
@@ -33,4 +33,7 @@
     <dubbo:reference provided-by="demo-provider" id="demoService" check="false"
                      interface="org.apache.dubbo.demo.DemoService"/>
 
+    <dubbo:reference provided-by="demo-provider" version="1.0.0" group="greeting" id="greetingService" check="false"
+                     interface="org.apache.dubbo.demo.GreetingService"/>
+
 </beans>
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/GreetingServiceImpl.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/GreetingServiceImpl.java
new file mode 100644
index 0000000..cc1b5de
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/GreetingServiceImpl.java
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.demo.provider;
+
+import org.apache.dubbo.demo.GreetingService;
+
+/**
+ *
+ */
+public class GreetingServiceImpl implements GreetingService {
+    @Override
+    public String hello() {
+        return "Greetings!";
+    }
+}
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/resources/spring/dubbo-provider.xml b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/resources/spring/dubbo-provider.xml
index 90a6ae3..856ff73 100644
--- a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/resources/spring/dubbo-provider.xml
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/resources/spring/dubbo-provider.xml
@@ -29,10 +29,13 @@
     <dubbo:metadata-report address="zookeeper://127.0.0.1:2181"/>
     <dubbo:registry id="registry1" address="zookeeper://127.0.0.1:2181?registry-type=service"/>
 
-    <dubbo:protocol name="dubbo"/>
+    <dubbo:protocol name="dubbo" port="-1"/>
 
     <bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl"/>
+    <bean id="greetingService" class="org.apache.dubbo.demo.provider.GreetingServiceImpl"/>
 
-    <dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService" registry="registry1"/>
+    <dubbo:service interface="org.apache.dubbo.demo.DemoService" timeout="3000" ref="demoService" registry="registry1"/>
+    <dubbo:service version="1.0.0" group="greeting" timeout="5000" interface="org.apache.dubbo.demo.GreetingService"
+                   ref="greetingService"/>
 
 </beans>