From d6d0bec0c6733abb43f17f0a19d1d6419c81fa5b Mon Sep 17 00:00:00 2001 From: Mercy Ma Date: Fri, 16 Aug 2019 11:10:51 +0800 Subject: [PATCH] [Refactor] Dubbo Cloud Native (#4844) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Polish apache/dubbo#4542 : [Enhancement] Adapt the Java standard Event/Listener mechanism * Polish apache/dubbo#4541 : [Feature] Add local File System DynamicConfigurationFactory‘s extension * Polish apache#4541 : Bugfix * Polish apache/dubbo#4541 : Optimization * Polish apache/dubbo#4541 : Add the compatibility for PollingWatchService on the some platforms * Polish apache/dubbo#4541 : Add delay publish without ThreadPoolExecutor * Polish apache/dubbo#4541 : Refactor the extension name * Polish apache/dubbo#4541 : Add remove ops * Polish apache/dubbo#4541 : Add testable constructor * Polish apache/dubbo#4541 : Add getConfigGroups method * Polish apache/dubbo#4610 : [Refactor] Refactor the bootstrap module * Polish apache/dubbo#4541 : Fix the nulling URL issue * Polish apache/dubbo#4622 : [Refactor] Refactor ConfigManager * Polish apache/dubbo#4622 : [Refactor] Refactor ConfigManager * Polish apache/dubbo#4622 : Support multiple configcenters * Polish apache/dubbo#4671 : ServiceNameMapping will not map the group, version and protocol * update referenceCount log (#4683) Add comments to support multiple shared connections * Polish /apache/dubbo#4687 : Remove the duplicated test code in dubbo-config-spring (#4688) * #4685 修改代码if判断false问题 if (hasException == false)修改成if (!hasException) (#4695) * Fixed Service annotation method parameters are not in effect (#4598) * keep demo simple, and switch to use zookeeper as registry center (#4705) * keep demo simple, and switch to use zookeeper as registry center * remove comment * @Reference auto-wires the instance of generic interface #4594 (#4677) * try to shorten maven output to make travis build pass (#4710) * use CountDownLatch to check zk registry if establish connection (#4589) * Minor change * Rename the extension name of WritableMetadataService * Polish apache/dubbo#4759 : [Refactor] Change the signature of methods of MetadataService #4759 * Merge remote-tracking branch 'upstream/master' into dubbo-cloud-native # Conflicts: # dubbo-all/pom.xml # dubbo-bom/pom.xml # dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java # dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ApplicationConfig.java # dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ConfigCenterConfig.java # dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java # dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/RegistryConfig.java # dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java # dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java # dubbo-configcenter/dubbo-configcenter-api/src/main/java/org/apache/dubbo/configcenter/DynamicConfiguration.java # dubbo-configcenter/dubbo-configcenter-api/src/test/java/org/apache/dubbo/configcenter/mock/MockDynamicConfiguration.java # dubbo-configcenter/dubbo-configcenter-consul/src/main/java/org/apache/dubbo/configcenter/consul/ConsulDynamicConfiguration.java # dubbo-configcenter/dubbo-configcenter-etcd/src/test/java/org/apache/dubbo/configcenter/support/etcd/EtcdDynamicConfigurationTest.java # dubbo-configcenter/dubbo-configcenter-nacos/src/main/java/org/apache/dubbo/configcenter/support/nacos/NacosDynamicConfiguration.java # dubbo-configcenter/dubbo-configcenter-nacos/src/test/java/org/apache/dubbo/configcenter/support/nacos/NacosDynamicConfigurationTest.java # dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/zookeeper/ZookeeperDynamicConfiguration.java # dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/definition/model/MethodDefinition.java # dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/MetadataIdentifier.java # dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReport.java # dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/report/identifier/MetadataIdentifierTest.java # dubbo-metadata/dubbo-metadata-definition-protobuf/src/main/java/org/apache/dubbo/metadata/definition/protobuf/ProtobufTypeBuilder.java # dubbo-metadata/dubbo-metadata-definition-protobuf/src/test/java/org/apache/dubbo/metadata/definition/protobuf/ProtobufTypeBuilderTest.java # dubbo-metadata/pom.xml # dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/AbstractConfiguratorListener.java # dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistry.java # dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistryFactory.java # dubbo-rpc/dubbo-rpc-xml/src/main/java/org/apache/dubbo/xml/rpc/protocol/xmlrpc/XmlRpcProtocol.java * Polish apache/dubbo#3984 : Add the implementation of Page getInstances(String serviceName, int offset, int pageSize, boolean healthyOnly) * Code merge * Fix the cases * Merge remote-tracking branch 'upstream/cloud-native' into dubbo-cloud-native # Conflicts: # dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceProviderBootstrap.java # dubbo-metadata/dubbo-metadata-definition-protobuf/pom.xml # dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/support/ServiceOrientedRegistryTest.java # dubbo-registry/dubbo-registry-consul/src/main/java/org/apache/dubbo/registry/consul/ConsulServiceDiscoveryFactory.java # dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdServiceDiscovery.java * Refactor ConfigManager * Refactor ConfigManager * Resolve the issues on ConfigManager * Refactor and add test-cases for ConfigManager * Polish apache/dubbo#4774 : [Feature] Dubbo Cloud Native - To Support in Spring * Polish apache/dubbo#4808 : [Feature] Add the registered/unregistered event mechanism ShutdownHook * Polish apache/dubbo#4807 : [Feature] Add the callback mechanism ShutdownHook #4807 * Polish apache/dubbo#4813 : [Feature] add Prioritized implementation for ServiceInstanceCustomizer * Polish apache/dubbo#4815 : [Feature] Add the ServiceLoader for Dubbo's services or components * Polish apache/dubbo#4815 : [Feature] Add the ServiceLoader for Dubbo's services or components * Polish apache/dubbo#4813 : [Feature] add Prioritized implementation for ServiceInstanceCustomizer * Polish apache/dubbo#4807 : Add sort implementation * Refactor * Refactor --- .../dubbo/bootstrap/DubboBootstrap.java | 165 +++++++++++------- .../DubboServiceConsumerBootstrap.java | 9 +- .../DubboServiceProvider2Bootstrap.java | 9 +- .../file/FileSystemDynamicConfiguration.java | 4 + .../common/constants/RegistryConstants.java | 2 +- .../dubbo/common/context/Lifecycle.java | 2 +- .../common/extension/ExtensionLoader.java | 14 ++ .../common/function/ThrowableAction.java | 33 ++-- .../apache/dubbo/common/lang/Prioritized.java | 3 +- .../dubbo/config/DubboShutdownHook.java | 2 +- .../ConfigurableMetadataServiceExporter.java | 59 ++++--- .../dubbo/config/DubboProviderBootstrap.java | 13 +- ...nfigurableMetadataServiceExporterTest.java | 2 +- .../dubbo/metadata/MetadataService.java | 2 +- .../metadata/MetadataServiceExporter.java | 23 ++- .../metadata/WritableMetadataService.java | 28 ++- .../AbstractServiceDiscoveryFactory.java | 16 +- .../DefaultServiceDiscoveryFactory.java | 54 ++++++ .../EventPublishingServiceDiscovery.java | 125 +++++++------ .../client/FileSystemServiceDiscovery.java | 22 +-- .../registry/client/ServiceDiscovery.java | 19 +- .../client/ServiceDiscoveryFactory.java | 29 ++- .../ServiceDiscoveryRegistry.java} | 84 +++++---- .../ServiceDiscoveryRegistryFactory.java} | 6 +- .../ServiceDiscoveryRegistryProtocol.java} | 4 +- ...va => ServiceDiscoveryDestroyedEvent.java} | 17 +- ...a => ServiceDiscoveryDestroyingEvent.java} | 17 +- .../client/event/ServiceDiscoveryEvent.java | 49 ++++++ .../event/ServiceDiscoveryExceptionEvent.java | 54 ++++++ ... => ServiceDiscoveryInitializedEvent.java} | 19 +- ...=> ServiceDiscoveryInitializingEvent.java} | 19 +- .../event/listener/LoggingEventListener.java | 20 +-- ...tedServicesRevisionMetadataCustomizer.java | 11 +- ...ataServiceURLParamsMetadataCustomizer.java | 9 +- .../RefreshServiceMetadataCustomizer.java | 7 +- .../ServiceInstanceMetadataUtils.java | 43 ++++- .../SpringCloudMetadataServiceURLBuilder.java | 4 +- ...bedServicesRevisionMetadataCustomizer.java | 11 +- .../org.apache.dubbo.registry.RegistryFactory | 2 +- ...che.dubbo.registry.client.ServiceDiscovery | 1 + ...bo.registry.client.ServiceDiscoveryFactory | 1 + .../internal/org.apache.dubbo.rpc.Protocol | 2 +- .../EventPublishingServiceDiscoveryTest.java | 44 ++--- .../client/InMemoryServiceDiscovery.java | 5 +- .../InMemoryServiceDiscoveryFactory.java | 33 ---- .../registry/client/ServiceDiscoveryTest.java | 8 +- .../listener/LoggingEventListenerTest.java | 23 +-- .../support/ServiceOrientedRegistryTest.java | 10 +- ...he.dubbo.registry.client.ServiceDiscovery} | 2 +- .../consul/ConsulServiceDiscovery.java | 23 ++- .../consul/ConsulServiceDiscoveryFactory.java | 31 ---- ...che.dubbo.registry.client.ServiceDiscovery | 1 + ...bo.registry.client.ServiceDiscoveryFactory | 1 - .../registry/etcd/EtcdServiceDiscovery.java | 36 ++-- .../etcd/EtcdServiceDiscoveryFactory.java | 39 ----- ...che.dubbo.registry.client.ServiceDiscovery | 1 + ...bo.registry.client.ServiceDiscoveryFactory | 1 - .../registry/nacos/NacosServiceDiscovery.java | 16 +- .../nacos/NacosServiceDiscoveryFactory.java | 36 ---- ...che.dubbo.registry.client.ServiceDiscovery | 1 + ...bo.registry.client.ServiceDiscoveryFactory | 1 - .../zookeeper/ZookeeperServiceDiscovery.java | 34 ++-- .../zookeeper/util/CuratorFrameworkUtils.java | 5 - ...he.dubbo.registry.client.ServiceDiscovery} | 2 +- ...bo.registry.client.ServiceDiscoveryFactory | 1 - .../ZookeeperServiceDiscoveryTest.java | 10 +- 66 files changed, 756 insertions(+), 623 deletions(-) rename dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperServiceDiscoveryFactory.java => dubbo-common/src/main/java/org/apache/dubbo/common/function/ThrowableAction.java (59%) create mode 100644 dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultServiceDiscoveryFactory.java rename dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/{service/ServiceOrientedRegistry.java => client/ServiceDiscoveryRegistry.java} (87%) rename dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/{service/ServiceOrientedRegistryFactory.java => client/ServiceDiscoveryRegistryFactory.java} (89%) rename dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/{service/ServiceOrientedRegistryProtocol.java => client/ServiceDiscoveryRegistryProtocol.java} (93%) rename dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/{ServiceDiscoveryStoppedEvent.java => ServiceDiscoveryDestroyedEvent.java} (72%) rename dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/{ServiceDiscoveryStartingEvent.java => ServiceDiscoveryDestroyingEvent.java} (72%) create mode 100644 dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryEvent.java create mode 100644 dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryExceptionEvent.java rename dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/{ServiceDiscoveryStartedEvent.java => ServiceDiscoveryInitializedEvent.java} (72%) rename dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/{ServiceDiscoveryStoppingEvent.java => ServiceDiscoveryInitializingEvent.java} (72%) create mode 100644 dubbo-registry/dubbo-registry-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscovery create mode 100644 dubbo-registry/dubbo-registry-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscoveryFactory delete mode 100644 dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/InMemoryServiceDiscoveryFactory.java rename dubbo-registry/dubbo-registry-api/src/test/resources/META-INF/services/{org.apache.dubbo.registry.client.ServiceDiscoveryFactory => org.apache.dubbo.registry.client.ServiceDiscovery} (86%) delete mode 100644 dubbo-registry/dubbo-registry-consul/src/main/java/org/apache/dubbo/registry/consul/ConsulServiceDiscoveryFactory.java create mode 100644 dubbo-registry/dubbo-registry-consul/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscovery delete mode 100644 dubbo-registry/dubbo-registry-consul/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscoveryFactory delete mode 100644 dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdServiceDiscoveryFactory.java create mode 100644 dubbo-registry/dubbo-registry-etcd3/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscovery delete mode 100644 dubbo-registry/dubbo-registry-etcd3/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscoveryFactory delete mode 100644 dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosServiceDiscoveryFactory.java create mode 100644 dubbo-registry/dubbo-registry-nacos/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscovery delete mode 100644 dubbo-registry/dubbo-registry-nacos/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscoveryFactory rename dubbo-registry/dubbo-registry-zookeeper/src/main/resources/META-INF/dubbo/internal/{org.apache.dubbo.registry.client.ServiceDiscoveryFactory => org.apache.dubbo.registry.client.ServiceDiscovery} (82%) delete mode 100644 dubbo-registry/dubbo-registry-zookeeper/src/main/resources/META-INF/services/org.apache.dubbo.registry.client.ServiceDiscoveryFactory diff --git a/dubbo-bootstrap/src/main/java/org/apache/dubbo/bootstrap/DubboBootstrap.java b/dubbo-bootstrap/src/main/java/org/apache/dubbo/bootstrap/DubboBootstrap.java index db5963a37b9..4ad55126639 100644 --- a/dubbo-bootstrap/src/main/java/org/apache/dubbo/bootstrap/DubboBootstrap.java +++ b/dubbo-bootstrap/src/main/java/org/apache/dubbo/bootstrap/DubboBootstrap.java @@ -21,7 +21,6 @@ import org.apache.dubbo.common.config.configcenter.DynamicConfiguration; import org.apache.dubbo.common.config.configcenter.wrapper.CompositeDynamicConfiguration; import org.apache.dubbo.common.context.Lifecycle; -import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.CollectionUtils; @@ -50,13 +49,14 @@ import org.apache.dubbo.config.utils.ReferenceConfigCache; import org.apache.dubbo.event.EventDispatcher; import org.apache.dubbo.event.EventListener; +import org.apache.dubbo.metadata.MetadataService; +import org.apache.dubbo.metadata.MetadataServiceExporter; +import org.apache.dubbo.metadata.WritableMetadataService; import org.apache.dubbo.metadata.report.MetadataReportInstance; import org.apache.dubbo.registry.client.AbstractServiceDiscoveryFactory; 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.rpc.Protocol; -import org.apache.dubbo.rpc.ProtocolServer; import java.io.IOException; import java.util.ArrayList; @@ -64,6 +64,7 @@ import java.util.HashMap; import java.util.List; import java.util.Set; +import java.util.SortedSet; import java.util.concurrent.ExecutorService; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.Condition; @@ -75,13 +76,11 @@ import static java.util.concurrent.Executors.newSingleThreadExecutor; import static org.apache.dubbo.common.config.ConfigurationUtils.parseProperties; import static org.apache.dubbo.common.config.configcenter.DynamicConfiguration.getDynamicConfiguration; -import static org.apache.dubbo.common.constants.CommonConstants.GROUP_CHAR_SEPERATOR; -import static org.apache.dubbo.common.constants.CommonConstants.METADATA_DEFAULT; -import static org.apache.dubbo.common.constants.CommonConstants.METADATA_KEY; import static org.apache.dubbo.common.constants.CommonConstants.METADATA_REMOTE; import static org.apache.dubbo.common.utils.StringUtils.isNotEmpty; import static org.apache.dubbo.config.context.ConfigManager.getInstance; -import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.MEATADATA_STORED_TYPE_KEY; +import static org.apache.dubbo.metadata.WritableMetadataService.getExtension; +import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.setMetadataStorageType; import static org.apache.dubbo.remoting.Constants.CLIENT_KEY; /** @@ -130,7 +129,13 @@ public class DubboBootstrap implements Lifecycle { */ private volatile boolean onlyRegisterProvider = false; - private ServiceInstance serviceInstance; + private volatile boolean defaultMetadataStorageType = true; + + private volatile ServiceInstance serviceInstance; + + private volatile MetadataService metadataService; + + private volatile MetadataServiceExporter metadataServiceExporter; public DubboBootstrap() { DubboShutdownHook.getDubboShutdownHook().register(); @@ -147,6 +152,19 @@ public DubboBootstrap onlyRegisterProvider(boolean onlyRegisterProvider) { return this; } + public boolean isOnlyRegisterProvider() { + return onlyRegisterProvider; + } + + public boolean isDefaultMetadataStorageType() { + return defaultMetadataStorageType; + } + + public DubboBootstrap defaultMetadataStorageType(boolean defaultMetadataStorageType) { + this.defaultMetadataStorageType = defaultMetadataStorageType; + return this; + } + public DubboBootstrap metadataReport(MetadataReportConfig metadataReportConfig) { configManager.addMetadataReport(metadataReportConfig); return this; @@ -161,7 +179,6 @@ public DubboBootstrap metadataReports(List metadataReportC return this; } - // {@link ApplicationConfig} correlative methods /** @@ -419,6 +436,10 @@ public DubboBootstrap initialize() { useRegistryAsConfigCenterIfNecessary(); + initMetadataService(); + + initMetadataServiceExporter(); + initialized = true; if (logger.isInfoEnabled()) { @@ -430,6 +451,24 @@ public DubboBootstrap initialize() { return this; } + /** + * Initialize {@link MetadataService} from {@link WritableMetadataService}'s extension + */ + private void initMetadataService() { + this.metadataService = getExtension(isDefaultMetadataStorageType()); + } + + /** + * Initialize {@link MetadataServiceExporter} + */ + private void initMetadataServiceExporter() { + this.metadataServiceExporter = new ConfigurableMetadataServiceExporter() + .setApplicationConfig(getApplication()) + .setRegistries(configManager.getRegistries()) + .setProtocols(configManager.getProtocols()) + .metadataService(metadataService); + } + private void loadRemoteConfigs() { // registry ids to registry configs List tmpRegistries = new ArrayList<>(); @@ -512,26 +551,16 @@ public DubboBootstrap start() { } if (!isStarted()) { + // 1. export Dubbo Services exportServices(); - // Not only provider register and some services are exported - if (!onlyRegisterProvider && !configManager.getServices().isEmpty()) { - /** - * export {@link MetadataService} - */ - // TODO, only export to default registry? - ApplicationConfig applicationConfig = configManager.getApplication().orElseThrow(() -> new IllegalStateException("ApplicationConfig cannot be null")); - if (!METADATA_REMOTE.equals(applicationConfig.getMetadata())) { - exportMetadataService( - applicationConfig, - configManager.getRegistries(), - configManager.getProtocols() - ); - } - /** - * Register the local {@link ServiceInstance} - */ - registerServiceInstance(applicationConfig); + // 2. export MetadataService + exportMetadataService(); + + // Not only provider register + if (!isOnlyRegisterProvider() || hasExportedServices()) { + //3. Register the local ServiceInstance if required + registerServiceInstance(); } referServices(); @@ -545,6 +574,14 @@ public DubboBootstrap start() { return this; } + private boolean hasExportedServices() { + return !metadataService.getExportedURLs().isEmpty(); + } + + private ApplicationConfig getApplication() { + return configManager.getApplication().orElseThrow(() -> new IllegalStateException("ApplicationConfig cannot be null")); + } + /** * Block current thread to be await. * @@ -578,6 +615,7 @@ public DubboBootstrap await() { public DubboBootstrap stop() { if (isInitialized() && isStarted()) { unregisterServiceInstance(); + unexportMetadataService(); unexportServices(); started = false; } @@ -699,14 +737,17 @@ public DubboBootstrap addEventListener(EventListener listener) { return this; } - private List exportMetadataService(ApplicationConfig applicationConfig, - Collection globalRegistryConfigs, - Collection globalProtocolConfigs) { - ConfigurableMetadataServiceExporter exporter = new ConfigurableMetadataServiceExporter(); - exporter.setApplicationConfig(applicationConfig); - exporter.setRegistries(globalRegistryConfigs); - exporter.setProtocols(globalProtocolConfigs); - return exporter.export(); + /** + * export {@link MetadataService} and get the exported {@link URL URLs} + * + * @return {@link MetadataServiceExporter#getExportedURLs()} + */ + private List exportMetadataService() { + return metadataServiceExporter.export().getExportedURLs(); + } + + private void unexportMetadataService() { + metadataServiceExporter.unexport(); } private void exportServices() { @@ -724,38 +765,42 @@ private void referServices() { configManager.getReferences().forEach(cache::get); } - public boolean isOnlyRegisterProvider() { - return onlyRegisterProvider; - } + private void registerServiceInstance() { - private void registerServiceInstance(ApplicationConfig applicationConfig) { - ExtensionLoader loader = ExtensionLoader.getExtensionLoader(Protocol.class); - Set protocols = loader.getLoadedExtensions(); - if (CollectionUtils.isEmpty(protocols)) { - throw new IllegalStateException("There should has at least one Protocol specified."); - } + ApplicationConfig application = getApplication(); - String protocol = findOneProtocolForServiceInstance(protocols); + String serviceName = application.getName(); - Protocol protocolInstance = loader.getExtension(protocol); + URL exportedURL = selectMetadataServiceExportedURL(); - String serviceName = applicationConfig.getName(); - // TODO, only support exporting one server - ProtocolServer server = protocolInstance.getServers().get(0); - String[] address = server.getAddress().split(GROUP_CHAR_SEPERATOR); - String host = address[0]; - int port = Integer.parseInt(address[1]); + String host = exportedURL.getHost(); - ServiceInstance serviceInstance = initServiceInstance( - serviceName, - host, - port, - applicationConfig.getMetadata() == null ? METADATA_DEFAULT : applicationConfig.getMetadata() - ); + int port = exportedURL.getPort(); + + ServiceInstance serviceInstance = initServiceInstance(serviceName, host, port); getServiceDiscoveries().forEach(serviceDiscovery -> serviceDiscovery.register(serviceInstance)); } + private URL selectMetadataServiceExportedURL() { + + URL selectedURL = null; + + SortedSet urlValues = metadataService.getExportedURLs(); + + for (String urlValue : urlValues) { + URL url = URL.valueOf(urlValue); + if ("rest".equals(url.getProtocol())) { // REST first + selectedURL = url; + break; + } else { + selectedURL = url; // If not found, take any one + } + } + + return selectedURL; + } + /** * Use rest protocol if there's one, otherwise, choose the first one available. * @@ -791,9 +836,9 @@ private void unregisterServiceInstance() { } - private ServiceInstance initServiceInstance(String serviceName, String host, int port, String metadataType) { + private ServiceInstance initServiceInstance(String serviceName, String host, int port) { this.serviceInstance = new DefaultServiceInstance(serviceName, host, port); - this.serviceInstance.getMetadata().put(MEATADATA_STORED_TYPE_KEY, metadataType); + setMetadataStorageType(serviceInstance, isDefaultMetadataStorageType()); return this.serviceInstance; } diff --git a/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceConsumerBootstrap.java b/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceConsumerBootstrap.java index cc0e16b19d4..bf1fe0ae5f8 100644 --- a/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceConsumerBootstrap.java +++ b/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceConsumerBootstrap.java @@ -17,16 +17,9 @@ package org.apache.dubbo.bootstrap; import org.apache.dubbo.bootstrap.rest.UserService; -import org.apache.dubbo.config.ApplicationConfig; -import org.apache.dubbo.config.MetadataReportConfig; import org.apache.dubbo.config.ReferenceConfig; import org.apache.dubbo.config.context.ConfigManager; -import java.util.HashMap; -import java.util.Map; - -import static org.apache.dubbo.common.constants.CommonConstants.METADATA_KEY; - /** * Dubbo Provider Bootstrap * @@ -40,7 +33,7 @@ public static void main(String[] args) throws Exception { .application("dubbo-consumer-demo") // Zookeeper .registry("zookeeper", builder -> builder.address("zookeeper://127.0.0.1:2181?registry.type=service&subscribed.services=dubbo-provider-demo")) - .metadataReport(new MetadataReportConfig("zookeeper://127.0.0.1:2181")) +// .metadataReport(new MetadataReportConfig("zookeeper://127.0.0.1:2181")) // Nacos // .registry("consul", builder -> builder.address("consul://127.0.0.1:8500?registry.type=service&subscribed.services=dubbo-provider-demo").group("namespace1")) .reference("echo", builder -> builder.interfaceClass(EchoService.class).protocol("dubbo")) diff --git a/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceProvider2Bootstrap.java b/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceProvider2Bootstrap.java index ad87adbbce9..83ea7a12ffd 100644 --- a/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceProvider2Bootstrap.java +++ b/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceProvider2Bootstrap.java @@ -16,6 +16,9 @@ */ package org.apache.dubbo.bootstrap; +import org.apache.dubbo.bootstrap.rest.UserService; +import org.apache.dubbo.bootstrap.rest.UserServiceImpl; + /** * Dubbo Provider Bootstrap * @@ -31,8 +34,10 @@ public static void main(String[] args) { // Nacos // .registry("nacos", builder -> builder.address("nacos://127.0.0.1:8848?registry.type=service")) // .registry(RegistryBuilder.newBuilder().address("etcd3://127.0.0.1:2379?registry.type=service").build()) - .protocol(builder -> builder.port(20885).name("dubbo")) - .service(builder -> builder.id("test").interfaceClass(EchoService.class).ref(new EchoServiceImpl())) + .protocol("dubbo", builder -> builder.port(20885).name("dubbo")) + .protocol("rest", builder -> builder.port(9090).name("rest")) + .service(builder -> builder.id("echo").interfaceClass(EchoService.class).ref(new EchoServiceImpl()).protocolIds("dubbo")) + .service(builder -> builder.id("user").interfaceClass(UserService.class).ref(new UserServiceImpl()).protocolIds("rest")) .start() .await(); } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/config/configcenter/file/FileSystemDynamicConfiguration.java b/dubbo-common/src/main/java/org/apache/dubbo/common/config/configcenter/file/FileSystemDynamicConfiguration.java index 3070c1f632a..45c2b6bb95b 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/config/configcenter/file/FileSystemDynamicConfiguration.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/config/configcenter/file/FileSystemDynamicConfiguration.java @@ -169,6 +169,10 @@ public class FileSystemDynamicConfiguration extends AbstractDynamicConfiguration private final Map> listenersRepository; + public FileSystemDynamicConfiguration() { + this(URL.valueOf("file:///default")); + } + public FileSystemDynamicConfiguration(URL url) { this(initDirectory(url), getEncoding(url), getThreadPoolPrefixName(url), getThreadPoolSize(url), getThreadPoolKeepAliveTime(url)); 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 b0d89b9b462..e7e604c7d1f 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 @@ -67,7 +67,7 @@ public interface RegistryConstants { */ String SERVICE_REGISTRY_TYPE = "service"; - String SERVICE_REGISTRY_PROTOCOL = "service-oriented-registry"; + String SERVICE_REGISTRY_PROTOCOL = "service-discovery-registry"; /** * The parameter key of the subscribed service names for Service-Oriented Registry diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/context/Lifecycle.java b/dubbo-common/src/main/java/org/apache/dubbo/common/context/Lifecycle.java index 672ec2cd22b..67d7b132b6a 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/context/Lifecycle.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/context/Lifecycle.java @@ -21,7 +21,7 @@ * * @since 2.7.4 */ -public interface Lifecycle { +public interface Lifecycle { /** * Initialize the component before {@link #start() start} diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/ExtensionLoader.java b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/ExtensionLoader.java index 1cace235039..4575dbba22a 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/ExtensionLoader.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/ExtensionLoader.java @@ -353,6 +353,16 @@ public T getExtension(String name) { return (T) instance; } + /** + * Get the extension by specified name if found, or {@link #getDefaultExtension() returns the default one} + * + * @param name the name of extension + * @return non-null + */ + public T getOrDefaultExtension(String name) { + return containsExtension(name) ? getExtension(name) : getDefaultExtension(); + } + /** * Return default extension, return null if it's not configured. */ @@ -543,6 +553,10 @@ private T createExtension(String name) { } } + private boolean containsExtension(String name) { + return getExtensionClasses().containsKey(name); + } + private T injectExtension(T instance) { try { if (objectFactory != null) { diff --git a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperServiceDiscoveryFactory.java b/dubbo-common/src/main/java/org/apache/dubbo/common/function/ThrowableAction.java similarity index 59% rename from dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperServiceDiscoveryFactory.java rename to dubbo-common/src/main/java/org/apache/dubbo/common/function/ThrowableAction.java index 4bf354dcb9b..17c96d84648 100644 --- a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperServiceDiscoveryFactory.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/function/ThrowableAction.java @@ -14,25 +14,36 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.registry.zookeeper; +package org.apache.dubbo.common.function; -import org.apache.dubbo.common.URL; -import org.apache.dubbo.registry.client.AbstractServiceDiscoveryFactory; -import org.apache.dubbo.registry.client.ServiceDiscovery; -import org.apache.dubbo.registry.client.ServiceDiscoveryFactory; +import java.util.function.Function; /** - * The zookeeper {@link ServiceDiscoveryFactory} implementation + * A function interface for action with {@link Throwable} * - * @see ServiceDiscoveryFactory + * @see Function + * @see Throwable * @since 2.7.4 */ -public class ZookeeperServiceDiscoveryFactory extends AbstractServiceDiscoveryFactory { +@FunctionalInterface +public interface ThrowableAction { - @Override - public ServiceDiscovery createDiscovery(URL connectionURL) { + /** + * Executes the action + * + * @throws Exception if met with error + */ + void execute() throws Exception; + + /** + * Executes {@link ThrowableAction} + * + * @param action {@link ThrowableAction} + * @throws RuntimeException wrap {@link Exception} to {@link RuntimeException} + */ + static void execute(ThrowableAction action) throws RuntimeException { try { - return new ZookeeperServiceDiscovery(connectionURL); + action.execute(); } catch (Exception e) { throw new RuntimeException(e); } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/lang/Prioritized.java b/dubbo-common/src/main/java/org/apache/dubbo/common/lang/Prioritized.java index 98bf3f8c5b9..f416ae0a1bb 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/lang/Prioritized.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/lang/Prioritized.java @@ -38,7 +38,7 @@ public interface Prioritized extends Comparable { return -1; } else if (b2 && !b1) { // two is Prioritized, one is not return 1; - } else if (b1 && b2) { // one and two both Prioritized + } else if (b1 && b2) { // one and two both are Prioritized return ((Prioritized) one).compareTo((Prioritized) two); } else { // no different return 0; @@ -54,6 +54,7 @@ public interface Prioritized extends Comparable { * The minimum priority */ int MIN_PRIORITY = Integer.MAX_VALUE; + /** * Normal Priority */ diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/DubboShutdownHook.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/DubboShutdownHook.java index 321649d7b0d..8172e140914 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/DubboShutdownHook.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/DubboShutdownHook.java @@ -34,7 +34,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import static java.util.Collections.sort; -import static java.util.ServiceLoader.load; +import static org.apache.dubbo.common.utils.DubboServiceLoader.load; /** * The shutdown hook thread to do the clean up stuff. diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/metadata/ConfigurableMetadataServiceExporter.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/metadata/ConfigurableMetadataServiceExporter.java index 1e8c1f694b3..b5ca2183c12 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/metadata/ConfigurableMetadataServiceExporter.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/metadata/ConfigurableMetadataServiceExporter.java @@ -21,20 +21,18 @@ import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.config.AbstractConfig; import org.apache.dubbo.config.ApplicationConfig; -import org.apache.dubbo.config.MetadataReportConfig; import org.apache.dubbo.config.ProtocolConfig; import org.apache.dubbo.config.RegistryConfig; import org.apache.dubbo.config.ServiceConfig; import org.apache.dubbo.config.context.ConfigManager; import org.apache.dubbo.metadata.MetadataService; import org.apache.dubbo.metadata.MetadataServiceExporter; -import org.apache.dubbo.metadata.WritableMetadataService; import java.util.Collection; import java.util.LinkedList; import java.util.List; -import static java.util.Collections.unmodifiableList; +import static java.util.Collections.emptyList; /** * {@link MetadataServiceExporter} implementation based on {@link AbstractConfig Dubbo configurations}, the clients @@ -43,6 +41,8 @@ *

* Typically, do not worry about their ready status, because they are initialized before * any {@link ServiceConfig} exports, or The Dubbo export will be failed. + *

+ * Being aware of it's not a thread-safe implementation. * * @see MetadataServiceExporter * @see ServiceConfig @@ -53,44 +53,52 @@ public class ConfigurableMetadataServiceExporter implements MetadataServiceExpor private final Logger logger = LoggerFactory.getLogger(getClass()); - private volatile ServiceConfig serviceConfig; - private ApplicationConfig applicationConfig; - private MetadataReportConfig metadataReportConfig; private List registries = new LinkedList<>(); private List protocols = new LinkedList<>(); - public void setApplicationConfig(ApplicationConfig applicationConfig) { + private MetadataService metadataService; + + private ServiceConfig serviceConfig; + + public ConfigurableMetadataServiceExporter setApplicationConfig(ApplicationConfig applicationConfig) { this.applicationConfig = applicationConfig; + return this; } - public void setRegistries(Collection registries) { + public ConfigurableMetadataServiceExporter setRegistries(Collection registries) { this.registries.clear(); this.registries.addAll(registries); + return this; } - public void setProtocols(Collection protocols) { + public ConfigurableMetadataServiceExporter setProtocols(Collection protocols) { this.protocols.clear(); - this.protocols.addAll(protocols); + // TODO only support "dubbo" protocol, add more in the future + protocols.stream().filter(protocolConfig -> "dubbo".equals(protocolConfig.getName())) + .forEach(this.protocols::add); + return this; + } + + public ConfigurableMetadataServiceExporter metadataService(MetadataService metadataService) { + this.metadataService = metadataService; + return this; } @Override - public List export() { + public ConfigurableMetadataServiceExporter export() { if (!isExported()) { - // FIXME, if uses remote metadata center, does not need to export MetadataService locally. - WritableMetadataService metadataService = WritableMetadataService.getDefaultExtension(); - ServiceConfig serviceConfig = new ServiceConfig<>(); serviceConfig.setApplication(applicationConfig); serviceConfig.setRegistries(registries); serviceConfig.setProtocols(protocols); serviceConfig.setInterface(MetadataService.class); serviceConfig.setRef(metadataService); - serviceConfig.setGroup(getApplicationConfig().getName()); + serviceConfig.setGroup(applicationConfig.getName()); serviceConfig.setVersion(metadataService.version()); // export @@ -107,29 +115,24 @@ public List export() { logger.warn("The MetadataService has been exported : " + serviceConfig.getExportedUrls()); } } - return serviceConfig.getExportedUrls(); + + return this; } @Override - public void unexport() { + public ConfigurableMetadataServiceExporter unexport() { if (isExported()) { serviceConfig.unexport(); } + return this; } - private List getProtocols() { - return unmodifiableList(protocols); - } - - private List getRegistries() { - return unmodifiableList(registries); - } - - private ApplicationConfig getApplicationConfig() { - return applicationConfig; + @Override + public List getExportedURLs() { + return serviceConfig != null ? serviceConfig.getExportedUrls() : emptyList(); } - private boolean isExported() { + public boolean isExported() { return serviceConfig != null && serviceConfig.isExported(); } } diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/DubboProviderBootstrap.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/DubboProviderBootstrap.java index ef8cca858dd..22530f85097 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/DubboProviderBootstrap.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/DubboProviderBootstrap.java @@ -17,7 +17,6 @@ package org.apache.dubbo.config; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.config.api.DemoService; import org.apache.dubbo.config.metadata.ConfigurableMetadataServiceExporter; @@ -27,14 +26,12 @@ import org.apache.dubbo.registry.client.ServiceDiscovery; import org.apache.dubbo.registry.client.ServiceDiscoveryFactory; -import java.io.IOException; - /** * Dubbo Provider Bootstrap */ public class DubboProviderBootstrap { - public static void main(String[] args) throws IOException { + public static void main(String[] args) throws Exception { ApplicationConfig application = new ApplicationConfig(); application.setName("dubbo-provider-demo"); @@ -68,11 +65,11 @@ public static void main(String[] args) throws IOException { // 暴露 MetadataService 服务 exporter.export(); - ServiceDiscoveryFactory factory = ExtensionLoader.getExtensionLoader(ServiceDiscoveryFactory.class).getAdaptiveExtension(); + ServiceDiscoveryFactory factory = ServiceDiscoveryFactory.getExtension(connectionURL); - ServiceDiscovery serviceDiscovery = factory.getDiscovery(connectionURL); + ServiceDiscovery serviceDiscovery = factory.getServiceDiscovery(connectionURL); - serviceDiscovery.start(); + serviceDiscovery.initialize(connectionURL); DefaultServiceInstance serviceInstance = new DefaultServiceInstance(application.getName(), "127.0.0.1", protocol.getPort()); @@ -80,6 +77,6 @@ public static void main(String[] args) throws IOException { System.in.read(); - serviceDiscovery.stop(); + serviceDiscovery.destroy(); } } diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/metadata/ConfigurableMetadataServiceExporterTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/metadata/ConfigurableMetadataServiceExporterTest.java index c53eb7df27c..1528b74cc0c 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/metadata/ConfigurableMetadataServiceExporterTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/metadata/ConfigurableMetadataServiceExporterTest.java @@ -71,7 +71,7 @@ private static RegistryConfig registryConfig() { public void testExportAndUnexport() { ConfigurableMetadataServiceExporter exporter = new ConfigurableMetadataServiceExporter(); exporter.setApplicationConfig(ConfigManager.getInstance().getApplication().get()); - List urls = exporter.export(); + List urls = exporter.export().getExportedURLs(); assertEquals(1, urls.size()); diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataService.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataService.java index c4cf2505b20..6beeba7accf 100644 --- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataService.java +++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataService.java @@ -31,7 +31,7 @@ /** * A framework interface of Dubbo Metadata Service defines the contract of Dubbo Services registartion and subscription - * between Dubbo service providers and its consumers. The implementationwill be exported as a normal Dubbo service that + * between Dubbo service providers and its consumers. The implementation will be exported as a normal Dubbo service that * the clients would subscribe, whose version comes from the {@link #version()} method and group gets from * {@link #serviceName()}, that means, The different Dubbo service(application) will export the different * {@link MetadataService} that persists all the exported and subscribed metadata, they are present by diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataServiceExporter.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataServiceExporter.java index 6ba99dee8b9..108b3a7a1b2 100644 --- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataServiceExporter.java +++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataServiceExporter.java @@ -33,12 +33,29 @@ public interface MetadataServiceExporter { /** * Exports the {@link MetadataService} as a Dubbo service * - * @return the exported {@link URL URLs} + * @return {@link MetadataServiceExporter itself} */ - List export(); + MetadataServiceExporter export(); /** * Unexports the {@link MetadataService} + * + * @return {@link MetadataServiceExporter itself} + */ + MetadataServiceExporter unexport(); + + /** + * Get the {@link URL URLs} that were exported + * + * @return non-null */ - void unexport(); + List getExportedURLs(); + + /** + * {@link MetadataService} is export or not + * + * @return if {@link #export()} was executed, return true, or false + */ + boolean isExported(); } + 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 7a8fd27e11e..e51fa2a060a 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 @@ -33,6 +33,16 @@ @SPI("default") public interface WritableMetadataService extends MetadataService { + /** + * The default storage type value as the extension name + */ + public static String DEFAULT_METADATA_STORAGE_TYPE = "default"; + + /** + * The remote storage type value as the extension name + */ + public static String REMOTE_METADATA_STORAGE_TYPE = "remote"; + /** * Gets the current Dubbo Service name * @@ -64,7 +74,7 @@ default String serviceName() { * * @return If success , return true */ - default boolean refreshMetadata(String exportedRevision, String subscribedRevision){ + default boolean refreshMetadata(String exportedRevision, String subscribedRevision) { return true; } @@ -96,9 +106,21 @@ static WritableMetadataService getDefaultExtension() { return getExtensionLoader(WritableMetadataService.class).getDefaultExtension(); } - static WritableMetadataService getExtension(String name) { - return getExtensionLoader(WritableMetadataService.class).getExtension(name); + /** + * Get the metadata's storage type + * + * @param isDefaultStorageType is default storage type or not + * @return non-null, {@link #DEFAULT_METADATA_STORAGE_TYPE "default"} or {@link #REMOTE_METADATA_STORAGE_TYPE "remote"} + */ + public static String getMetadataStorageType(boolean isDefaultStorageType) { + return isDefaultStorageType ? DEFAULT_METADATA_STORAGE_TYPE : REMOTE_METADATA_STORAGE_TYPE; } + static WritableMetadataService getExtension(boolean isDefaultStorageType) { + return getExtension(getMetadataStorageType(isDefaultStorageType)); + } + static WritableMetadataService getExtension(String name) { + return getExtensionLoader(WritableMetadataService.class).getOrDefaultExtension(name); + } } diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscoveryFactory.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscoveryFactory.java index c9c6ec03cd2..b8d941b6483 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscoveryFactory.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscoveryFactory.java @@ -40,12 +40,12 @@ public static Collection getDiscoveries() { */ public static void destroyAll() { if (logger.isInfoEnabled()) { - logger.info("Closing all ServiceDicovery instances: " + getDiscoveries()); + logger.info("Closing all ServiceDiscovery instances: " + getDiscoveries()); } for (ServiceDiscovery discovery : getDiscoveries()) { try { - discovery.stop(); + discovery.destroy(); } catch (Throwable e) { logger.error("Error trying to close ServiceDiscovery instance.", e); } @@ -54,17 +54,13 @@ public static void destroyAll() { } /** - * @param url "zookeeper://ip:port/RegistryService?xxx" + * @param registryURL "zookeeper://ip:port/RegistryService?xxx" * @return */ @Override - public ServiceDiscovery getDiscovery(URL url) { - String key = url.toServiceStringWithoutResolving(); - - return discoveries.computeIfAbsent(key, k -> { - ServiceDiscovery discovery = createDiscovery(url); - return new EventPublishingServiceDiscovery(discovery); - }); + public ServiceDiscovery getServiceDiscovery(URL registryURL) { + String key = registryURL.toServiceStringWithoutResolving(); + return discoveries.computeIfAbsent(key, k -> createDiscovery(registryURL)); } protected abstract ServiceDiscovery createDiscovery(URL url); diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultServiceDiscoveryFactory.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultServiceDiscoveryFactory.java new file mode 100644 index 00000000000..0d38acec504 --- /dev/null +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultServiceDiscoveryFactory.java @@ -0,0 +1,54 @@ +/* + * 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.registry.client; + +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.extension.ExtensionLoader.getExtensionLoader; + +/** + * The default {@link SPI} implementation of {@link ServiceDiscoveryFactory} to {@link #getServiceDiscovery(URL) get the + * instance of ServiceDiscovery} via the {@link URL#getProtocol() protocol} from the {@link URL} that will connect + * the infrastructure of Service registration and discovery. The {@link URL#getProtocol() protocol} will be used as the + * extension name by which the {@link ServiceDiscovery} instance is loaded. + * + * @see AbstractServiceDiscoveryFactory + * @see EventPublishingServiceDiscovery + * @since 2.7.4 + */ +public class DefaultServiceDiscoveryFactory extends AbstractServiceDiscoveryFactory { + + @Override + protected ServiceDiscovery createDiscovery(URL connectionURL) { + ServiceDiscovery serviceDiscovery = load(connectionURL); + return new EventPublishingServiceDiscovery(serviceDiscovery); + } + + /** + * Load the {@link ServiceDiscovery} by {@link URL#getProtocol() the protocol} from {@link URL connection URL} + * + * @param connectionURL the {@link URL url} to connect + * @return non-null + */ + private ServiceDiscovery load(URL connectionURL) { + String protocol = connectionURL.getProtocol(); + ExtensionLoader loader = getExtensionLoader(ServiceDiscovery.class); + return loader.getOrDefaultExtension(protocol); + } +} diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/EventPublishingServiceDiscovery.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/EventPublishingServiceDiscovery.java index b8e6b3d167f..f85138f2283 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/EventPublishingServiceDiscovery.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/EventPublishingServiceDiscovery.java @@ -16,15 +16,18 @@ */ package org.apache.dubbo.registry.client; +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.function.ThrowableAction; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.Page; import org.apache.dubbo.event.Event; import org.apache.dubbo.event.EventDispatcher; -import org.apache.dubbo.registry.client.event.ServiceDiscoveryStartedEvent; -import org.apache.dubbo.registry.client.event.ServiceDiscoveryStartingEvent; -import org.apache.dubbo.registry.client.event.ServiceDiscoveryStoppedEvent; -import org.apache.dubbo.registry.client.event.ServiceDiscoveryStoppingEvent; +import org.apache.dubbo.registry.client.event.ServiceDiscoveryDestroyedEvent; +import org.apache.dubbo.registry.client.event.ServiceDiscoveryDestroyingEvent; +import org.apache.dubbo.registry.client.event.ServiceDiscoveryExceptionEvent; +import org.apache.dubbo.registry.client.event.ServiceDiscoveryInitializedEvent; +import org.apache.dubbo.registry.client.event.ServiceDiscoveryInitializingEvent; import org.apache.dubbo.registry.client.event.ServiceInstancePreRegisteredEvent; import org.apache.dubbo.registry.client.event.ServiceInstancePreUnregisteredEvent; import org.apache.dubbo.registry.client.event.ServiceInstanceRegisteredEvent; @@ -55,14 +58,14 @@ * * * - * {@link #START_ACTION start} - * {@link ServiceDiscoveryStartingEvent} - * {@link ServiceDiscoveryStartedEvent} + * {@link #INITIALIZE_ACTION start} + * {@link ServiceDiscoveryInitializingEvent} + * {@link ServiceDiscoveryInitializedEvent} * * - * {@link #STOP_ACTION stop} - * {@link ServiceDiscoveryStoppingEvent} - * {@link ServiceDiscoveryStoppedEvent} + * {@link #DESTROY_ACTION stop} + * {@link ServiceDiscoveryDestroyingEvent} + * {@link ServiceDiscoveryDestroyedEvent} * * * @@ -96,15 +99,15 @@ * * * @see ServiceDiscovery - * @see ServiceDiscoveryStartingEvent - * @see ServiceDiscoveryStartedEvent + * @see ServiceDiscoveryInitializingEvent + * @see ServiceDiscoveryInitializedEvent * @see ServiceInstancePreRegisteredEvent * @see ServiceInstanceRegisteredEvent - * @see ServiceDiscoveryStoppingEvent - * @see ServiceDiscoveryStoppedEvent + * @see ServiceDiscoveryDestroyingEvent + * @see ServiceDiscoveryDestroyedEvent * @since 2.7.4 */ -class EventPublishingServiceDiscovery implements ServiceDiscovery { +final class EventPublishingServiceDiscovery implements ServiceDiscovery { /** * @see ServiceInstancePreRegisteredEvent @@ -117,22 +120,22 @@ class EventPublishingServiceDiscovery implements ServiceDiscovery { protected static final String UNREGISTER_ACTION = "unregister"; /** - * @see ServiceDiscoveryStartingEvent - * @see ServiceDiscoveryStartedEvent + * @see ServiceDiscoveryInitializingEvent + * @see ServiceDiscoveryInitializedEvent */ - protected static final String START_ACTION = "start"; + protected static final String INITIALIZE_ACTION = "initialize"; /** - * @see ServiceDiscoveryStoppingEvent - * @see ServiceDiscoveryStoppedEvent + * @see ServiceDiscoveryDestroyingEvent + * @see ServiceDiscoveryDestroyedEvent */ - protected static final String STOP_ACTION = "stop"; + protected static final String DESTROY_ACTION = "destroy"; protected final EventDispatcher eventDispatcher = EventDispatcher.getDefaultExtension(); - protected final AtomicBoolean started = new AtomicBoolean(false); + protected final AtomicBoolean initialized = new AtomicBoolean(false); - protected final AtomicBoolean stopped = new AtomicBoolean(false); + protected final AtomicBoolean destroyed = new AtomicBoolean(false); protected final Logger logger = LoggerFactory.getLogger(getClass()); @@ -148,8 +151,8 @@ protected EventPublishingServiceDiscovery(ServiceDiscovery serviceDiscovery) { @Override public final void register(ServiceInstance serviceInstance) throws RuntimeException { - requireStarted(REGISTER_ACTION); - requireNotStopped(REGISTER_ACTION); + assertDestroyed(REGISTER_ACTION); + assertInitialized(REGISTER_ACTION); executeWithEvents( of(new ServiceInstancePreRegisteredEvent(serviceDiscovery, serviceInstance)), @@ -161,8 +164,8 @@ public final void register(ServiceInstance serviceInstance) throws RuntimeExcept @Override public final void update(ServiceInstance serviceInstance) throws RuntimeException { - requireStarted(UPDATE_ACTION); - requireNotStopped(UPDATE_ACTION); + assertDestroyed(UPDATE_ACTION); + assertInitialized(UPDATE_ACTION); executeWithEvents( empty(), @@ -174,8 +177,8 @@ public final void update(ServiceInstance serviceInstance) throws RuntimeExceptio @Override public final void unregister(ServiceInstance serviceInstance) throws RuntimeException { - requireStarted(UNREGISTER_ACTION); - requireNotStopped(UNREGISTER_ACTION); + assertDestroyed(UNREGISTER_ACTION); + assertInitialized(UNREGISTER_ACTION); executeWithEvents( of(new ServiceInstancePreUnregisteredEvent(this, serviceInstance)), @@ -220,11 +223,11 @@ public void addServiceInstancesChangedListener(String serviceName, ServiceInstan } @Override - public final void start() { + public void initialize(URL registryURL) { - requireNotStopped(START_ACTION); + assertInitialized(INITIALIZE_ACTION); - if (isStarted()) { + if (isInitialized()) { if (logger.isWarnEnabled()) { logger.warn("It's ignored to start current ServiceDiscovery, because it has been started."); } @@ -232,21 +235,21 @@ public final void start() { } executeWithEvents( - of(new ServiceDiscoveryStartingEvent(serviceDiscovery)), - serviceDiscovery::start, - of(new ServiceDiscoveryStartedEvent(serviceDiscovery)) + of(new ServiceDiscoveryInitializingEvent(serviceDiscovery)), + () -> serviceDiscovery.initialize(registryURL), + of(new ServiceDiscoveryInitializedEvent(serviceDiscovery)) ); // doesn't start -> started - started.compareAndSet(false, true); + initialized.compareAndSet(false, true); } @Override - public final void stop() { + public void destroy() { - requireStarted(STOP_ACTION); + assertDestroyed(DESTROY_ACTION); - if (isStopped()) { + if (isDestroyed()) { if (logger.isWarnEnabled()) { logger.warn("It's ignored to stop current ServiceDiscovery, because it has been stopped."); } @@ -254,40 +257,48 @@ public final void stop() { } executeWithEvents( - of(new ServiceDiscoveryStoppingEvent(serviceDiscovery)), - serviceDiscovery::stop, - of(new ServiceDiscoveryStoppedEvent(serviceDiscovery)) + of(new ServiceDiscoveryDestroyingEvent(serviceDiscovery)), + serviceDiscovery::destroy, + of(new ServiceDiscoveryDestroyedEvent(serviceDiscovery)) ); // doesn't stop -> stopped - stopped.compareAndSet(false, true); + destroyed.compareAndSet(false, true); } protected final void executeWithEvents(Optional beforeEvent, - Runnable action, + ThrowableAction action, Optional afterEvent) { - beforeEvent.ifPresent(eventDispatcher::dispatch); - action.run(); - afterEvent.ifPresent(eventDispatcher::dispatch); + beforeEvent.ifPresent(this::dispatchEvent); + try { + action.execute(); + } catch (Exception e) { + dispatchEvent(new ServiceDiscoveryExceptionEvent(serviceDiscovery, e)); + } + afterEvent.ifPresent(this::dispatchEvent); + } + + private void dispatchEvent(Event event) { + eventDispatcher.dispatch(event); } - public final boolean isStarted() { - return started.get(); + public final boolean isInitialized() { + return initialized.get(); } - public final boolean isStopped() { - return stopped.get(); + public final boolean isDestroyed() { + return destroyed.get(); } - protected void requireStarted(String action) throws IllegalStateException { - if (!isStarted()) { - throw new IllegalStateException("The action[" + action + "] is rejected, because the ServiceDiscovery is not started yet."); + protected void assertDestroyed(String action) throws IllegalStateException { + if (!isInitialized()) { + throw new IllegalStateException("The action[" + action + "] is rejected, because the ServiceDiscovery is not initialized yet."); } } - protected void requireNotStopped(String action) throws IllegalStateException { - if (isStopped()) { - throw new IllegalStateException("The action[" + action + "] is rejected, because the ServiceDiscovery is stopped already."); + protected void assertInitialized(String action) throws IllegalStateException { + if (isDestroyed()) { + throw new IllegalStateException("The action[" + action + "] is rejected, because the ServiceDiscovery is destroyed already."); } } } diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/FileSystemServiceDiscovery.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/FileSystemServiceDiscovery.java index 1dbf90d6121..e49d392436b 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/FileSystemServiceDiscovery.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/FileSystemServiceDiscovery.java @@ -36,35 +36,21 @@ */ public class FileSystemServiceDiscovery implements ServiceDiscovery, EventListener { - private final URL connectionURL; - private FileSystemDynamicConfiguration dynamicConfiguration; - public FileSystemServiceDiscovery(URL connectionURL) { - this.connectionURL = connectionURL; - } - @Override public void onEvent(ServiceInstancesChangedEvent event) { } @Override - public void start() { - if (dynamicConfiguration == null) { - dynamicConfiguration = createDynamicConfiguration(connectionURL); - } + public void initialize(URL registryURL) throws Exception { + dynamicConfiguration = createDynamicConfiguration(registryURL); } @Override - public void stop() { - try { - if (dynamicConfiguration != null) { - dynamicConfiguration.close(); - } - } catch (Exception e) { - throw new RuntimeException(e); - } + public void destroy() throws Exception { + dynamicConfiguration.close(); } private String getConfigKey(ServiceInstance serviceInstance) { diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscovery.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscovery.java index 914ec958a9a..3381d5a562a 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscovery.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscovery.java @@ -16,6 +16,9 @@ */ package org.apache.dubbo.registry.client; +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.extension.SPI; +import org.apache.dubbo.common.lang.Prioritized; import org.apache.dubbo.common.utils.Page; import org.apache.dubbo.registry.client.event.listener.ServiceInstancesChangedListener; @@ -33,19 +36,25 @@ * * @since 2.7.4 */ -public interface ServiceDiscovery { +@SPI("file") +public interface ServiceDiscovery extends Prioritized { // ==================================== Lifecycle ==================================== // /** - * Starts the ServiceRegistry. This is a lifecycle method. + * Initializes the {@link ServiceDiscovery} + * + * @param registryURL the {@link URL url} to connect service registry + * @throws Exception If met with error */ - void start(); + void initialize(URL registryURL) throws Exception; /** - * Stops the ServiceRegistry. This is a lifecycle method. + * Destroy the {@link ServiceDiscovery} + * + * @throws Exception If met with error */ - void stop(); + void destroy() throws Exception; // ==================================================================================== // diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryFactory.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryFactory.java index 20257f4a642..b9b8bb5ef7b 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryFactory.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryFactory.java @@ -17,24 +17,37 @@ package org.apache.dubbo.registry.client; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.extension.Adaptive; +import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.extension.SPI; +import static org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader; + /** - * The Factory interface to create an instance of {@link ServiceDiscovery} + * The factory to create {@link ServiceDiscovery} * * @see ServiceDiscovery * @since 2.7.4 */ -@SPI("zookeeper") +@SPI("default") public interface ServiceDiscoveryFactory { /** - * Creates an instance of {@link ServiceDiscovery}. + * Get the instance of {@link ServiceDiscovery} + * + * @param registryURL the {@link URL} to connect the registry + * @return non-null + */ + ServiceDiscovery getServiceDiscovery(URL registryURL); + + /** + * Get the extension instance of {@link ServiceDiscoveryFactory} by {@link URL#getProtocol() the protocol} * - * @param connectionURL the {@link URL connection url} - * @return an instance of {@link ServiceDiscovery} + * @param registryURL the {@link URL} to connect the registry + * @return non-null */ - @Adaptive({"protocol"}) - ServiceDiscovery getDiscovery(URL connectionURL); + static ServiceDiscoveryFactory getExtension(URL registryURL) { + String protocol = registryURL.getProtocol(); + ExtensionLoader loader = getExtensionLoader(ServiceDiscoveryFactory.class); + return loader.getOrDefaultExtension(protocol); + } } diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/service/ServiceOrientedRegistry.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java similarity index 87% rename from dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/service/ServiceOrientedRegistry.java rename to dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java index 5fdc35f39d9..40d0a099b8a 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/service/ServiceOrientedRegistry.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java @@ -14,21 +14,19 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.registry.service; +package org.apache.dubbo.registry.client; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.metadata.MetadataService; import org.apache.dubbo.metadata.ServiceNameMapping; import org.apache.dubbo.metadata.WritableMetadataService; +import org.apache.dubbo.metadata.store.InMemoryWritableMetadataService; +import org.apache.dubbo.metadata.store.RemoteWritableMetadataService; import org.apache.dubbo.registry.NotifyListener; import org.apache.dubbo.registry.Registry; -import org.apache.dubbo.registry.client.ServiceDiscovery; -import org.apache.dubbo.registry.client.ServiceDiscoveryFactory; -import org.apache.dubbo.registry.client.ServiceInstance; import org.apache.dubbo.registry.client.metadata.proxy.MetadataServiceProxyFactory; import org.apache.dubbo.registry.client.selector.ServiceInstanceSelector; import org.apache.dubbo.registry.support.FailbackRegistry; @@ -54,7 +52,6 @@ import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_PROTOCOL; import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.METADATA_DEFAULT; import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE; import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY; @@ -63,29 +60,36 @@ import static org.apache.dubbo.common.constants.RegistryConstants.SERVICE_REGISTRY_TYPE; import static org.apache.dubbo.common.constants.RegistryConstants.SUBSCRIBED_SERVICE_NAMES_KEY; import static org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader; +import static org.apache.dubbo.common.function.ThrowableAction.execute; import static org.apache.dubbo.common.utils.CollectionUtils.isEmpty; import static org.apache.dubbo.common.utils.CollectionUtils.isNotEmpty; import static org.apache.dubbo.common.utils.StringUtils.isBlank; import static org.apache.dubbo.metadata.WritableMetadataService.DEFAULT_EXTENSION; -import static org.apache.dubbo.metadata.report.support.Constants.METADATA_REPORT_KEY; +import static org.apache.dubbo.registry.client.ServiceDiscoveryFactory.getExtension; import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getExportedServicesRevision; import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getMetadataServiceURLsParams; -import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getMetadataStoredType; +import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getMetadataStorageType; import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getProviderHost; import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getProviderPort; /** - * Service-Oriented {@link Registry} that is dislike the traditional {@link Registry} will not communicate to - * registry immediately instead of persisting into the metadata's repository when the Dubbo service exports. - * The metadata repository will be used as the data source of Dubbo Metadata service that is about to export and be - * subscribed by the consumers. + * {@link ServiceDiscoveryRegistry} is the service-oriented {@link Registry} and dislike the traditional one that + * {@link #register(URL) registers} to and {@link #subscribe(URL, NotifyListener) discoveries} + * the Dubbo's {@link URL urls} from the external registry. In the {@link #register(URL) registration} + * phase, The {@link URL urls} of Dubbo services will be {@link WritableMetadataService#exportURL(URL) exported} into + * {@link WritableMetadataService} that is either {@link InMemoryWritableMetadataService in-memory} or + * {@link RemoteWritableMetadataService remote}, + *

+ * it's decided by metadata + * subscribes from the remote proxy of {@link MetadataService} + * *

* * @see ServiceDiscovery * @see FailbackRegistry * @since 2.7.4 */ -public class ServiceOrientedRegistry extends FailbackRegistry { +public class ServiceDiscoveryRegistry extends FailbackRegistry { protected final Logger logger = LoggerFactory.getLogger(getClass()); @@ -97,20 +101,17 @@ public class ServiceOrientedRegistry extends FailbackRegistry { private final WritableMetadataService writableMetadataService; - - public ServiceOrientedRegistry(URL registryURL) { + public ServiceDiscoveryRegistry(URL registryURL) { super(registryURL); - this.serviceDiscovery = buildServiceDiscovery(registryURL); - this.subscribedServices = buildSubscribedServices(registryURL); + this.serviceDiscovery = getServiceDiscovery(registryURL); + this.subscribedServices = getSubscribedServices(registryURL); this.serviceNameMapping = ServiceNameMapping.getDefaultExtension(); - - String metadata = registryURL.getParameter(METADATA_REPORT_KEY, METADATA_DEFAULT); - // FIXME - this.writableMetadataService = WritableMetadataService.getExtension(metadata); + String metadataStorageType = getMetadataStorageType(registryURL); + this.writableMetadataService = WritableMetadataService.getExtension(metadataStorageType); } - private Set buildSubscribedServices(URL url) { - String subscribedServiceNames = url.getParameter(SUBSCRIBED_SERVICE_NAMES_KEY); + protected Set getSubscribedServices(URL registryURL) { + String subscribedServiceNames = registryURL.getParameter(SUBSCRIBED_SERVICE_NAMES_KEY); return isBlank(subscribedServiceNames) ? emptySet() : unmodifiableSet(of(subscribedServiceNames.split(",")) .map(String::trim) @@ -118,17 +119,24 @@ private Set buildSubscribedServices(URL url) { .collect(toSet())); } - private ServiceDiscovery buildServiceDiscovery(URL url) { - ServiceDiscoveryFactory factory = ExtensionLoader.getExtensionLoader(ServiceDiscoveryFactory.class).getAdaptiveExtension(); - ServiceDiscovery serviceDiscovery = factory.getDiscovery(url - .addParameter(INTERFACE_KEY, ServiceDiscovery.class.getName()) - .removeParameter(REGISTRY_TYPE_KEY) - ); - serviceDiscovery.start(); + /** + * Get the {@link ServiceDiscovery} from the connection {@link URL} + * + * @param registryURL the {@link URL} to connect the registry + * @return non-null + */ + protected ServiceDiscovery getServiceDiscovery(URL registryURL) { + ServiceDiscoveryFactory factory = getExtension(registryURL); + ServiceDiscovery serviceDiscovery = factory.getServiceDiscovery(registryURL); + execute(() -> { + serviceDiscovery.initialize(registryURL.addParameter(INTERFACE_KEY, ServiceDiscovery.class.getName()) + .removeParameter(REGISTRY_TYPE_KEY)); + }); return serviceDiscovery; } protected boolean shouldRegister(URL providerURL) { + String side = providerURL.getParameter(SIDE_KEY); boolean should = PROVIDER_SIDE.equals(side); // Only register the Provider. @@ -222,8 +230,10 @@ public boolean isAvailable() { @Override public void destroy() { super.destroy(); - // stop ServiceDiscovery - serviceDiscovery.stop(); + execute(() -> { + // stop ServiceDiscovery + serviceDiscovery.destroy(); + }); } protected void subscribeURLs(URL url, NotifyListener listener) { @@ -429,11 +439,11 @@ protected List getProviderExportedURLs(URL subscribedURL, ServiceInstance p String version = subscribedURL.getParameter(VERSION_KEY); // The subscribed protocol may be null String protocol = subscribedURL.getParameter(PROTOCOL_KEY); - String metadataServiceType = getMetadataStoredType(providerInstance); + String metadataStorageType = getMetadataStorageType(providerInstance); try { MetadataService metadataService = MetadataServiceProxyFactory - .getExtension(metadataServiceType == null ? DEFAULT_EXTENSION : metadataServiceType) + .getExtension(metadataStorageType == null ? DEFAULT_EXTENSION : metadataStorageType) .getProxy(providerInstance); SortedSet urls = metadataService.getExportedURLs(serviceInterface, group, version, protocol); exportedURLs = urls.stream().map(URL::valueOf).collect(Collectors.toList()); @@ -478,13 +488,13 @@ protected Set findMappedServices(URL subscribedURL) { } /** - * Create an instance of {@link ServiceOrientedRegistry} if supported + * Create an instance of {@link ServiceDiscoveryRegistry} if supported * * @param registryURL the {@link URL url} of registry * @return null if not supported */ - public static ServiceOrientedRegistry create(URL registryURL) { - return supports(registryURL) ? new ServiceOrientedRegistry(registryURL) : null; + public static ServiceDiscoveryRegistry create(URL registryURL) { + return supports(registryURL) ? new ServiceDiscoveryRegistry(registryURL) : null; } /** diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/service/ServiceOrientedRegistryFactory.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistryFactory.java similarity index 89% rename from dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/service/ServiceOrientedRegistryFactory.java rename to dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistryFactory.java index ea7027c5482..ebf3c5a7a3f 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/service/ServiceOrientedRegistryFactory.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistryFactory.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.registry.service; +package org.apache.dubbo.registry.client; import org.apache.dubbo.common.URL; import org.apache.dubbo.registry.Registry; @@ -24,7 +24,7 @@ import static org.apache.dubbo.common.constants.RegistryConstants.SERVICE_REGISTRY_PROTOCOL; import static org.apache.dubbo.registry.Constants.DEFAULT_REGISTRY; -public class ServiceOrientedRegistryFactory extends AbstractRegistryFactory { +public class ServiceDiscoveryRegistryFactory extends AbstractRegistryFactory { @Override protected Registry createRegistry(URL url) { @@ -32,7 +32,7 @@ protected Registry createRegistry(URL url) { String protocol = url.getParameter(REGISTRY_KEY, DEFAULT_REGISTRY); url = url.setProtocol(protocol).removeParameter(REGISTRY_KEY); } - return new ServiceOrientedRegistry(url); + return new ServiceDiscoveryRegistry(url); } } diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/service/ServiceOrientedRegistryProtocol.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistryProtocol.java similarity index 93% rename from dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/service/ServiceOrientedRegistryProtocol.java rename to dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistryProtocol.java index 733e8b89616..c89c6c97da5 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/service/ServiceOrientedRegistryProtocol.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistryProtocol.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.registry.service; +package org.apache.dubbo.registry.client; import org.apache.dubbo.common.URL; import org.apache.dubbo.registry.integration.RegistryProtocol; @@ -25,7 +25,7 @@ /** * TODO, replace RegistryProtocol completely in the future. */ -public class ServiceOrientedRegistryProtocol extends RegistryProtocol { +public class ServiceDiscoveryRegistryProtocol extends RegistryProtocol { @Override protected URL getRegistryUrl(Invoker originInvoker) { diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryStoppedEvent.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryDestroyedEvent.java similarity index 72% rename from dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryStoppedEvent.java rename to dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryDestroyedEvent.java index 406a5662d8d..2303335708c 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryStoppedEvent.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryDestroyedEvent.java @@ -16,16 +16,15 @@ */ package org.apache.dubbo.registry.client.event; -import org.apache.dubbo.event.Event; import org.apache.dubbo.registry.client.ServiceDiscovery; /** - * An event raised after the {@link ServiceDiscovery Service Discovery} stopped. + * An event raised after the {@link ServiceDiscovery Service Discovery} destroyed. * - * @see ServiceDiscovery#stop() + * @see ServiceDiscovery#destroy() * @since 2.7.4 */ -public class ServiceDiscoveryStoppedEvent extends Event { +public class ServiceDiscoveryDestroyedEvent extends ServiceDiscoveryEvent { /** * Constructs a prototypical Event. @@ -33,16 +32,8 @@ public class ServiceDiscoveryStoppedEvent extends Event { * @param serviceDiscovery The instance of {@link ServiceDiscovery} as source * @throws IllegalArgumentException if source is null. */ - public ServiceDiscoveryStoppedEvent(ServiceDiscovery serviceDiscovery) { + public ServiceDiscoveryDestroyedEvent(ServiceDiscovery serviceDiscovery) { super(serviceDiscovery); } - /** - * Get the instance of {@link ServiceDiscovery} as source - * - * @return the instance of {@link ServiceDiscovery} as source - */ - public ServiceDiscovery getServiceDiscovery() { - return (ServiceDiscovery) getSource(); - } } diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryStartingEvent.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryDestroyingEvent.java similarity index 72% rename from dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryStartingEvent.java rename to dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryDestroyingEvent.java index 9f30d673fa4..60500eefddb 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryStartingEvent.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryDestroyingEvent.java @@ -16,16 +16,15 @@ */ package org.apache.dubbo.registry.client.event; -import org.apache.dubbo.event.Event; import org.apache.dubbo.registry.client.ServiceDiscovery; /** - * An event raised when the {@link ServiceDiscovery Service Discovery} is starting. + * An event raised when the {@link ServiceDiscovery Service Discovery} is destroying. * - * @see ServiceDiscovery#start + * @see ServiceDiscovery#destroy() * @since 2.7.4 */ -public class ServiceDiscoveryStartingEvent extends Event { +public class ServiceDiscoveryDestroyingEvent extends ServiceDiscoveryEvent { /** * Constructs a prototypical Event. @@ -33,16 +32,8 @@ public class ServiceDiscoveryStartingEvent extends Event { * @param serviceDiscovery The instance of {@link ServiceDiscovery} as source * @throws IllegalArgumentException if source is null. */ - public ServiceDiscoveryStartingEvent(ServiceDiscovery serviceDiscovery) { + public ServiceDiscoveryDestroyingEvent(ServiceDiscovery serviceDiscovery) { super(serviceDiscovery); } - /** - * Get the instance of {@link ServiceDiscovery} as source - * - * @return the instance of {@link ServiceDiscovery} as source - */ - public ServiceDiscovery getServiceDiscovery() { - return (ServiceDiscovery) getSource(); - } } diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryEvent.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryEvent.java new file mode 100644 index 00000000000..a319fd51fdf --- /dev/null +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryEvent.java @@ -0,0 +1,49 @@ +/* + * 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.registry.client.event; + +import org.apache.dubbo.event.Event; +import org.apache.dubbo.registry.client.ServiceDiscovery; + +/** + * An abstract {@link Event} class for {@link ServiceDiscovery} + * + * @see Event + * @see ServiceDiscovery + * @since 2.7.4 + */ +public abstract class ServiceDiscoveryEvent extends Event { + + /** + * Constructs a prototypical Event. + * + * @param serviceDiscovery The {@link ServiceDiscovery} on which the Event initially occurred. + * @throws IllegalArgumentException if source is null. + */ + public ServiceDiscoveryEvent(ServiceDiscovery serviceDiscovery) { + super(serviceDiscovery); + } + + /** + * Get the {@link ServiceDiscovery} on which the Event initially occurred. + * + * @return {@link ServiceDiscovery} instance + */ + public final ServiceDiscovery getServiceDiscovery() { + return (ServiceDiscovery) getSource(); + } +} diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryExceptionEvent.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryExceptionEvent.java new file mode 100644 index 00000000000..49582aba472 --- /dev/null +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryExceptionEvent.java @@ -0,0 +1,54 @@ +/* + * 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.registry.client.event; + +import org.apache.dubbo.registry.client.ServiceDiscovery; + +/** + * An event raised when the {@link ServiceDiscovery Service Discovery} met with some exception + * + * @see ServiceDiscovery + * @see org.apache.dubbo.event.Event + * @since 2.7.4 + */ +public class ServiceDiscoveryExceptionEvent extends ServiceDiscoveryEvent { + + private final Exception cause; + + /** + * Constructs a prototypical Event. + * + * @param serviceDiscovery The {@link ServiceDiscovery} on which the Event initially occurred. + * @throws IllegalArgumentException if any argument is null. + */ + public ServiceDiscoveryExceptionEvent(ServiceDiscovery serviceDiscovery, Exception cause) { + super(serviceDiscovery); + if (cause == null) { + throw new NullPointerException("The cause of Exception must not null"); + } + this.cause = cause; + } + + /** + * The cause of {@link Exception} + * + * @return non-nul + */ + public Exception getCause() { + return cause; + } +} diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryStartedEvent.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryInitializedEvent.java similarity index 72% rename from dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryStartedEvent.java rename to dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryInitializedEvent.java index 08af0a698f4..31bcc79d8e4 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryStartedEvent.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryInitializedEvent.java @@ -16,16 +16,16 @@ */ package org.apache.dubbo.registry.client.event; -import org.apache.dubbo.event.Event; +import org.apache.dubbo.common.URL; import org.apache.dubbo.registry.client.ServiceDiscovery; /** - * An event raised after the {@link ServiceDiscovery Service Discovery} started + * An event raised after the {@link ServiceDiscovery Service Discovery} initialized * - * @see ServiceDiscovery#start() + * @see ServiceDiscovery#initialize(URL) * @since 2.7.4 */ -public class ServiceDiscoveryStartedEvent extends Event { +public class ServiceDiscoveryInitializedEvent extends ServiceDiscoveryEvent { /** * Constructs a prototypical Event. @@ -33,17 +33,8 @@ public class ServiceDiscoveryStartedEvent extends Event { * @param serviceDiscovery The instance of {@link ServiceDiscovery} as source * @throws IllegalArgumentException if source is null. */ - public ServiceDiscoveryStartedEvent(ServiceDiscovery serviceDiscovery) { + public ServiceDiscoveryInitializedEvent(ServiceDiscovery serviceDiscovery) { super(serviceDiscovery); } - /** - * Get the instance of {@link ServiceDiscovery} as source - * - * @return the instance of {@link ServiceDiscovery} as source - */ - public ServiceDiscovery getServiceDiscovery() { - return (ServiceDiscovery) getSource(); - } - } diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryStoppingEvent.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryInitializingEvent.java similarity index 72% rename from dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryStoppingEvent.java rename to dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryInitializingEvent.java index 26ea1fbf598..92ef4d1e90f 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryStoppingEvent.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/ServiceDiscoveryInitializingEvent.java @@ -16,16 +16,16 @@ */ package org.apache.dubbo.registry.client.event; -import org.apache.dubbo.event.Event; +import org.apache.dubbo.common.URL; import org.apache.dubbo.registry.client.ServiceDiscovery; /** - * An event raised when the {@link ServiceDiscovery Service Discovery} is stopping. + * An event raised when the {@link ServiceDiscovery Service Discovery} is initializing. * - * @see ServiceDiscovery#stop() + * @see ServiceDiscovery#initialize(URL) * @since 2.7.4 */ -public class ServiceDiscoveryStoppingEvent extends Event { +public class ServiceDiscoveryInitializingEvent extends ServiceDiscoveryEvent { /** * Constructs a prototypical Event. @@ -33,16 +33,7 @@ public class ServiceDiscoveryStoppingEvent extends Event { * @param serviceDiscovery The instance of {@link ServiceDiscovery} as source * @throws IllegalArgumentException if source is null. */ - public ServiceDiscoveryStoppingEvent(ServiceDiscovery serviceDiscovery) { + public ServiceDiscoveryInitializingEvent(ServiceDiscovery serviceDiscovery) { super(serviceDiscovery); } - - /** - * Get the instance of {@link ServiceDiscovery} as source - * - * @return the instance of {@link ServiceDiscovery} as source - */ - public ServiceDiscovery getServiceDiscovery() { - return (ServiceDiscovery) getSource(); - } } diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/listener/LoggingEventListener.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/listener/LoggingEventListener.java index ef99d6fa865..f2205bb23d1 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/listener/LoggingEventListener.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/listener/LoggingEventListener.java @@ -20,10 +20,10 @@ import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.event.Event; import org.apache.dubbo.event.GenericEventListener; -import org.apache.dubbo.registry.client.event.ServiceDiscoveryStartedEvent; -import org.apache.dubbo.registry.client.event.ServiceDiscoveryStartingEvent; -import org.apache.dubbo.registry.client.event.ServiceDiscoveryStoppedEvent; -import org.apache.dubbo.registry.client.event.ServiceDiscoveryStoppingEvent; +import org.apache.dubbo.registry.client.event.ServiceDiscoveryDestroyedEvent; +import org.apache.dubbo.registry.client.event.ServiceDiscoveryDestroyingEvent; +import org.apache.dubbo.registry.client.event.ServiceDiscoveryInitializedEvent; +import org.apache.dubbo.registry.client.event.ServiceDiscoveryInitializingEvent; import org.apache.dubbo.registry.client.event.ServiceInstancePreRegisteredEvent; import org.apache.dubbo.registry.client.event.ServiceInstancePreUnregisteredEvent; import org.apache.dubbo.registry.client.event.ServiceInstanceRegisteredEvent; @@ -41,12 +41,12 @@ public class LoggingEventListener extends GenericEventListener { private final Logger logger = LoggerFactory.getLogger(getClass()); - public void onEvent(ServiceDiscoveryStartingEvent event) { - info("%s is starting...", event.getServiceDiscovery()); + public void onEvent(ServiceDiscoveryInitializingEvent event) { + info("%s is initializing...", event.getServiceDiscovery()); } - public void onEvent(ServiceDiscoveryStartedEvent event) { - info("%s is started.", event.getServiceDiscovery()); + public void onEvent(ServiceDiscoveryInitializedEvent event) { + info("%s is initialized.", event.getServiceDiscovery()); } public void onEvent(ServiceInstancePreRegisteredEvent event) { @@ -69,11 +69,11 @@ public void onEvent(ServiceInstanceUnregisteredEvent event) { info("%s has been unregistered from %s.", event.getServiceInstance(), event.getSource()); } - public void onEvent(ServiceDiscoveryStoppingEvent event) { + public void onEvent(ServiceDiscoveryDestroyingEvent event) { info("%s is stopping...", event.getServiceDiscovery()); } - public void onEvent(ServiceDiscoveryStoppedEvent event) { + public void onEvent(ServiceDiscoveryDestroyedEvent event) { info("%s is stopped.", event.getServiceDiscovery()); } diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/ExportedServicesRevisionMetadataCustomizer.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/ExportedServicesRevisionMetadataCustomizer.java index 31dac6bef6a..1045aaa8818 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/ExportedServicesRevisionMetadataCustomizer.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/ExportedServicesRevisionMetadataCustomizer.java @@ -29,8 +29,9 @@ import static java.lang.String.valueOf; import static java.util.Objects.hash; +import static org.apache.dubbo.metadata.WritableMetadataService.getExtension; import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.EXPORTED_SERVICES_REVISION_KEY; -import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.MEATADATA_STORED_TYPE_KEY; +import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getMetadataStorageType; /** * The customizer to a add the metadata that the reversion of Dubbo exported services calculates. @@ -48,9 +49,11 @@ protected String buildMetadataKey(ServiceInstance serviceInstance) { @Override protected String buildMetadataValue(ServiceInstance serviceInstance) { - WritableMetadataService writableMetadataService = WritableMetadataService.getExtension( - serviceInstance.getMetadata().get(MEATADATA_STORED_TYPE_KEY) - ); + + String metadataStorageType = getMetadataStorageType(serviceInstance); + + WritableMetadataService writableMetadataService = getExtension(metadataStorageType); + SortedSet exportedURLs = writableMetadataService.getExportedURLs(); Object[] data = exportedURLs.stream() .map(URL::valueOf) // String to URL 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 b1baa6536d2..8ea5a9807d5 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 @@ -25,9 +25,10 @@ import java.util.SortedSet; import static org.apache.dubbo.metadata.MetadataService.toURLs; -import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.MEATADATA_STORED_TYPE_KEY; +import static org.apache.dubbo.metadata.WritableMetadataService.getExtension; import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.METADATA_SERVICE_URL_PARAMS_KEY; import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getMetadataServiceParameter; +import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getMetadataStorageType; /** * An {@link ServiceInstanceMetadataCustomizer} to customize the {@link URL urls} of {@link MetadataService} @@ -46,9 +47,9 @@ public String buildMetadataKey(ServiceInstance serviceInstance) { @Override public String buildMetadataValue(ServiceInstance serviceInstance) { - WritableMetadataService writableMetadataService = WritableMetadataService.getExtension( - serviceInstance.getMetadata().get(MEATADATA_STORED_TYPE_KEY) - ); + String metadataStorageType = getMetadataStorageType(serviceInstance); + + WritableMetadataService writableMetadataService = getExtension(metadataStorageType); String serviceInterface = MetadataService.class.getName(); diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/RefreshServiceMetadataCustomizer.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/RefreshServiceMetadataCustomizer.java index 5fd089ee3e6..32657097318 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/RefreshServiceMetadataCustomizer.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/RefreshServiceMetadataCustomizer.java @@ -4,12 +4,9 @@ import org.apache.dubbo.registry.client.ServiceInstance; import org.apache.dubbo.registry.client.ServiceInstanceCustomizer; -import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.REVISION_KEY; import static org.apache.dubbo.metadata.WritableMetadataService.DEFAULT_EXTENSION; -import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.EXPORTED_SERVICES_REVISION_KEY; import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getExportedServicesRevision; -import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getMetadataStoredType; +import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getMetadataStorageType; import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getSubscribedServicesRevision; /** @@ -26,7 +23,7 @@ public int getPriority() { @Override public void customize(ServiceInstance serviceInstance) { // FIXME to define the constant - String metadataStoredType = getMetadataStoredType(serviceInstance); + String metadataStoredType = getMetadataStorageType(serviceInstance); WritableMetadataService remoteWritableMetadataService = WritableMetadataService.getExtension(metadataStoredType == null ? DEFAULT_EXTENSION : metadataStoredType); 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 329dea8c678..78402633e56 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 @@ -18,6 +18,7 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.metadata.MetadataService; +import org.apache.dubbo.metadata.WritableMetadataService; import org.apache.dubbo.registry.client.ServiceInstance; import com.alibaba.fastjson.JSON; @@ -30,6 +31,7 @@ import static java.lang.String.valueOf; import static java.util.Collections.emptyMap; import static org.apache.dubbo.common.utils.StringUtils.isBlank; +import static org.apache.dubbo.metadata.WritableMetadataService.DEFAULT_METADATA_STORAGE_TYPE; import static org.apache.dubbo.registry.integration.RegistryProtocol.DEFAULT_REGISTER_PROVIDER_KEYS; /** @@ -45,19 +47,19 @@ public class ServiceInstanceMetadataUtils { /** * The prefix of {@link MetadataService} : "dubbo.metadata-service." */ - public static final String DUBBO_METADATA_SERVICE_PREFIX = "dubbo.metadata-service."; + public static final String METADATA_SERVICE_PREFIX = "dubbo.metadata-service."; /** * The key of metadata JSON of {@link MetadataService}'s {@link URL} */ - public static String METADATA_SERVICE_URL_PARAMS_KEY = DUBBO_METADATA_SERVICE_PREFIX + "url-params"; + public static String METADATA_SERVICE_URL_PARAMS_KEY = METADATA_SERVICE_PREFIX + "url-params"; /** * The {@link URL URLs} property name of {@link MetadataService} : * "dubbo.metadata-service.urls", which is used to be compatible with Dubbo Spring Cloud and * discovery the metadata of instance */ - public static final String DUBBO_METADATA_SERVICE_URLS_PROPERTY_NAME = DUBBO_METADATA_SERVICE_PREFIX + "urls"; + public static final String METADATA_SERVICE_URLS_PROPERTY_NAME = METADATA_SERVICE_PREFIX + "urls"; /** * The key of The revision for all exported Dubbo services. @@ -70,9 +72,9 @@ public class ServiceInstanceMetadataUtils { public static String SUBSCRIBER_SERVICES_REVISION_KEY = "dubbo.subscribed-services.revision"; /** - * The key of metadata store type. + * The key of metadata storage type. */ - public static String MEATADATA_STORED_TYPE_KEY = "dubbo.metadata.stored-type"; + public static String METADATA_STORAGE_TYPE_KEY = "dubbo.metadata.storage-type"; /** * The {@link URL url's} parameter name of Dubbo Provider host @@ -181,14 +183,37 @@ public static String getSubscribedServicesRevision(ServiceInstance serviceInstan } /** - * The metadata is stored in the type used to which {@link org.apache.dubbo.metadata.WritableMetadataService} instance. + * Get metadata's storage type + * + * @param registryURL the {@link URL} to connect the registry + * @return if not found in {@link URL#getParameters() parameters} of {@link URL registry URL}, return + * {@link WritableMetadataService#DEFAULT_METADATA_STORAGE_TYPE "default"} + */ + public static String getMetadataStorageType(URL registryURL) { + return registryURL.getParameter(METADATA_STORAGE_TYPE_KEY, DEFAULT_METADATA_STORAGE_TYPE); + } + + /** + * Get the metadata's storage type is used to which {@link WritableMetadataService} instance. * * @param serviceInstance the specified {@link ServiceInstance} - * @return null if not exits + * @return if not found in {@link ServiceInstance#getMetadata() metadata} of {@link ServiceInstance}, return + * {@link WritableMetadataService#DEFAULT_METADATA_STORAGE_TYPE "default"} + */ + public static String getMetadataStorageType(ServiceInstance serviceInstance) { + Map metadata = serviceInstance.getMetadata(); + return metadata.getOrDefault(METADATA_STORAGE_TYPE_KEY, DEFAULT_METADATA_STORAGE_TYPE); + } + + /** + * Set the metadata storage type in specified {@link ServiceInstance service instance} + * + * @param serviceInstance {@link ServiceInstance service instance} + * @param isDefaultStorageType is default storage type or not */ - public static String getMetadataStoredType(ServiceInstance serviceInstance) { + public static void setMetadataStorageType(ServiceInstance serviceInstance, boolean isDefaultStorageType) { Map metadata = serviceInstance.getMetadata(); - return metadata.get(MEATADATA_STORED_TYPE_KEY); + metadata.put(METADATA_STORAGE_TYPE_KEY, WritableMetadataService.getMetadataStorageType(isDefaultStorageType)); } private static void setProviderHostParam(Map params, URL providerURL) { diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/SpringCloudMetadataServiceURLBuilder.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/SpringCloudMetadataServiceURLBuilder.java index 7eb3664d1cf..efc3253a10b 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/SpringCloudMetadataServiceURLBuilder.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/SpringCloudMetadataServiceURLBuilder.java @@ -27,7 +27,7 @@ import java.util.Map; import java.util.stream.Collectors; -import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.DUBBO_METADATA_SERVICE_URLS_PROPERTY_NAME; +import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.METADATA_SERVICE_URLS_PROPERTY_NAME; /** * The {@link MetadataServiceURLBuilder} implementation for The standard Dubbo scenario @@ -39,7 +39,7 @@ public class SpringCloudMetadataServiceURLBuilder implements MetadataServiceURLB @Override public List build(ServiceInstance serviceInstance) { Map metadata = serviceInstance.getMetadata(); - String dubboURLsJSON = metadata.get(DUBBO_METADATA_SERVICE_URLS_PROPERTY_NAME); + String dubboURLsJSON = metadata.get(METADATA_SERVICE_URLS_PROPERTY_NAME); if (StringUtils.isBlank(dubboURLsJSON)) { return Collections.emptyList(); } diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/SubscribedServicesRevisionMetadataCustomizer.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/SubscribedServicesRevisionMetadataCustomizer.java index ca89b266dc2..c0e2c4ab493 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/SubscribedServicesRevisionMetadataCustomizer.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/SubscribedServicesRevisionMetadataCustomizer.java @@ -29,8 +29,9 @@ import static java.lang.String.valueOf; import static java.util.Objects.hash; -import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.MEATADATA_STORED_TYPE_KEY; +import static org.apache.dubbo.metadata.WritableMetadataService.getExtension; import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.SUBSCRIBER_SERVICES_REVISION_KEY; +import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getMetadataStorageType; /** * The customizer to a add the metadata that the reversion of Dubbo subscribed services calculates. @@ -48,9 +49,11 @@ protected String buildMetadataKey(ServiceInstance serviceInstance) { @Override protected String buildMetadataValue(ServiceInstance serviceInstance) { - WritableMetadataService writableMetadataService = WritableMetadataService.getExtension( - serviceInstance.getMetadata().get(MEATADATA_STORED_TYPE_KEY) - ); + + String metadataStorageType = getMetadataStorageType(serviceInstance); + + WritableMetadataService writableMetadataService = getExtension(metadataStorageType); + SortedSet subscribedURLs = writableMetadataService.getSubscribedURLs(); Object[] data = subscribedURLs.stream() .map(URL::valueOf) // String to URL diff --git a/dubbo-registry/dubbo-registry-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.RegistryFactory b/dubbo-registry/dubbo-registry-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.RegistryFactory index d533e06240b..0dc70024c68 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.RegistryFactory +++ b/dubbo-registry/dubbo-registry-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.RegistryFactory @@ -1 +1 @@ -service-oriented-registry=org.apache.dubbo.registry.service.ServiceOrientedRegistryFactory \ No newline at end of file +service-discovery-registry=org.apache.dubbo.registry.client.ServiceDiscoveryRegistryFactory \ No newline at end of file diff --git a/dubbo-registry/dubbo-registry-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscovery b/dubbo-registry/dubbo-registry-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscovery new file mode 100644 index 00000000000..d64d12ca25b --- /dev/null +++ b/dubbo-registry/dubbo-registry-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscovery @@ -0,0 +1 @@ +file=org.apache.dubbo.registry.client.FileSystemServiceDiscovery \ No newline at end of file diff --git a/dubbo-registry/dubbo-registry-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscoveryFactory b/dubbo-registry/dubbo-registry-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscoveryFactory new file mode 100644 index 00000000000..d30ea0c1866 --- /dev/null +++ b/dubbo-registry/dubbo-registry-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscoveryFactory @@ -0,0 +1 @@ +default=org.apache.dubbo.registry.client.DefaultServiceDiscoveryFactory \ No newline at end of file diff --git a/dubbo-registry/dubbo-registry-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.Protocol b/dubbo-registry/dubbo-registry-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.Protocol index ed2df317faf..c8903b0abf0 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.Protocol +++ b/dubbo-registry/dubbo-registry-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.Protocol @@ -1,2 +1,2 @@ registry=org.apache.dubbo.registry.integration.RegistryProtocol -service-oriented-registry=org.apache.dubbo.registry.service.ServiceOrientedRegistryProtocol \ No newline at end of file +service-discovery-registry=org.apache.dubbo.registry.client.ServiceDiscoveryRegistryProtocol \ No newline at end of file diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/EventPublishingServiceDiscoveryTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/EventPublishingServiceDiscoveryTest.java index 5bc272972c5..97ccf8eaf31 100644 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/EventPublishingServiceDiscoveryTest.java +++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/EventPublishingServiceDiscoveryTest.java @@ -19,10 +19,10 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.event.EventDispatcher; import org.apache.dubbo.event.EventListener; -import org.apache.dubbo.registry.client.event.ServiceDiscoveryStartedEvent; -import org.apache.dubbo.registry.client.event.ServiceDiscoveryStartingEvent; -import org.apache.dubbo.registry.client.event.ServiceDiscoveryStoppedEvent; -import org.apache.dubbo.registry.client.event.ServiceDiscoveryStoppingEvent; +import org.apache.dubbo.registry.client.event.ServiceDiscoveryDestroyedEvent; +import org.apache.dubbo.registry.client.event.ServiceDiscoveryDestroyingEvent; +import org.apache.dubbo.registry.client.event.ServiceDiscoveryInitializedEvent; +import org.apache.dubbo.registry.client.event.ServiceDiscoveryInitializingEvent; import org.apache.dubbo.registry.client.event.ServiceInstancePreRegisteredEvent; import org.apache.dubbo.registry.client.event.ServiceInstanceRegisteredEvent; @@ -53,7 +53,7 @@ public class EventPublishingServiceDiscoveryTest { private ServiceDiscoveryTest serviceDiscoveryTest; @BeforeEach - public void init() { + public void init() throws Exception { // remove all EventListeners eventDispatcher.removeAllEventListeners(); @@ -67,17 +67,17 @@ public void init() { serviceDiscoveryTest.setServiceDiscovery(serviceDiscovery); // ServiceDiscoveryStartingEvent - eventDispatcher.addEventListener(new EventListener() { + eventDispatcher.addEventListener(new EventListener() { @Override - public void onEvent(ServiceDiscoveryStartingEvent event) { + public void onEvent(ServiceDiscoveryInitializingEvent event) { assertEquals(delegate, event.getServiceDiscovery()); } }); // ServiceDiscoveryStartedEvent - eventDispatcher.addEventListener(new EventListener() { + eventDispatcher.addEventListener(new EventListener() { @Override - public void onEvent(ServiceDiscoveryStartedEvent event) { + public void onEvent(ServiceDiscoveryInitializedEvent event) { assertEquals(delegate, event.getServiceDiscovery()); } }); @@ -98,43 +98,43 @@ public void onEvent(ServiceInstanceRegisteredEvent event) { } }); - assertFalse(serviceDiscovery.isStarted()); - assertFalse(serviceDiscovery.isStopped()); + assertFalse(serviceDiscovery.isInitialized()); + assertFalse(serviceDiscovery.isDestroyed()); // test start() serviceDiscoveryTest.init(); - assertTrue(serviceDiscovery.isStarted()); - assertFalse(serviceDiscovery.isStopped()); + assertTrue(serviceDiscovery.isInitialized()); + assertFalse(serviceDiscovery.isDestroyed()); } @AfterEach - public void destroy() { + public void destroy() throws Exception { // ServiceDiscoveryStoppingEvent - eventDispatcher.addEventListener(new EventListener() { + eventDispatcher.addEventListener(new EventListener() { @Override - public void onEvent(ServiceDiscoveryStoppingEvent event) { + public void onEvent(ServiceDiscoveryDestroyingEvent event) { assertEquals(delegate, event.getServiceDiscovery()); } }); // ServiceDiscoveryStoppedEvent - eventDispatcher.addEventListener(new EventListener() { + eventDispatcher.addEventListener(new EventListener() { @Override - public void onEvent(ServiceDiscoveryStoppedEvent event) { + public void onEvent(ServiceDiscoveryDestroyedEvent event) { assertEquals(delegate, event.getServiceDiscovery()); } }); - assertTrue(serviceDiscovery.isStarted()); - assertFalse(serviceDiscovery.isStopped()); + assertTrue(serviceDiscovery.isInitialized()); + assertFalse(serviceDiscovery.isDestroyed()); // test stop() serviceDiscoveryTest.destroy(); - assertTrue(serviceDiscovery.isStarted()); - assertTrue(serviceDiscovery.isStopped()); + assertTrue(serviceDiscovery.isInitialized()); + assertTrue(serviceDiscovery.isDestroyed()); } @Test diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/InMemoryServiceDiscovery.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/InMemoryServiceDiscovery.java index a8954c550e7..6f7c3ea4330 100644 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/InMemoryServiceDiscovery.java +++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/InMemoryServiceDiscovery.java @@ -16,6 +16,7 @@ */ package org.apache.dubbo.registry.client; +import org.apache.dubbo.common.URL; import org.apache.dubbo.common.utils.DefaultPage; import org.apache.dubbo.common.utils.Page; import org.apache.dubbo.event.EventDispatcher; @@ -95,12 +96,12 @@ public void unregister(ServiceInstance serviceInstance) throws RuntimeException } @Override - public void start() { + public void initialize(URL registryURL) throws Exception { } @Override - public void stop() { + public void destroy() { } @Override diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/InMemoryServiceDiscoveryFactory.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/InMemoryServiceDiscoveryFactory.java deleted file mode 100644 index 6264611d7c2..00000000000 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/InMemoryServiceDiscoveryFactory.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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.registry.client; - -import org.apache.dubbo.common.URL; - -/** - * {@link InMemoryServiceDiscovery} Factory - * - * @see InMemoryServiceDiscovery - * @since 2.7.4 - */ -public class InMemoryServiceDiscoveryFactory extends AbstractServiceDiscoveryFactory { - - @Override - public ServiceDiscovery createDiscovery(URL connectionURL) { - return new InMemoryServiceDiscovery(); - } -} diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/ServiceDiscoveryTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/ServiceDiscoveryTest.java index aa607418c77..1d0e0d3fa88 100644 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/ServiceDiscoveryTest.java +++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/ServiceDiscoveryTest.java @@ -41,18 +41,18 @@ public class ServiceDiscoveryTest { private ServiceDiscovery serviceDiscovery; @BeforeEach - public void init() { + public void init() throws Exception { if (serviceDiscovery == null) { setServiceDiscovery(new InMemoryServiceDiscovery()); } // test start() - serviceDiscovery.start(); + serviceDiscovery.initialize(null); } @AfterEach - public void destroy() { + public void destroy() throws Exception { // test stop() - serviceDiscovery.stop(); + serviceDiscovery.destroy(); } @Test diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/event/listener/LoggingEventListenerTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/event/listener/LoggingEventListenerTest.java index ddd3aecd08e..f594a43c3fb 100644 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/event/listener/LoggingEventListenerTest.java +++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/event/listener/LoggingEventListenerTest.java @@ -19,10 +19,10 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.registry.client.FileSystemServiceDiscovery; import org.apache.dubbo.registry.client.ServiceDiscovery; -import org.apache.dubbo.registry.client.event.ServiceDiscoveryStartedEvent; -import org.apache.dubbo.registry.client.event.ServiceDiscoveryStartingEvent; -import org.apache.dubbo.registry.client.event.ServiceDiscoveryStoppedEvent; -import org.apache.dubbo.registry.client.event.ServiceDiscoveryStoppingEvent; +import org.apache.dubbo.registry.client.event.ServiceDiscoveryDestroyedEvent; +import org.apache.dubbo.registry.client.event.ServiceDiscoveryDestroyingEvent; +import org.apache.dubbo.registry.client.event.ServiceDiscoveryInitializedEvent; +import org.apache.dubbo.registry.client.event.ServiceDiscoveryInitializingEvent; import org.apache.dubbo.registry.client.event.ServiceInstancePreRegisteredEvent; import org.apache.dubbo.registry.client.event.ServiceInstancePreUnregisteredEvent; import org.apache.dubbo.registry.client.event.ServiceInstanceRegisteredEvent; @@ -50,16 +50,19 @@ public void init() { } @Test - public void testOnEvent() { + public void testOnEvent() throws Exception { URL connectionURL = URL.valueOf("file:///Users/Home"); - ServiceDiscovery serviceDiscovery = new FileSystemServiceDiscovery(connectionURL); + + ServiceDiscovery serviceDiscovery = new FileSystemServiceDiscovery(); + + serviceDiscovery.initialize(connectionURL); // ServiceDiscoveryStartingEvent - listener.onEvent(new ServiceDiscoveryStartingEvent(serviceDiscovery)); + listener.onEvent(new ServiceDiscoveryInitializingEvent(serviceDiscovery)); // ServiceDiscoveryStartedEvent - listener.onEvent(new ServiceDiscoveryStartedEvent(serviceDiscovery)); + listener.onEvent(new ServiceDiscoveryInitializedEvent(serviceDiscovery)); // ServiceInstancePreRegisteredEvent listener.onEvent(new ServiceInstancePreRegisteredEvent(serviceDiscovery, createInstance())); @@ -77,9 +80,9 @@ public void testOnEvent() { listener.onEvent(new ServiceInstanceUnregisteredEvent(serviceDiscovery, createInstance())); // ServiceDiscoveryStoppingEvent - listener.onEvent(new ServiceDiscoveryStoppingEvent(serviceDiscovery)); + listener.onEvent(new ServiceDiscoveryDestroyingEvent(serviceDiscovery)); // ServiceDiscoveryStoppedEvent - listener.onEvent(new ServiceDiscoveryStoppedEvent(serviceDiscovery)); + listener.onEvent(new ServiceDiscoveryDestroyedEvent(serviceDiscovery)); } } diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/support/ServiceOrientedRegistryTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/support/ServiceOrientedRegistryTest.java index 5d9157d624a..f884b9d167d 100644 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/support/ServiceOrientedRegistryTest.java +++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/support/ServiceOrientedRegistryTest.java @@ -19,7 +19,7 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.metadata.WritableMetadataService; import org.apache.dubbo.registry.NotifyListener; -import org.apache.dubbo.registry.service.ServiceOrientedRegistry; +import org.apache.dubbo.registry.client.ServiceDiscoveryRegistry; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -43,7 +43,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; /** - * {@link ServiceOrientedRegistry} Test + * {@link ServiceDiscoveryRegistry} Test * * @since 2.7.4 */ @@ -72,20 +72,20 @@ public class ServiceOrientedRegistryTest { private WritableMetadataService metadataService; - private ServiceOrientedRegistry registry; + private ServiceDiscoveryRegistry registry; private NotifyListener notifyListener; @BeforeEach public void init() { - registry = ServiceOrientedRegistry.create(registryURL); + registry = ServiceDiscoveryRegistry.create(registryURL); metadataService = WritableMetadataService.getDefaultExtension(); notifyListener = new MyNotifyListener(); } @Test public void testSupports() { - assertTrue(ServiceOrientedRegistry.supports(registryURL)); + assertTrue(ServiceDiscoveryRegistry.supports(registryURL)); } @Test diff --git a/dubbo-registry/dubbo-registry-api/src/test/resources/META-INF/services/org.apache.dubbo.registry.client.ServiceDiscoveryFactory b/dubbo-registry/dubbo-registry-api/src/test/resources/META-INF/services/org.apache.dubbo.registry.client.ServiceDiscovery similarity index 86% rename from dubbo-registry/dubbo-registry-api/src/test/resources/META-INF/services/org.apache.dubbo.registry.client.ServiceDiscoveryFactory rename to dubbo-registry/dubbo-registry-api/src/test/resources/META-INF/services/org.apache.dubbo.registry.client.ServiceDiscovery index 2a280741612..53725cbe6ae 100644 --- a/dubbo-registry/dubbo-registry-api/src/test/resources/META-INF/services/org.apache.dubbo.registry.client.ServiceDiscoveryFactory +++ b/dubbo-registry/dubbo-registry-api/src/test/resources/META-INF/services/org.apache.dubbo.registry.client.ServiceDiscovery @@ -1 +1 @@ -in-memory=org.apache.dubbo.registry.client.InMemoryServiceDiscoveryFactory \ No newline at end of file +in-memory=org.apache.dubbo.registry.client.InMemoryServiceDiscovery \ No newline at end of file diff --git a/dubbo-registry/dubbo-registry-consul/src/main/java/org/apache/dubbo/registry/consul/ConsulServiceDiscovery.java b/dubbo-registry/dubbo-registry-consul/src/main/java/org/apache/dubbo/registry/consul/ConsulServiceDiscovery.java index 503823a1bf6..22e44a149c8 100644 --- a/dubbo-registry/dubbo-registry-consul/src/main/java/org/apache/dubbo/registry/consul/ConsulServiceDiscovery.java +++ b/dubbo-registry/dubbo-registry-consul/src/main/java/org/apache/dubbo/registry/consul/ConsulServiceDiscovery.java @@ -63,6 +63,7 @@ * 2019-07-31 */ public class ConsulServiceDiscovery implements ServiceDiscovery, EventListener { + private static final Logger logger = LoggerFactory.getLogger(ConsulServiceDiscovery.class); private ConsulClient client; @@ -72,28 +73,24 @@ public class ConsulServiceDiscovery implements ServiceDiscovery, EventListener childListenerMap = new ConcurrentHashMap<>(); private final ConcurrentMap> etcdListeners = new ConcurrentHashMap<>(); - private final EtcdClient etcdClient; - private final EventDispatcher dispatcher; + + private EtcdClient etcdClient; + private EventDispatcher dispatcher; private ServiceInstance serviceInstance; - public EtcdServiceDiscovery(URL url, EtcdTransporter etcdTransporter) { - if (url.isAnyHost()) { - throw new IllegalStateException("Service discovery address is invalid, actual: '" + url.getHost() + "'"); + @Override + public void onEvent(ServiceInstancesChangedEvent event) { + registerServiceWatcher(event.getServiceName()); + } + + @Override + public void initialize(URL registryURL) throws Exception { + + EtcdTransporter etcdTransporter = ExtensionLoader.getExtensionLoader(EtcdTransporter.class).getAdaptiveExtension(); + + if (registryURL.isAnyHost()) { + throw new IllegalStateException("Service discovery address is invalid, actual: '" + registryURL.getHost() + "'"); } - etcdClient = etcdTransporter.connect(url); + + etcdClient = etcdTransporter.connect(registryURL); etcdClient.addStateListener(state -> { if (state == StateListener.CONNECTED) { @@ -82,17 +94,7 @@ public EtcdServiceDiscovery(URL url, EtcdTransporter etcdTransporter) { } @Override - public void onEvent(ServiceInstancesChangedEvent event) { - registerServiceWatcher(event.getServiceName()); - } - - @Override - public void start() { - - } - - @Override - public void stop() { + public void destroy() { } diff --git a/dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdServiceDiscoveryFactory.java b/dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdServiceDiscoveryFactory.java deleted file mode 100644 index fb7500669d3..00000000000 --- a/dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdServiceDiscoveryFactory.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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.registry.etcd; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.registry.client.AbstractServiceDiscoveryFactory; -import org.apache.dubbo.registry.client.ServiceDiscovery; -import org.apache.dubbo.remoting.etcd.EtcdTransporter; - -/** - * 2019-07-08 - */ -public class EtcdServiceDiscoveryFactory extends AbstractServiceDiscoveryFactory { - - private EtcdTransporter etcdTransporter; - - public void setEtcdTransporter(EtcdTransporter etcdTransporter) { - this.etcdTransporter = etcdTransporter; - } - - @Override - public ServiceDiscovery createDiscovery(URL connectionURL) { - return new EtcdServiceDiscovery(connectionURL, etcdTransporter); - } -} diff --git a/dubbo-registry/dubbo-registry-etcd3/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscovery b/dubbo-registry/dubbo-registry-etcd3/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscovery new file mode 100644 index 00000000000..60f47ac9e4f --- /dev/null +++ b/dubbo-registry/dubbo-registry-etcd3/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscovery @@ -0,0 +1 @@ +etcd=org.apache.dubbo.registry.etcd.EtcdServiceDiscovery \ No newline at end of file diff --git a/dubbo-registry/dubbo-registry-etcd3/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscoveryFactory b/dubbo-registry/dubbo-registry-etcd3/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscoveryFactory deleted file mode 100644 index ad5937af662..00000000000 --- a/dubbo-registry/dubbo-registry-etcd3/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscoveryFactory +++ /dev/null @@ -1 +0,0 @@ -etcd3=org.apache.dubbo.registry.etcd.EtcdServiceDiscoveryFactory 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 0db74301b9f..8863e8915c4 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 @@ -51,24 +51,18 @@ public class NacosServiceDiscovery implements ServiceDiscovery { private final Logger logger = LoggerFactory.getLogger(getClass()); - private final URL connectionURL; - - private final String group; + private String group; private NamingService namingService; - public NacosServiceDiscovery(URL connectionURL) { - this.connectionURL = connectionURL; - this.group = getGroup(connectionURL); - } - @Override - public void start() { - this.namingService = createNamingService(connectionURL); + public void initialize(URL registryURL) throws Exception { + this.namingService = createNamingService(registryURL); + this.group = getGroup(registryURL); } @Override - public void stop() { + public void destroy() { this.namingService = null; } diff --git a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosServiceDiscoveryFactory.java b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosServiceDiscoveryFactory.java deleted file mode 100644 index e2ac08bb14d..00000000000 --- a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosServiceDiscoveryFactory.java +++ /dev/null @@ -1,36 +0,0 @@ -package org.apache.dubbo.registry.nacos;/* - * 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. - */ - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.registry.client.AbstractServiceDiscoveryFactory; -import org.apache.dubbo.registry.client.ServiceDiscovery; -import org.apache.dubbo.registry.client.ServiceDiscoveryFactory; - -/** - * Nacos {@link ServiceDiscoveryFactory} - * - * @see ServiceDiscoveryFactory - * @see ServiceDiscovery - * @since 2.7.4 - */ -public class NacosServiceDiscoveryFactory extends AbstractServiceDiscoveryFactory { - - @Override - public ServiceDiscovery createDiscovery(URL connectionURL) { - return new NacosServiceDiscovery(connectionURL); - } -} diff --git a/dubbo-registry/dubbo-registry-nacos/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscovery b/dubbo-registry/dubbo-registry-nacos/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscovery new file mode 100644 index 00000000000..e3819e092ab --- /dev/null +++ b/dubbo-registry/dubbo-registry-nacos/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscovery @@ -0,0 +1 @@ +nacos=org.apache.dubbo.registry.nacos.NacosServiceDiscovery \ No newline at end of file diff --git a/dubbo-registry/dubbo-registry-nacos/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscoveryFactory b/dubbo-registry/dubbo-registry-nacos/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscoveryFactory deleted file mode 100644 index 4fc3f4a4dd2..00000000000 --- a/dubbo-registry/dubbo-registry-nacos/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscoveryFactory +++ /dev/null @@ -1 +0,0 @@ -nacos=org.apache.dubbo.registry.nacos.NacosServiceDiscoveryFactory \ No newline at end of file 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 9ee5dde7cf8..feac09358f1 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 @@ -56,25 +56,31 @@ public class ZookeeperServiceDiscovery implements ServiceDiscovery, EventListene private final Logger logger = LoggerFactory.getLogger(getClass()); - private final CuratorFramework curatorFramework; + private EventDispatcher dispatcher; - private final String rootPath; + private CuratorFramework curatorFramework; - private final org.apache.curator.x.discovery.ServiceDiscovery serviceDiscovery; + private String rootPath; - private final EventDispatcher dispatcher; + private org.apache.curator.x.discovery.ServiceDiscovery serviceDiscovery; /** * The Key is watched Zookeeper path, the value is an instance of {@link CuratorWatcher} */ private final Map watcherCaches = new ConcurrentHashMap<>(); - public ZookeeperServiceDiscovery(URL connectionURL) throws Exception { - this.curatorFramework = buildCuratorFramework(connectionURL); - this.rootPath = ROOT_PATH.getParameterValue(connectionURL); - this.serviceDiscovery = buildServiceDiscovery(curatorFramework, rootPath); + @Override + public void initialize(URL registryURL) throws Exception { this.dispatcher = EventDispatcher.getDefaultExtension(); this.dispatcher.addEventListener(this); + this.curatorFramework = buildCuratorFramework(registryURL); + this.rootPath = ROOT_PATH.getParameterValue(registryURL); + this.serviceDiscovery = buildServiceDiscovery(curatorFramework, rootPath); + this.serviceDiscovery.start(); + } + + public void destroy() throws Exception { + serviceDiscovery.close(); } public void register(ServiceInstance serviceInstance) throws RuntimeException { @@ -95,18 +101,6 @@ public void unregister(ServiceInstance serviceInstance) throws RuntimeException }); } - public void start() { - doInServiceRegistry(serviceDiscovery -> { - serviceDiscovery.start(); - }); - } - - public void stop() { - doInServiceRegistry(serviceDiscovery -> { - serviceDiscovery.close(); - }); - } - @Override public Set getServices() { return doInServiceDiscovery(s -> new LinkedHashSet<>(s.queryForNames())); diff --git a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/util/CuratorFrameworkUtils.java b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/util/CuratorFrameworkUtils.java index 901acfcbb50..e72b87dd68b 100644 --- a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/util/CuratorFrameworkUtils.java +++ b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/util/CuratorFrameworkUtils.java @@ -20,7 +20,6 @@ import org.apache.dubbo.registry.client.DefaultServiceInstance; import org.apache.dubbo.registry.client.ServiceInstance; import org.apache.dubbo.registry.zookeeper.ZookeeperInstance; -import org.apache.dubbo.registry.zookeeper.ZookeeperServiceDiscovery; import org.apache.curator.RetryPolicy; import org.apache.curator.framework.CuratorFramework; @@ -49,10 +48,6 @@ */ public abstract class CuratorFrameworkUtils { - public static ZookeeperServiceDiscovery buildZookeeperServiceDiscovery(URL connectionURL) throws Exception { - return new ZookeeperServiceDiscovery(connectionURL); - } - public static ServiceDiscovery buildServiceDiscovery(CuratorFramework curatorFramework, String basePath) { return ServiceDiscoveryBuilder.builder(ZookeeperInstance.class) diff --git a/dubbo-registry/dubbo-registry-zookeeper/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscoveryFactory b/dubbo-registry/dubbo-registry-zookeeper/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscovery similarity index 82% rename from dubbo-registry/dubbo-registry-zookeeper/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscoveryFactory rename to dubbo-registry/dubbo-registry-zookeeper/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscovery index 12262adad29..5532b31946d 100644 --- a/dubbo-registry/dubbo-registry-zookeeper/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscoveryFactory +++ b/dubbo-registry/dubbo-registry-zookeeper/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceDiscovery @@ -1 +1 @@ -zookeeper=org.apache.dubbo.registry.zookeeper.ZookeeperServiceDiscoveryFactory \ No newline at end of file +zookeeper=org.apache.dubbo.registry.zookeeper.ZookeeperServiceDiscovery \ No newline at end of file diff --git a/dubbo-registry/dubbo-registry-zookeeper/src/main/resources/META-INF/services/org.apache.dubbo.registry.client.ServiceDiscoveryFactory b/dubbo-registry/dubbo-registry-zookeeper/src/main/resources/META-INF/services/org.apache.dubbo.registry.client.ServiceDiscoveryFactory deleted file mode 100644 index 9e2e9ec9c91..00000000000 --- a/dubbo-registry/dubbo-registry-zookeeper/src/main/resources/META-INF/services/org.apache.dubbo.registry.client.ServiceDiscoveryFactory +++ /dev/null @@ -1 +0,0 @@ -org.apache.dubbo.registry.zookeeper.ZookeeperServiceDiscoveryFactory \ No newline at end of file diff --git a/dubbo-registry/dubbo-registry-zookeeper/src/test/java/org/apache/dubbo/registry/zookeeper/ZookeeperServiceDiscoveryTest.java b/dubbo-registry/dubbo-registry-zookeeper/src/test/java/org/apache/dubbo/registry/zookeeper/ZookeeperServiceDiscoveryTest.java index 5f14ec5849e..12c1a1981c7 100644 --- a/dubbo-registry/dubbo-registry-zookeeper/src/test/java/org/apache/dubbo/registry/zookeeper/ZookeeperServiceDiscoveryTest.java +++ b/dubbo-registry/dubbo-registry-zookeeper/src/test/java/org/apache/dubbo/registry/zookeeper/ZookeeperServiceDiscoveryTest.java @@ -27,7 +27,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.io.IOException; import java.util.HashMap; import java.util.LinkedList; import java.util.List; @@ -36,7 +35,6 @@ import static java.util.Arrays.asList; import static org.apache.dubbo.common.utils.NetUtils.getAvailablePort; -import static org.apache.dubbo.registry.zookeeper.util.CuratorFrameworkUtils.buildZookeeperServiceDiscovery; import static org.apache.dubbo.registry.zookeeper.util.CuratorFrameworkUtils.generateId; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -67,13 +65,13 @@ public void init() throws Exception { zkServer.start(); this.registryUrl = URL.valueOf("zookeeper://127.0.0.1:" + zkServerPort); - this.discovery = buildZookeeperServiceDiscovery(registryUrl); - this.discovery.start(); + this.discovery = new ZookeeperServiceDiscovery(); + this.discovery.initialize(registryUrl); } @AfterEach - public void close() throws IOException { - discovery.stop(); + public void close() throws Exception { + discovery.destroy(); zkServer.stop(); }