From d105b744159046dc2e420b5d0441e4d0b6e381e9 Mon Sep 17 00:00:00 2001 From: Mercy Ma Date: Fri, 23 Aug 2019 10:39:24 +0800 Subject: [PATCH] Dubbo cloud native (#4923) 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 * Polish apache/dubbo#4845 : [Feature] Enhance the Event-Publishing feature to original ServiceDiscovery * Merge remote-tracking branch 'upstream/cloud-native' into dubbo-cloud-native # Conflicts: # dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultServiceDiscoveryFactory.java # dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java * Merge remote-tracking branch 'upstream/cloud-native' into dubbo-cloud-native # Conflicts: # dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultServiceDiscoveryFactory.java # dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java * Polish apache/dubbo#4854 : [Feature] MetadataService supports the Dubbo protocol under auto-increased port * Polish apache/dubbo#4857 : [Enhancement] Sync the Metadata storage type into ApplicationConfig * Polish apache/dubbo#4868 : [Enhancement] Refactor ConfigChangeEvent * Polish apache/dubbo#4868 : [Enhancement] Refactor ConfigChangeEvent * Polish apache/dubbo#4873 : [Feature] Add a conditional EventListener into Event Module * Polish apache/dubbo#4875 : [Feature] Refactor ServiceInstancesChangedListener * Remove the cycle dependencies * Remove the cycle dependencies * Polish apache/dubbo#4903 : [Feature] Set source into the BeanDefinition of Dubbo Config * Polish apache/dubbo#4902 : [Feature] Dubbo Cloud Native to Spring XML scenario * Polish apache/dubbo#4713 : Initial the new module and dependencies * Polish apache/dubbo#4690 : AnnotatedBeanDefinitionRegistryUtils#registerBeans can't remove the duplicated bean definitions * Polish apache/dubbo#4690 : AnnotatedBeanDefinitionRegistryUtils#registerBeans can't remove the duplicated bean definitions * Polish apache/dubbo#4690 : AnnotatedBeanDefinitionRegistryUtils#registerBeans can't remove the duplicated bean definitions * Polish apache/dubbo#4910 : [Feature] To suppoort DubboLifecycleComponentApplicationListener in Spring XML scenario * Polish apache/dubbo#4713 : Add Service discovery implementation for Eureka #4713 * Polish apache/dubbo#4713 : Add Service registration and discovery implementation for Eureka * Polish apache/dubbo#4713 : Add Service registration and discovery implementation for Eureka * Polish apache/dubbo#4920 : [Refactor] Extract the common implementation for URLs' revision --- ...tedServicesRevisionMetadataCustomizer.java | 29 ++----- ...ataServiceURLParamsMetadataCustomizer.java | 4 +- .../ServiceInstanceMetadataUtils.java | 54 ++++++++----- ...bedServicesRevisionMetadataCustomizer.java | 30 ++------ .../client/metadata/URLRevisionResolver.java | 77 +++++++++++++++++++ .../proxy/RemoteMetadataServiceProxy.java | 5 +- .../metadata/URLRevisionResolverTest.java | 47 +++++++++++ .../consul/ConsulServiceDiscovery.java | 2 +- 8 files changed, 172 insertions(+), 76 deletions(-) create mode 100644 dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/URLRevisionResolver.java create mode 100644 dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/URLRevisionResolverTest.java 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 1045aaa8818..0fc450c84d5 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 @@ -16,21 +16,14 @@ */ package org.apache.dubbo.registry.client.metadata; -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.compiler.support.ClassUtils; -import org.apache.dubbo.metadata.MetadataService; import org.apache.dubbo.metadata.WritableMetadataService; import org.apache.dubbo.registry.client.ServiceInstance; import org.apache.dubbo.registry.client.ServiceInstanceMetadataCustomizer; -import java.util.Arrays; -import java.util.Collection; import java.util.SortedSet; -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.EXPORTED_SERVICES_REVISION_PROPERTY_NAME; import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getMetadataStorageType; /** @@ -44,7 +37,7 @@ public class ExportedServicesRevisionMetadataCustomizer extends ServiceInstanceM @Override protected String buildMetadataKey(ServiceInstance serviceInstance) { - return EXPORTED_SERVICES_REVISION_KEY; + return EXPORTED_SERVICES_REVISION_PROPERTY_NAME; } @Override @@ -55,21 +48,9 @@ protected String buildMetadataValue(ServiceInstance serviceInstance) { WritableMetadataService writableMetadataService = getExtension(metadataStorageType); SortedSet exportedURLs = writableMetadataService.getExportedURLs(); - Object[] data = exportedURLs.stream() - .map(URL::valueOf) // String to URL - .map(URL::getServiceInterface) // get the service interface - .filter(this::isNotMetadataService) // filter not MetadataService interface - .map(ClassUtils::forName) // load business interface class - .map(Class::getMethods) // get all public methods from business interface - .map(Arrays::asList) // Array to List - .flatMap(Collection::stream) // flat Stream to be Stream - .map(Object::toString) // Method to String - .sorted() // sort methods marking sure the calculation of reversion is stable - .toArray(); // Stream to Array - return valueOf(hash(data)); // calculate the hash code as reversion - } - private boolean isNotMetadataService(String serviceInterface) { - return !MetadataService.class.getName().equals(serviceInterface); + URLRevisionResolver resolver = new URLRevisionResolver(); + + return resolver.resolve(exportedURLs); } } 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 8ea5a9807d5..39f7c27bb06 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 @@ -26,7 +26,7 @@ import static org.apache.dubbo.metadata.MetadataService.toURLs; 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.METADATA_SERVICE_URL_PARAMS_PROPERTY_NAME; import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getMetadataServiceParameter; import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getMetadataStorageType; @@ -41,7 +41,7 @@ public class MetadataServiceURLParamsMetadataCustomizer extends ServiceInstanceM @Override public String buildMetadataKey(ServiceInstance serviceInstance) { - return METADATA_SERVICE_URL_PARAMS_KEY; + return METADATA_SERVICE_URL_PARAMS_PROPERTY_NAME; } @Override 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 55cdfd1d2d3..3db63a265ba 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 @@ -50,9 +50,9 @@ public class ServiceInstanceMetadataUtils { public static final String METADATA_SERVICE_PREFIX = "dubbo.metadata-service."; /** - * The key of metadata JSON of {@link MetadataService}'s {@link URL} + * The property name of metadata JSON of {@link MetadataService}'s {@link URL} */ - public static String METADATA_SERVICE_URL_PARAMS_KEY = METADATA_SERVICE_PREFIX + "url-params"; + public static String METADATA_SERVICE_URL_PARAMS_PROPERTY_NAME = METADATA_SERVICE_PREFIX + "url-params"; /** * The {@link URL URLs} property name of {@link MetadataService} : @@ -62,29 +62,29 @@ public class ServiceInstanceMetadataUtils { public static final String METADATA_SERVICE_URLS_PROPERTY_NAME = METADATA_SERVICE_PREFIX + "urls"; /** - * The key of The revision for all exported Dubbo services. + * The property name of The revision for all exported Dubbo services. */ - public static String EXPORTED_SERVICES_REVISION_KEY = "dubbo.exported-services.revision"; + public static String EXPORTED_SERVICES_REVISION_PROPERTY_NAME = "dubbo.exported-services.revision"; /** - * The key of The revision for all subscribed Dubbo services. + * The property name of The revision for all subscribed Dubbo services. */ - public static String SUBSCRIBER_SERVICES_REVISION_KEY = "dubbo.subscribed-services.revision"; + public static String SUBSCRIBER_SERVICES_REVISION_PROPERTY_NAME = "dubbo.subscribed-services.revision"; /** - * The key of metadata storage type. + * The property name of metadata storage type. */ - public static String METADATA_STORAGE_TYPE_KEY = "dubbo.metadata.storage-type"; + public static String METADATA_STORAGE_TYPE_PROPERTY_NAME = "dubbo.metadata.storage-type"; /** - * The {@link URL url's} parameter name of Dubbo Provider host + * The property name of {@link URL url's} parameter name of Dubbo Provider host */ - public static final String HOST_PARAM_NAME = "provider.host"; + public static final String PROVIDER_HOST_PROPERTY_NAME = "provider.host"; /** * The {@link URL url's} parameter name of Dubbo Provider port */ - public static final String PORT_PARAM_NAME = "provider.port"; + public static final String PROVIDER_PORT_PROPERTY_NAME = "provider.port"; /** * Get the multiple {@link URL urls'} parameters of {@link MetadataService MetadataService's} Metadata @@ -95,7 +95,7 @@ public class ServiceInstanceMetadataUtils { */ public static Map> getMetadataServiceURLsParams(ServiceInstance serviceInstance) { Map metadata = serviceInstance.getMetadata(); - String param = metadata.get(METADATA_SERVICE_URL_PARAMS_KEY); + String param = metadata.get(METADATA_SERVICE_URL_PARAMS_PROPERTY_NAME); return isBlank(param) ? emptyMap() : (Map) JSON.parse(param); } @@ -153,11 +153,11 @@ private static Map getParams(URL providerURL) { } public static String getProviderHost(Map params) { - return valueOf(params.get(HOST_PARAM_NAME)); + return valueOf(params.get(PROVIDER_HOST_PROPERTY_NAME)); } public static Integer getProviderPort(Map params) { - return Integer.valueOf(valueOf(params.get(PORT_PARAM_NAME))); + return Integer.valueOf(valueOf(params.get(PROVIDER_PORT_PROPERTY_NAME))); } /** @@ -168,7 +168,7 @@ public static Integer getProviderPort(Map params) { */ public static String getExportedServicesRevision(ServiceInstance serviceInstance) { Map metadata = serviceInstance.getMetadata(); - return metadata.get(EXPORTED_SERVICES_REVISION_KEY); + return metadata.get(EXPORTED_SERVICES_REVISION_PROPERTY_NAME); } /** @@ -179,7 +179,7 @@ public static String getExportedServicesRevision(ServiceInstance serviceInstance */ public static String getSubscribedServicesRevision(ServiceInstance serviceInstance) { Map metadata = serviceInstance.getMetadata(); - return metadata.get(SUBSCRIBER_SERVICES_REVISION_KEY); + return metadata.get(SUBSCRIBER_SERVICES_REVISION_PROPERTY_NAME); } /** @@ -190,7 +190,7 @@ public static String getSubscribedServicesRevision(ServiceInstance serviceInstan * {@link WritableMetadataService#DEFAULT_METADATA_STORAGE_TYPE} */ public static String getMetadataStorageType(URL registryURL) { - return registryURL.getParameter(METADATA_STORAGE_TYPE_KEY, DEFAULT_METADATA_STORAGE_TYPE); + return registryURL.getParameter(METADATA_STORAGE_TYPE_PROPERTY_NAME, DEFAULT_METADATA_STORAGE_TYPE); } /** @@ -202,7 +202,7 @@ public static String getMetadataStorageType(URL registryURL) { */ public static String getMetadataStorageType(ServiceInstance serviceInstance) { Map metadata = serviceInstance.getMetadata(); - return metadata.getOrDefault(METADATA_STORAGE_TYPE_KEY, DEFAULT_METADATA_STORAGE_TYPE); + return metadata.getOrDefault(METADATA_STORAGE_TYPE_PROPERTY_NAME, DEFAULT_METADATA_STORAGE_TYPE); } /** @@ -213,7 +213,19 @@ public static String getMetadataStorageType(ServiceInstance serviceInstance) { */ public static void setMetadataStorageType(ServiceInstance serviceInstance, String metadataType) { Map metadata = serviceInstance.getMetadata(); - metadata.put(METADATA_STORAGE_TYPE_KEY, metadataType); + metadata.put(METADATA_STORAGE_TYPE_PROPERTY_NAME, metadataType); + } + + /** + * Is Dubbo Service instance or not + * + * @param serviceInstance {@link ServiceInstance service instance} + * @return if Dubbo Service instance, return true, or false + */ + public static boolean isDubboServiceInstance(ServiceInstance serviceInstance) { + Map metadata = serviceInstance.getMetadata(); + return metadata.containsKey(METADATA_SERVICE_URL_PARAMS_PROPERTY_NAME) + || metadata.containsKey(METADATA_SERVICE_URLS_PROPERTY_NAME); } /** @@ -229,11 +241,11 @@ public static boolean isDubboServiceInstance(ServiceInstance serviceInstance) { } private static void setProviderHostParam(Map params, URL providerURL) { - params.put(HOST_PARAM_NAME, providerURL.getHost()); + params.put(PROVIDER_HOST_PROPERTY_NAME, providerURL.getHost()); } private static void setProviderPortParam(Map params, URL providerURL) { - params.put(PORT_PARAM_NAME, valueOf(providerURL.getPort())); + params.put(PROVIDER_PORT_PROPERTY_NAME, valueOf(providerURL.getPort())); } /** 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 c0e2c4ab493..0620b3ce100 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 @@ -16,21 +16,14 @@ */ package org.apache.dubbo.registry.client.metadata; -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.compiler.support.ClassUtils; -import org.apache.dubbo.metadata.MetadataService; import org.apache.dubbo.metadata.WritableMetadataService; import org.apache.dubbo.registry.client.ServiceInstance; import org.apache.dubbo.registry.client.ServiceInstanceMetadataCustomizer; -import java.util.Arrays; -import java.util.Collection; import java.util.SortedSet; -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.SUBSCRIBER_SERVICES_REVISION_KEY; +import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.SUBSCRIBER_SERVICES_REVISION_PROPERTY_NAME; import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getMetadataStorageType; /** @@ -44,7 +37,7 @@ public class SubscribedServicesRevisionMetadataCustomizer extends ServiceInstanc @Override protected String buildMetadataKey(ServiceInstance serviceInstance) { - return SUBSCRIBER_SERVICES_REVISION_KEY; + return SUBSCRIBER_SERVICES_REVISION_PROPERTY_NAME; } @Override @@ -55,21 +48,10 @@ protected String buildMetadataValue(ServiceInstance serviceInstance) { WritableMetadataService writableMetadataService = getExtension(metadataStorageType); SortedSet subscribedURLs = writableMetadataService.getSubscribedURLs(); - Object[] data = subscribedURLs.stream() - .map(URL::valueOf) // String to URL - .map(URL::getServiceInterface) // get the service interface - .filter(this::isNotMetadataService) // filter not MetadataService interface - .map(ClassUtils::forName) // load business interface class - .map(Class::getMethods) // get all public methods from business interface - .map(Arrays::asList) // Array to List - .flatMap(Collection::stream) // flat Stream to be Stream - .map(Object::toString) // Method to String - .sorted() // sort methods marking sure the calculation of reversion is stable - .toArray(); // Stream to Array - return valueOf(hash(data)); // calculate the hash code as reversion - } - private boolean isNotMetadataService(String serviceInterface) { - return !MetadataService.class.getName().equals(serviceInterface); + URLRevisionResolver resolver = new URLRevisionResolver(); + + return resolver.resolve(subscribedURLs); } + } diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/URLRevisionResolver.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/URLRevisionResolver.java new file mode 100644 index 00000000000..a8acd3d33c3 --- /dev/null +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/URLRevisionResolver.java @@ -0,0 +1,77 @@ +/* + * 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.metadata; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.compiler.support.ClassUtils; +import org.apache.dubbo.metadata.MetadataService; + +import java.util.Arrays; +import java.util.Collection; + +import static org.apache.dubbo.common.utils.CollectionUtils.isEmpty; + +/** + * A class to resolve the version from {@link URL URLs} + * + * @since 2.7.4 + */ +public class URLRevisionResolver { + + public static final String NO_REVISION = "N/A"; + + /** + * Resolve revision as {@link String} + * + * @param urls {@link URL#toFullString() strings} presenting the {@link URL URLs} + * @return non-null + */ + public String resolve(Collection urls) { + + if (isEmpty(urls)) { + return NO_REVISION; + } + + return urls.stream() + .map(URL::valueOf) // String to URL + .map(URL::getServiceInterface) // get the service interface + .filter(this::isNotMetadataService) // filter not MetadataService interface + .map(ClassUtils::forName) // load business interface class + .map(Class::getMethods) // get all public methods from business interface + .map(Arrays::asList) // Array to List + .flatMap(Collection::stream) // flat Stream to be Stream + .map(Object::toString) // Method to String + .sorted() // sort methods marking sure the calculation of reversion is stable + .map(URLRevisionResolver::hashCode) // generate Long hashCode + .reduce(Long::sum) // sum hashCode + .map(String::valueOf) // Long to String + .orElse(NO_REVISION); // NO_REVISION as default + } + + private static long hashCode(String value) { + long h = 0; + char[] chars = value.toCharArray(); + for (int i = 0; i < chars.length; i++) { + h = 31L * h + chars[i]; + } + return h; + } + + private boolean isNotMetadataService(String serviceInterface) { + return !MetadataService.class.getName().equals(serviceInterface); + } +} diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/proxy/RemoteMetadataServiceProxy.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/proxy/RemoteMetadataServiceProxy.java index 44c71443948..3f1745d23f1 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/proxy/RemoteMetadataServiceProxy.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/proxy/RemoteMetadataServiceProxy.java @@ -8,7 +8,6 @@ import org.apache.dubbo.metadata.report.MetadataReportInstance; import org.apache.dubbo.metadata.report.identifier.MetadataIdentifier; import org.apache.dubbo.metadata.report.identifier.ServiceMetadataIdentifier; -import org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier; import org.apache.dubbo.registry.client.ServiceInstance; import org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils; @@ -17,9 +16,7 @@ import java.util.SortedSet; import java.util.TreeSet; -import static org.apache.dubbo.common.constants.CommonConstants.METADATA_REVISION; import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE; -import static org.apache.dubbo.common.constants.CommonConstants.REVISION_KEY; /** * 2019-08-09 @@ -35,7 +32,7 @@ public class RemoteMetadataServiceProxy implements MetadataService { public RemoteMetadataServiceProxy(ServiceInstance serviceInstance) { this.serviceName = serviceInstance.getServiceName(); this.revision = serviceInstance.getMetadata() - .getOrDefault(ServiceInstanceMetadataUtils.EXPORTED_SERVICES_REVISION_KEY, ""); + .getOrDefault(ServiceInstanceMetadataUtils.EXPORTED_SERVICES_REVISION_PROPERTY_NAME, ""); } @Override diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/URLRevisionResolverTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/URLRevisionResolverTest.java new file mode 100644 index 00000000000..e038f282c1e --- /dev/null +++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/URLRevisionResolverTest.java @@ -0,0 +1,47 @@ +/* + * 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.metadata; + +import org.junit.jupiter.api.Test; + +import static java.util.Arrays.asList; +import static org.apache.dubbo.registry.client.metadata.URLRevisionResolver.NO_REVISION; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * {@link URLRevisionResolver} Test + * + * @since 2.7.4 + */ +public class URLRevisionResolverTest { + + private static final String URL = "dubbo://192.168.0.102:20881/org.apache.dubbo.registry.client.metadata.URLRevisionResolverTest"; + + private final URLRevisionResolver resolver = new URLRevisionResolver(); + + @Test + public void testResolve() { + String revision = resolver.resolve(asList()); + assertEquals(NO_REVISION, revision); + + revision = resolver.resolve(null); + assertEquals(NO_REVISION, revision); + + revision = resolver.resolve(asList(URL)); + assertEquals("7960327984321481979", revision); + } +} 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 830c23ba7cc..d2ee2bcd890 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 @@ -125,7 +125,7 @@ public List getInstances(String serviceName) throws NullPointer private List convert(List services) { return services.stream() .map(HealthService::getService) - .filter(service -> Objects.nonNull(service) && service.getMeta().containsKey(ServiceInstanceMetadataUtils.METADATA_SERVICE_URL_PARAMS_KEY)) + .filter(service -> Objects.nonNull(service) && service.getMeta().containsKey(ServiceInstanceMetadataUtils.METADATA_SERVICE_URL_PARAMS_PROPERTY_NAME)) .map(service -> { ServiceInstance instance = new DefaultServiceInstance( service.getService(),