This is an automated email from the ASF dual-hosted git repository. liujun pushed a commit to branch 3.0 in repository https://gitbox.apache.org/repos/asf/dubbo.git
commit 4cacf33f785af73eb673f9f762537e176b518117 Author: ken.lj <[email protected]> AuthorDate: Thu Jul 23 10:46:22 2020 +0800 fix metadata conflicts after master merged --- .../dubbo/config/bootstrap/DubboBootstrap.java | 223 ++++++++++++++--- .../metadata/AbstractMetadataServiceExporter.java | 150 ------------ .../ConfigurableMetadataServiceExporter.java | 69 +++--- .../metadata/RemoteMetadataServiceExporter.java | 79 ------ .../PublishingServiceDefinitionListenerTest.java | 2 +- .../RemoteMetadataServiceExporterTest.java | 106 -------- dubbo-dependencies-bom/pom.xml | 8 +- .../metadata/CompositeServiceNameMapping.java | 96 -------- .../org/apache/dubbo/metadata/MetadataInfo.java | 4 +- .../metadata/ParameterizedServiceNameMapping.java | 47 ---- .../metadata/PropertiesFileServiceNameMapping.java | 148 ----------- .../dubbo/metadata/ReadOnlyServiceNameMapping.java | 45 ---- .../report/support/AbstractMetadataReport.java | 133 +++------- .../support/ConfigCenterBasedMetadataReport.java | 162 ------------- .../ConfigCenterBasedMetadataReportFactory.java | 90 ------- .../file/FileSystemMetadataReportFactory.java | 33 --- .../org.apache.dubbo.metadata.MetadataParamsFilter | 1 + .../metadata/CompositeServiceNameMappingTest.java | 107 -------- .../dubbo/metadata/MetadataConstantsTest.java | 35 --- .../ParameterizedServiceNameMappingTest.java | 67 ----- .../PropertiesFileServiceNameMappingTest.java | 60 ----- .../dubbo/metadata/ServiceNameMappingTest.java | 8 +- .../support/AbstractMetadataReportFactoryTest.java | 270 ++++++++++----------- .../report/support/AbstractMetadataReportTest.java | 58 +++-- .../ConfigCenterBasedMetadataReportTest.java | 155 ------------ .../store/zookeeper/ZookeeperMetadataReport.java | 29 --- .../zookeeper/ZookeeperMetadataReportFactory.java | 22 +- dubbo-metadata/pom.xml | 12 +- .../registry/client/ServiceDiscoveryRegistry.java | 4 +- .../CompositeMetadataServiceProxyFactory.java | 133 ---------- .../proxy/BaseMetadataServiceProxyFactoryTest.java | 78 ------ .../CompositeMetadataServiceProxyFactoryTest.java | 96 -------- .../proxy/MetadataServiceProxyFactoryTest.java | 49 ---- .../proxy/MyMetadataServiceProxyFactory.java | 29 --- 34 files changed, 471 insertions(+), 2137 deletions(-) diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/bootstrap/DubboBootstrap.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/bootstrap/DubboBootstrap.java index a9f2bcf..34977b3 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/bootstrap/DubboBootstrap.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/bootstrap/DubboBootstrap.java @@ -20,6 +20,7 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.config.ConfigurationUtils; import org.apache.dubbo.common.config.Environment; import org.apache.dubbo.common.config.configcenter.DynamicConfiguration; +import org.apache.dubbo.common.config.configcenter.DynamicConfigurationFactory; import org.apache.dubbo.common.config.configcenter.wrapper.CompositeDynamicConfiguration; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.lang.ShutdownHookCallback; @@ -28,6 +29,7 @@ import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.threadpool.concurrent.ScheduledCompletableFuture; import org.apache.dubbo.common.threadpool.manager.ExecutorRepository; +import org.apache.dubbo.common.utils.ArrayUtils; import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.config.ApplicationConfig; @@ -62,6 +64,7 @@ import org.apache.dubbo.event.GenericEventListener; import org.apache.dubbo.metadata.MetadataService; import org.apache.dubbo.metadata.MetadataServiceExporter; import org.apache.dubbo.metadata.WritableMetadataService; +import org.apache.dubbo.metadata.report.MetadataReportFactory; import org.apache.dubbo.metadata.report.MetadataReportInstance; import org.apache.dubbo.registry.client.DefaultServiceInstance; import org.apache.dubbo.registry.client.ServiceInstance; @@ -89,14 +92,19 @@ import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import java.util.function.Consumer; +import java.util.function.Supplier; +import static java.lang.String.format; import static java.util.Arrays.asList; 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.DEFAULT_METADATA_STORAGE_TYPE; +import static org.apache.dubbo.common.constants.CommonConstants.REGISTRY_SPLIT_PATTERN; import static org.apache.dubbo.common.constants.CommonConstants.REMOTE_METADATA_STORAGE_TYPE; +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.StringUtils.isEmpty; import static org.apache.dubbo.common.utils.StringUtils.isNotEmpty; import static org.apache.dubbo.metadata.MetadataConstants.DEFAULT_METADATA_PUBLISH_DELAY; import static org.apache.dubbo.metadata.MetadataConstants.METADATA_PUBLISH_DELAY_KEY; @@ -108,9 +116,9 @@ import static org.apache.dubbo.remoting.Constants.CLIENT_KEY; /** * See {@link ApplicationModel} and {@link ExtensionLoader} for why this class is designed to be singleton. - * + * <p> * The bootstrap class of Dubbo - * + * <p> * Get singleton instance by calling static method {@link #getInstance()}. * Designed as singleton because some classes inside Dubbo, such as ExtensionLoader, are designed only for one instance per process. * @@ -134,7 +142,7 @@ public class DubboBootstrap extends GenericEventListener { private final Logger logger = LoggerFactory.getLogger(getClass()); - private static DubboBootstrap instance; + private static volatile DubboBootstrap instance; private final AtomicBoolean awaited = new AtomicBoolean(false); @@ -148,7 +156,7 @@ public class DubboBootstrap extends GenericEventListener { private final EventDispatcher eventDispatcher = EventDispatcher.getDefaultExtension(); - private final ExecutorRepository executorRepository = ExtensionLoader.getExtensionLoader(ExecutorRepository.class).getDefaultExtension(); + private final ExecutorRepository executorRepository = getExtensionLoader(ExecutorRepository.class).getDefaultExtension(); private final ConfigManager configManager; @@ -183,9 +191,13 @@ public class DubboBootstrap extends GenericEventListener { /** * See {@link ApplicationModel} and {@link ExtensionLoader} for why DubboBootstrap is designed to be singleton. */ - public static synchronized DubboBootstrap getInstance() { + public static DubboBootstrap getInstance() { if (instance == null) { - instance = new DubboBootstrap(); + synchronized (DubboBootstrap.class) { + if (instance == null) { + instance = new DubboBootstrap(); + } + } } return instance; } @@ -507,7 +519,7 @@ public class DubboBootstrap extends GenericEventListener { /** * Initialize */ - private void initialize() { + public void initialize() { if (!initialized.compareAndSet(false, true)) { return; } @@ -516,12 +528,13 @@ public class DubboBootstrap extends GenericEventListener { startConfigCenter(); - useRegistryAsConfigCenterIfNecessary(); - loadRemoteConfigs(); checkGlobalConfigs(); + // @since 2.7.8 + startMetadataCenter(); + initMetadataService(); initEventListener(); @@ -590,6 +603,9 @@ public class DubboBootstrap extends GenericEventListener { } private void startConfigCenter() { + + useRegistryAsConfigCenterIfNecessary(); + Collection<ConfigCenterConfig> configCenters = configManager.getConfigCenters(); // check Config Center @@ -617,7 +633,10 @@ public class DubboBootstrap extends GenericEventListener { configManager.refreshAll(); } - private void startMetadataReport() { + private void startMetadataCenter() { + + useRegistryAsMetadataCenterIfNecessary(); + ApplicationConfig applicationConfig = getApplication(); String metadataType = applicationConfig.getMetadataType(); @@ -654,33 +673,159 @@ public class DubboBootstrap extends GenericEventListener { return; } - configManager.getDefaultRegistries().stream() - .filter(registryConfig -> registryConfig.getUseAsConfigCenter() == null || registryConfig.getUseAsConfigCenter()) - .forEach(registryConfig -> { - String protocol = registryConfig.getProtocol(); - String id = "config-center-" + protocol + "-" + registryConfig.getPort(); - ConfigCenterConfig cc = new ConfigCenterConfig(); - cc.setId(id); - if (cc.getParameters() == null) { - cc.setParameters(new HashMap<>()); - } - if (registryConfig.getParameters() != null) { - cc.getParameters().putAll(registryConfig.getParameters()); - } - cc.getParameters().put(CLIENT_KEY, registryConfig.getClient()); - cc.setProtocol(registryConfig.getProtocol()); - cc.setPort(registryConfig.getPort()); - cc.setAddress(registryConfig.getAddress()); - cc.setNamespace(registryConfig.getGroup()); - cc.setUsername(registryConfig.getUsername()); - cc.setPassword(registryConfig.getPassword()); - if (registryConfig.getTimeout() != null) { - cc.setTimeout(registryConfig.getTimeout().longValue()); - } - cc.setHighestPriority(false); - configManager.addConfigCenter(cc); - }); - startConfigCenter(); + configManager + .getDefaultRegistries() + .stream() + .filter(this::isUsedRegistryAsConfigCenter) + .map(this::registryAsConfigCenter) + .forEach(configManager::addConfigCenter); + } + + private boolean isUsedRegistryAsConfigCenter(RegistryConfig registryConfig) { + return isUsedRegistryAsCenter(registryConfig, registryConfig::getUseAsConfigCenter, "config", + DynamicConfigurationFactory.class); + } + + private ConfigCenterConfig registryAsConfigCenter(RegistryConfig registryConfig) { + String protocol = registryConfig.getProtocol(); + Integer port = registryConfig.getPort(); + String id = "config-center-" + protocol + "-" + port; + ConfigCenterConfig cc = new ConfigCenterConfig(); + cc.setId(id); + if (cc.getParameters() == null) { + cc.setParameters(new HashMap<>()); + } + if (registryConfig.getParameters() != null) { + cc.getParameters().putAll(registryConfig.getParameters()); // copy the parameters + } + cc.getParameters().put(CLIENT_KEY, registryConfig.getClient()); + cc.setProtocol(protocol); + cc.setPort(port); + cc.setGroup(registryConfig.getGroup()); + cc.setAddress(getRegistryCompatibleAddress(registryConfig)); + cc.setNamespace(registryConfig.getGroup()); + cc.setUsername(registryConfig.getUsername()); + cc.setPassword(registryConfig.getPassword()); + if (registryConfig.getTimeout() != null) { + cc.setTimeout(registryConfig.getTimeout().longValue()); + } + cc.setHighestPriority(false); + return cc; + } + + private void useRegistryAsMetadataCenterIfNecessary() { + + Collection<MetadataReportConfig> metadataConfigs = configManager.getMetadataConfigs(); + + if (CollectionUtils.isNotEmpty(metadataConfigs)) { + return; + } + + configManager + .getDefaultRegistries() + .stream() + .filter(this::isUsedRegistryAsMetadataCenter) + .map(this::registryAsMetadataCenter) + .forEach(configManager::addMetadataReport); + + } + + private boolean isUsedRegistryAsMetadataCenter(RegistryConfig registryConfig) { + return isUsedRegistryAsCenter(registryConfig, registryConfig::getUseAsMetadataCenter, "metadata", + MetadataReportFactory.class); + } + + /** + * Is used the specified registry as a center infrastructure + * + * @param registryConfig the {@link RegistryConfig} + * @param usedRegistryAsCenter the configured value on + * @param centerType the type name of center + * @param extensionClass an extension class of a center infrastructure + * @return + * @since 2.7.8 + */ + private boolean isUsedRegistryAsCenter(RegistryConfig registryConfig, Supplier<Boolean> usedRegistryAsCenter, + String centerType, + Class<?> extensionClass) { + final boolean supported; + + Boolean configuredValue = usedRegistryAsCenter.get(); + if (configuredValue != null) { // If configured, take its value. + supported = configuredValue.booleanValue(); + } else { // Or check the extension existence + String protocol = registryConfig.getProtocol(); + supported = supportsExtension(extensionClass, protocol); + if (logger.isInfoEnabled()) { + logger.info(format("No value is configured in the registry, the %s extension[name : %s] %s as the %s center" + , extensionClass.getSimpleName(), protocol, supported ? "supports" : "does not support", centerType)); + } + } + + if (logger.isInfoEnabled()) { + logger.info(format("The registry[%s] will be %s as the %s center", registryConfig, + supported ? "used" : "not used", centerType)); + } + return supported; + } + + /** + * Supports the extension with the specified class and name + * + * @param extensionClass the {@link Class} of extension + * @param name the name of extension + * @return if supports, return <code>true</code>, or <code>false</code> + * @since 2.7.8 + */ + private boolean supportsExtension(Class<?> extensionClass, String name) { + if (isNotEmpty(name)) { + ExtensionLoader extensionLoader = getExtensionLoader(extensionClass); + return extensionLoader.hasExtension(name); + } + return false; + } + + private MetadataReportConfig registryAsMetadataCenter(RegistryConfig registryConfig) { + String protocol = registryConfig.getProtocol(); + Integer port = registryConfig.getPort(); + String id = "metadata-center-" + protocol + "-" + port; + MetadataReportConfig metadataReportConfig = new MetadataReportConfig(); + metadataReportConfig.setId(id); + if (metadataReportConfig.getParameters() == null) { + metadataReportConfig.setParameters(new HashMap<>()); + } + if (registryConfig.getParameters() != null) { + metadataReportConfig.getParameters().putAll(registryConfig.getParameters()); // copy the parameters + } + metadataReportConfig.getParameters().put(CLIENT_KEY, registryConfig.getClient()); + metadataReportConfig.setGroup(registryConfig.getGroup()); + metadataReportConfig.setAddress(getRegistryCompatibleAddress(registryConfig)); + metadataReportConfig.setUsername(registryConfig.getUsername()); + metadataReportConfig.setPassword(registryConfig.getPassword()); + metadataReportConfig.setTimeout(registryConfig.getTimeout()); + return metadataReportConfig; + } + + private String getRegistryCompatibleAddress(RegistryConfig registryConfig) { + String registryAddress = registryConfig.getAddress(); + String[] addresses = REGISTRY_SPLIT_PATTERN.split(registryAddress); + if (ArrayUtils.isEmpty(addresses)) { + throw new IllegalStateException("Invalid registry address found."); + } + String address = addresses[0]; + // since 2.7.8 + // Issue : https://github.com/apache/dubbo/issues/6476 + StringBuilder metadataAddressBuilder = new StringBuilder(); + URL url = URL.valueOf(address); + String protocolFromAddress = url.getProtocol(); + if (isEmpty(protocolFromAddress)) { + // If the protocol from address is missing, is like : + // "dubbo.registry.address = 127.0.0.1:2181" + String protocolFromConfig = registryConfig.getProtocol(); + metadataAddressBuilder.append(protocolFromConfig).append("://"); + } + metadataAddressBuilder.append(address); + return metadataAddressBuilder.toString(); } private void loadRemoteConfigs() { @@ -722,7 +867,7 @@ public class DubboBootstrap extends GenericEventListener { * Initialize {@link MetadataService} from {@link WritableMetadataService}'s extension */ private void initMetadataService() { - startMetadataReport(); + startMetadataCenter(); this.metadataService = getDefaultExtension(); this.metadataServiceExporter = new ConfigurableMetadataServiceExporter(metadataService); } @@ -1144,7 +1289,7 @@ public class DubboBootstrap extends GenericEventListener { } private void clearConfigs() { - configManager.clear(); + configManager.destroy(); if (logger.isDebugEnabled()) { logger.debug(NAME + "'s configs have been clear."); } diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/metadata/AbstractMetadataServiceExporter.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/metadata/AbstractMetadataServiceExporter.java deleted file mode 100644 index ce2b389..0000000 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/metadata/AbstractMetadataServiceExporter.java +++ /dev/null @@ -1,150 +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.config.metadata; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.logger.Logger; -import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.metadata.MetadataService; -import org.apache.dubbo.metadata.MetadataServiceExporter; -import org.apache.dubbo.metadata.MetadataServiceType; -import org.apache.dubbo.metadata.WritableMetadataService; - -import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; - -import static java.util.EnumSet.of; -import static org.apache.dubbo.metadata.MetadataServiceType.getOrDefault; - -/** - * The abstract implementation of {@link MetadataServiceExporter} to provider the commons features for sub-types - * - * @see MetadataServiceExporter - * @see MetadataService - * @since 2.7.8 - */ -public abstract class AbstractMetadataServiceExporter implements MetadataServiceExporter { - - protected final Logger logger = LoggerFactory.getLogger(getClass()); - - protected final WritableMetadataService metadataService; - - private final int priority; - - private final Set<MetadataServiceType> supportedMetadataServiceTypes; - - private volatile boolean exported = false; - - public AbstractMetadataServiceExporter(String metadataType, - int priority, - MetadataServiceType supportMetadataServiceType, - MetadataServiceType... otherSupportMetadataServiceTypes) { - this(metadataType, priority, of(supportMetadataServiceType, otherSupportMetadataServiceTypes)); - } - - public AbstractMetadataServiceExporter(String metadataType, - int priority, - Set<MetadataServiceType> supportedMetadataServiceTypes) { - this.metadataService = WritableMetadataService.getExtension(metadataType); - this.priority = priority; - this.supportedMetadataServiceTypes = supportedMetadataServiceTypes; - } - - @Override - public final MetadataServiceExporter export() { - if (!isExported()) { - try { - doExport(); - exported = true; - } catch (Exception e) { - if (logger.isErrorEnabled()) { - logger.error("Exporting the MetadataService fails", e); - } - exported = false; - } - } else { - if (logger.isWarnEnabled()) { - logger.warn("The MetadataService has been exported : " + getExportedURLs()); - } - } - return this; - } - - @Override - public final MetadataServiceExporter unexport() { - if (isExported()) { - try { - doUnexport(); - exported = false; - } catch (Exception e) { - if (logger.isErrorEnabled()) { - logger.error("UnExporting the MetadataService fails", e); - } - } - } - return this; - } - - @Override - public List<URL> getExportedURLs() { - return metadataService - .getExportedURLs() - .stream() - .map(URL::valueOf) - .collect(Collectors.toList()); - } - - @Override - public boolean isExported() { - return exported; - } - - @Override - public final boolean supports(String metadataType) { - MetadataServiceType metadataServiceType = getOrDefault(metadataType); - return supportedMetadataServiceTypes.contains(metadataServiceType); - } - - @Override - public int getPriority() { - return priority; - } - - /** - * Exports the {@link MetadataService} - * - * @throws Exception If some exception occurs - */ - protected abstract void doExport() throws Exception; - - /** - * Unexports the {@link MetadataService} - * - * @throws Exception If some exception occurs - */ - protected abstract void doUnexport() throws Exception; - - /** - * Get the underlying of {@link MetadataService} - * - * @return non-null - */ - public WritableMetadataService getMetadataService() { - return metadataService; - } -} 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 d89fb77..fdb011f 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 @@ -17,6 +17,8 @@ package org.apache.dubbo.config.metadata; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.logger.Logger; +import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.config.ApplicationConfig; import org.apache.dubbo.config.ProtocolConfig; import org.apache.dubbo.config.RegistryConfig; @@ -24,15 +26,12 @@ 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.MetadataServiceType; import org.apache.dubbo.rpc.model.ApplicationModel; import java.util.ArrayList; import java.util.List; import static java.util.Collections.emptyList; -import static java.util.EnumSet.allOf; -import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_METADATA_STORAGE_TYPE; import static org.apache.dubbo.common.constants.CommonConstants.DUBBO; /** @@ -50,41 +49,56 @@ import static org.apache.dubbo.common.constants.CommonConstants.DUBBO; * @see ConfigManager * @since 2.7.5 */ -public class ConfigurableMetadataServiceExporter extends AbstractMetadataServiceExporter { +public class ConfigurableMetadataServiceExporter implements MetadataServiceExporter { + + private final Logger logger = LoggerFactory.getLogger(getClass()); + + private final MetadataService metadataService; private volatile ServiceConfig<MetadataService> serviceConfig; - public ConfigurableMetadataServiceExporter() { - super(DEFAULT_METADATA_STORAGE_TYPE, MAX_PRIORITY, allOf(MetadataServiceType.class)); + public ConfigurableMetadataServiceExporter(MetadataService metadataService) { + this.metadataService = metadataService; } @Override - protected void doExport() throws Exception { - - ServiceConfig<MetadataService> serviceConfig = new ServiceConfig<>(); - serviceConfig.setApplication(getApplicationConfig()); - serviceConfig.setRegistries(getRegistries()); - serviceConfig.setProtocol(generateMetadataProtocol()); - serviceConfig.setInterface(MetadataService.class); - serviceConfig.setRef(metadataService); - serviceConfig.setGroup(getApplicationConfig().getName()); - serviceConfig.setVersion(metadataService.version()); - - // export - serviceConfig.export(); - - if (logger.isInfoEnabled()) { - logger.info("The MetadataService exports urls : " + serviceConfig.getExportedUrls()); + public ConfigurableMetadataServiceExporter export() { + + if (!isExported()) { + + ServiceConfig<MetadataService> serviceConfig = new ServiceConfig<>(); + serviceConfig.setApplication(getApplicationConfig()); + serviceConfig.setRegistries(getRegistries()); + serviceConfig.setProtocol(generateMetadataProtocol()); + serviceConfig.setInterface(MetadataService.class); + serviceConfig.setRef(metadataService); + serviceConfig.setGroup(getApplicationConfig().getName()); + serviceConfig.setVersion(metadataService.version()); + + // export + serviceConfig.export(); + + if (logger.isInfoEnabled()) { + logger.info("The MetadataService exports urls : " + serviceConfig.getExportedUrls()); + } + + this.serviceConfig = serviceConfig; + + } else { + if (logger.isWarnEnabled()) { + logger.warn("The MetadataService has been exported : " + serviceConfig.getExportedUrls()); + } } - this.serviceConfig = serviceConfig; + return this; } @Override - protected void doUnexport() throws Exception { - if (serviceConfig != null) { + public ConfigurableMetadataServiceExporter unexport() { + if (isExported()) { serviceConfig.unexport(); } + return this; } @Override @@ -96,11 +110,6 @@ public class ConfigurableMetadataServiceExporter extends AbstractMetadataService return serviceConfig != null && serviceConfig.isExported(); } - @Override - public int getPriority() { - return MAX_PRIORITY; - } - private ApplicationConfig getApplicationConfig() { return ApplicationModel.getConfigManager().getApplication().get(); } diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/metadata/RemoteMetadataServiceExporter.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/metadata/RemoteMetadataServiceExporter.java deleted file mode 100644 index 6d48921..0000000 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/metadata/RemoteMetadataServiceExporter.java +++ /dev/null @@ -1,79 +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.config.metadata; - -import org.apache.dubbo.common.constants.CommonConstants; -import org.apache.dubbo.metadata.MetadataServiceExporter; -import org.apache.dubbo.metadata.MetadataServiceType; -import org.apache.dubbo.metadata.URLRevisionResolver; -import org.apache.dubbo.metadata.WritableMetadataService; -import org.apache.dubbo.metadata.report.MetadataReport; -import org.apache.dubbo.metadata.report.MetadataReportInstance; -import org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier; - -import java.util.SortedSet; - -import static org.apache.dubbo.common.constants.CommonConstants.REMOTE_METADATA_STORAGE_TYPE; - -/** - * The implementation of {@link MetadataServiceExporter} for - * {@link CommonConstants#REMOTE_METADATA_STORAGE_TYPE "remote" metadata storage type} - * - * @see MetadataServiceExporter - * @since 2.7.8 - */ -public class RemoteMetadataServiceExporter extends AbstractMetadataServiceExporter { - - private final URLRevisionResolver urlRevisionResolver; - - public RemoteMetadataServiceExporter() { - super(REMOTE_METADATA_STORAGE_TYPE, MIN_PRIORITY, MetadataServiceType.REMOTE, MetadataServiceType.COMPOSITE); - this.urlRevisionResolver = URLRevisionResolver.INSTANCE; - } - - @Override - protected void doExport() throws Exception { - WritableMetadataService metadataServiceDelegate = WritableMetadataService.getDefaultExtension(); - if (publishServiceMetadata(metadataServiceDelegate)) { - publicConsumerMetadata(metadataServiceDelegate); - } - } - - private boolean publishServiceMetadata(WritableMetadataService metadataServiceDelegate) { - String serviceName = metadataServiceDelegate.serviceName(); - SortedSet<String> exportedURLs = metadataServiceDelegate.getExportedURLs(); - String revision = urlRevisionResolver.resolve(exportedURLs); - return getMetadataReport().saveExportedURLs(serviceName, revision, exportedURLs); - } - - private boolean publicConsumerMetadata(WritableMetadataService metadataServiceDelegate) { - String serviceName = metadataServiceDelegate.serviceName(); - SortedSet<String> subscribedURLs = metadataServiceDelegate.getSubscribedURLs(); - String revision = urlRevisionResolver.resolve(subscribedURLs); - getMetadataReport().saveSubscribedData(new SubscriberMetadataIdentifier(serviceName, revision), subscribedURLs); - return true; - } - - private MetadataReport getMetadataReport() { - return MetadataReportInstance.getMetadataReport(true); - } - - @Override - protected void doUnexport() throws Exception { - // DOES NOTHING - } -} diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/event/listener/PublishingServiceDefinitionListenerTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/event/listener/PublishingServiceDefinitionListenerTest.java index 67a562d..e7e5376 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/event/listener/PublishingServiceDefinitionListenerTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/event/listener/PublishingServiceDefinitionListenerTest.java @@ -59,7 +59,7 @@ public class PublishingServiceDefinitionListenerTest { ApplicationConfig applicationConfig = new ApplicationConfig("dubbo-demo-provider"); applicationConfig.setMetadataType(metadataType); configManager.setApplication(applicationConfig); - this.writableMetadataService = WritableMetadataService.getExtension(metadataType); + this.writableMetadataService = WritableMetadataService.getDefaultExtension(); } @AfterEach diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/metadata/RemoteMetadataServiceExporterTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/metadata/RemoteMetadataServiceExporterTest.java deleted file mode 100644 index e4d13a1..0000000 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/metadata/RemoteMetadataServiceExporterTest.java +++ /dev/null @@ -1,106 +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.config.metadata; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.config.ApplicationConfig; -import org.apache.dubbo.metadata.MetadataServiceExporter; -import org.apache.dubbo.metadata.WritableMetadataService; -import org.apache.dubbo.metadata.report.MetadataReportInstance; -import org.apache.dubbo.rpc.model.ApplicationModel; -import org.apache.dubbo.rpc.service.EchoService; - -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import static java.util.Arrays.asList; -import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.COMPOSITE_METADATA_STORAGE_TYPE; -import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_METADATA_STORAGE_TYPE; -import static org.apache.dubbo.common.constants.CommonConstants.REMOTE_METADATA_STORAGE_TYPE; -import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY; -import static org.apache.dubbo.metadata.MetadataServiceExporter.getExtension; -import static org.apache.dubbo.metadata.report.support.Constants.SYNC_REPORT_KEY; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - -/** - * {@link RemoteMetadataServiceExporter} Test-Cases - * - * @since 2.7.8 - */ -public class RemoteMetadataServiceExporterTest { - - private static final URL METADATA_REPORT_URL = URL.valueOf("file://") - .addParameter(APPLICATION_KEY, "test") - .addParameter(SYNC_REPORT_KEY, "true"); - - private static final Class<EchoService> INTERFACE_CLASS = EchoService.class; - - private static final String INTERFACE_NAME = INTERFACE_CLASS.getName(); - - private static final String APP_NAME = "test-service"; - - private static final URL BASE_URL = URL - .valueOf("dubbo://127.0.0.1:20880") - .setPath(INTERFACE_NAME) - .addParameter(APPLICATION_KEY, APP_NAME) - .addParameter(SIDE_KEY, "provider"); - - private final MetadataServiceExporter exporter = getExtension(REMOTE_METADATA_STORAGE_TYPE); - - private WritableMetadataService writableMetadataService; - - @BeforeEach - public void init() { - ApplicationModel.getConfigManager().setApplication(new ApplicationConfig(APP_NAME)); - MetadataReportInstance.init(METADATA_REPORT_URL); - writableMetadataService = WritableMetadataService.getDefaultExtension(); - writableMetadataService.exportURL(BASE_URL); - } - - @AfterEach - public void reset() { - ApplicationModel.reset(); - } - - @Test - public void testType() { - assertEquals(RemoteMetadataServiceExporter.class, exporter.getClass()); - } - - @Test - public void testSupports() { - assertTrue(exporter.supports(REMOTE_METADATA_STORAGE_TYPE)); - assertTrue(exporter.supports(COMPOSITE_METADATA_STORAGE_TYPE)); - assertFalse(exporter.supports(DEFAULT_METADATA_STORAGE_TYPE)); - } - - @Test - public void testExportAndUnexport() { - assertFalse(exporter.isExported()); - assertEquals(exporter, exporter.export()); - assertTrue(exporter.isExported()); - - assertEquals(asList(BASE_URL), exporter.getExportedURLs()); - - assertEquals(exporter, exporter.unexport()); - assertFalse(exporter.isExported()); - } -} diff --git a/dubbo-dependencies-bom/pom.xml b/dubbo-dependencies-bom/pom.xml index 92f2a90..9c40c96 100644 --- a/dubbo-dependencies-bom/pom.xml +++ b/dubbo-dependencies-bom/pom.xml @@ -97,7 +97,7 @@ <grizzly_version>2.1.4</grizzly_version> <httpclient_version>4.5.3</httpclient_version> <httpcore_version>4.4.6</httpcore_version> - <fastjson_version>1.2.68</fastjson_version> + <fastjson_version>1.2.70</fastjson_version> <zookeeper_version>3.4.13</zookeeper_version> <curator_version>4.0.1</curator_version> <curator_test_version>2.12.0</curator_test_version> @@ -130,7 +130,7 @@ <resteasy_version>3.0.19.Final</resteasy_version> <tomcat_embed_version>8.5.31</tomcat_embed_version> <jetcd_version>0.4.1</jetcd_version> - <nacos_version>1.1.1</nacos_version> + <nacos_version>1.3.1</nacos_version> <grpc.version>1.22.1</grpc.version> <!-- Log libs --> <slf4j_version>1.7.25</slf4j_version> @@ -146,13 +146,13 @@ <eureka.version>1.9.12</eureka.version> <!-- Alibaba --> - <alibaba_spring_context_support_version>1.0.6</alibaba_spring_context_support_version> + <alibaba_spring_context_support_version>1.0.8</alibaba_spring_context_support_version> <jaxb_version>2.2.7</jaxb_version> <activation_version>1.2.0</activation_version> <test_container_version>1.11.2</test_container_version> <etcd_launcher_version>0.3.0</etcd_launcher_version> - <hessian_lite_version>3.2.7</hessian_lite_version> + <hessian_lite_version>3.2.8</hessian_lite_version> <swagger_version>1.5.19</swagger_version> <spring_test_version>4.3.16.RELEASE</spring_test_version> diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/CompositeServiceNameMapping.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/CompositeServiceNameMapping.java deleted file mode 100644 index 9ad130b..0000000 --- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/CompositeServiceNameMapping.java +++ /dev/null @@ -1,96 +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.metadata; - - -import org.apache.dubbo.common.URL; - -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; - -import static java.util.Collections.emptySet; -import static java.util.Collections.unmodifiableSet; -import static org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader; -import static org.apache.dubbo.common.utils.CollectionUtils.isNotEmpty; - -/** - * The composite implementation of {@link ServiceNameMapping} - * - * @see ParameterizedServiceNameMapping - * @see PropertiesFileServiceNameMapping - * @see DynamicConfigurationServiceNameMapping - * @since 2.7.8 - */ -public class CompositeServiceNameMapping implements ServiceNameMapping { - - private volatile List<ServiceNameMapping> serviceNameMappings; - - private List<ServiceNameMapping> getServiceNameMappings() { - if (this.serviceNameMappings == null) { - synchronized (this) { - if (this.serviceNameMappings == null) { - Set<ServiceNameMapping> serviceNameMappings = loadAllServiceNameMappings(); - - removeSelf(serviceNameMappings); - - this.serviceNameMappings = new LinkedList<>(serviceNameMappings); - } - } - } - return this.serviceNameMappings; - } - - private Set<ServiceNameMapping> loadAllServiceNameMappings() { - return getExtensionLoader(ServiceNameMapping.class).getSupportedExtensionInstances(); - } - - private void removeSelf(Set<ServiceNameMapping> serviceNameMappings) { - Iterator<ServiceNameMapping> iterator = serviceNameMappings.iterator(); - while (iterator.hasNext()) { - ServiceNameMapping serviceNameMapping = iterator.next(); - if (this.getClass().equals(serviceNameMapping.getClass())) { - iterator.remove(); // Remove self - } - } - } - - @Override - public void map(URL exportedURL) { - List<ServiceNameMapping> serviceNameMappings = getServiceNameMappings(); - serviceNameMappings.forEach(serviceNameMapping -> serviceNameMapping.map(exportedURL)); - } - - @Override - public Set<String> get(URL subscribedURL) { - List<ServiceNameMapping> serviceNameMappings = getServiceNameMappings(); - Set<String> serviceNames = null; - for (ServiceNameMapping serviceNameMapping : serviceNameMappings) { - serviceNames = serviceNameMapping.get(subscribedURL); - if (isNotEmpty(serviceNames)) { - break; - } - } - return serviceNames == null ? emptySet() : unmodifiableSet(serviceNames); - } - - @Override - public int getPriority() { - return MIN_PRIORITY; - } -} diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataInfo.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataInfo.java index e3f6881..f479830 100644 --- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataInfo.java +++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataInfo.java @@ -36,7 +36,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicBoolean; import static org.apache.dubbo.common.constants.CommonConstants.DOT_SEPARATOR; -import static org.apache.dubbo.common.constants.CommonConstants.GROUP_CHAR_SEPERATOR; +import static org.apache.dubbo.common.constants.CommonConstants.GROUP_CHAR_SEPARATOR; import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; import static org.apache.dubbo.common.constants.CommonConstants.METHODS_KEY; import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY; @@ -236,7 +236,7 @@ public class MetadataInfo implements Serializable { private String buildMatchKey() { matchKey = getServiceKey(); if (StringUtils.isNotEmpty(protocol)) { - matchKey = getServiceKey() + GROUP_CHAR_SEPERATOR + protocol; + matchKey = getServiceKey() + GROUP_CHAR_SEPARATOR + protocol; } return matchKey; } diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/ParameterizedServiceNameMapping.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/ParameterizedServiceNameMapping.java deleted file mode 100644 index 893a6f4..0000000 --- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/ParameterizedServiceNameMapping.java +++ /dev/null @@ -1,47 +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.metadata; - -import org.apache.dubbo.common.URL; - -import java.util.Set; - -import static org.apache.dubbo.common.constants.RegistryConstants.SUBSCRIBED_SERVICE_NAMES_KEY; - -/** - * The parameterized implementation of {@link ServiceNameMapping} - * - * @see ReadOnlyServiceNameMapping - * @since 2.7.8 - */ -public class ParameterizedServiceNameMapping extends ReadOnlyServiceNameMapping { - - /** - * The priority of {@link PropertiesFileServiceNameMapping} - */ - static final int PRIORITY = MAX_PRIORITY + 99; - - @Override - public Set<String> get(URL subscribedURL) { - return getValue(subscribedURL.getParameter(SUBSCRIBED_SERVICE_NAMES_KEY)); - } - - @Override - public int getPriority() { - return PRIORITY; - } -} diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/PropertiesFileServiceNameMapping.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/PropertiesFileServiceNameMapping.java deleted file mode 100644 index 7870895..0000000 --- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/PropertiesFileServiceNameMapping.java +++ /dev/null @@ -1,148 +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.metadata; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.config.Configuration; -import org.apache.dubbo.common.constants.CommonConstants; -import org.apache.dubbo.common.utils.ClassUtils; -import org.apache.dubbo.common.utils.PathUtils; -import org.apache.dubbo.common.utils.StringUtils; -import org.apache.dubbo.rpc.model.ApplicationModel; - -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.util.Enumeration; -import java.util.LinkedList; -import java.util.List; -import java.util.Properties; -import java.util.Set; - -import static java.lang.String.format; -import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_SERVICE_NAME_MAPPING_PROPERTIES_PATH; -import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.SERVICE_NAME_MAPPING_PROPERTIES_FILE_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY; -import static org.apache.dubbo.common.utils.StringUtils.SLASH; -import static org.apache.dubbo.metadata.MetadataConstants.KEY_SEPARATOR; - -/** - * The externalized {@link Properties} file implementation of {@link ServiceNameMapping}, - * the default properties class path is - * {@link CommonConstants#DEFAULT_SERVICE_NAME_MAPPING_PROPERTIES_PATH "/META-INF/dubbo/service-name-mapping.properties"}, - * whose format as following: - * <pre> - * dubbo\:com.acme.Interface1\:default = Service1 - * thirft\:com.acme.InterfaceX = Service1,Service2 - * rest\:com.acme.interfaceN = Service3 - * </pre> - * <p> - * THe search path could be configured by the externalized property {@link CommonConstants#SERVICE_NAME_MAPPING_PROPERTIES_FILE_KEY} - * - * @see ReadOnlyServiceNameMapping - * @see ParameterizedServiceNameMapping - * @since 2.7.8 - */ -public class PropertiesFileServiceNameMapping extends ReadOnlyServiceNameMapping { - - /** - * The priority of {@link PropertiesFileServiceNameMapping} is - * lower than {@link ParameterizedServiceNameMapping} - */ - static final int PRIORITY = ParameterizedServiceNameMapping.PRIORITY + 1; - - - private final List<Properties> propertiesList; - - public PropertiesFileServiceNameMapping() { - this.propertiesList = loadPropertiesList(); - } - - @Override - public Set<String> get(URL subscribedURL) { - String propertyKey = getPropertyKey(subscribedURL); - String propertyValue = null; - - for (Properties properties : propertiesList) { - propertyValue = properties.getProperty(propertyKey); - if (propertyValue != null) { - break; - } - } - - return getValue(propertyValue); - } - - private String getPropertyKey(URL url) { - String protocol = url.getProtocol(); - String serviceInterface = url.getServiceInterface(); - // Optional - String group = url.getParameter(GROUP_KEY); - String version = url.getParameter(VERSION_KEY); - - StringBuilder propertyKeyBuilder = new StringBuilder(protocol) - .append(KEY_SEPARATOR) - .append(serviceInterface); - - appendIfPresent(propertyKeyBuilder, group); - appendIfPresent(propertyKeyBuilder, version); - - return propertyKeyBuilder.toString(); - } - - private void appendIfPresent(StringBuilder builder, String value) { - if (!StringUtils.isBlank(value)) { - builder.append(KEY_SEPARATOR).append(value); - } - } - - private List<Properties> loadPropertiesList() { - List<Properties> propertiesList = new LinkedList<>(); - String propertiesPath = getPropertiesPath(); - try { - Enumeration<java.net.URL> resources = ClassUtils.getClassLoader().getResources(propertiesPath); - while (resources.hasMoreElements()) { - java.net.URL resource = resources.nextElement(); - InputStream inputStream = resource.openStream(); - Properties properties = new Properties(); - properties.load(new InputStreamReader(inputStream, "UTF-8")); - propertiesList.add(properties); - } - } catch (IOException e) { - if (logger.isErrorEnabled()) { - logger.error(format("The path of ServiceNameMapping's Properties file[path : %s] can't be loaded", propertiesPath), e); - } - } - return propertiesList; - } - - private String getPropertiesPath() { - Configuration configuration = ApplicationModel.getEnvironment().getConfiguration(); - String propertyPath = configuration.getString(SERVICE_NAME_MAPPING_PROPERTIES_FILE_KEY, DEFAULT_SERVICE_NAME_MAPPING_PROPERTIES_PATH); - propertyPath = PathUtils.normalize(propertyPath); - if (propertyPath.startsWith(SLASH)) { - propertyPath = propertyPath.substring(SLASH.length()); - } - return propertyPath; - } - - @Override - public int getPriority() { - return PRIORITY; - } -} diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/ReadOnlyServiceNameMapping.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/ReadOnlyServiceNameMapping.java deleted file mode 100644 index 58035d3..0000000 --- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/ReadOnlyServiceNameMapping.java +++ /dev/null @@ -1,45 +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.metadata; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.logger.Logger; -import org.apache.dubbo.common.logger.LoggerFactory; - -import java.util.Set; - -import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SEPARATOR_CHAR; -import static org.apache.dubbo.common.utils.StringUtils.splitToSet; - -/** - * Read-Only implementation of {@link ServiceNameMapping} - * - * @since 2.7.8 - */ -public abstract class ReadOnlyServiceNameMapping implements ServiceNameMapping { - - protected final Logger logger = LoggerFactory.getLogger(getClass()); - - @Override - public void map(URL exportedURL) { - // DO NOTING for mapping - } - - protected Set<String> getValue(String rawValue) { - return splitToSet(rawValue, COMMA_SEPARATOR_CHAR, true); - } -} diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReport.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReport.java index f3b90db..1d39401 100644 --- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReport.java +++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReport.java @@ -44,7 +44,6 @@ import java.nio.channels.FileChannel; import java.nio.channels.FileLock; import java.util.ArrayList; import java.util.Calendar; -import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -53,6 +52,7 @@ import java.util.Set; import java.util.SortedSet; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ThreadLocalRandom; @@ -61,15 +61,11 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; -import static java.util.concurrent.Executors.newScheduledThreadPool; -import static java.util.concurrent.Executors.newSingleThreadExecutor; -import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY; import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SIDE; import static org.apache.dubbo.common.constants.CommonConstants.FILE_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE; import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY; -import static org.apache.dubbo.common.utils.StringUtils.replace; import static org.apache.dubbo.metadata.report.support.Constants.CYCLE_REPORT_KEY; import static org.apache.dubbo.metadata.report.support.Constants.DEFAULT_METADATA_REPORT_CYCLE_REPORT; import static org.apache.dubbo.metadata.report.support.Constants.DEFAULT_METADATA_REPORT_RETRY_PERIOD; @@ -90,52 +86,24 @@ public abstract class AbstractMetadataReport implements MetadataReport { // Log output protected final Logger logger = LoggerFactory.getLogger(getClass()); - private final AtomicBoolean initialized = new AtomicBoolean(false); - + // Local disk cache, where the special key value.registries records the list of metadata centers, and the others are the list of notified service providers + final Properties properties = new Properties(); + private final ExecutorService reportCacheExecutor = Executors.newFixedThreadPool(1, new NamedThreadFactory("DubboSaveMetadataReport", true)); final Map<MetadataIdentifier, Object> allMetadataReports = new ConcurrentHashMap<>(4); + private final AtomicLong lastCacheChanged = new AtomicLong(); final Map<MetadataIdentifier, Object> failedReports = new ConcurrentHashMap<>(4); - private URL reportURL; boolean syncReport; - // Local disk cache file - File localCacheFile; - // Local disk cache, where the special key value.registries records the list of metadata centers, and the others are the list of notified service providers - final Properties properties = new Properties(); - - private final AtomicLong lastCacheChanged = new AtomicLong(); - - // ThreadPoolExecutors - private final ExecutorService reportCacheExecutor; - - public final MetadataReportRetry metadataReportRetry; - - private final ScheduledExecutorService cycleReportExecutor; + File file; + private AtomicBoolean initialized = new AtomicBoolean(false); + public MetadataReportRetry metadataReportRetry; public AbstractMetadataReport(URL reportServerURL) { setUrl(reportServerURL); - - this.localCacheFile = initializeLocalCacheFile(reportServerURL); - loadProperties(); - syncReport = reportServerURL.getParameter(SYNC_REPORT_KEY, false); - metadataReportRetry = new MetadataReportRetry(reportServerURL.getParameter(RETRY_TIMES_KEY, DEFAULT_METADATA_REPORT_RETRY_TIMES), - reportServerURL.getParameter(RETRY_PERIOD_KEY, DEFAULT_METADATA_REPORT_RETRY_PERIOD)); - this.reportCacheExecutor = newSingleThreadExecutor(new NamedThreadFactory("DubboSaveMetadataReport", true)); - this.cycleReportExecutor = newSingleThreadScheduledExecutor(new NamedThreadFactory("DubboMetadataReportTimer", true)); - // cycle report the data switch - if (reportServerURL.getParameter(CYCLE_REPORT_KEY, DEFAULT_METADATA_REPORT_CYCLE_REPORT)) { - cycleReportExecutor.scheduleAtFixedRate(this::publishAll, calculateStartTime(), ONE_DAY_IN_MILLISECONDS, TimeUnit.MILLISECONDS); - } - } - - private File initializeLocalCacheFile(URL reportServerURL) { // Start file save timer - String defaultFilename = System.getProperty("user.home") + - "/.dubbo/dubbo-metadata-" + - reportServerURL.getParameter(APPLICATION_KEY) + "-" + - replace(reportServerURL.getAddress(), ":", "-") + - ".cache"; + String defaultFilename = System.getProperty("user.home") + "/.dubbo/dubbo-metadata-" + reportServerURL.getParameter(APPLICATION_KEY) + "-" + reportServerURL.getAddress().replaceAll(":", "-") + ".cache"; String filename = reportServerURL.getParameter(FILE_KEY, defaultFilename); File file = null; if (ConfigUtils.isNotEmpty(filename)) { @@ -150,7 +118,16 @@ public abstract class AbstractMetadataReport implements MetadataReport { file.delete(); } } - return file; + this.file = file; + loadProperties(); + syncReport = reportServerURL.getParameter(SYNC_REPORT_KEY, false); + metadataReportRetry = new MetadataReportRetry(reportServerURL.getParameter(RETRY_TIMES_KEY, DEFAULT_METADATA_REPORT_RETRY_TIMES), + reportServerURL.getParameter(RETRY_PERIOD_KEY, DEFAULT_METADATA_REPORT_RETRY_PERIOD)); + // cycle report the data switch + if (reportServerURL.getParameter(CYCLE_REPORT_KEY, DEFAULT_METADATA_REPORT_CYCLE_REPORT)) { + ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("DubboMetadataReportTimer", true)); + scheduler.scheduleAtFixedRate(this::publishAll, calculateStartTime(), ONE_DAY_IN_MILLISECONDS, TimeUnit.MILLISECONDS); + } } public URL getUrl() { @@ -168,12 +145,12 @@ public abstract class AbstractMetadataReport implements MetadataReport { if (version < lastCacheChanged.get()) { return; } - if (localCacheFile == null) { + if (file == null) { return; } // Save try { - File lockfile = new File(localCacheFile.getAbsolutePath() + ".lock"); + File lockfile = new File(file.getAbsolutePath() + ".lock"); if (!lockfile.exists()) { lockfile.createNewFile(); } @@ -181,14 +158,14 @@ public abstract class AbstractMetadataReport implements MetadataReport { FileChannel channel = raf.getChannel()) { FileLock lock = channel.tryLock(); if (lock == null) { - throw new IOException("Can not lock the metadataReport cache file " + localCacheFile.getAbsolutePath() + ", ignore and retry later, maybe multi java process use the file, please config: dubbo.metadata.file=xxx.properties"); + throw new IOException("Can not lock the metadataReport cache file " + file.getAbsolutePath() + ", ignore and retry later, maybe multi java process use the file, please config: dubbo.metadata.file=xxx.properties"); } // Save try { - if (!localCacheFile.exists()) { - localCacheFile.createNewFile(); + if (!file.exists()) { + file.createNewFile(); } - try (FileOutputStream outputFile = new FileOutputStream(localCacheFile)) { + try (FileOutputStream outputFile = new FileOutputStream(file)) { properties.store(outputFile, "Dubbo metadataReport Cache"); } } finally { @@ -206,20 +183,20 @@ public abstract class AbstractMetadataReport implements MetadataReport { } void loadProperties() { - if (localCacheFile != null && localCacheFile.exists()) { - try (InputStream in = new FileInputStream(localCacheFile)) { + if (file != null && file.exists()) { + try (InputStream in = new FileInputStream(file)) { properties.load(in); if (logger.isInfoEnabled()) { - logger.info("Load service store file " + localCacheFile + ", data: " + properties); + logger.info("Load service store file " + file + ", data: " + properties); } } catch (Throwable e) { - logger.warn("Failed to load service store file " + localCacheFile, e); + logger.warn("Failed to load service store file " + file, e); } } } private void saveProperties(MetadataIdentifier metadataIdentifier, String value, boolean add, boolean sync) { - if (localCacheFile == null) { + if (file == null) { return; } @@ -341,7 +318,7 @@ public abstract class AbstractMetadataReport implements MetadataReport { } @Override - public void saveSubscribedData(SubscriberMetadataIdentifier subscriberMetadataIdentifier, Collection<String> urls) { + public void saveSubscribedData(SubscriberMetadataIdentifier subscriberMetadataIdentifier, Set<String> urls) { if (syncReport) { doSaveSubscriberData(subscriberMetadataIdentifier, new Gson().toJson(urls)); } else { @@ -351,7 +328,7 @@ public abstract class AbstractMetadataReport implements MetadataReport { @Override - public Set<String> getSubscribedURLs(SubscriberMetadataIdentifier subscriberMetadataIdentifier) { + public List<String> getSubscribedURLs(SubscriberMetadataIdentifier subscriberMetadataIdentifier) { String content = doGetSubscribedURLs(subscriberMetadataIdentifier); Type setType = new TypeToken<SortedSet<String>>() { }.getType(); @@ -415,7 +392,7 @@ public abstract class AbstractMetadataReport implements MetadataReport { class MetadataReportRetry { protected final Logger logger = LoggerFactory.getLogger(getClass()); - final ScheduledExecutorService retryExecutor = newScheduledThreadPool(0, new NamedThreadFactory("DubboMetadataReportRetryTimer", true)); + final ScheduledExecutorService retryExecutor = Executors.newScheduledThreadPool(0, new NamedThreadFactory("DubboMetadataReportRetryTimer", true)); volatile ScheduledFuture retryScheduledFuture; final AtomicInteger retryCounter = new AtomicInteger(0); // retry task schedule period @@ -458,10 +435,8 @@ public abstract class AbstractMetadataReport implements MetadataReport { } void cancelRetryTask() { - if (retryScheduledFuture != null) { - retryScheduledFuture.cancel(false); - } - shutdown(retryExecutor); + retryScheduledFuture.cancel(false); + retryExecutor.shutdown(); } } @@ -476,13 +451,6 @@ public abstract class AbstractMetadataReport implements MetadataReport { doSaveSubscriberData(subscriberMetadataIdentifier, encodedUrlList); } - @Override - public final void close() throws Exception { - this.shutdownThreadPoolExecutors(); - this.clearCache(); - doClose(); - } - protected abstract void doStoreProviderMetadata(MetadataIdentifier providerMetadataIdentifier, String serviceDefinitions); protected abstract void doStoreConsumerMetadata(MetadataIdentifier consumerMetadataIdentifier, String serviceParameterString); @@ -497,35 +465,4 @@ public abstract class AbstractMetadataReport implements MetadataReport { protected abstract String doGetSubscribedURLs(SubscriberMetadataIdentifier subscriberMetadataIdentifier); - /** - * Close other resources - * - * @since 2.7.8 - */ - protected void doClose() throws Exception { - - } - - private void clearCache() { - this.properties.clear(); - this.allMetadataReports.clear(); - this.failedReports.clear(); - this.localCacheFile.delete(); - } - - private void shutdownThreadPoolExecutors() { - this.metadataReportRetry.cancelRetryTask(); - shutdown(this.reportCacheExecutor); - shutdown(cycleReportExecutor); - } - - private static void shutdown(ExecutorService executorService) { - if (executorService == null) { - return; - } - if (!executorService.isShutdown()) { - executorService.shutdown(); - } - } - } diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/ConfigCenterBasedMetadataReport.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/ConfigCenterBasedMetadataReport.java deleted file mode 100644 index d962ca5..0000000 --- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/ConfigCenterBasedMetadataReport.java +++ /dev/null @@ -1,162 +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.metadata.report.support; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.config.configcenter.DynamicConfiguration; -import org.apache.dubbo.common.config.configcenter.DynamicConfigurationFactory; -import org.apache.dubbo.metadata.report.MetadataReport; -import org.apache.dubbo.metadata.report.identifier.BaseMetadataIdentifier; -import org.apache.dubbo.metadata.report.identifier.KeyTypeEnum; -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 java.util.List; - -import static org.apache.dubbo.common.config.configcenter.DynamicConfigurationFactory.getDynamicConfigurationFactory; -import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; -import static org.apache.dubbo.metadata.MetadataConstants.EXPORTED_URLS_TAG; - -/** - * The generic implementation of {@link MetadataReport} based on {@link DynamicConfiguration - * the config-center infrastructure} - * - * @see AbstractMetadataReport - * @since 2.7.8 - */ -public class ConfigCenterBasedMetadataReport extends AbstractMetadataReport { - - private final KeyTypeEnum keyType; - - private final String group; - - private final DynamicConfiguration dynamicConfiguration; - - public ConfigCenterBasedMetadataReport(URL reportServerURL, KeyTypeEnum keyTypeEnum) { - super(reportServerURL); - this.keyType = keyTypeEnum; - this.group = reportServerURL.getParameter(GROUP_KEY, DEFAULT_ROOT); - String extensionName = reportServerURL.getProtocol(); - DynamicConfigurationFactory dynamicConfigurationFactory = getDynamicConfigurationFactory(extensionName); - dynamicConfiguration = dynamicConfigurationFactory.getDynamicConfiguration(reportServerURL); - } - - - @Override - protected void doStoreProviderMetadata(MetadataIdentifier providerMetadataIdentifier, String serviceDefinitions) { - saveMetadata(providerMetadataIdentifier, serviceDefinitions); - } - - @Override - protected void doStoreConsumerMetadata(MetadataIdentifier consumerMetadataIdentifier, String serviceParameterString) { - saveMetadata(consumerMetadataIdentifier, serviceParameterString); - } - - @Override - protected void doSaveMetadata(ServiceMetadataIdentifier metadataIdentifier, URL url) { - saveMetadata(metadataIdentifier, URL.encode(url.toFullString())); - } - - @Override - protected void doRemoveMetadata(ServiceMetadataIdentifier metadataIdentifier) { - removeMetadata(metadataIdentifier); - } - - @Override - protected List<String> doGetExportedURLs(ServiceMetadataIdentifier metadataIdentifier) { - throw new UnsupportedOperationException("doGetExportedURLs method will not be supported!"); - } - - @Override - protected void doSaveSubscriberData(SubscriberMetadataIdentifier subscriberMetadataIdentifier, String urlListStr) { - saveMetadata(subscriberMetadataIdentifier, urlListStr); - } - - @Override - protected String doGetSubscribedURLs(SubscriberMetadataIdentifier subscriberMetadataIdentifier) { - return getMetadata(subscriberMetadataIdentifier); - } - - @Override - public String getServiceDefinition(MetadataIdentifier metadataIdentifier) { - return getMetadata(metadataIdentifier); - } - - @Override - public boolean saveExportedURLs(String serviceName, String exportedServicesRevision, String exportedURLsContent) { - String key = buildExportedURLsMetadataKey(serviceName, exportedServicesRevision); - return dynamicConfiguration.publishConfig(key, group, exportedURLsContent); - } - - @Override - public String getExportedURLsContent(String serviceName, String exportedServicesRevision) { - String key = buildExportedURLsMetadataKey(serviceName, exportedServicesRevision); - return dynamicConfiguration.getConfig(key, group); - } - - private String buildExportedURLsMetadataKey(String serviceName, String exportedServicesRevision) { - return keyType.build(EXPORTED_URLS_TAG, serviceName, exportedServicesRevision); - } - - protected void saveMetadata(BaseMetadataIdentifier metadataIdentifier, String value) { - String key = getKey(metadataIdentifier); - dynamicConfiguration.publishConfig(key, group, value); - } - - protected void saveMetadata(MetadataIdentifier metadataIdentifier, String value) { - String key = getKey(metadataIdentifier); - dynamicConfiguration.publishConfig(key, group, value); - } - - protected String getMetadata(ServiceMetadataIdentifier metadataIdentifier) { - String key = getKey(metadataIdentifier); - return dynamicConfiguration.getConfig(key, group); - } - - protected String getMetadata(MetadataIdentifier metadataIdentifier) { - String key = getKey(metadataIdentifier); - return dynamicConfiguration.getConfig(key, group); - } - - protected String getMetadata(SubscriberMetadataIdentifier metadataIdentifier) { - String key = getKey(metadataIdentifier); - return dynamicConfiguration.getConfig(key, group); - } - - protected void removeMetadata(MetadataIdentifier metadataIdentifier) { - String key = getKey(metadataIdentifier); - dynamicConfiguration.removeConfig(key, group); - } - - protected void removeMetadata(ServiceMetadataIdentifier metadataIdentifier) { - String key = getKey(metadataIdentifier); - dynamicConfiguration.removeConfig(key, group); - } - - protected String getKey(BaseMetadataIdentifier metadataIdentifier) { - return metadataIdentifier.getUniqueKey(keyType); - } - - protected String getKey(MetadataIdentifier metadataIdentifier) { - return metadataIdentifier.getUniqueKey(keyType); - } - - protected void doClose() throws Exception { - this.dynamicConfiguration.close(); - } -} diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/ConfigCenterBasedMetadataReportFactory.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/ConfigCenterBasedMetadataReportFactory.java deleted file mode 100644 index b445feb..0000000 --- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/ConfigCenterBasedMetadataReportFactory.java +++ /dev/null @@ -1,90 +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.metadata.report.support; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.config.configcenter.DynamicConfiguration; -import org.apache.dubbo.metadata.report.MetadataReport; -import org.apache.dubbo.metadata.report.MetadataReportFactory; -import org.apache.dubbo.metadata.report.identifier.KeyTypeEnum; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import static org.apache.dubbo.common.config.configcenter.TreePathDynamicConfiguration.CONFIG_ROOT_PATH_PARAM_NAME; -import static org.apache.dubbo.common.utils.StringUtils.SLASH; -import static org.apache.dubbo.common.utils.StringUtils.isBlank; -import static org.apache.dubbo.metadata.report.identifier.KeyTypeEnum.PATH; -import static org.apache.dubbo.rpc.cluster.Constants.EXPORT_KEY; -import static org.apache.dubbo.rpc.cluster.Constants.REFER_KEY; - -/** - * The abstract implementation of {@link MetadataReportFactory} based on - * {@link DynamicConfiguration the config-center infrastructure} - * - * @see MetadataReportFactory - * @see MetadataReport - * @see DynamicConfiguration - * @since 2.7.8 - */ -public abstract class ConfigCenterBasedMetadataReportFactory implements MetadataReportFactory { - - /** - * org.apache.dubbo.metadata.report.MetadataReport - */ - private static final String URL_PATH = MetadataReport.class.getName(); - - // Registry Collection Map<metadataAddress, MetadataReport> - private static final Map<String, ConfigCenterBasedMetadataReport> metadataReportCache = new ConcurrentHashMap(); - - private final KeyTypeEnum keyType; - - public ConfigCenterBasedMetadataReportFactory(KeyTypeEnum keyType) { - if (keyType == null) { - throw new NullPointerException("The keyType argument must not be null!"); - } - this.keyType = keyType; - } - - @Override - public ConfigCenterBasedMetadataReport getMetadataReport(URL url) { - url = url.setPath(URL_PATH); - final URL actualURL = resolveURLParameters(url); - String key = actualURL.toServiceString(); - // Lock the metadata access process to ensure a single instance of the metadata instance - return metadataReportCache.computeIfAbsent(key, k -> new ConfigCenterBasedMetadataReport(actualURL, keyType)); - } - - private URL resolveURLParameters(URL url) { - URL resolvedURL = url.removeParameters(EXPORT_KEY, REFER_KEY); - if (PATH.equals(getKeyType())) { // Only handles for "PATH" type - if (isBlank(resolvedURL.getParameter(CONFIG_ROOT_PATH_PARAM_NAME))) { - resolvedURL = resolvedURL.addParameter(CONFIG_ROOT_PATH_PARAM_NAME, SLASH); - } - } - return resolvedURL; - } - - /** - * Get {@link KeyTypeEnum the key type} - * - * @return non-null - */ - protected KeyTypeEnum getKeyType() { - return keyType; - } -} diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/file/FileSystemMetadataReportFactory.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/file/FileSystemMetadataReportFactory.java deleted file mode 100644 index c4cc487..0000000 --- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/file/FileSystemMetadataReportFactory.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.metadata.report.support.file; - -import org.apache.dubbo.metadata.report.identifier.KeyTypeEnum; -import org.apache.dubbo.metadata.report.support.ConfigCenterBasedMetadataReportFactory; - -/** - * The implementation of {@link ConfigCenterBasedMetadataReportFactory} based on File System - * - * @see ConfigCenterBasedMetadataReportFactory - * @since 2.7.8 - */ -public class FileSystemMetadataReportFactory extends ConfigCenterBasedMetadataReportFactory { - - public FileSystemMetadataReportFactory() { - super(KeyTypeEnum.PATH); - } -} diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.metadata.MetadataParamsFilter b/dubbo-metadata/dubbo-metadata-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.metadata.MetadataParamsFilter new file mode 100644 index 0000000..6754126 --- /dev/null +++ b/dubbo-metadata/dubbo-metadata-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.metadata.MetadataParamsFilter @@ -0,0 +1 @@ +default=org.apache.dubbo.metadata.DefaultMetadataParamsFilter \ No newline at end of file diff --git a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/CompositeServiceNameMappingTest.java b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/CompositeServiceNameMappingTest.java deleted file mode 100644 index 48243f8..0000000 --- a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/CompositeServiceNameMappingTest.java +++ /dev/null @@ -1,107 +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.metadata; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.config.configcenter.file.FileSystemDynamicConfiguration; -import org.apache.dubbo.config.ApplicationConfig; -import org.apache.dubbo.rpc.model.ApplicationModel; -import org.apache.dubbo.rpc.service.EchoService; - -import org.apache.commons.io.FileUtils; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.util.Set; - -import static java.util.Collections.singleton; -import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY; -import static org.apache.dubbo.common.constants.RegistryConstants.SUBSCRIBED_SERVICE_NAMES_KEY; -import static org.apache.dubbo.common.utils.CollectionUtils.ofSet; -import static org.apache.dubbo.metadata.DynamicConfigurationServiceNameMapping.buildGroup; -import static org.apache.dubbo.rpc.model.ApplicationModel.getApplicationConfig; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -/** - * {@link CompositeServiceNameMapping} Test - * - * @since 2.7.8 - */ -public class CompositeServiceNameMappingTest { - - private static final URL BASE_URL = URL.valueOf("dubbo://127.0.0.1:20880") - .setPath(EchoService.class.getName()) - .addParameter(GROUP_KEY, "default") - .addParameter(VERSION_KEY, "1.0.0"); - - private static final String APP_NAME = "test-service"; - - private ServiceNameMapping serviceNameMapping; - - private FileSystemDynamicConfiguration dynamicConfiguration; - - @BeforeEach - public void init() { - serviceNameMapping = ServiceNameMapping.getDefaultExtension(); - dynamicConfiguration = new FileSystemDynamicConfiguration(); - ApplicationModel.getConfigManager().setApplication(new ApplicationConfig(APP_NAME)); - ApplicationModel.getEnvironment().setDynamicConfiguration(dynamicConfiguration); - } - - @AfterEach - public void reset() { - FileUtils.deleteQuietly(dynamicConfiguration.getRootDirectory()); - ApplicationModel.reset(); - } - - @Test - public void testType() { - assertEquals(CompositeServiceNameMapping.class, serviceNameMapping.getClass()); - } - - @Test - public void testMap() { - serviceNameMapping.map(BASE_URL); - assertNotNull(dynamicConfiguration.getConfig(APP_NAME, - buildGroup(BASE_URL.getServiceInterface(), null, null, null))); - } - - @Test - public void testGet() { - serviceNameMapping.map(BASE_URL); - Set<String> serviceNames = serviceNameMapping.get(BASE_URL); - assertEquals(singleton(APP_NAME), serviceNames); - - getApplicationConfig().setName("service1"); - serviceNameMapping.map(BASE_URL); - serviceNames = serviceNameMapping.get(BASE_URL); - assertEquals(ofSet(APP_NAME, "service1"), serviceNames); - - serviceNames = serviceNameMapping.get(BASE_URL - .setPath("com.acme.Interface1") - .removeParameter(VERSION_KEY) - ); - assertEquals(singleton("Service1"), serviceNames); - - serviceNames = serviceNameMapping.get(BASE_URL.addParameter(SUBSCRIBED_SERVICE_NAMES_KEY, "s1 , s2 , s3 ")); - assertEquals(ofSet("s1", "s2", "s3"), serviceNames); - } -} - diff --git a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/MetadataConstantsTest.java b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/MetadataConstantsTest.java deleted file mode 100644 index d0c201b..0000000 --- a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/MetadataConstantsTest.java +++ /dev/null @@ -1,35 +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.metadata; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -/** - * {@link MetadataConstants} Test-Cases - * - * @since 2.7.8 - */ -public class MetadataConstantsTest { - - @Test - public void testConstants() { - assertEquals("exported-urls", MetadataConstants.EXPORTED_URLS_TAG); - assertEquals("subscribed-urls", MetadataConstants.SUBSCRIBED_URLS_TAG); - } -} diff --git a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/ParameterizedServiceNameMappingTest.java b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/ParameterizedServiceNameMappingTest.java deleted file mode 100644 index dbe5454..0000000 --- a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/ParameterizedServiceNameMappingTest.java +++ /dev/null @@ -1,67 +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.metadata; - -import org.apache.dubbo.common.URL; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.util.Arrays; -import java.util.LinkedHashSet; -import java.util.Set; - -import static java.util.Collections.emptySet; -import static java.util.Collections.singleton; -import static org.apache.dubbo.common.constants.RegistryConstants.SUBSCRIBED_SERVICE_NAMES_KEY; -import static org.junit.jupiter.api.Assertions.assertEquals; - -/** - * {@link ParameterizedServiceNameMapping} Test - * - * @see ParameterizedServiceNameMapping - * @since 2.7.8 - */ -public class ParameterizedServiceNameMappingTest { - - private static final URL BASE_URL = URL.valueOf("dubbo://127.0.0.1:20880"); - - private ParameterizedServiceNameMapping serviceNameMapping; - - @BeforeEach - public void init() { - serviceNameMapping = new ParameterizedServiceNameMapping(); - } - - @Test - public void testMap() { - // NOTHING to happen - serviceNameMapping.map(BASE_URL); - } - - @Test - public void testGet() { - Set<String> serviceNames = serviceNameMapping.get(BASE_URL); - assertEquals(emptySet(), serviceNames); - - serviceNames = serviceNameMapping.get(BASE_URL.addParameter(SUBSCRIBED_SERVICE_NAMES_KEY, " Service1 ")); - assertEquals(singleton("Service1"), serviceNames); - - serviceNames = serviceNameMapping.get(BASE_URL.addParameter(SUBSCRIBED_SERVICE_NAMES_KEY, "Service1 , Service2 ")); - assertEquals(new LinkedHashSet(Arrays.asList("Service1", "Service2")), serviceNames); - } -} diff --git a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/PropertiesFileServiceNameMappingTest.java b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/PropertiesFileServiceNameMappingTest.java deleted file mode 100644 index 68a6ebf..0000000 --- a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/PropertiesFileServiceNameMappingTest.java +++ /dev/null @@ -1,60 +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.metadata; - -import org.apache.dubbo.common.URL; - -import org.junit.jupiter.api.Test; - -import java.util.Arrays; -import java.util.LinkedHashSet; - -import static java.util.Collections.singleton; -import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.SERVICE_NAME_MAPPING_PROPERTIES_FILE_KEY; -import static org.junit.jupiter.api.Assertions.assertEquals; - -/** - * {@link PropertiesFileServiceNameMapping} Test - * - * @since 2.7.8 - */ -public class PropertiesFileServiceNameMappingTest { - - private static final URL BASE_URL = URL.valueOf("dubbo://127.0.0.1:20880"); - - - @Test - public void testMap() { - PropertiesFileServiceNameMapping serviceNameMapping = new PropertiesFileServiceNameMapping(); - serviceNameMapping.map(BASE_URL); - } - - @Test - public void testGet() { - - PropertiesFileServiceNameMapping serviceNameMapping = new PropertiesFileServiceNameMapping(); - URL url = BASE_URL.setServiceInterface("com.acme.Interface1").addParameter(GROUP_KEY, "default"); - assertEquals(singleton("Service1"), serviceNameMapping.get(url)); - - System.setProperty(SERVICE_NAME_MAPPING_PROPERTIES_FILE_KEY, "///META-INF//dubbo/service-name-mapping.properties"); - serviceNameMapping = new PropertiesFileServiceNameMapping(); - - url = BASE_URL.setProtocol("thirft").setServiceInterface("com.acme.InterfaceX"); - assertEquals(new LinkedHashSet<>(Arrays.asList("Service1", "Service2")), serviceNameMapping.get(url)); - } -} diff --git a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/ServiceNameMappingTest.java b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/ServiceNameMappingTest.java index 8c37998..e4b4c1e 100644 --- a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/ServiceNameMappingTest.java +++ b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/ServiceNameMappingTest.java @@ -13,7 +13,8 @@ * 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.metadata; import org.apache.dubbo.common.URL; @@ -39,11 +40,13 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; +*/ /** * {@link ServiceNameMapping} Test * * @since 2.7.8 - */ + *//* + public class ServiceNameMappingTest { private static final URL BASE_URL = URL.valueOf("dubbo://127.0.0.1:20880"); @@ -119,3 +122,4 @@ public class ServiceNameMappingTest { } } +*/ diff --git a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReportFactoryTest.java b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReportFactoryTest.java index 3aa5c02..5b0ffd7 100644 --- a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReportFactoryTest.java +++ b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReportFactoryTest.java @@ -1,135 +1,135 @@ -/* - * 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.metadata.report.support; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.utils.NetUtils; -import org.apache.dubbo.metadata.definition.model.ServiceDefinition; -import org.apache.dubbo.metadata.report.MetadataReport; -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 com.alibaba.fastjson.JSON; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -/** - * 2018/9/14 - */ -public class AbstractMetadataReportFactoryTest { - - private AbstractMetadataReportFactory metadataReportFactory = new AbstractMetadataReportFactory() { - @Override - protected MetadataReport createMetadataReport(URL url) { - return new MetadataReport() { - - @Override - public void storeProviderMetadata(MetadataIdentifier providerMetadataIdentifier, ServiceDefinition serviceDefinition) { - store.put(providerMetadataIdentifier.getIdentifierKey(), JSON.toJSONString(serviceDefinition)); - } - - @Override - public void saveServiceMetadata(ServiceMetadataIdentifier metadataIdentifier, URL url) { - - } - - @Override - public void removeServiceMetadata(ServiceMetadataIdentifier metadataIdentifier) { - - } - - @Override - public List<String> getExportedURLs(ServiceMetadataIdentifier metadataIdentifier) { - return null; - } - - @Override - public void saveSubscribedData(SubscriberMetadataIdentifier subscriberMetadataIdentifier, - Collection<String> urls) { - - } - - @Override - public List<String> getSubscribedURLs(SubscriberMetadataIdentifier subscriberMetadataIdentifier) { - return null; - } - - @Override - public String getServiceDefinition(MetadataIdentifier consumerMetadataIdentifier) { - return null; - } - - @Override - public void storeConsumerMetadata(MetadataIdentifier consumerMetadataIdentifier, Map serviceParameterMap) { - store.put(consumerMetadataIdentifier.getIdentifierKey(), JSON.toJSONString(serviceParameterMap)); - } - - @Override - public void close() throws Exception { - - } - - Map<String, String> store = new ConcurrentHashMap<>(); - - - }; - } - }; - - @Test - public void testGetOneMetadataReport() { - URL url = URL.valueOf("zookeeper://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService?version=1.0.0&application=vic"); - MetadataReport metadataReport1 = metadataReportFactory.getMetadataReport(url); - MetadataReport metadataReport2 = metadataReportFactory.getMetadataReport(url); - Assertions.assertEquals(metadataReport1, metadataReport2); - } - - @Test - public void testGetOneMetadataReportForIpFormat() { - String hostName = NetUtils.getLocalAddress().getHostName(); - String ip = NetUtils.getIpByHost(hostName); - URL url1 = URL.valueOf("zookeeper://" + hostName + ":4444/org.apache.dubbo.TestService?version=1.0.0&application=vic"); - URL url2 = URL.valueOf("zookeeper://" + ip + ":4444/org.apache.dubbo.TestService?version=1.0.0&application=vic"); - MetadataReport metadataReport1 = metadataReportFactory.getMetadataReport(url1); - MetadataReport metadataReport2 = metadataReportFactory.getMetadataReport(url2); - Assertions.assertEquals(metadataReport1, metadataReport2); - } - - @Test - public void testGetForDiffService() { - URL url1 = URL.valueOf("zookeeper://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService1?version=1.0.0&application=vic"); - URL url2 = URL.valueOf("zookeeper://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService2?version=1.0.0&application=vic"); - MetadataReport metadataReport1 = metadataReportFactory.getMetadataReport(url1); - MetadataReport metadataReport2 = metadataReportFactory.getMetadataReport(url2); - Assertions.assertEquals(metadataReport1, metadataReport2); - } - - @Test - public void testGetForDiffGroup() { - URL url1 = URL.valueOf("zookeeper://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService?version=1.0.0&application=vic&group=aaa"); - URL url2 = URL.valueOf("zookeeper://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService?version=1.0.0&application=vic&group=bbb"); - MetadataReport metadataReport1 = metadataReportFactory.getMetadataReport(url1); - MetadataReport metadataReport2 = metadataReportFactory.getMetadataReport(url2); - Assertions.assertNotEquals(metadataReport1, metadataReport2); - } -} +///* +// * 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.metadata.report.support; +// +//import org.apache.dubbo.common.URL; +//import org.apache.dubbo.common.utils.NetUtils; +//import org.apache.dubbo.metadata.definition.model.ServiceDefinition; +//import org.apache.dubbo.metadata.report.MetadataReport; +//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 com.alibaba.fastjson.JSON; +//import org.junit.jupiter.api.Assertions; +//import org.junit.jupiter.api.Test; +// +//import java.util.Collection; +//import java.util.List; +//import java.util.Map; +//import java.util.concurrent.ConcurrentHashMap; +// +///** +// * 2018/9/14 +// */ +//public class AbstractMetadataReportFactoryTest { +// +// private AbstractMetadataReportFactory metadataReportFactory = new AbstractMetadataReportFactory() { +// @Override +// protected MetadataReport createMetadataReport(URL url) { +// return new MetadataReport() { +// +// @Override +// public void storeProviderMetadata(MetadataIdentifier providerMetadataIdentifier, ServiceDefinition serviceDefinition) { +// store.put(providerMetadataIdentifier.getIdentifierKey(), JSON.toJSONString(serviceDefinition)); +// } +// +// @Override +// public void saveServiceMetadata(ServiceMetadataIdentifier metadataIdentifier, URL url) { +// +// } +// +// @Override +// public void removeServiceMetadata(ServiceMetadataIdentifier metadataIdentifier) { +// +// } +// +// @Override +// public List<String> getExportedURLs(ServiceMetadataIdentifier metadataIdentifier) { +// return null; +// } +// +// @Override +// public void saveSubscribedData(SubscriberMetadataIdentifier subscriberMetadataIdentifier, +// Collection<String> urls) { +// +// } +// +// @Override +// public List<String> getSubscribedURLs(SubscriberMetadataIdentifier subscriberMetadataIdentifier) { +// return null; +// } +// +// @Override +// public String getServiceDefinition(MetadataIdentifier consumerMetadataIdentifier) { +// return null; +// } +// +// @Override +// public void storeConsumerMetadata(MetadataIdentifier consumerMetadataIdentifier, Map serviceParameterMap) { +// store.put(consumerMetadataIdentifier.getIdentifierKey(), JSON.toJSONString(serviceParameterMap)); +// } +// +// @Override +// public void close() throws Exception { +// +// } +// +// Map<String, String> store = new ConcurrentHashMap<>(); +// +// +// }; +// } +// }; +// +// @Test +// public void testGetOneMetadataReport() { +// URL url = URL.valueOf("zookeeper://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService?version=1.0.0&application=vic"); +// MetadataReport metadataReport1 = metadataReportFactory.getMetadataReport(url); +// MetadataReport metadataReport2 = metadataReportFactory.getMetadataReport(url); +// Assertions.assertEquals(metadataReport1, metadataReport2); +// } +// +// @Test +// public void testGetOneMetadataReportForIpFormat() { +// String hostName = NetUtils.getLocalAddress().getHostName(); +// String ip = NetUtils.getIpByHost(hostName); +// URL url1 = URL.valueOf("zookeeper://" + hostName + ":4444/org.apache.dubbo.TestService?version=1.0.0&application=vic"); +// URL url2 = URL.valueOf("zookeeper://" + ip + ":4444/org.apache.dubbo.TestService?version=1.0.0&application=vic"); +// MetadataReport metadataReport1 = metadataReportFactory.getMetadataReport(url1); +// MetadataReport metadataReport2 = metadataReportFactory.getMetadataReport(url2); +// Assertions.assertEquals(metadataReport1, metadataReport2); +// } +// +// @Test +// public void testGetForDiffService() { +// URL url1 = URL.valueOf("zookeeper://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService1?version=1.0.0&application=vic"); +// URL url2 = URL.valueOf("zookeeper://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService2?version=1.0.0&application=vic"); +// MetadataReport metadataReport1 = metadataReportFactory.getMetadataReport(url1); +// MetadataReport metadataReport2 = metadataReportFactory.getMetadataReport(url2); +// Assertions.assertEquals(metadataReport1, metadataReport2); +// } +// +// @Test +// public void testGetForDiffGroup() { +// URL url1 = URL.valueOf("zookeeper://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService?version=1.0.0&application=vic&group=aaa"); +// URL url2 = URL.valueOf("zookeeper://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService?version=1.0.0&application=vic&group=bbb"); +// MetadataReport metadataReport1 = metadataReportFactory.getMetadataReport(url1); +// MetadataReport metadataReport2 = metadataReportFactory.getMetadataReport(url2); +// Assertions.assertNotEquals(metadataReport1, metadataReport2); +// } +//} diff --git a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReportTest.java b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReportTest.java index c5325a1..37b0d3c 100644 --- a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReportTest.java +++ b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReportTest.java @@ -13,7 +13,8 @@ * 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.metadata.report.support; import org.apache.dubbo.common.URL; @@ -49,9 +50,19 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; +*/ /** + * Test {@link MetadataReport#saveExportedURLs(String, String, String)} method * - */ + * @since 2.7.8 + * <p> + * Test {@link MetadataReport#getExportedURLs(String, String)} method + * @since 2.7.8 + * <p> + * Test {@link MetadataReport#getExportedURLsContent(String, String)} method + * @since 2.7.8 + *//* + public class AbstractMetadataReportTest { private NewMetadataReport abstractMetadataReport; @@ -266,11 +277,19 @@ public class AbstractMetadataReportTest { } } - /** - * Test {@link MetadataReport#saveExportedURLs(String, String, String)} method - * - * @since 2.7.8 - */ + */ +/** + * Test {@link MetadataReport#saveExportedURLs(String, String, String)} method + * + * @since 2.7.8 + * <p> + * Test {@link MetadataReport#getExportedURLs(String, String)} method + * @since 2.7.8 + * <p> + * Test {@link MetadataReport#getExportedURLsContent(String, String)} method + * @since 2.7.8 + *//* + @Test public void testSaveExportedURLs() { String serviceName = null; @@ -284,11 +303,13 @@ public class AbstractMetadataReportTest { assertTrue(abstractMetadataReport.saveExportedURLs(serviceName, exportedServiceRevision, exportedURLsContent)); } - /** - * Test {@link MetadataReport#getExportedURLs(String, String)} method - * - * @since 2.7.8 - */ + */ +/** + * Test {@link MetadataReport#getExportedURLs(String, String)} method + * + * @since 2.7.8 + *//* + @Test public void testGetExportedURLs() { String serviceName = null; @@ -296,11 +317,13 @@ public class AbstractMetadataReportTest { assertEquals(emptySet(), abstractMetadataReport.getExportedURLs(serviceName, exportedServiceRevision)); } - /** - * Test {@link MetadataReport#getExportedURLsContent(String, String)} method - * - * @since 2.7.8 - */ + */ +/** + * Test {@link MetadataReport#getExportedURLsContent(String, String)} method + * + * @since 2.7.8 + *//* + @Test public void testGetExportedURLsContent() { String serviceName = null; @@ -446,3 +469,4 @@ public class AbstractMetadataReportTest { } +*/ diff --git a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/report/support/ConfigCenterBasedMetadataReportTest.java b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/report/support/ConfigCenterBasedMetadataReportTest.java deleted file mode 100644 index 32205b9..0000000 --- a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/report/support/ConfigCenterBasedMetadataReportTest.java +++ /dev/null @@ -1,155 +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.metadata.report.support; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.config.ApplicationConfig; -import org.apache.dubbo.metadata.URLRevisionResolver; -import org.apache.dubbo.metadata.definition.ServiceDefinitionBuilder; -import org.apache.dubbo.metadata.definition.model.ServiceDefinition; -import org.apache.dubbo.metadata.report.MetadataReport; -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.metadata.report.support.file.FileSystemMetadataReportFactory; -import org.apache.dubbo.rpc.model.ApplicationModel; -import org.apache.dubbo.rpc.service.EchoService; - -import com.google.gson.Gson; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.util.Collection; -import java.util.Map; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; - -import static java.util.Collections.singleton; -import static java.util.stream.Collectors.toSet; -import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY; -import static org.apache.dubbo.metadata.report.support.Constants.SYNC_REPORT_KEY; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; - -/** - * {@link ConfigCenterBasedMetadataReport} Test-Cases - * - * @since 2.7.8 - */ -public class ConfigCenterBasedMetadataReportTest { - - private static final URL REPORT_SERVER_URL = URL.valueOf("file://") - .addParameter(APPLICATION_KEY, "test") - .addParameter(SYNC_REPORT_KEY, "true"); - - private static final Class<EchoService> INTERFACE_CLASS = EchoService.class; - - private static final String INTERFACE_NAME = INTERFACE_CLASS.getName(); - - private static final String APP_NAME = "test-service"; - - private static final URL BASE_URL = URL - .valueOf("dubbo://127.0.0.1:20880") - .setPath(INTERFACE_NAME) - .addParameter(APPLICATION_KEY, APP_NAME) - .addParameter(SIDE_KEY, "provider"); - - private ConfigCenterBasedMetadataReport metadataReport; - - @BeforeEach - public void init() { - ApplicationModel.getConfigManager().setApplication(new ApplicationConfig("test-service")); - this.metadataReport = new FileSystemMetadataReportFactory().getMetadataReport(REPORT_SERVER_URL); - } - - @AfterEach - public void reset() throws Exception { - ApplicationModel.reset(); - this.metadataReport.close(); - } - - /** - * Test {@link MetadataReport#storeProviderMetadata(MetadataIdentifier, ServiceDefinition)} and - * {@link MetadataReport#getServiceDefinition(MetadataIdentifier)} - */ - @Test - public void testStoreProviderMetadataAndGetServiceDefinition() { - MetadataIdentifier metadataIdentifier = new MetadataIdentifier(BASE_URL); - ServiceDefinition serviceDefinition = ServiceDefinitionBuilder.buildFullDefinition(INTERFACE_CLASS, BASE_URL.getParameters()); - metadataReport.storeProviderMetadata(metadataIdentifier, serviceDefinition); - String serviceDefinitionJSON = metadataReport.getServiceDefinition(metadataIdentifier); - assertEquals(serviceDefinitionJSON, new Gson().toJson(serviceDefinition)); - } - - /** - * Test {@link MetadataReport#storeConsumerMetadata(MetadataIdentifier, Map)} and - * {@link MetadataReport#getServiceDefinition(MetadataIdentifier)} - */ - @Test - public void testStoreConsumerMetadata() { - MetadataIdentifier metadataIdentifier = new MetadataIdentifier(BASE_URL); - metadataReport.storeConsumerMetadata(metadataIdentifier, BASE_URL.getParameters()); - String parametersJSON = metadataReport.getServiceDefinition(metadataIdentifier); - assertEquals(parametersJSON, new Gson().toJson(BASE_URL.getParameters())); - } - - /** - * Test {@link MetadataReport#saveServiceMetadata(ServiceMetadataIdentifier, URL)} and - * {@link MetadataReport#removeServiceMetadata(ServiceMetadataIdentifier)} - */ - @Test - public void testSaveServiceMetadataAndRemoveServiceMetadata() { - ServiceMetadataIdentifier metadataIdentifier = new ServiceMetadataIdentifier(BASE_URL); - metadataReport.saveServiceMetadata(metadataIdentifier, BASE_URL); - String metadata = metadataReport.getMetadata(metadataIdentifier); - assertEquals(URL.encode(BASE_URL.toFullString()), metadata); - metadataReport.removeServiceMetadata(metadataIdentifier); - assertNull(metadataReport.getMetadata(metadataIdentifier)); - } - - /** - * Test {@link MetadataReport#saveSubscribedData(SubscriberMetadataIdentifier, Collection)} and - * {@link MetadataReport#getSubscribedURLs(SubscriberMetadataIdentifier)} - */ - @Test - public void testSaveSubscribedDataAndGetSubscribedURLs() { - SubscriberMetadataIdentifier metadataIdentifier = new SubscriberMetadataIdentifier(BASE_URL); - Set<String> urls = singleton(BASE_URL).stream().map(URL::toIdentityString).collect(toSet()); - metadataReport.saveSubscribedData(metadataIdentifier, urls); - Collection<String> subscribedURLs = metadataReport.getSubscribedURLs(metadataIdentifier); - assertEquals(1, subscribedURLs.size()); - assertEquals(urls, subscribedURLs); - } - - /** - * Test {@link MetadataReport#saveExportedURLs(SortedSet)}, - * {@link MetadataReport#getExportedURLsContent(String, String)} and - * {@link MetadataReport#getExportedURLs(String, String)} - */ - @Test - public void testSaveExportedURLsAndGetExportedURLs() { - SortedSet<String> urls = singleton(BASE_URL).stream().map(URL::toIdentityString).collect(TreeSet::new, Set::add, Set::addAll); - metadataReport.saveExportedURLs(urls); - - URLRevisionResolver urlRevisionResolver = URLRevisionResolver.INSTANCE; - String revision = urlRevisionResolver.resolve(urls); - assertEquals(urls, metadataReport.getExportedURLs(APP_NAME, revision)); - } -} diff --git a/dubbo-metadata/dubbo-metadata-report-zookeeper/src/main/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReport.java b/dubbo-metadata/dubbo-metadata-report-zookeeper/src/main/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReport.java index 3a74f8d..9ca2e42 100644 --- a/dubbo-metadata/dubbo-metadata-report-zookeeper/src/main/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReport.java +++ b/dubbo-metadata/dubbo-metadata-report-zookeeper/src/main/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReport.java @@ -19,7 +19,6 @@ package org.apache.dubbo.metadata.store.zookeeper; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.PathUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.metadata.report.identifier.BaseMetadataIdentifier; import org.apache.dubbo.metadata.report.identifier.KeyTypeEnum; @@ -27,7 +26,6 @@ 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.metadata.report.support.AbstractMetadataReport; -import org.apache.dubbo.metadata.report.support.ConfigCenterBasedMetadataReport; import org.apache.dubbo.remoting.zookeeper.ZookeeperClient; import org.apache.dubbo.remoting.zookeeper.ZookeeperTransporter; @@ -38,15 +36,10 @@ import java.util.List; import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PATH_SEPARATOR; -import static org.apache.dubbo.metadata.MetadataConstants.DEFAULT_PATH_TAG; -import static org.apache.dubbo.metadata.MetadataConstants.EXPORTED_URLS_TAG; /** * ZookeeperMetadataReport - * - * @deprecated 2.7.8 This class will be removed in the future, {@link ConfigCenterBasedMetadataReport} as a substitute. */ -@Deprecated public class ZookeeperMetadataReport extends AbstractMetadataReport { private final static Logger logger = LoggerFactory.getLogger(ZookeeperMetadataReport.class); @@ -127,26 +120,4 @@ public class ZookeeperMetadataReport extends AbstractMetadataReport { return toRootDir() + metadataIdentifier.getUniqueKey(KeyTypeEnum.PATH); } - @Override - public boolean saveExportedURLs(String serviceName, String exportedServicesRevision, String exportedURLsContent) { - String path = buildExportedURLsMetadataPath(serviceName, exportedServicesRevision); - zkClient.create(path, exportedURLsContent, false); - return true; - } - - @Override - public String getExportedURLsContent(String serviceName, String exportedServicesRevision) { - String path = buildExportedURLsMetadataPath(serviceName, exportedServicesRevision); - String content = zkClient.getContent(path); - return content; - } - - private String buildExportedURLsMetadataPath(String serviceName, String exportedServicesRevision) { - return buildPath(DEFAULT_PATH_TAG, EXPORTED_URLS_TAG, serviceName, exportedServicesRevision); - } - - private String buildPath(String... paths) { - return PathUtils.buildPath(toRootDir(), paths); - } - } diff --git a/dubbo-metadata/dubbo-metadata-report-zookeeper/src/main/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReportFactory.java b/dubbo-metadata/dubbo-metadata-report-zookeeper/src/main/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReportFactory.java index 4773e1e..0ffed8d 100644 --- a/dubbo-metadata/dubbo-metadata-report-zookeeper/src/main/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReportFactory.java +++ b/dubbo-metadata/dubbo-metadata-report-zookeeper/src/main/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReportFactory.java @@ -16,17 +16,25 @@ */ package org.apache.dubbo.metadata.store.zookeeper; -import org.apache.dubbo.metadata.report.identifier.KeyTypeEnum; -import org.apache.dubbo.metadata.report.support.ConfigCenterBasedMetadataReportFactory; +import org.apache.dubbo.common.URL; +import org.apache.dubbo.metadata.report.MetadataReport; +import org.apache.dubbo.metadata.report.support.AbstractMetadataReportFactory; +import org.apache.dubbo.remoting.zookeeper.ZookeeperTransporter; /** * ZookeeperRegistryFactory. - * - * @revised 2.7.8 {@link ConfigCenterBasedMetadataReportFactory} */ -public class ZookeeperMetadataReportFactory extends ConfigCenterBasedMetadataReportFactory { +public class ZookeeperMetadataReportFactory extends AbstractMetadataReportFactory { + + private ZookeeperTransporter zookeeperTransporter; - public ZookeeperMetadataReportFactory() { - super(KeyTypeEnum.PATH); + public void setZookeeperTransporter(ZookeeperTransporter zookeeperTransporter) { + this.zookeeperTransporter = zookeeperTransporter; } + + @Override + public MetadataReport createMetadataReport(URL url) { + return new ZookeeperMetadataReport(url, zookeeperTransporter); + } + } diff --git a/dubbo-metadata/pom.xml b/dubbo-metadata/pom.xml index 17ceada..a112779 100644 --- a/dubbo-metadata/pom.xml +++ b/dubbo-metadata/pom.xml @@ -29,13 +29,13 @@ <packaging>pom</packaging> <modules> <module>dubbo-metadata-api</module> - <module>dubbo-metadata-definition-protobuf</module> + <!-- <module>dubbo-metadata-definition-protobuf</module>--> <module>dubbo-metadata-report-zookeeper</module> - <module>dubbo-metadata-report-redis</module> - <module>dubbo-metadata-report-consul</module> - <module>dubbo-metadata-report-etcd</module> - <module>dubbo-metadata-report-nacos</module> - <module>dubbo-metadata-processor</module> + <!-- <module>dubbo-metadata-report-redis</module>--> + <!-- <module>dubbo-metadata-report-consul</module>--> + <!-- <module>dubbo-metadata-report-etcd</module>--> + <!-- <module>dubbo-metadata-report-nacos</module>--> + <!-- <module>dubbo-metadata-processor</module>--> </modules> </project> diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java index 7d4d48e..8e5a800 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java @@ -53,7 +53,7 @@ import static java.util.Collections.unmodifiableSet; import static java.util.stream.Collectors.toSet; import static java.util.stream.Stream.of; import static org.apache.dubbo.common.constants.CommonConstants.DUBBO; -import static org.apache.dubbo.common.constants.CommonConstants.GROUP_CHAR_SEPERATOR; +import static org.apache.dubbo.common.constants.CommonConstants.GROUP_CHAR_SEPARATOR; 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.MAPPING_KEY; @@ -318,7 +318,7 @@ public class ServiceDiscoveryRegistry implements Registry { List<ServiceInstance> serviceInstances = serviceDiscovery.getInstances(serviceName); serviceListener.onEvent(new ServiceInstancesChangedEvent(serviceName, serviceInstances)); }); - listener.notify(serviceListener.getUrls(url.getServiceKey() + GROUP_CHAR_SEPERATOR + url.getParameter(PROTOCOL_KEY, DUBBO))); + listener.notify(serviceListener.getUrls(url.getServiceKey() + GROUP_CHAR_SEPARATOR + url.getParameter(PROTOCOL_KEY, DUBBO))); serviceListener.addListener(url.getProtocolServiceKey(), listener); registerServiceInstancesChangedListener(url, serviceListener); diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/proxy/CompositeMetadataServiceProxyFactory.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/proxy/CompositeMetadataServiceProxyFactory.java deleted file mode 100644 index f8c95d6..0000000 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/proxy/CompositeMetadataServiceProxyFactory.java +++ /dev/null @@ -1,133 +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.metadata.proxy; - -import org.apache.dubbo.common.logger.Logger; -import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.metadata.MetadataService; -import org.apache.dubbo.registry.client.ServiceInstance; - -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Method; -import java.util.List; -import java.util.Objects; -import java.util.stream.Collectors; - -import static java.lang.String.format; -import static java.lang.reflect.Proxy.newProxyInstance; -import static org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader; - -/** - * The composite implementation of {@link MetadataServiceProxyFactory} - * - * @since 2.7.8 - */ -public class CompositeMetadataServiceProxyFactory extends BaseMetadataServiceProxyFactory { - - private static final Logger logger = LoggerFactory.getLogger(CompositeMetadataServiceProxyFactory.class); - - @Override - public MetadataService createProxy(ServiceInstance serviceInstance) { - MetadataService metadataService = (MetadataService) newProxyInstance( - getClass().getClassLoader(), - new Class[]{MetadataService.class}, - new MetadataServiceInvocationHandler(serviceInstance, this) - ); - return metadataService; - } - - static class MetadataServiceInvocationHandler implements InvocationHandler { - - private final ServiceInstance serviceInstance; - - private final MetadataServiceProxyFactory excluded; - - private volatile List<MetadataService> metadataServices; - - MetadataServiceInvocationHandler(ServiceInstance serviceInstance, - MetadataServiceProxyFactory excluded) { - this.serviceInstance = serviceInstance; - this.excluded = excluded; - } - - private List<MetadataService> loadMetadataServices() { - return getExtensionLoader(MetadataServiceProxyFactory.class) - .getSupportedExtensionInstances() - .stream() - .filter(this::isRequiredFactory) - .map(this::getProxy) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - } - - private List<MetadataService> getMetadataServices() { - if (metadataServices == null) { - metadataServices = loadMetadataServices(); - if (metadataServices.isEmpty()) { - throw new IllegalStateException(format("No valid proxy of %s can't be loaded.", - MetadataService.class.getName())); - } - } - return metadataServices; - } - - private boolean isRequiredFactory(MetadataServiceProxyFactory factory) { - return !factory.equals(excluded); - } - - private MetadataService getProxy(MetadataServiceProxyFactory factory) { - MetadataService metadataService = null; - try { - metadataService = factory.getProxy(serviceInstance); - } catch (Exception e) { - if (logger.isErrorEnabled()) { - logger.error(format("The proxy of %s can't be gotten by %s [from : %s].", - MetadataService.class.getName(), - factory.getClass().getName(), - serviceInstance.toString())); - } - } - return metadataService; - } - - @Override - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { - - if (Object.class.equals(method.getDeclaringClass())) { - return method.invoke(proxy, args); - } - - Object result = null; - - for (MetadataService metadataService : getMetadataServices()) { - try { - result = method.invoke(metadataService, args); - if (result != null) { - break; - } - } catch (Exception e) { - if (logger.isErrorEnabled()) { - logger.error(format("MetadataService[type : %s] executes failed.", metadataService.getClass().getName()), e); - } - } - } - - return result; - } - } - -} diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/proxy/BaseMetadataServiceProxyFactoryTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/proxy/BaseMetadataServiceProxyFactoryTest.java deleted file mode 100644 index 6aca4c6..0000000 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/proxy/BaseMetadataServiceProxyFactoryTest.java +++ /dev/null @@ -1,78 +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.metadata.proxy; - -import org.apache.dubbo.metadata.MetadataService; -import org.apache.dubbo.registry.client.DefaultServiceInstance; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.util.HashMap; -import java.util.Map; - -import static java.lang.String.valueOf; -import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.EXPORTED_SERVICES_REVISION_PROPERTY_NAME; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotSame; -import static org.junit.jupiter.api.Assertions.assertSame; - -/** - * {@link BaseMetadataServiceProxyFactory} Test-Cases - * - * @since 2.7.8 - */ -public class BaseMetadataServiceProxyFactoryTest { - - private MyMetadataServiceProxyFactory factory; - - private DefaultServiceInstance instance; - - @BeforeEach - public void init() { - factory = new MyMetadataServiceProxyFactory(); - instance = createServiceInstance(); - } - - private DefaultServiceInstance createServiceInstance() { - DefaultServiceInstance serviceInstance = new DefaultServiceInstance(valueOf(System.nanoTime()), "A", "127.0.0.1", 8080); - Map<String, String> metadata = new HashMap<>(); - metadata.put(EXPORTED_SERVICES_REVISION_PROPERTY_NAME, "X"); - serviceInstance.setMetadata(metadata); - return serviceInstance; - } - - @Test - public void testCreateProxyCacheKey() { - assertEquals("A#X", factory.createProxyCacheKey(instance)); - } - - @Test - public void testCreateProxy() { - MetadataService metadataService = factory.createProxy(instance); - MetadataService metadataService2 = factory.createProxy(instance); - assertNotSame(metadataService, metadataService2); - } - - @Test - public void testGetProxy() { - MetadataService metadataService = factory.getProxy(instance); - MetadataService metadataService2 = factory.getProxy(instance); - assertSame(metadataService, metadataService2); - } - -} diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/proxy/CompositeMetadataServiceProxyFactoryTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/proxy/CompositeMetadataServiceProxyFactoryTest.java deleted file mode 100644 index ddd78e0..0000000 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/proxy/CompositeMetadataServiceProxyFactoryTest.java +++ /dev/null @@ -1,96 +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.metadata.proxy; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.config.ApplicationConfig; -import org.apache.dubbo.metadata.MetadataService; -import org.apache.dubbo.metadata.report.MetadataReportInstance; -import org.apache.dubbo.registry.client.DefaultServiceInstance; -import org.apache.dubbo.rpc.model.ApplicationModel; - -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.util.HashMap; -import java.util.Map; -import java.util.SortedSet; - -import static java.lang.String.valueOf; -import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.COMPOSITE_METADATA_STORAGE_TYPE; -import static org.apache.dubbo.metadata.report.support.Constants.SYNC_REPORT_KEY; -import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.EXPORTED_SERVICES_REVISION_PROPERTY_NAME; -import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.METADATA_SERVICE_URL_PARAMS_PROPERTY_NAME; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertTrue; - -/** - * {@link CompositeMetadataServiceProxyFactory} Test-Cases - * - * @since 2.7.8 - */ -public class CompositeMetadataServiceProxyFactoryTest { - - private static final URL METADATA_REPORT_URL = URL.valueOf("file://") - .addParameter(APPLICATION_KEY, "test") - .addParameter(SYNC_REPORT_KEY, "true"); - - private static final String APP_NAME = "test-service"; - - private MetadataServiceProxyFactory factory; - - private DefaultServiceInstance instance; - - @BeforeEach - public void init() { - ApplicationModel.getConfigManager().setApplication(new ApplicationConfig(APP_NAME)); - MetadataReportInstance.init(METADATA_REPORT_URL); - factory = MetadataServiceProxyFactory.getExtension(COMPOSITE_METADATA_STORAGE_TYPE); - instance = createServiceInstance(); - } - - @AfterEach - public void reset() throws Exception { - ApplicationModel.reset(); - MetadataReportInstance.getMetadataReport().close(); - } - - private DefaultServiceInstance createServiceInstance() { - DefaultServiceInstance serviceInstance = new DefaultServiceInstance(valueOf(System.nanoTime()), "A", "127.0.0.1", 8080); - Map<String, String> metadata = new HashMap<>(); - metadata.put(EXPORTED_SERVICES_REVISION_PROPERTY_NAME, "X"); - metadata.put(METADATA_SERVICE_URL_PARAMS_PROPERTY_NAME, "{\"dubbo\":{\"application\":\"dubbo-provider-demo\",\"deprecated\":\"false\",\"group\":\"dubbo-provider-demo\",\"version\":\"1.0.0\",\"timestamp\":\"1564845042651\",\"dubbo\":\"2.0.2\",\"host\":\"192.168.0.102\",\"port\":\"20880\"}}"); - serviceInstance.setMetadata(metadata); - return serviceInstance; - } - - @Test - public void testGetProxy() { - MetadataService metadataService = factory.getProxy(instance); - MetadataService metadataService2 = factory.getProxy(instance); - assertSame(metadataService, metadataService2); - } - - @Test - public void testGetExportedURLs() { - MetadataService metadataService = factory.getProxy(instance); - SortedSet<String> exportedURLs = metadataService.getExportedURLs(); - assertTrue(exportedURLs.isEmpty()); - } -} diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/proxy/MetadataServiceProxyFactoryTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/proxy/MetadataServiceProxyFactoryTest.java deleted file mode 100644 index 2c31c22..0000000 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/proxy/MetadataServiceProxyFactoryTest.java +++ /dev/null @@ -1,49 +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.metadata.proxy; - -import org.junit.jupiter.api.Test; - -import static org.apache.dubbo.common.constants.CommonConstants.COMPOSITE_METADATA_STORAGE_TYPE; -import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_METADATA_STORAGE_TYPE; -import static org.apache.dubbo.common.constants.CommonConstants.REMOTE_METADATA_STORAGE_TYPE; -import static org.apache.dubbo.registry.client.metadata.proxy.MetadataServiceProxyFactory.getDefaultExtension; -import static org.apache.dubbo.registry.client.metadata.proxy.MetadataServiceProxyFactory.getExtension; -import static org.junit.jupiter.api.Assertions.assertEquals; - -/** - * {@link MetadataServiceProxyFactory} Test-Cases - * - * @since 2.7.8 - */ -public class MetadataServiceProxyFactoryTest { - - @Test - public void testExtension() { - MetadataServiceProxyFactory defaultFactory = getDefaultExtension(); - MetadataServiceProxyFactory factory = getExtension(DEFAULT_METADATA_STORAGE_TYPE); - assertEquals(defaultFactory, factory); - - assertEquals(MyMetadataServiceProxyFactory.class, factory.getClass()); - - factory = getExtension(REMOTE_METADATA_STORAGE_TYPE); - assertEquals(RemoteMetadataServiceProxyFactory.class, factory.getClass()); - - factory = getExtension(COMPOSITE_METADATA_STORAGE_TYPE); - assertEquals(CompositeMetadataServiceProxyFactory.class, factory.getClass()); - } -} diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/proxy/MyMetadataServiceProxyFactory.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/proxy/MyMetadataServiceProxyFactory.java deleted file mode 100644 index 86247e1..0000000 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/proxy/MyMetadataServiceProxyFactory.java +++ /dev/null @@ -1,29 +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.metadata.proxy; - -import org.apache.dubbo.metadata.MetadataService; -import org.apache.dubbo.metadata.store.InMemoryWritableMetadataService; -import org.apache.dubbo.registry.client.ServiceInstance; - -public class MyMetadataServiceProxyFactory extends BaseMetadataServiceProxyFactory { - - @Override - protected MetadataService createProxy(ServiceInstance serviceInstance) { - return new InMemoryWritableMetadataService(); - } -}
