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 2021/01/15 08:05:22 UTC
[dubbo] branch 3.0 updated: 3.0 migration applications (#7102)
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
The following commit(s) were added to refs/heads/3.0 by this push:
new 829c0df 3.0 migration applications (#7102)
829c0df is described below
commit 829c0df27368640122bd6455567fa4d766ba39f8
Author: ken.lj <ke...@gmail.com>
AuthorDate: Fri Jan 15 16:04:58 2021 +0800
3.0 migration applications (#7102)
---
.../dubbo/config/AbstractInterfaceConfig.java | 7 ++--
.../org/apache/dubbo/config/RegistryConfig.java | 2 ++
.../apache/dubbo/demo/consumer/Application.java | 25 +++++++++++--
.../src/main/resources/spring/dubbo-consumer.xml | 5 ++-
.../apache/dubbo/metadata/MappingChangedEvent.java | 7 ++++
.../dubbo/metadata/WritableMetadataService.java | 7 ++++
.../registry/client/AbstractServiceDiscovery.java | 6 ++++
.../registry/client/DefaultServiceInstance.java | 5 +--
.../registry/client/ServiceDiscoveryRegistry.java | 13 ++++---
.../listener/ServiceInstancesChangedListener.java | 25 +++++++++++--
.../metadata/ServiceInstanceMetadataUtils.java | 14 +++++---
.../store/InMemoryWritableMetadataService.java | 41 +++++++++++++++++++---
.../registry/nacos/NacosServiceDiscovery.java | 2 +-
.../zookeeper/ZookeeperServiceDiscovery.java | 12 ++-----
14 files changed, 137 insertions(+), 34 deletions(-)
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java b/dubbo-common/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java
index 4be28bf..b4a80b2 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java
@@ -466,10 +466,11 @@ public abstract class AbstractInterfaceConfig extends AbstractMethodConfig {
this.application = application;
if (application != null) {
ConfigManager configManager = ApplicationModel.getConfigManager();
- configManager.getApplication().orElseGet(() -> {
+ if (!configManager.getApplication().isPresent()) {
configManager.setApplication(application);
- return application;
- });
+ } else {
+ application.refresh();
+ }
}
}
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/RegistryConfig.java b/dubbo-common/src/main/java/org/apache/dubbo/config/RegistryConfig.java
index 2999682..c84f20a 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/config/RegistryConfig.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/config/RegistryConfig.java
@@ -25,6 +25,7 @@ import java.util.Map;
import static org.apache.dubbo.common.constants.CommonConstants.EXTRA_KEYS_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.SHUTDOWN_WAIT_KEY;
+import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_CLUSTER_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_PUBLISH_INSTANCE_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_PUBLISH_INTERFACE_KEY;
import static org.apache.dubbo.common.constants.RemotingConstants.BACKUP_KEY;
@@ -201,6 +202,7 @@ public class RegistryConfig extends AbstractConfig {
}
@Override
+ @Parameter(key = REGISTRY_CLUSTER_KEY)
public String getId() {
return super.getId();
}
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 afe8cb3..d13b6c1 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
@@ -18,6 +18,7 @@ package org.apache.dubbo.demo.consumer;
import org.apache.dubbo.demo.DemoService;
import org.apache.dubbo.demo.GreetingService;
+import org.apache.dubbo.demo.RestDemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -33,17 +34,35 @@ public class Application {
context.start();
DemoService demoService = context.getBean("demoService", DemoService.class);
GreetingService greetingService = context.getBean("greetingService", GreetingService.class);
+ RestDemoService restDemoService = context.getBean("restDemoService", RestDemoService.class);
new Thread(() -> {
while (true) {
try {
String greetings = greetingService.hello();
System.out.println(greetings + " from separated thread.");
-
- Thread.sleep(100);
} catch (Exception e) {
// e.printStackTrace();
}
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException e) {
+ }
+ }
+ }).start();
+
+ new Thread(() -> {
+ while (true) {
+ try {
+ String restResult = restDemoService.sayHello("rest");
+ System.out.println(restResult + " from separated thread.");
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException e) {
+ }
}
}).start();
@@ -58,7 +77,7 @@ public class Application {
// e.printStackTrace();
}
- Thread.sleep(500);
+ Thread.sleep(5000);
}
}
}
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 f0c6df1..539a645 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
@@ -27,7 +27,7 @@
<!-- <dubbo:metadata-report address="zookeeper://127.0.0.1:2181"/>-->
- <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
+ <dubbo:registry address="zookeeper://127.0.0.1:2181?registry-type=service"/>
<dubbo:reference id="demoService" check="false"
interface="org.apache.dubbo.demo.DemoService"/>
@@ -35,4 +35,7 @@
<dubbo:reference version="1.0.0" group="greeting" id="greetingService" check="false"
interface="org.apache.dubbo.demo.GreetingService"/>
+ <dubbo:reference protocol="rest" version="1.0.0" id="restDemoService" check="false"
+ interface="org.apache.dubbo.demo.RestDemoService"/>
+
</beans>
diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MappingChangedEvent.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MappingChangedEvent.java
index 91a25f4..88476ab 100644
--- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MappingChangedEvent.java
+++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MappingChangedEvent.java
@@ -38,4 +38,11 @@ public class MappingChangedEvent {
this.apps = apps;
}
+ @Override
+ public String toString() {
+ StringBuilder sb = new StringBuilder();
+ sb.append("{serviceKey: ").append(serviceKey).append(", apps: ");
+ sb.append(apps.toString()).append("}");
+ return sb.toString();
+ }
}
diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/WritableMetadataService.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/WritableMetadataService.java
index 6a044cf..84a7b73 100644
--- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/WritableMetadataService.java
+++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/WritableMetadataService.java
@@ -21,6 +21,7 @@ import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.extension.SPI;
import org.apache.dubbo.rpc.model.ApplicationModel;
+import java.util.Map;
import java.util.Set;
import static org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader;
@@ -87,10 +88,16 @@ public interface WritableMetadataService extends MetadataService {
void putCachedMapping(String serviceKey, Set<String> apps);
+ Map<String, Set<String>> getCachedMapping();
+
+ Set<String> getCachedMapping(String mappingKey);
+
Set<String> getCachedMapping(URL consumerURL);
Set<String> removeCachedMapping(String serviceKey);
+ MetadataInfo getDefaultMetadataInfo();
+
/**
* Get {@link ExtensionLoader#getDefaultExtension() the defautl extension} of {@link WritableMetadataService}
*
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java
index bebc73a..e9a48cb 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java
@@ -16,6 +16,8 @@
*/
package org.apache.dubbo.registry.client;
+import org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils;
+
public abstract class AbstractServiceDiscovery implements ServiceDiscovery {
protected ServiceInstance serviceInstance;
@@ -27,6 +29,10 @@ public abstract class AbstractServiceDiscovery implements ServiceDiscovery {
@Override
public void register(ServiceInstance serviceInstance) throws RuntimeException {
+ this.serviceInstance = serviceInstance;
+ if (ServiceInstanceMetadataUtils.getExportedServicesRevision(serviceInstance) == null) {
+ ServiceInstanceMetadataUtils.calInstanceRevision(this, serviceInstance);
+ }
}
@Override
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 241acfe..93ba70e 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
@@ -63,7 +63,6 @@ public class DefaultServiceInstance implements ServiceInstance {
}
public DefaultServiceInstance(DefaultServiceInstance other) {
- this.id = other.id;
this.serviceName = other.serviceName;
this.host = other.host;
this.port = other.port;
@@ -73,6 +72,8 @@ public class DefaultServiceInstance implements ServiceInstance {
this.serviceMetadata = other.serviceMetadata;
this.extendParams = other.extendParams;
this.endpoints = other.endpoints;
+ this.address = null;
+ this.id = null;
}
public DefaultServiceInstance(String id, String serviceName, String host, Integer port) {
@@ -186,7 +187,7 @@ public class DefaultServiceInstance implements ServiceInstance {
public DefaultServiceInstance copy(Endpoint endpoint) {
DefaultServiceInstance copyOfInstance = new DefaultServiceInstance(this);
copyOfInstance.setPort(endpoint.getPort());
-// copyOfInstance.setId(copyOfInstance.getAddress());
+ copyOfInstance.setId(copyOfInstance.getAddress());
return copyOfInstance;
}
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 8ca6cd3..d19c8de 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
@@ -22,7 +22,6 @@ import org.apache.dubbo.common.extension.SPI;
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.metadata.ServiceNameMapping;
import org.apache.dubbo.metadata.WritableMetadataService;
import org.apache.dubbo.registry.NotifyListener;
import org.apache.dubbo.registry.Registry;
@@ -57,7 +56,6 @@ import static org.apache.dubbo.common.constants.RegistryConstants.SERVICE_REGIST
import static org.apache.dubbo.common.function.ThrowableAction.execute;
import static org.apache.dubbo.metadata.ServiceNameMapping.toStringKeys;
import static org.apache.dubbo.registry.client.ServiceDiscoveryFactory.getExtension;
-import static org.apache.dubbo.rpc.Constants.ID_KEY;
/**
* Being different to the traditional registry, {@link ServiceDiscoveryRegistry} that is a new service-oriented
@@ -246,7 +244,7 @@ public class ServiceDiscoveryRegistry implements Registry {
}
private URL addRegistryClusterKey(URL url) {
- String registryCluster = serviceDiscovery.getUrl().getParameter(ID_KEY);
+ String registryCluster = serviceDiscovery.getUrl().getParameter(REGISTRY_CLUSTER_KEY);
if (registryCluster != null && url.getParameter(REGISTRY_CLUSTER_KEY) == null) {
url = url.addParameter(REGISTRY_CLUSTER_KEY, registryCluster);
}
@@ -256,7 +254,7 @@ public class ServiceDiscoveryRegistry implements Registry {
public void doUnsubscribe(URL url, NotifyListener listener) {
writableMetadataService.unsubscribeURL(url);
String protocolServiceKey = url.getServiceKey() + GROUP_CHAR_SEPARATOR + url.getParameter(PROTOCOL_KEY, DUBBO);
- Set<String> serviceNames = writableMetadataService.removeCachedMapping(ServiceNameMapping.buildMappingKey(url));
+ Set<String> serviceNames = writableMetadataService.getCachedMapping(url);
if (CollectionUtils.isNotEmpty(serviceNames)) {
ServiceInstancesChangedListener instancesChangedListener = serviceListeners.get(toStringKeys(serviceNames));
instancesChangedListener.removeListener(protocolServiceKey);
@@ -410,4 +408,11 @@ public class ServiceDiscoveryRegistry implements Registry {
|| Objects.equals(protocol, targetURL.getProtocol());
}
+ public Set<String> getRegisteredListeners() {
+ return registeredListeners;
+ }
+
+ public Map<String, ServiceInstancesChangedListener> getServiceListeners() {
+ return serviceListeners;
+ }
}
\ No newline at end of file
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 4a4e634..f930c70 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
@@ -112,7 +112,7 @@ public class ServiceInstancesChangedListener implements ConditionalEventListener
Map<String, List<ServiceInstance>> revisionToInstances = new HashMap<>();
Map<ServiceInfo, Set<String>> localServiceToRevisions = new HashMap<>();
- Map<Set<String>, List<URL>> revisionsToUrls = new HashMap<>();
+ Map<String, Map<Set<String>, List<URL>>> protocolRevisionsToUrls = new HashMap<>();
Map<String, List<URL>> newServiceUrls = new HashMap<>();//TODO
Map<String, MetadataInfo> newRevisionToMetadata = new HashMap<>();
@@ -150,6 +150,10 @@ public class ServiceInstancesChangedListener implements ConditionalEventListener
this.revisionToMetadata = newRevisionToMetadata;
localServiceToRevisions.forEach((serviceInfo, revisions) -> {
+ String protocol = serviceInfo.getProtocol();
+ Map<Set<String>, List<URL>> revisionsToUrls = protocolRevisionsToUrls.computeIfAbsent(protocol, k -> {
+ return new HashMap<>();
+ });
List<URL> urls = revisionsToUrls.get(revisions);
if (urls != null) {
newServiceUrls.put(serviceInfo.getMatchKey(), urls);
@@ -159,7 +163,7 @@ public class ServiceInstancesChangedListener implements ConditionalEventListener
for (ServiceInstance i : revisionToInstances.get(r)) {
// different protocols may have ports specified in meta
if (ServiceInstanceMetadataUtils.hasEndpoints(i)) {
- DefaultServiceInstance.Endpoint endpoint = ServiceInstanceMetadataUtils.getEndpoint(i, serviceInfo.getProtocol());
+ DefaultServiceInstance.Endpoint endpoint = ServiceInstanceMetadataUtils.getEndpoint(i, protocol);
if (endpoint != null && !endpoint.getPort().equals(i.getPort())) {
urls.add(((DefaultServiceInstance)i).copy(endpoint).toURL());
break;
@@ -213,6 +217,22 @@ public class ServiceInstancesChangedListener implements ConditionalEventListener
return url;
}
+ public Map<String, List<ServiceInstance>> getAllInstances() {
+ return allInstances;
+ }
+
+ public List<ServiceInstance> getInstancesOfApp(String appName) {
+ return allInstances.get(appName);
+ }
+
+ public Map<String, MetadataInfo> getRevisionToMetadata() {
+ return revisionToMetadata;
+ }
+
+ public MetadataInfo getMetadata(String revision) {
+ return revisionToMetadata.get(revision);
+ }
+
/**
* @param event {@link ServiceInstancesChangedEvent event}
* @return If service name matches, return <code>true</code>, or <code>false</code>
@@ -221,7 +241,6 @@ public class ServiceInstancesChangedListener implements ConditionalEventListener
return serviceNames.contains(event.getServiceName());
}
-
private boolean isRetryAndExpired(ServiceInstancesChangedEvent event) {
String appName = event.getServiceName();
List<ServiceInstance> appInstances = event.getServiceInstances();
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 acfa492..01949a1 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
@@ -29,6 +29,7 @@ import org.apache.dubbo.registry.client.DefaultServiceInstance.Endpoint;
import org.apache.dubbo.registry.client.ServiceDiscovery;
import org.apache.dubbo.registry.client.ServiceInstance;
import org.apache.dubbo.registry.client.ServiceInstanceCustomizer;
+import org.apache.dubbo.registry.client.metadata.store.InMemoryWritableMetadataService;
import org.apache.dubbo.registry.client.metadata.store.RemoteMetadataServiceImpl;
import org.apache.dubbo.registry.support.AbstractRegistryFactory;
import org.apache.dubbo.rpc.model.ApplicationModel;
@@ -42,15 +43,16 @@ import java.util.Map;
import static java.util.Collections.emptyMap;
import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY;
+import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_METADATA_STORAGE_TYPE;
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PORT_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.TIMESTAMP_KEY;
+import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_CLUSTER_KEY;
import static org.apache.dubbo.common.utils.StringUtils.isBlank;
import static org.apache.dubbo.registry.integration.InterfaceCompatibleRegistryProtocol.DEFAULT_REGISTER_PROVIDER_KEYS;
import static org.apache.dubbo.rpc.Constants.DEPRECATED_KEY;
-import static org.apache.dubbo.rpc.Constants.ID_KEY;
/**
* The Utilities class for the {@link ServiceInstance#getMetadata() metadata of the service instance}
@@ -227,11 +229,15 @@ public class ServiceInstanceMetadataUtils {
}
public static void calInstanceRevision(ServiceDiscovery serviceDiscovery, ServiceInstance instance) {
- String registryCluster = serviceDiscovery.getUrl().getParameter(ID_KEY);
+ String registryCluster = serviceDiscovery.getUrl().getParameter(REGISTRY_CLUSTER_KEY);
if (registryCluster == null) {
- return;
+ registryCluster = DEFAULT_KEY;
+ }
+ WritableMetadataService writableMetadataService = WritableMetadataService.getDefaultExtension();
+ MetadataInfo metadataInfo = writableMetadataService.getMetadataInfos().get(registryCluster);
+ if (metadataInfo == null) {
+ metadataInfo = ((InMemoryWritableMetadataService)writableMetadataService).getDefaultMetadataInfo();
}
- MetadataInfo metadataInfo = WritableMetadataService.getDefaultExtension().getMetadataInfos().get(registryCluster);
if (metadataInfo != null) {
String existingInstanceRevision = instance.getMetadata().get(EXPORTED_SERVICES_REVISION_PROPERTY_NAME);
if (!metadataInfo.calAndGetRevision().equals(existingInstanceRevision)) {
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 736f413..e31745a 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
@@ -19,6 +19,7 @@ package org.apache.dubbo.registry.client.metadata.store;
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.common.utils.StringUtils;
import org.apache.dubbo.metadata.MetadataInfo;
import org.apache.dubbo.metadata.MetadataInfo.ServiceInfo;
@@ -52,6 +53,7 @@ import java.util.concurrent.locks.ReentrantLock;
import static java.util.Collections.emptySortedSet;
import static java.util.Collections.unmodifiableSortedSet;
import static org.apache.dubbo.common.URL.buildKey;
+import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY;
import static org.apache.dubbo.common.utils.CollectionUtils.isEmpty;
import static org.apache.dubbo.rpc.Constants.GENERIC_KEY;
@@ -144,8 +146,8 @@ public class InMemoryWritableMetadataService implements WritableMetadataService
this.metadataServiceURL = url;
return true;
}
- String registryCluster = RegistryClusterIdentifier.getExtension(url).providerKey(url);
- String[] clusters = registryCluster.split(",");
+
+ String[] clusters = getRegistryCluster(url).split(",");
for (String cluster : clusters) {
MetadataInfo metadataInfo = metadataInfos.computeIfAbsent(cluster, k -> {
return new MetadataInfo(ApplicationModel.getName());
@@ -163,8 +165,8 @@ public class InMemoryWritableMetadataService implements WritableMetadataService
this.metadataServiceURL = null;
return true;
}
- String registryCluster = RegistryClusterIdentifier.getExtension(url).providerKey(url);
- String[] clusters = registryCluster.split(",");
+
+ String[] clusters = getRegistryCluster(url).split(",");
for (String cluster : clusters) {
MetadataInfo metadataInfo = metadataInfos.get(cluster);
metadataInfo.removeService(url.getProtocolServiceKey());
@@ -176,6 +178,14 @@ public class InMemoryWritableMetadataService implements WritableMetadataService
return removeURL(exportedServiceURLs, url);
}
+ private String getRegistryCluster(URL url){
+ String registryCluster = RegistryClusterIdentifier.getExtension(url).providerKey(url);
+ if (StringUtils.isEmpty(registryCluster)) {
+ registryCluster = DEFAULT_KEY;
+ }
+ return registryCluster;
+ }
+
@Override
public boolean subscribeURL(URL url) {
return addURL(subscribedServiceURLs, url);
@@ -230,6 +240,19 @@ public class InMemoryWritableMetadataService implements WritableMetadataService
return null;
}
+ @Override
+ public MetadataInfo getDefaultMetadataInfo() {
+ if (CollectionUtils.isEmptyMap(metadataInfos)) {
+ return null;
+ }
+ for (Map.Entry<String, MetadataInfo> entry : metadataInfos.entrySet()) {
+ if (entry.getKey().equalsIgnoreCase(DEFAULT_KEY)) {
+ return entry.getValue();
+ }
+ }
+ return metadataInfos.entrySet().iterator().next().getValue();
+ }
+
public void blockUntilUpdated() {
try {
metadataSemaphore.acquire();
@@ -258,6 +281,16 @@ public class InMemoryWritableMetadataService implements WritableMetadataService
}
@Override
+ public Map<String, Set<String>> getCachedMapping() {
+ return serviceToAppsMapping;
+ }
+
+ @Override
+ public Set<String> getCachedMapping(String mappingKey) {
+ return serviceToAppsMapping.get(mappingKey);
+ }
+
+ @Override
public Set<String> getCachedMapping(URL consumerURL) {
String serviceKey = ServiceNameMapping.buildMappingKey(consumerURL);
return serviceToAppsMapping.get(serviceKey);
diff --git a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosServiceDiscovery.java b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosServiceDiscovery.java
index 10a0714..7a346fa 100644
--- a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosServiceDiscovery.java
+++ b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosServiceDiscovery.java
@@ -72,7 +72,7 @@ public class NacosServiceDiscovery extends AbstractServiceDiscovery {
@Override
public void register(ServiceInstance serviceInstance) throws RuntimeException {
- this.serviceInstance = serviceInstance;
+ super.register(serviceInstance);
execute(namingService, service -> {
Instance instance = toInstance(serviceInstance);
service.registerInstance(instance.getServiceName(), group, instance);
diff --git a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperServiceDiscovery.java b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperServiceDiscovery.java
index d9f084e..dce7d20 100644
--- a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperServiceDiscovery.java
+++ b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperServiceDiscovery.java
@@ -23,6 +23,7 @@ import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.DefaultPage;
import org.apache.dubbo.common.utils.Page;
+import org.apache.dubbo.registry.client.AbstractServiceDiscovery;
import org.apache.dubbo.registry.client.ServiceDiscovery;
import org.apache.dubbo.registry.client.ServiceInstance;
import org.apache.dubbo.registry.client.event.listener.ServiceInstancesChangedListener;
@@ -52,7 +53,7 @@ import static org.apache.dubbo.rpc.RpcException.REGISTRY_EXCEPTION;
* Zookeeper {@link ServiceDiscovery} implementation based on
* <a href="https://curator.apache.org/curator-x-discovery/index.html">Apache Curator X Discovery</a>
*/
-public class ZookeeperServiceDiscovery implements ServiceDiscovery {
+public class ZookeeperServiceDiscovery extends AbstractServiceDiscovery {
private final Logger logger = LoggerFactory.getLogger(getClass());
@@ -64,8 +65,6 @@ public class ZookeeperServiceDiscovery implements ServiceDiscovery {
private org.apache.curator.x.discovery.ServiceDiscovery<ZookeeperInstance> serviceDiscovery;
- private ServiceInstance serviceInstance;
-
/**
* The Key is watched Zookeeper path, the value is an instance of {@link CuratorWatcher}
*/
@@ -89,13 +88,8 @@ public class ZookeeperServiceDiscovery implements ServiceDiscovery {
serviceDiscovery.close();
}
- @Override
- public ServiceInstance getLocalInstance() {
- return serviceInstance;
- }
-
public void register(ServiceInstance serviceInstance) throws RuntimeException {
- this.serviceInstance = serviceInstance;
+ super.register(serviceInstance);
try {
serviceDiscovery.registerService(build(serviceInstance));
} catch (Exception e) {