diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java index 138a68e130c..f03d49ffc2d 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java @@ -34,7 +34,6 @@ import org.apache.dubbo.config.utils.ConfigValidationUtils; import org.apache.dubbo.event.Event; import org.apache.dubbo.event.EventDispatcher; -import org.apache.dubbo.metadata.WritableMetadataService; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.Protocol; import org.apache.dubbo.rpc.ProxyFactory; @@ -62,20 +61,16 @@ import static org.apache.dubbo.common.constants.CommonConstants.CLUSTER_KEY; import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SEPARATOR; import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SIDE; -import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_METADATA_STORAGE_TYPE; import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY; import static org.apache.dubbo.common.constants.CommonConstants.LOCALHOST_VALUE; -import static org.apache.dubbo.common.constants.CommonConstants.METADATA_KEY; import static org.apache.dubbo.common.constants.CommonConstants.METHODS_KEY; import static org.apache.dubbo.common.constants.CommonConstants.MONITOR_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PROXY_CLASS_REF; -import static org.apache.dubbo.common.constants.CommonConstants.REMOTE_METADATA_STORAGE_TYPE; import static org.apache.dubbo.common.constants.CommonConstants.REVISION_KEY; import static org.apache.dubbo.common.constants.CommonConstants.SEMICOLON_SPLIT_PATTERN; import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY; import static org.apache.dubbo.common.utils.NetUtils.isInvalidLocalHost; import static org.apache.dubbo.config.Constants.DUBBO_IP_TO_REGISTRY; -import static org.apache.dubbo.registry.Constants.CONSUMER_PROTOCOL; import static org.apache.dubbo.registry.Constants.REGISTER_IP_KEY; import static org.apache.dubbo.rpc.Constants.LOCAL_PROTOCOL; import static org.apache.dubbo.rpc.cluster.Constants.REFER_KEY; @@ -221,10 +216,6 @@ public synchronized void init() { // appendParameters(map, consumer, Constants.DEFAULT_KEY); AbstractConfig.appendParameters(map, consumer); AbstractConfig.appendParameters(map, this); - MetadataReportConfig metadataReportConfig = getMetadataReportConfig(); - if (metadataReportConfig != null && metadataReportConfig.isValid()) { - map.putIfAbsent(METADATA_KEY, REMOTE_METADATA_STORAGE_TYPE); - } Map attributes = null; if (CollectionUtils.isNotEmpty(getMethods())) { attributes = new HashMap<>(); @@ -352,16 +343,6 @@ private T createProxy(Map map) { if (logger.isInfoEnabled()) { logger.info("Refer dubbo service " + interfaceClass.getName() + " from url " + invoker.getUrl()); } - /** - * @since 2.7.0 - * ServiceData Store - */ - String metadata = map.get(METADATA_KEY); - WritableMetadataService metadataService = WritableMetadataService.getExtension(metadata == null ? DEFAULT_METADATA_STORAGE_TYPE : metadata); - if (metadataService != null) { - URL consumerURL = new URL(CONSUMER_PROTOCOL, map.remove(REGISTER_IP_KEY), 0, map.get(INTERFACE_KEY), map); - metadataService.publishServiceDefinition(consumerURL); - } // create service proxy return (T) PROXY_FACTORY.getProxy(invoker, ProtocolUtils.isGeneric(generic)); } @@ -466,7 +447,7 @@ public void setBootstrap(DubboBootstrap bootstrap) { } private void postProcessConfig() { - List configPostProcessors =ExtensionLoader.getExtensionLoader(ConfigPostProcessor.class) + List configPostProcessors = ExtensionLoader.getExtensionLoader(ConfigPostProcessor.class) .getActivateExtension(URL.valueOf("configPostProcessor://"), (String[]) null); configPostProcessors.forEach(component -> component.postProcessReferConfig(this)); } diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java index 3ad4545f382..c2d4a85f429 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java @@ -37,7 +37,6 @@ import org.apache.dubbo.config.utils.ConfigValidationUtils; import org.apache.dubbo.event.Event; import org.apache.dubbo.event.EventDispatcher; -import org.apache.dubbo.metadata.WritableMetadataService; import org.apache.dubbo.rpc.Exporter; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.Protocol; @@ -68,16 +67,13 @@ import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_KEY; import static org.apache.dubbo.common.constants.CommonConstants.ANY_VALUE; -import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_METADATA_STORAGE_TYPE; import static org.apache.dubbo.common.constants.CommonConstants.DUBBO; import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_IP_TO_BIND; import static org.apache.dubbo.common.constants.CommonConstants.LOCALHOST_VALUE; -import static org.apache.dubbo.common.constants.CommonConstants.METADATA_KEY; import static org.apache.dubbo.common.constants.CommonConstants.METHODS_KEY; import static org.apache.dubbo.common.constants.CommonConstants.MONITOR_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE; import static org.apache.dubbo.common.constants.CommonConstants.REGISTER_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.REMOTE_METADATA_STORAGE_TYPE; import static org.apache.dubbo.common.constants.CommonConstants.REVISION_KEY; import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY; import static org.apache.dubbo.common.constants.RegistryConstants.DYNAMIC_KEY; @@ -344,10 +340,6 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List r AbstractConfig.appendParameters(map, provider); AbstractConfig.appendParameters(map, protocolConfig); AbstractConfig.appendParameters(map, this); - MetadataReportConfig metadataReportConfig = getMetadataReportConfig(); - if (metadataReportConfig != null && metadataReportConfig.isValid()) { - map.putIfAbsent(METADATA_KEY, REMOTE_METADATA_STORAGE_TYPE); - } if (CollectionUtils.isNotEmpty(getMethods())) { for (MethodConfig method : getMethods()) { AbstractConfig.appendParameters(map, method, method.getName()); @@ -502,14 +494,6 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List r Exporter exporter = PROTOCOL.export(wrapperInvoker); exporters.add(exporter); } - /** - * @since 2.7.0 - * ServiceData Store - */ - WritableMetadataService metadataService = WritableMetadataService.getExtension(url.getParameter(METADATA_KEY, DEFAULT_METADATA_STORAGE_TYPE)); - if (metadataService != null) { - metadataService.publishServiceDefinition(url); - } } } this.urls.add(url); diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/event/listener/PublishingServiceDefinitionListener.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/event/listener/PublishingServiceDefinitionListener.java new file mode 100644 index 00000000000..9731034c70a --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/event/listener/PublishingServiceDefinitionListener.java @@ -0,0 +1,74 @@ +/* + * 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.event.listener; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.config.AbstractInterfaceConfig; +import org.apache.dubbo.config.ApplicationConfig; +import org.apache.dubbo.config.MetadataReportConfig; +import org.apache.dubbo.config.event.ReferenceConfigInitializedEvent; +import org.apache.dubbo.config.event.ServiceConfigExportedEvent; +import org.apache.dubbo.event.EventListener; +import org.apache.dubbo.event.GenericEventListener; +import org.apache.dubbo.metadata.WritableMetadataService; + +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.metadata.WritableMetadataService.getExtension; + +/** + * An {@link EventListener} {@link WritableMetadataService#publishServiceDefinition(URL) publishs the service definition} + * when {@link ServiceConfigExportedEvent the event of the exported Dubbo service} and + * {@link ReferenceConfigInitializedEvent the event of the referenced Dubbo service} is raised. + * + * @see GenericEventListener + * @see ServiceConfigExportedEvent + * @see ReferenceConfigInitializedEvent + * @since 2.7.8 + */ +public class PublishingServiceDefinitionListener extends GenericEventListener { + + public void onEvent(ReferenceConfigInitializedEvent event) { + handleEvent(event.getReferenceConfig()); + } + + public void onEvent(ServiceConfigExportedEvent event) { + handleEvent(event.getServiceConfig()); + } + + private void handleEvent(AbstractInterfaceConfig config) { + String metadataType = getMetadataType(config); + for (URL exportedURL : config.getExportedUrls()) { + WritableMetadataService metadataService = getExtension(metadataType); + if (metadataService != null) { + metadataService.publishServiceDefinition(exportedURL); + } + } + } + + private String getMetadataType(AbstractInterfaceConfig config) { + ApplicationConfig applicationConfig = config.getApplication(); + String metadataType = applicationConfig.getMetadataType(); + if (REMOTE_METADATA_STORAGE_TYPE.equals(metadataType)) { + MetadataReportConfig metadataReportConfig = config.getMetadataReportConfig(); + if (metadataReportConfig == null || !metadataReportConfig.isValid()) { + metadataType = DEFAULT_METADATA_STORAGE_TYPE; + } + } + return metadataType; + } +} diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/utils/ConfigValidationUtils.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/utils/ConfigValidationUtils.java index f9b0e3512c4..ba8b9a77f80 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/utils/ConfigValidationUtils.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/utils/ConfigValidationUtils.java @@ -297,6 +297,7 @@ public static void validateAbstractInterfaceConfig(AbstractInterfaceConfig confi checkExtension(ProxyFactory.class, PROXY_KEY, config.getProxy()); checkExtension(Cluster.class, CLUSTER_KEY, config.getCluster()); checkMultiExtension(Filter.class, FILE_KEY, config.getFilter()); + checkMultiExtension(InvokerListener.class, LISTENER_KEY, config.getListener()); checkNameHasSymbol(LAYER_KEY, config.getLayer()); List methods = config.getMethods(); @@ -311,7 +312,7 @@ public static void validateServiceConfig(ServiceConfig config) { checkName(TOKEN_KEY, config.getToken()); checkPathName(PATH_KEY, config.getPath()); - checkMultiExtension(ExporterListener.class, LISTENER_KEY, config.getListener()); + checkMultiExtension(ExporterListener.class, "listener", config.getListener()); validateAbstractInterfaceConfig(config); @@ -336,7 +337,7 @@ public static void validateServiceConfig(ServiceConfig config) { } public static void validateReferenceConfig(ReferenceConfig config) { - checkMultiExtension(InvokerListener.class, LISTENER_KEY, config.getListener()); + checkMultiExtension(InvokerListener.class, "listener", config.getListener()); checkKey(VERSION_KEY, config.getVersion()); checkKey(GROUP_KEY, config.getGroup()); checkName(CLIENT_KEY, config.getClient()); diff --git a/dubbo-config/dubbo-config-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.event.EventListener b/dubbo-config/dubbo-config-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.event.EventListener index db73041bd43..b1946c09f30 100644 --- a/dubbo-config/dubbo-config-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.event.EventListener +++ b/dubbo-config/dubbo-config-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.event.EventListener @@ -1,2 +1,4 @@ service-mapping=org.apache.dubbo.config.event.listener.ServiceNameMappingListener -config-logging=org.apache.dubbo.config.event.listener.LoggingEventListener \ No newline at end of file +config-logging=org.apache.dubbo.config.event.listener.LoggingEventListener +# since 2.7.8 +publishing-service-definition=org.apache.dubbo.config.event.listener.PublishingServiceDefinitionListener \ No newline at end of file diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/DynamicConfigurationServiceNameMapping.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/DynamicConfigurationServiceNameMapping.java index d84eba02b65..b03148aba29 100644 --- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/DynamicConfigurationServiceNameMapping.java +++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/DynamicConfigurationServiceNameMapping.java @@ -27,6 +27,7 @@ import static java.lang.String.valueOf; import static java.util.Arrays.asList; +import static org.apache.dubbo.common.utils.CollectionUtils.isNotEmpty; import static org.apache.dubbo.common.utils.StringUtils.SLASH; import static org.apache.dubbo.rpc.model.ApplicationModel.getName; @@ -72,7 +73,9 @@ public Set get(String serviceInterface, String group, String version, St Set serviceNames = new LinkedHashSet<>(); execute(() -> { Set keys = dynamicConfiguration.getConfigKeys(buildGroup(serviceInterface, group, version, protocol)); - serviceNames.addAll(keys); + if (isNotEmpty(keys)) { + serviceNames.addAll(keys); + } }); return Collections.unmodifiableSet(serviceNames); }