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 0ba9ffc6e8a16ae3397eb0f7b4996c6cb4446db4
Author: ken.lj <ken.lj...@gmail.com>
AuthorDate: Tue Jun 16 01:29:42 2020 +0800

    skip when metadata is null
---
 .../registry/client/DefaultServiceInstance.java    |  4 +++-
 .../dubbo/registry/client/InstanceAddressURL.java  |  4 ++--
 .../registry/client/ServiceDiscoveryRegistry.java  |  7 ++++++
 .../listener/ServiceInstancesChangedListener.java  | 27 +++++++++++++++++-----
 .../registry/integration/RegistryDirectory.java    |  2 +-
 5 files changed, 34 insertions(+), 10 deletions(-)

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 3d80c22..b394024 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
@@ -140,7 +140,9 @@ public class DefaultServiceInstance implements 
ServiceInstance {
 
     @Override
     public URL toURL(String protocol, String path, String interfaceName, 
String group, String version, String serviceKey) {
-        return new InstanceAddressURL(protocol, host, port, path, 
interfaceName, group, version, serviceKey);
+        InstanceAddressURL url = new InstanceAddressURL(protocol, host, port, 
path, interfaceName, group, version, serviceKey);
+        url.setMetadata(this.getServiceMetadata());
+        return url;
     }
 
     @Override
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 a4ece68..6ec4df2 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
@@ -72,7 +72,7 @@ public class InstanceAddressURL extends URL {
         }
 
         String value = super.getParameter(key);
-        if (StringUtils.isEmpty(value)) {
+        if (StringUtils.isEmpty(value) && metadataInfo != null) {
             value = metadataInfo.getParameter(key, this.getServiceKey());
         }
         return value;
@@ -112,7 +112,7 @@ public class InstanceAddressURL extends URL {
     @Override
     public Map<String, String> getParameters() {
         Map<String, String> instanceParams = super.getParameters();
-        Map<String, String> metadataParams = 
metadataInfo.getParameters(getServiceKey());
+        Map<String, String> metadataParams = (metadataInfo == null ? new 
HashMap<>() : metadataInfo.getParameters(getServiceKey()));
         int i = instanceParams == null ? 0 : instanceParams.size();
         int j = metadataParams == null ? 0 : metadataParams.size();
         Map<String, String> params = new HashMap<>((int) ((i + j) / 0.75) + 1);
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 92db129..878b9e6 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
@@ -27,6 +27,7 @@ import org.apache.dubbo.metadata.ServiceNameMapping;
 import org.apache.dubbo.metadata.WritableMetadataService;
 import org.apache.dubbo.registry.NotifyListener;
 import org.apache.dubbo.registry.Registry;
+import org.apache.dubbo.registry.client.event.ServiceInstancesChangedEvent;
 import 
org.apache.dubbo.registry.client.event.listener.ServiceInstancesChangedListener;
 import org.apache.dubbo.registry.client.metadata.SubscribedURLsSynthesizer;
 import org.apache.dubbo.registry.support.FailbackRegistry;
@@ -289,6 +290,12 @@ public class ServiceDiscoveryRegistry extends 
FailbackRegistry {
                 });
         serviceListener.setUrl(url);
         listener.addServiceListener(serviceListener);
+
+        // FIXME, sync notification
+        List<ServiceInstance> serviceInstances = 
serviceDiscovery.getInstances(serviceName);
+        serviceListener.onEvent(new ServiceInstancesChangedEvent(serviceName, 
serviceInstances));
+        listener.notifyServiceInstances();
+
         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 3bd4042..36365c1 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
@@ -17,12 +17,15 @@
 package org.apache.dubbo.registry.client.event.listener;
 
 import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.logger.Logger;
+import org.apache.dubbo.common.logger.LoggerFactory;
 import org.apache.dubbo.common.utils.CollectionUtils;
 import org.apache.dubbo.event.ConditionalEventListener;
 import org.apache.dubbo.event.EventListener;
 import org.apache.dubbo.metadata.MetadataInfo;
 import org.apache.dubbo.metadata.MetadataInfo.ServiceInfo;
 import org.apache.dubbo.metadata.MetadataService;
+import org.apache.dubbo.registry.client.DefaultServiceInstance;
 import org.apache.dubbo.registry.client.ServiceDiscovery;
 import org.apache.dubbo.registry.client.ServiceInstance;
 import org.apache.dubbo.registry.client.event.ServiceInstancesChangedEvent;
@@ -51,6 +54,8 @@ import static 
org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataU
  */
 public abstract class ServiceInstancesChangedListener implements 
ConditionalEventListener<ServiceInstancesChangedEvent> {
 
+    private static final Logger logger = 
LoggerFactory.getLogger(ServiceInstancesChangedListener.class);
+
     private final String serviceName;
     private final ServiceDiscovery serviceDiscovery;
     private URL url;
@@ -87,14 +92,24 @@ public abstract class ServiceInstancesChangedListener 
implements ConditionalEven
             Collection<ServiceInstance> rInstances = 
localRevisionToInstances.computeIfAbsent(revision, r -> new ArrayList<>());
             rInstances.add(instance);
 
-            MetadataInfo metadata = revisionToMetadata.get(revision);
-            if (metadata != null) {
+            MetadataInfo metadata = null;
+            if (revisionToMetadata != null && ((metadata = 
revisionToMetadata.get(revision)) != null)) {
                 localRevisionToMetadata.put(revision, metadata);
             } else {
                 metadata = getMetadataInfo(instance);
-                localRevisionToMetadata.put(revision, 
getMetadataInfo(instance));
+                if (metadata != null) {
+                    localRevisionToMetadata.put(revision, 
getMetadataInfo(instance));
+                }
+            }
+
+            if (metadata != null) {
+                parse(revision, metadata, localServiceToRevisions);
+                ((DefaultServiceInstance) 
instance).setServiceMetadata(metadata);
+            } else {
+                logger.error("Failed to load service metadata for instance " + 
instance);
+                Set<String> set = 
localServiceToRevisions.computeIfAbsent(url.getServiceKey(), k -> new 
HashSet<>());
+                set.add(revision);
             }
-            parse(revision, metadata, localServiceToRevisions);
         }
 
         this.revisionToInstances = localRevisionToInstances;
@@ -126,8 +141,8 @@ public abstract class ServiceInstancesChangedListener 
implements ConditionalEven
 
     private Map<String, Set<String>> parse(String revision, MetadataInfo 
metadata, Map<String, Set<String>> localServiceToRevisions) {
         Map<String, ServiceInfo> serviceInfos = metadata.getServices();
-        for (Map.Entry<String, ServiceInfo> serviceInfo : 
serviceInfos.entrySet()) {
-            String serviceKey = serviceInfo.getValue().getServiceKey();
+        for (Map.Entry<String, ServiceInfo> entry : serviceInfos.entrySet()) {
+            String serviceKey = entry.getValue().getServiceKey();
             Set<String> set = 
localServiceToRevisions.computeIfAbsent(serviceKey, k -> new HashSet<>());
             set.add(revision);
         }
diff --git 
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java
 
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java
index 1372556..fe6d1a2 100644
--- 
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java
+++ 
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java
@@ -151,7 +151,7 @@ public class RegistryDirectory<T> extends 
AbstractDirectory<T> implements Notify
             throw new IllegalArgumentException("registry serviceKey is null.");
         }
         this.serviceType = serviceType;
-        this.serviceKey = url.getServiceKey();
+        this.serviceKey = super.getConsumerUrl().getServiceKey();
         this.queryMap = 
StringUtils.parseQueryString(url.getParameterAndDecoded(REFER_KEY));
         this.overrideDirectoryUrl = this.directoryUrl = 
turnRegistryUrlToConsumerUrl(url);
         String group = directoryUrl.getParameter(GROUP_KEY, "");

Reply via email to