This is an automated email from the ASF dual-hosted git repository.

albumenj pushed a commit to branch 3.3-config-api-refactor
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.3-config-api-refactor by 
this push:
     new 9eb83d16bb Refactor service metadata export process (#12990)
9eb83d16bb is described below

commit 9eb83d16bb867998984da58ca84436aa6e75f11b
Author: namelessssssssssss 
<[email protected]>
AuthorDate: Mon Sep 11 11:15:27 2023 +0800

    Refactor service metadata export process (#12990)
    
    * * Migrate service metadata export process to MetadataPublisher
    
    * * Fix ut
    
    * * Move metadata API out of dubbo-config-api
    
    * * Move metadata API spi file out of dubbo-config-api
    
    * * Merge remote branch
    
    * * Merge remote branch
    
    * * Add license
    
    * Add license
    
    * Update MetadataPublisherRegister.java
    
    * Update DefaultMetadataPublisher.java
    
    * Update MetadataServiceExporterTest.java
    
    * * Bug fix
    
    * Update MetadataPublisher.java
    
    * * Remove unnecessary SPI for MetadataPublisher
    
    * * Update pom
    
    * * Move spi file back
    
    * * Add MetadataPublisherRegister
    
    * * Add license
    
    * Update MetadataPublisherRegister.java
    
    * * Bug fix
---
 .../apache/dubbo/metadata/MetadataPublisher.java   |  32 +++++++
 .../org/apache/dubbo/config/ReferenceConfig.java   |   8 +-
 .../org/apache/dubbo/config/ServiceConfig.java     |  13 ++-
 .../metadata/MetadataServiceExporterTest.java      |   6 +-
 .../dubbo/metadata/MetadataPublisherRegister.java  |  44 +++++++++
 .../metadata/report/DefaultMetadataPublisher.java  | 100 +++++++++++++++++++++
 ...e.dubbo.common.deploy.ApplicationDeployListener |   1 +
 .../registry/client/metadata/MetadataUtils.java    |  60 -------------
 8 files changed, 196 insertions(+), 68 deletions(-)

diff --git 
a/dubbo-common/src/main/java/org/apache/dubbo/metadata/MetadataPublisher.java 
b/dubbo-common/src/main/java/org/apache/dubbo/metadata/MetadataPublisher.java
new file mode 100644
index 0000000000..3011f458d0
--- /dev/null
+++ 
b/dubbo-common/src/main/java/org/apache/dubbo/metadata/MetadataPublisher.java
@@ -0,0 +1,32 @@
+/*
+ * 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.rpc.model.ApplicationModel;
+import org.apache.dubbo.rpc.model.ServiceDescriptor;
+
+/**
+ * Metadata publisher.
+ */
+public interface MetadataPublisher {
+
+    /**
+     * Publish a service metadata to all available MetadataReport.
+     */
+    void publishServiceDefinition(URL url, ServiceDescriptor 
serviceDescriptor, ApplicationModel applicationModel);
+}
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 924b1a9cb8..a194968afd 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,7 @@ import org.apache.dubbo.common.utils.UrlUtils;
 import org.apache.dubbo.config.annotation.Reference;
 import org.apache.dubbo.config.support.Parameter;
 import org.apache.dubbo.config.utils.ConfigValidationUtils;
-import org.apache.dubbo.registry.client.metadata.MetadataUtils;
+import org.apache.dubbo.metadata.MetadataPublisher;
 import org.apache.dubbo.rpc.Invoker;
 import org.apache.dubbo.rpc.Protocol;
 import org.apache.dubbo.rpc.ProxyFactory;
@@ -471,7 +471,11 @@ public class ReferenceConfig<T> extends 
ReferenceConfigBase<T> {
                 referenceParameters.get(INTERFACE_KEY), referenceParameters);
         consumerUrl = consumerUrl.setScopeModel(getScopeModel());
         consumerUrl = consumerUrl.setServiceModel(consumerModel);
-        MetadataUtils.publishServiceDefinition(consumerUrl, 
consumerModel.getServiceModel(), getApplicationModel());
+
+        MetadataPublisher metadataPublisher = 
getApplicationModel().getBeanFactory().getBean(MetadataPublisher.class);
+        if(metadataPublisher != null){
+            metadataPublisher.publishServiceDefinition(consumerUrl, 
consumerModel.getServiceModel(), getApplicationModel());
+        }
 
         // create service proxy
         return (T) proxyFactory.getProxy(invoker, 
ProtocolUtils.isGeneric(generic));
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 91dde23fda..954ac84b35 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,10 +37,10 @@ import org.apache.dubbo.config.annotation.Service;
 import org.apache.dubbo.config.invoker.DelegateProviderMetaDataInvoker;
 import org.apache.dubbo.config.support.Parameter;
 import org.apache.dubbo.config.utils.ConfigValidationUtils;
+import org.apache.dubbo.metadata.MetadataPublisher;
 import org.apache.dubbo.metadata.ServiceNameMapping;
 import org.apache.dubbo.metrics.event.MetricsEventBus;
 import org.apache.dubbo.metrics.event.MetricsInitEvent;
-import org.apache.dubbo.registry.client.metadata.MetadataUtils;
 import org.apache.dubbo.rpc.Exporter;
 import org.apache.dubbo.rpc.Invoker;
 import org.apache.dubbo.rpc.Protocol;
@@ -768,7 +768,7 @@ public class ServiceConfig<T> extends ServiceConfigBase<T> {
 
                 url = exportRemote(url, registryURLs, registerType);
                 if (!isGeneric(generic) && !getScopeModel().isInternal()) {
-                    MetadataUtils.publishServiceDefinition(url, 
providerModel.getServiceModel(), getApplicationModel());
+                    publishServiceDefinition(url);
                 }
 
                 if (StringUtils.isNotBlank(extProtocol)) {
@@ -783,7 +783,7 @@ public class ServiceConfig<T> extends ServiceConfigBase<T> {
                             build();
                         localUrl = exportRemote(localUrl, registryURLs, 
registerType);
                         if (!isGeneric(generic) && 
!getScopeModel().isInternal()) {
-                            MetadataUtils.publishServiceDefinition(localUrl, 
providerModel.getServiceModel(), getApplicationModel());
+                            publishServiceDefinition(url);
                         }
                         this.urls.add(localUrl);
                     }
@@ -1062,4 +1062,11 @@ public class ServiceConfig<T> extends 
ServiceConfigBase<T> {
     public Runnable getDestroyRunner() {
         return this::unexport;
     }
+
+    private void publishServiceDefinition(URL url){
+        MetadataPublisher metadataPublisher = 
getApplicationModel().getBeanFactory().getBean(MetadataPublisher.class);
+        if(metadataPublisher != null){
+            metadataPublisher.publishServiceDefinition(url, 
providerModel.getServiceModel(), getApplicationModel());
+        }
+    }
 }
diff --git 
a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/metadata/MetadataServiceExporterTest.java
 
b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/metadata/MetadataServiceExporterTest.java
index 93869ad63d..7e1b155bbc 100644
--- 
a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/metadata/MetadataServiceExporterTest.java
+++ 
b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/metadata/MetadataServiceExporterTest.java
@@ -25,8 +25,8 @@
 //import org.apache.dubbo.config.ServiceConfig;
 //import org.apache.dubbo.config.api.DemoService;
 //import org.apache.dubbo.config.bootstrap.DubboBootstrap;
-//import org.apache.dubbo.config.metadata.ConfigurableMetadataServiceExporter;
-//import org.apache.dubbo.config.metadata.ExporterDeployListener;
+//import org.apache.dubbo.metadata.deploy.ConfigurableMetadataServiceExporter;
+//import org.apache.dubbo.metadata.deploy.ExporterDeployListener;
 //import org.apache.dubbo.registry.client.metadata.MetadataServiceDelegation;
 //import org.apache.dubbo.config.provider.impl.DemoServiceImpl;
 //import org.apache.dubbo.rpc.model.ApplicationModel;
@@ -253,4 +253,4 @@
 //        return 
(ExporterDeployListener)model.getExtensionLoader(ApplicationDeployListener.class).getExtension("exporter");
 //    }
 //
-//}
\ No newline at end of file
+//}
diff --git 
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataPublisherRegister.java
 
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataPublisherRegister.java
new file mode 100644
index 0000000000..2910dfb1de
--- /dev/null
+++ 
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataPublisherRegister.java
@@ -0,0 +1,44 @@
+/*
+ * 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.deploy.ApplicationDeployListener;
+import org.apache.dubbo.metadata.report.DefaultMetadataPublisher;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+
+public class MetadataPublisherRegister implements ApplicationDeployListener {
+
+    @Override
+    public void onStarting(ApplicationModel scopeModel) {
+         
scopeModel.getBeanFactory().getOrRegisterBean(DefaultMetadataPublisher.class);
+    }
+
+    @Override
+    public void onInitialize(ApplicationModel scopeModel) {}
+
+    @Override
+    public void onStarted(ApplicationModel scopeModel) {}
+
+    @Override
+    public void onStopping(ApplicationModel scopeModel) {}
+
+    @Override
+    public void onStopped(ApplicationModel scopeModel) {}
+
+    @Override
+    public void onFailure(ApplicationModel scopeModel, Throwable cause) {}
+}
diff --git 
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/DefaultMetadataPublisher.java
 
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/DefaultMetadataPublisher.java
new file mode 100644
index 0000000000..d1c5836ad8
--- /dev/null
+++ 
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/DefaultMetadataPublisher.java
@@ -0,0 +1,100 @@
+/*
+ * 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;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
+import org.apache.dubbo.common.logger.LoggerFactory;
+import org.apache.dubbo.common.utils.StringUtils;
+import org.apache.dubbo.metadata.MetadataPublisher;
+import org.apache.dubbo.metadata.definition.model.FullServiceDefinition;
+import org.apache.dubbo.metadata.report.identifier.MetadataIdentifier;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+import org.apache.dubbo.rpc.model.ServiceDescriptor;
+
+import java.util.Map;
+
+import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SIDE;
+import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE;
+import static 
org.apache.dubbo.common.constants.LoggerCodeConstants.REGISTRY_FAILED_CREATE_INSTANCE;
+import static 
org.apache.dubbo.common.constants.LoggerCodeConstants.REGISTRY_FAILED_LOAD_METADATA;
+
+public class DefaultMetadataPublisher implements MetadataPublisher {
+
+    private static final ErrorTypeAwareLogger logger = 
LoggerFactory.getErrorTypeAwareLogger(DefaultMetadataPublisher.class);
+    @Override
+    public void publishServiceDefinition(URL url, ServiceDescriptor 
serviceDescriptor, ApplicationModel applicationModel) {
+        if (getMetadataReports(applicationModel).isEmpty()) {
+            String msg = "Remote Metadata Report Server is not provided or 
unavailable, will stop registering service definition to remote center!";
+            logger.warn(REGISTRY_FAILED_LOAD_METADATA, "", "", msg);
+            return;
+        }
+
+        try {
+            String side = url.getSide();
+            if (PROVIDER_SIDE.equalsIgnoreCase(side)) {
+                String serviceKey = url.getServiceKey();
+                FullServiceDefinition serviceDefinition = 
serviceDescriptor.getFullServiceDefinition(serviceKey);
+
+                if (StringUtils.isNotEmpty(serviceKey) && serviceDefinition != 
null) {
+                    serviceDefinition.setParameters(url.getParameters());
+                    for (Map.Entry<String, MetadataReport> entry : 
getMetadataReports(applicationModel).entrySet()) {
+                        MetadataReport metadataReport = entry.getValue();
+                        if (!metadataReport.shouldReportDefinition()) {
+                            logger.info("Report of service definition is 
disabled for " + entry.getKey());
+                            continue;
+                        }
+                        metadataReport.storeProviderMetadata(
+                                new MetadataIdentifier(
+                                        url.getServiceInterface(),
+                                        url.getVersion() == null ? "" : 
url.getVersion(),
+                                        url.getGroup() == null ? "" : 
url.getGroup(),
+                                        PROVIDER_SIDE,
+                                        applicationModel.getApplicationName())
+                                , serviceDefinition);
+                    }
+                }
+            } else {
+                for (Map.Entry<String, MetadataReport> entry : 
getMetadataReports(applicationModel).entrySet()) {
+                    MetadataReport metadataReport = entry.getValue();
+                    if (!metadataReport.shouldReportDefinition()) {
+                        logger.info("Report of service definition is disabled 
for " + entry.getKey());
+                        continue;
+                    }
+                    metadataReport.storeConsumerMetadata(
+                            new MetadataIdentifier(
+                                    url.getServiceInterface(),
+                                    url.getVersion() == null ? "" : 
url.getVersion(),
+                                    url.getGroup() == null ? "" : 
url.getGroup(),
+                                    CONSUMER_SIDE,
+                                    applicationModel.getApplicationName()),
+                            url.getParameters());
+                }
+            }
+        } catch (Exception e) {
+            //ignore error
+            logger.error(REGISTRY_FAILED_CREATE_INSTANCE, "", "", "publish 
service definition metadata error.", e);
+        }
+    }
+
+
+    private static Map<String, MetadataReport> 
getMetadataReports(ApplicationModel applicationModel) {
+        return 
applicationModel.getBeanFactory().getBean(MetadataReportInstance.class).getMetadataReports(false);
+    }
+
+
+}
diff --git 
a/dubbo-metadata/dubbo-metadata-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.deploy.ApplicationDeployListener
 
b/dubbo-metadata/dubbo-metadata-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.deploy.ApplicationDeployListener
new file mode 100644
index 0000000000..e769b99d1b
--- /dev/null
+++ 
b/dubbo-metadata/dubbo-metadata-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.deploy.ApplicationDeployListener
@@ -0,0 +1 @@
+metadata-register=org.apache.dubbo.metadata.MetadataPublisherRegister
diff --git 
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataUtils.java
 
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataUtils.java
index 398f5cc55f..33a0ba4ee5 100644
--- 
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataUtils.java
+++ 
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataUtils.java
@@ -25,10 +25,8 @@ import org.apache.dubbo.common.utils.CollectionUtils;
 import org.apache.dubbo.common.utils.StringUtils;
 import org.apache.dubbo.metadata.MetadataInfo;
 import org.apache.dubbo.metadata.MetadataService;
-import org.apache.dubbo.metadata.definition.model.FullServiceDefinition;
 import org.apache.dubbo.metadata.report.MetadataReport;
 import org.apache.dubbo.metadata.report.MetadataReportInstance;
-import org.apache.dubbo.metadata.report.identifier.MetadataIdentifier;
 import 
org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier;
 import org.apache.dubbo.registry.client.ServiceInstance;
 import org.apache.dubbo.rpc.Invoker;
@@ -37,7 +35,6 @@ import org.apache.dubbo.rpc.ProxyFactory;
 import org.apache.dubbo.rpc.model.ApplicationModel;
 import org.apache.dubbo.rpc.model.ConsumerModel;
 import org.apache.dubbo.rpc.model.ModuleModel;
-import org.apache.dubbo.rpc.model.ServiceDescriptor;
 import org.apache.dubbo.rpc.service.Destroyable;
 
 import java.util.HashMap;
@@ -45,11 +42,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ThreadLocalRandom;
 
-import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SIDE;
-import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE;
 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.LoggerCodeConstants.REGISTRY_FAILED_CREATE_INSTANCE;
 import static 
org.apache.dubbo.common.constants.LoggerCodeConstants.REGISTRY_FAILED_LOAD_METADATA;
 import static 
org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_CLUSTER_KEY;
 import static 
org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.METADATA_SERVICE_URLS_PROPERTY_NAME;
@@ -57,60 +51,6 @@ import static 
org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataU
 public class MetadataUtils {
     public static final ErrorTypeAwareLogger logger = 
LoggerFactory.getErrorTypeAwareLogger(MetadataUtils.class);
 
-    public static void publishServiceDefinition(URL url, ServiceDescriptor 
serviceDescriptor, ApplicationModel applicationModel) {
-        if (getMetadataReports(applicationModel).size() == 0) {
-            String msg = "Remote Metadata Report Server is not provided or 
unavailable, will stop registering service definition to remote center!";
-            logger.warn(REGISTRY_FAILED_LOAD_METADATA, "", "", msg);
-            return;
-        }
-
-        try {
-            String side = url.getSide();
-            if (PROVIDER_SIDE.equalsIgnoreCase(side)) {
-                String serviceKey = url.getServiceKey();
-                FullServiceDefinition serviceDefinition = 
serviceDescriptor.getFullServiceDefinition(serviceKey);
-
-                if (StringUtils.isNotEmpty(serviceKey) && serviceDefinition != 
null) {
-                    serviceDefinition.setParameters(url.getParameters());
-                    for (Map.Entry<String, MetadataReport> entry : 
getMetadataReports(applicationModel).entrySet()) {
-                        MetadataReport metadataReport = entry.getValue();
-                        if (!metadataReport.shouldReportDefinition()) {
-                            logger.info("Report of service definition is 
disabled for " + entry.getKey());
-                            continue;
-                        }
-                        metadataReport.storeProviderMetadata(
-                            new MetadataIdentifier(
-                                url.getServiceInterface(),
-                                url.getVersion() == null ? "" : 
url.getVersion(),
-                                url.getGroup() == null ? "" : url.getGroup(),
-                                PROVIDER_SIDE,
-                                applicationModel.getApplicationName())
-                            , serviceDefinition);
-                    }
-                }
-            } else {
-                for (Map.Entry<String, MetadataReport> entry : 
getMetadataReports(applicationModel).entrySet()) {
-                    MetadataReport metadataReport = entry.getValue();
-                    if (!metadataReport.shouldReportDefinition()) {
-                        logger.info("Report of service definition is disabled 
for " + entry.getKey());
-                        continue;
-                    }
-                    metadataReport.storeConsumerMetadata(
-                        new MetadataIdentifier(
-                            url.getServiceInterface(),
-                            url.getVersion() == null ? "" : url.getVersion(),
-                            url.getGroup() == null ? "" : url.getGroup(),
-                            CONSUMER_SIDE,
-                            applicationModel.getApplicationName()),
-                        url.getParameters());
-                }
-            }
-        } catch (Exception e) {
-            //ignore error
-            logger.error(REGISTRY_FAILED_CREATE_INSTANCE, "", "", "publish 
service definition metadata error.", e);
-        }
-    }
-
     public static ProxyHolder referProxy(ServiceInstance instance) {
         MetadataServiceURLBuilder builder;
         ExtensionLoader<MetadataServiceURLBuilder> loader = 
instance.getApplicationModel()

Reply via email to