You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2021/09/01 07:28:04 UTC

[dubbo] branch 3.0 updated: test: Support checking metadata service in multiple registry center (#8606)

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


The following commit(s) were added to refs/heads/3.0 by this push:
     new 17042b4  test: Support checking metadata service in multiple registry center (#8606)
17042b4 is described below

commit 17042b4ed0f6e79f2f423f107fc69fb71286cda3
Author: Xiong, Pin <pi...@foxmail.com>
AuthorDate: Wed Sep 1 02:27:21 2021 -0500

    test: Support checking metadata service in multiple registry center (#8606)
---
 ...gistryCenterExportMetadataExporterListener.java |  34 ++++
 ...egistryCenterExportMetadataIntegrationTest.java | 207 +++++++++++++++++++++
 ...ultipleRegistryCenterExportMetadataService.java |  28 +++
 ...pleRegistryCenterExportMetadataServiceImpl.java |  31 +++
 ...egistryCenterExportMetadataServiceListener.java |  35 ++++
 .../org.apache.dubbo.config.ServiceListener        |   1 +
 .../services/org.apache.dubbo.rpc.ExporterListener |   1 +
 .../registrycenter/AbstractRegistryCenter.java     |   4 +-
 8 files changed, 339 insertions(+), 2 deletions(-)

diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/integration/multiple/exportmetadata/MultipleRegistryCenterExportMetadataExporterListener.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/integration/multiple/exportmetadata/MultipleRegistryCenterExportMetadataExporterListener.java
new file mode 100644
index 0000000..796a5e4
--- /dev/null
+++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/integration/multiple/exportmetadata/MultipleRegistryCenterExportMetadataExporterListener.java
@@ -0,0 +1,34 @@
+/*
+ * 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.integration.multiple.exportmetadata;
+
+import org.apache.dubbo.common.constants.CommonConstants;
+import org.apache.dubbo.common.extension.Activate;
+import org.apache.dubbo.integration.AbstractRegistryCenterExporterListener;
+import org.apache.dubbo.metadata.MetadataService;
+
+@Activate(group = CommonConstants.PROVIDER, order = 1000)
+public class MultipleRegistryCenterExportMetadataExporterListener extends AbstractRegistryCenterExporterListener {
+
+    /**
+     * Returns the interface of exported service.
+     */
+    @Override
+    protected Class<?> getInterface() {
+        return MetadataService.class;
+    }
+}
diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/integration/multiple/exportmetadata/MultipleRegistryCenterExportMetadataIntegrationTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/integration/multiple/exportmetadata/MultipleRegistryCenterExportMetadataIntegrationTest.java
new file mode 100644
index 0000000..958d558
--- /dev/null
+++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/integration/multiple/exportmetadata/MultipleRegistryCenterExportMetadataIntegrationTest.java
@@ -0,0 +1,207 @@
+/*
+ * 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.integration.multiple.exportmetadata;
+
+import org.apache.dubbo.common.extension.ExtensionLoader;
+import org.apache.dubbo.config.RegistryConfig;
+import org.apache.dubbo.config.ServiceConfig;
+import org.apache.dubbo.config.ApplicationConfig;
+import org.apache.dubbo.config.ProtocolConfig;
+import org.apache.dubbo.config.ServiceListener;
+import org.apache.dubbo.config.bootstrap.DubboBootstrap;
+import org.apache.dubbo.integration.IntegrationTest;
+import org.apache.dubbo.metadata.MetadataService;
+import org.apache.dubbo.registrycenter.RegistryCenter;
+import org.apache.dubbo.registrycenter.ZookeeperMultipleRegistryCenter;
+import org.apache.dubbo.rpc.Exporter;
+import org.apache.dubbo.rpc.ExporterListener;
+import org.apache.dubbo.rpc.Filter;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+
+import static org.apache.dubbo.rpc.Constants.SCOPE_LOCAL;
+
+/**
+ * The testcases are only for checking the process of exporting metadata service.
+ */
+public class MultipleRegistryCenterExportMetadataIntegrationTest implements IntegrationTest {
+
+    private static final Logger logger = LoggerFactory.getLogger(MultipleRegistryCenterExportMetadataIntegrationTest.class);
+
+    /**
+     * Define the provider application name.
+     */
+    private static String PROVIDER_APPLICATION_NAME = "multiple-registry-center-export-metadata";
+
+    /**
+     * The name for getting the specified instance, which is loaded using SPI.
+     */
+    private static String SPI_NAME = "multipleConfigCenterExportMetadata";
+
+    /**
+     * Define the protocol's name.
+     */
+    private static String PROTOCOL_NAME = "injvm";
+    /**
+     * Define the {@link ServiceConfig} instance.
+     */
+    private ServiceConfig<MultipleRegistryCenterExportMetadataService> serviceConfig;
+
+    /**
+     * The listener to record exported services
+     */
+    private MultipleRegistryCenterExportMetadataServiceListener serviceListener;
+
+    /**
+     * The listener to record exported exporters.
+     */
+    private MultipleRegistryCenterExportMetadataExporterListener exporterListener;
+
+    /**
+     * Define a registry center.
+     */
+    private RegistryCenter registryCenter;
+
+    @BeforeEach
+    public void setUp() throws Exception {
+        logger.info(getClass().getSimpleName() + " testcase is beginning...");
+        DubboBootstrap.reset();
+        registryCenter = new ZookeeperMultipleRegistryCenter();
+        registryCenter.startup();
+        // initialize service config
+        serviceConfig = new ServiceConfig<>();
+        serviceConfig.setInterface(MultipleRegistryCenterExportMetadataService.class);
+        serviceConfig.setRef(new MultipleRegistryCenterExportMetadataServiceImpl());
+        serviceConfig.setAsync(false);
+        serviceConfig.setScope(SCOPE_LOCAL);
+
+        // initailize bootstrap
+        DubboBootstrap.getInstance()
+            .application(new ApplicationConfig(PROVIDER_APPLICATION_NAME))
+            .protocol(new ProtocolConfig(PROTOCOL_NAME))
+            .service(serviceConfig);
+        for (RegistryCenter.Instance instance : registryCenter.getRegistryCenterInstance()) {
+            DubboBootstrap.getInstance().registry(new RegistryConfig(String.format("%s://%s:%s",
+                instance.getType(),
+                instance.getHostname(),
+                instance.getPort())));
+        }
+    }
+
+    /**
+     * Define {@link ServiceListener}, {@link ExporterListener} and {@link Filter} for helping check.
+     * <p>Use SPI to load them before exporting.
+     * <p>After that, there are some checkpoints need to verify as follow:
+     * <ul>
+     *     <li>There is nothing in ServiceListener or not</li>
+     *     <li>There is nothing in ExporterListener or not</li>
+     *     <li>ServiceConfig is exported or not</li>
+     * </ul>
+     */
+    private void beforeExport() {
+        // ---------------initialize--------------- //
+        serviceListener = (MultipleRegistryCenterExportMetadataServiceListener) ExtensionLoader.getExtensionLoader(ServiceListener.class).getExtension(SPI_NAME);
+        exporterListener = (MultipleRegistryCenterExportMetadataExporterListener) ExtensionLoader.getExtensionLoader(ExporterListener.class).getExtension(SPI_NAME);
+
+        // ---------------checkpoints--------------- //
+        // There is nothing in ServiceListener
+        Assertions.assertTrue(serviceListener.getExportedServices().isEmpty());
+        // There is nothing in ExporterListener
+        Assertions.assertTrue(exporterListener.getExportedExporters().isEmpty());
+        // ServiceConfig isn't exported
+        Assertions.assertFalse(serviceConfig.isExported());
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Test
+    @Override
+    public void integrate() {
+        beforeExport();
+        DubboBootstrap.getInstance().start();
+        afterExport();
+    }
+
+    /**
+     * There are some checkpoints need to check after exported as follow:
+     * <ul>
+     *     <li>The metadata service is only one or not</li>
+     *     <li>The exported service is MetadataService or not</li>
+     *     <li>The MetadataService is exported or not</li>
+     *     <li>The exported exporters are right or not</li>
+     * </ul>
+     */
+    private void afterExport() {
+        // The metadata service is only one
+        Assertions.assertEquals(serviceListener.getExportedServices().size(), 1);
+        // The exported service is MetadataService
+        Assertions.assertEquals(serviceListener.getExportedServices().get(0).getInterfaceClass(),
+            MetadataService.class);
+        // The MetadataService is exported
+        Assertions.assertTrue(serviceListener.getExportedServices().get(0).isExported());
+        // FIXME there may be something wrong with the whole process of
+        //  registering service-discovery-registry.
+        //  So, all testcases may need to be modified.
+        // There are two exported exporters
+        // 1. Metadata Service exporter with dubbo protocol
+        // 2. MultipleRegistryCenterExportMetadataService exporter with Injvm protocol
+        Assertions.assertEquals(exporterListener.getExportedExporters().size(), 2);
+        // Obtain MultipleRegistryCenterExportMetadataService exporter with Injvm protocol
+        Exporter<?> injvmExporter = (Exporter<?>) exporterListener.getExportedExporters()
+            .stream()
+            .filter(
+                exporter -> PROTOCOL_NAME.equalsIgnoreCase(exporter.getInvoker().getUrl().getProtocol())
+            )
+            .findFirst()
+            .get();
+        // Obtain Metadata Service exporter with dubbo protocol
+        Exporter<?> metadataExporter = (Exporter<?>) exporterListener.getExportedExporters()
+            .stream()
+            .filter(
+                exporter -> !PROTOCOL_NAME.equalsIgnoreCase(exporter.getInvoker().getUrl().getProtocol())
+            )
+            .filter(
+                exporter -> exporter.getInvoker().getInterface().equals(MetadataService.class)
+            )
+            .findFirst()
+            .get();
+        // Make sure injvmExporter is not null
+        Assertions.assertNotNull(injvmExporter);
+        // Make sure metadataExporter is not null
+        Assertions.assertNotNull(metadataExporter);
+    }
+
+    @AfterEach
+    public void tearDown() throws IOException {
+        DubboBootstrap.reset();
+        PROVIDER_APPLICATION_NAME = null;
+        serviceConfig = null;
+        // The exported service has been unexported
+        Assertions.assertTrue(serviceListener.getExportedServices().isEmpty());
+        serviceListener = null;
+        logger.info(getClass().getSimpleName() + " testcase is ending...");
+        registryCenter.shutdown();
+        registryCenter = null;
+    }
+}
diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/integration/multiple/exportmetadata/MultipleRegistryCenterExportMetadataService.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/integration/multiple/exportmetadata/MultipleRegistryCenterExportMetadataService.java
new file mode 100644
index 0000000..5cd33e3
--- /dev/null
+++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/integration/multiple/exportmetadata/MultipleRegistryCenterExportMetadataService.java
@@ -0,0 +1,28 @@
+/*
+ * 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.integration.multiple.exportmetadata;
+
+/**
+ * This interface is used to check if the exported metadata service works well or not in multiple registry center.
+ */
+public interface MultipleRegistryCenterExportMetadataService {
+
+    /**
+     * The simple method for testing.
+     */
+    String hello(String name);
+}
diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/integration/multiple/exportmetadata/MultipleRegistryCenterExportMetadataServiceImpl.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/integration/multiple/exportmetadata/MultipleRegistryCenterExportMetadataServiceImpl.java
new file mode 100644
index 0000000..9836d74
--- /dev/null
+++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/integration/multiple/exportmetadata/MultipleRegistryCenterExportMetadataServiceImpl.java
@@ -0,0 +1,31 @@
+/*
+ * 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.integration.multiple.exportmetadata;
+
+/**
+ * The simple implementation for {@link MultipleRegistryCenterExportMetadataService}
+ */
+public class MultipleRegistryCenterExportMetadataServiceImpl implements MultipleRegistryCenterExportMetadataService {
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String hello(String name) {
+        return "Hello " + name;
+    }
+}
diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/integration/multiple/exportmetadata/MultipleRegistryCenterExportMetadataServiceListener.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/integration/multiple/exportmetadata/MultipleRegistryCenterExportMetadataServiceListener.java
new file mode 100644
index 0000000..13605be
--- /dev/null
+++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/integration/multiple/exportmetadata/MultipleRegistryCenterExportMetadataServiceListener.java
@@ -0,0 +1,35 @@
+/*
+ * 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.integration.multiple.exportmetadata;
+
+import org.apache.dubbo.config.ServiceListener;
+import org.apache.dubbo.integration.AbstractRegistryCenterServiceListener;
+import org.apache.dubbo.metadata.MetadataService;
+
+/**
+ * This implementation of {@link ServiceListener} is to record exported metadata services in multiple registry center.
+ */
+public class MultipleRegistryCenterExportMetadataServiceListener extends AbstractRegistryCenterServiceListener {
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    protected Class<?> getInterface() {
+        return MetadataService.class;
+    }
+}
diff --git a/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/org.apache.dubbo.config.ServiceListener b/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/org.apache.dubbo.config.ServiceListener
index cda278a..6aa34fa 100644
--- a/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/org.apache.dubbo.config.ServiceListener
+++ b/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/org.apache.dubbo.config.ServiceListener
@@ -4,4 +4,5 @@ singleConfigCenterInjvm=org.apache.dubbo.integration.single.injvm.SingleRegistry
 multipleConfigCenterInjvm=org.apache.dubbo.integration.multiple.injvm.MultipleRegistryCenterInjvmServiceListener
 singleConfigCenterExportProvider=org.apache.dubbo.integration.single.exportprovider.SingleRegistryCenterExportProviderServiceListener
 singleConfigCenterExportMetadata=org.apache.dubbo.integration.single.exportmetadata.SingleRegistryCenterExportMetadataServiceListener
+multipleConfigCenterExportMetadata=org.apache.dubbo.integration.multiple.exportmetadata.MultipleRegistryCenterExportMetadataServiceListener
 multipleConfigCenterExportProvider=org.apache.dubbo.integration.multiple.exportprovider.MultipleRegistryCenterExportProviderServiceListener
diff --git a/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/org.apache.dubbo.rpc.ExporterListener b/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/org.apache.dubbo.rpc.ExporterListener
index 7d03552..b76b514 100644
--- a/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/org.apache.dubbo.rpc.ExporterListener
+++ b/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/org.apache.dubbo.rpc.ExporterListener
@@ -3,4 +3,5 @@ singleConfigCenterInjvm=org.apache.dubbo.integration.single.injvm.SingleRegistry
 multipleConfigCenterInjvm=org.apache.dubbo.integration.multiple.injvm.MultipleRegistryCenterInjvmExporterListener
 singleConfigCenterExportProvider=org.apache.dubbo.integration.single.exportprovider.SingleRegistryCenterExportProviderExporterListener
 singleConfigCenterExportMetadata=org.apache.dubbo.integration.single.exportmetadata.SingleRegistryCenterExportMetadataExporterListener
+multipleConfigCenterExportMetadata=org.apache.dubbo.integration.multiple.exportmetadata.MultipleRegistryCenterExportMetadataExporterListener
 multipleConfigCenterExportProvider=org.apache.dubbo.integration.multiple.exportprovider.MultipleRegistryCenterExportProviderExporterListener
diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/registrycenter/AbstractRegistryCenter.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/registrycenter/AbstractRegistryCenter.java
index 5b64122..ba78896 100644
--- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/registrycenter/AbstractRegistryCenter.java
+++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/registrycenter/AbstractRegistryCenter.java
@@ -53,9 +53,9 @@ abstract class AbstractRegistryCenter implements RegistryCenter {
     private static final int DEFAULT_SERVER_ID = -1;
 
     /**
-     * The default tick time is 6000
+     * The default tick time is 10000
      */
-    private static final int DEFAULT_TICK_TIME = 6 * 1000;
+    private static final int DEFAULT_TICK_TIME = 10 * 1000;
 
     /**
      * The default value is -1.