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/08/04 07:10:03 UTC
[dubbo] 26/27: fix address notification issue
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 7acf2db35c614c0716301b0923b5df0517bd5a38
Author: ken.lj <ke...@gmail.com>
AuthorDate: Tue Aug 4 12:59:48 2020 +0800
fix address notification issue
---
.../client/ServiceDiscoveryRegistryDirectory.java | 35 ++++++++++++----------
.../listener/ServiceInstancesChangedListener.java | 10 +++----
.../ServiceInstanceMetadataCustomizer.java | 2 +-
.../metadata/store/RemoteMetadataServiceImpl.java | 2 +-
4 files changed, 27 insertions(+), 22 deletions(-)
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistryDirectory.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistryDirectory.java
index f65a927..15c187f 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistryDirectory.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistryDirectory.java
@@ -23,6 +23,7 @@ import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.Assert;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.NetUtils;
+import org.apache.dubbo.registry.AddressListener;
import org.apache.dubbo.registry.NotifyListener;
import org.apache.dubbo.registry.client.event.listener.ServiceInstancesChangedListener;
import org.apache.dubbo.registry.integration.DynamicDirectory;
@@ -73,26 +74,30 @@ public class ServiceDiscoveryRegistryDirectory<T> extends DynamicDirectory<T> im
public synchronized void notify(List<URL> instanceUrls) {
// Set the context of the address notification thread.
RpcContext.setRpcContext(getConsumerUrl());
+
+ /**
+ * 3.x added for extend URL address
+ */
+ ExtensionLoader<AddressListener> addressListenerExtensionLoader = ExtensionLoader.getExtensionLoader(AddressListener.class);
+ List<AddressListener> supportedListeners = addressListenerExtensionLoader.getActivateExtension(getUrl(), (String[]) null);
+ if (supportedListeners != null && !supportedListeners.isEmpty()) {
+ for (AddressListener addressListener : supportedListeners) {
+ instanceUrls = addressListener.notify(instanceUrls, getConsumerUrl(), this);
+ }
+ }
+
refreshInvoker(instanceUrls);
}
private void refreshInvoker(List<URL> invokerUrls) {
- Assert.notNull(invokerUrls, "invokerUrls should not be null, use empty InstanceAddressURL to clear address.");
+ Assert.notNull(invokerUrls, "invokerUrls should not be null, use empty url list to clear address.");
- if (invokerUrls.size() == 1) {
- URL url = invokerUrls.get(0);
- if (!(url instanceof InstanceAddressURL)) {
- throw new IllegalStateException("use empty InstanceAddressURL to clear address");
- } else {
- InstanceAddressURL instanceAddressURL = (InstanceAddressURL) url;
- if (instanceAddressURL.getInstance() == null) {
- this.forbidden = true; // Forbid to access
- this.invokers = Collections.emptyList();
- routerChain.setInvokers(this.invokers);
- destroyAllInvokers(); // Close all invokers
- return;
- }
- }
+ if (invokerUrls.size() == 0) {
+ this.forbidden = true; // Forbid to access
+ this.invokers = Collections.emptyList();
+ routerChain.setInvokers(this.invokers);
+ destroyAllInvokers(); // Close all invokers
+ return;
}
this.forbidden = false; // Allow to access
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 b915cb3..e99b40c 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
@@ -19,7 +19,6 @@ 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;
@@ -27,7 +26,6 @@ import org.apache.dubbo.metadata.MetadataInfo.ServiceInfo;
import org.apache.dubbo.metadata.MetadataService;
import org.apache.dubbo.registry.NotifyListener;
import org.apache.dubbo.registry.client.DefaultServiceInstance;
-import org.apache.dubbo.registry.client.InstanceAddressURL;
import org.apache.dubbo.registry.client.RegistryClusterIdentifier;
import org.apache.dubbo.registry.client.ServiceDiscovery;
import org.apache.dubbo.registry.client.ServiceInstance;
@@ -37,6 +35,7 @@ import org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils;
import org.apache.dubbo.registry.client.metadata.store.RemoteMetadataServiceImpl;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
@@ -86,6 +85,7 @@ public class ServiceInstancesChangedListener implements ConditionalEventListener
* @param event {@link ServiceInstancesChangedEvent}
*/
public synchronized void onEvent(ServiceInstancesChangedEvent event) {
+ logger.info("Received instance notification, serviceName: " + event.getServiceName() + ", instances: " + event.getServiceInstances().size());
String appName = event.getServiceName();
allInstances.put(appName, event.getServiceInstances());
@@ -106,6 +106,7 @@ public class ServiceInstancesChangedListener implements ConditionalEventListener
MetadataInfo metadata = revisionToMetadata.get(revision);
if (metadata == null) {
metadata = getMetadataInfo(instance);
+ logger.info("MetadataInfo for instance " + instance.getAddress() + "?revision=" + revision + " is " + metadata);
if (metadata != null) {
revisionToMetadata.put(revision, getMetadataInfo(instance));
} else {
@@ -183,9 +184,8 @@ public class ServiceInstancesChangedListener implements ConditionalEventListener
}
private List<URL> toUrlsWithEmpty(List<URL> urls) {
- if (CollectionUtils.isEmpty(urls)) {
- urls = new ArrayList<>();
- urls.add(new InstanceAddressURL());
+ if (urls == null) {
+ urls = Collections.emptyList();
}
return urls;
}
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/ServiceInstanceMetadataCustomizer.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/ServiceInstanceMetadataCustomizer.java
index 76fb765..8b8a5b7 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/ServiceInstanceMetadataCustomizer.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/ServiceInstanceMetadataCustomizer.java
@@ -51,7 +51,7 @@ public class ServiceInstanceMetadataCustomizer implements ServiceInstanceCustomi
// FIXME, check the same key in different urls has the same value
MetadataInfo metadataInfo = localMetadataService.getMetadataInfos().values().iterator().next();
MetadataInfo.ServiceInfo serviceInfo = metadataInfo.getServices().values().iterator().next();
- Map<String, String> allParams = new HashMap<>(serviceInfo.getParams());
+ Map<String, String> allParams = new HashMap<>(serviceInfo.getUrl().getParameters());
// load instance params users want to load.
// TODO, duplicate logic with that in ApplicationConfig
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 93c02fe..01c4eff 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
@@ -85,7 +85,7 @@ public class RemoteMetadataServiceImpl {
if (metadataReport == null) {
metadataReport = getMetadataReports().entrySet().iterator().next().getValue();
}
- return metadataReport.getAppMetadata(identifier, instance.getMetadata());
+ return metadataReport.getAppMetadata(identifier, instance.getExtendParams());
}
public void publishServiceDefinition(URL url) {