diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/store/RemoteMetadataServiceImpl.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/store/RemoteMetadataServiceImpl.java index 41806caebbd..ae4d3164c5a 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/store/RemoteMetadataServiceImpl.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/store/RemoteMetadataServiceImpl.java @@ -100,8 +100,8 @@ public MetadataInfo getMetadata(ServiceInstance instance) { metadataReport = getMetadataReports().entrySet().iterator().next().getValue(); } Map params = new HashMap<>(instance.getExtendParams()); - if (instance.getRegistryCluster() != null && !instance.getRegistryCluster().equalsIgnoreCase(params.get(REGISTRY_CLUSTER_KEY))) { - params.put(REGISTRY_CLUSTER_KEY, instance.getRegistryCluster()); + if (registryCluster != null && !registryCluster.equalsIgnoreCase(params.get(REGISTRY_CLUSTER_KEY))) { + params.put(REGISTRY_CLUSTER_KEY, registryCluster); } return metadataReport.getAppMetadata(identifier, params); } diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/event/listener/ServiceInstancesChangedListenerTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/event/listener/ServiceInstancesChangedListenerTest.java index 8ae003e9582..042a071a04a 100644 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/event/listener/ServiceInstancesChangedListenerTest.java +++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/event/listener/ServiceInstancesChangedListenerTest.java @@ -501,7 +501,6 @@ static List buildInstances(List rawURls) { String rawURL = (String)obj; DefaultServiceInstance instance = new DefaultServiceInstance(); final URL dubboUrl = URL.valueOf(rawURL); - instance = new DefaultServiceInstance(); instance.setRawAddress(rawURL); instance.setHost(dubboUrl.getHost()); instance.setEnabled(true); diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/MetadataServiceURLParamsMetadataCustomizerTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/MetadataServiceURLParamsMetadataCustomizerTest.java index 2ea0e4fd39f..f3ea39d680f 100644 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/MetadataServiceURLParamsMetadataCustomizerTest.java +++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/MetadataServiceURLParamsMetadataCustomizerTest.java @@ -34,7 +34,12 @@ import java.io.IOException; import java.util.Map; -import static org.apache.dubbo.common.constants.CommonConstants.*; +import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.PORT_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.TIMESTAMP_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY; import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.METADATA_SERVICE_URL_PARAMS_PROPERTY_NAME; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/store/MockMetadataReport.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/store/MockMetadataReport.java deleted file mode 100644 index a266e3c989a..00000000000 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/store/MockMetadataReport.java +++ /dev/null @@ -1,119 +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.store; - -import com.google.gson.Gson; -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.MetadataInfo; -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 org.apache.dubbo.metadata.report.support.AbstractMetadataReport; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -/** - * ZookeeperRegistry - */ -public class MockMetadataReport extends AbstractMetadataReport { - - private final static Logger logger = LoggerFactory.getLogger(MockMetadataReport.class); - - private Gson gson = new Gson(); - - private Map reversion2MetadataIdentifier = new HashMap<>(); - - public MockMetadataReport(URL url) { - super(url); - } - - public volatile Map store = new ConcurrentHashMap<>(); - - - private static String getProtocol(URL url) { - String protocol = url.getSide(); - protocol = protocol == null ? url.getProtocol() : protocol; - return protocol; - } - - @Override - protected void doStoreProviderMetadata(MetadataIdentifier providerMetadataIdentifier, String serviceDefinitions) { - store.put(providerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), serviceDefinitions); - } - - @Override - protected void doStoreConsumerMetadata(MetadataIdentifier consumerMetadataIdentifier, String serviceParameterString) { - store.put(consumerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), serviceParameterString); - } - - @Override - protected void doSaveMetadata(ServiceMetadataIdentifier metadataIdentifier, URL url) { - store.put(metadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), url.toFullString()); - } - - @Override - protected void doRemoveMetadata(ServiceMetadataIdentifier metadataIdentifier) { - store.remove(metadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY)); - } - - @Override - protected List doGetExportedURLs(ServiceMetadataIdentifier metadataIdentifier) { - return Arrays.asList(store.getOrDefault(metadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), "")); - } - - @Override - protected void doSaveSubscriberData(SubscriberMetadataIdentifier subscriberMetadataIdentifier, String urls) { - store.put(subscriberMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), urls); - } - - @Override - protected String doGetSubscribedURLs(SubscriberMetadataIdentifier metadataIdentifier) { - throw new UnsupportedOperationException("This extension does not support working as a remote metadata center."); - } - - public static String getProviderKey(URL url) { - return new MetadataIdentifier(url).getUniqueKey(KeyTypeEnum.UNIQUE_KEY); - } - - public static String getConsumerKey(URL url) { - return new MetadataIdentifier(url).getUniqueKey(KeyTypeEnum.UNIQUE_KEY); - } - - @Override - public String getServiceDefinition(MetadataIdentifier consumerMetadataIdentifier) { - return store.get(consumerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY)); - } - - @Override - public void publishAppMetadata(SubscriberMetadataIdentifier identifier, MetadataInfo metadataInfo) { - String content = gson.toJson(metadataInfo); - reversion2MetadataIdentifier.put(identifier.getRevision(), content); - } - - @Override - public MetadataInfo getAppMetadata(SubscriberMetadataIdentifier identifier, Map instanceMetadata) { - String content = reversion2MetadataIdentifier.get(identifier.getRevision()); - return gson.fromJson(content,MetadataInfo.class); - } -} diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/store/MockMetadataReportFactory.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/store/MockMetadataReportFactory.java deleted file mode 100644 index 29bba50042f..00000000000 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/store/MockMetadataReportFactory.java +++ /dev/null @@ -1,28 +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.store; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.metadata.report.MetadataReport; -import org.apache.dubbo.metadata.report.support.AbstractMetadataReportFactory; - -public class MockMetadataReportFactory extends AbstractMetadataReportFactory { - @Override - protected MetadataReport createMetadataReport(URL url) { - return new MockMetadataReport(url); - } -} diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/store/RemoteMetadataServiceImplTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/store/RemoteMetadataServiceImplTest.java index 9743f91497e..a7e7f8543f3 100644 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/store/RemoteMetadataServiceImplTest.java +++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/store/RemoteMetadataServiceImplTest.java @@ -17,54 +17,75 @@ package org.apache.dubbo.registry.client.metadata.store; import org.apache.dubbo.common.URL; -import org.apache.dubbo.config.ApplicationConfig; -import org.apache.dubbo.config.MetadataReportConfig; +import org.apache.dubbo.common.beans.factory.ScopeBeanFactory; import org.apache.dubbo.metadata.MetadataInfo; 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 org.apache.dubbo.registry.client.DefaultServiceInstance; -import org.apache.dubbo.registry.client.ServiceInstance; import org.apache.dubbo.rpc.model.ApplicationModel; -import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.stubbing.Answer; +import java.util.HashMap; import java.util.Map; import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.EXPORTED_SERVICES_REVISION_PROPERTY_NAME; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.anyBoolean; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; public class RemoteMetadataServiceImplTest { - private static RemoteMetadataServiceImpl remoteMetadataService; - private static ApplicationModel scopeModel; + private static final String REGISTRY_CLUSTER = "registry9103"; + private static final String SERVICE_NAME = "A"; + private RemoteMetadataServiceImpl remoteMetadataService; + private MetadataReport metadataReport; + private MetadataInfo metadataInfo; + private String reversion; + + @BeforeEach + public void setUp() { + ApplicationModel applicationModel = spy(ApplicationModel.defaultModel()); + ScopeBeanFactory beanFactory = mock(ScopeBeanFactory.class); + MetadataReportInstance metadataReportInstance = mock(MetadataReportInstance.class); + metadataReport = mock(MetadataReport.class); + + Map clusterToMetadataReport = new HashMap<>(); + clusterToMetadataReport.put(REGISTRY_CLUSTER, metadataReport); + when(metadataReportInstance.getMetadataReports(anyBoolean())).thenReturn(clusterToMetadataReport); + when(applicationModel.getBeanFactory()).thenReturn(beanFactory); + when(beanFactory.getBean(MetadataReportInstance.class)).thenReturn(metadataReportInstance); + + metadataInfo = new MetadataInfo(); + URL url = URL.valueOf("dubbo://30.225.21.30:20880/org.apache.dubbo.registry.service.DemoService"); + metadataInfo.addService(new MetadataInfo.ServiceInfo(url)); + reversion = metadataInfo.calAndGetRevision(); + Map clusterToMetadataInfo = new HashMap<>(); + clusterToMetadataInfo.put(REGISTRY_CLUSTER, metadataInfo); + + WritableMetadataService writableMetadataService = mock(WritableMetadataService.class); + when(applicationModel.getDefaultExtension(WritableMetadataService.class)).thenReturn(writableMetadataService); + when(writableMetadataService.getMetadataInfos()).thenReturn(clusterToMetadataInfo); + when(metadataReport.getAppMetadata(any(),any())).thenAnswer((Answer) invocationOnMock -> { + SubscriberMetadataIdentifier identifier = invocationOnMock.getArgument(0, SubscriberMetadataIdentifier.class); + if (SERVICE_NAME.equals(identifier.getApplication()) && reversion.equals(identifier.getRevision())) { + return metadataInfo; + } + return null; + }); - @BeforeAll - public static void setUp() { - ApplicationConfig applicationConfig = new ApplicationConfig(); - applicationConfig.setName("A"); - scopeModel = ApplicationModel.defaultModel(); - scopeModel.getApplicationConfigManager().setApplication(applicationConfig); - - URL url = URL.valueOf("metadata://127.0.0.1:20880/TestService?version=1.0.0&metadata=mock&sync.report=true"); - MetadataReportConfig metadataReportConfig = mock(MetadataReportConfig.class); - when(metadataReportConfig.getApplicationModel()).thenReturn(scopeModel); - when(metadataReportConfig.toUrl()).thenReturn(url); - when(metadataReportConfig.getScopeModel()).thenReturn(scopeModel); - when(metadataReportConfig.getRegistry()).thenReturn(REGISTRY_CLUSTER); - MetadataReportInstance metadataReportInstance = scopeModel.getBeanFactory().getBean(MetadataReportInstance.class); - metadataReportInstance.init(metadataReportConfig); remoteMetadataService = new RemoteMetadataServiceImpl(); - remoteMetadataService.setScopeModel(scopeModel); - } - - @AfterAll - public static void clearUp() { - ApplicationModel.reset(); + remoteMetadataService.setScopeModel(applicationModel); } @Test @@ -73,37 +94,41 @@ public void testPublishAndGetMetadata() { // test getMetadataReports Map metadataReports = remoteMetadataService.getMetadataReports(); Assertions.assertTrue(metadataReports.containsKey(REGISTRY_CLUSTER)); - Assertions.assertTrue(metadataReports.get(REGISTRY_CLUSTER) instanceof MockMetadataReport); // test publishMetadata - WritableMetadataService writableMetadataService = scopeModel.getDefaultExtension(WritableMetadataService.class); - URL url = URL.valueOf("dubbo://30.225.21.30:20880/org.apache.dubbo.registry.service.DemoService?REGISTRY_CLUSTER=" + REGISTRY_CLUSTER); - writableMetadataService.exportURL(url); - remoteMetadataService.publishMetadata("A"); + remoteMetadataService.publishMetadata(SERVICE_NAME); + + ArgumentCaptor identifierArgumentCaptor = ArgumentCaptor.forClass(SubscriberMetadataIdentifier.class); + ArgumentCaptor metadataInfoArgumentCaptor = ArgumentCaptor.forClass(MetadataInfo.class); + verify(metadataReport, times(1)).publishAppMetadata(identifierArgumentCaptor.capture(), metadataInfoArgumentCaptor.capture()); + SubscriberMetadataIdentifier identifier = identifierArgumentCaptor.getValue(); + Assertions.assertEquals(identifier.getRevision(), reversion); + Assertions.assertEquals(identifier.getApplication(), SERVICE_NAME); // test getMetadata - ServiceInstance serviceInstance = new DefaultServiceInstance(); + DefaultServiceInstance serviceInstance = new DefaultServiceInstance(SERVICE_NAME, "127.0.0.1", 20880, ApplicationModel.defaultModel()); serviceInstance.setRegistryCluster(REGISTRY_CLUSTER); - Map metadataInfos = writableMetadataService.getMetadataInfos(); - MetadataInfo localMetadataInfo = metadataInfos.get(REGISTRY_CLUSTER); - String revision = localMetadataInfo.calAndGetRevision(); - serviceInstance.getMetadata().put(EXPORTED_SERVICES_REVISION_PROPERTY_NAME, revision); + serviceInstance.getMetadata().put(EXPORTED_SERVICES_REVISION_PROPERTY_NAME, reversion); MetadataInfo remoteMetadataInfo = remoteMetadataService.getMetadata(serviceInstance); - Assertions.assertEquals(localMetadataInfo, remoteMetadataInfo); + Assertions.assertEquals(remoteMetadataInfo, metadataInfo); + + serviceInstance.setServiceName("FAIL_SERVICE_NAME"); + remoteMetadataInfo = remoteMetadataService.getMetadata(serviceInstance); + Assertions.assertNull(remoteMetadataInfo); } @Test public void testPublishServiceDefinition() { + // test provider publishServiceDefinition URL providerURL = URL.valueOf("dubbo://127.0.0.1:8888/org.apache.dubbo.registry.service.DemoService?side=provider"); remoteMetadataService.publishServiceDefinition(providerURL); + verify(metadataReport, times(1)).storeProviderMetadata(any(),any()); + // test consumer publishServiceDefinition URL consumerURL = URL.valueOf("dubbo://127.0.0.1:8888/org.apache.dubbo.registry.service.DemoService?side=consumer"); remoteMetadataService.publishServiceDefinition(consumerURL); - - Map metadataReports = remoteMetadataService.getMetadataReports(); - MockMetadataReport metadataReport = (MockMetadataReport) metadataReports.get(REGISTRY_CLUSTER); - Assertions.assertEquals(metadataReport.store.size(),2); + verify(metadataReport, times(1)).storeConsumerMetadata(any(),any()); } } diff --git a/dubbo-registry/dubbo-registry-api/src/test/resources/META-INF/dubbo/org.apache.dubbo.metadata.report.MetadataReportFactory b/dubbo-registry/dubbo-registry-api/src/test/resources/META-INF/dubbo/org.apache.dubbo.metadata.report.MetadataReportFactory deleted file mode 100644 index 598102d2b7f..00000000000 --- a/dubbo-registry/dubbo-registry-api/src/test/resources/META-INF/dubbo/org.apache.dubbo.metadata.report.MetadataReportFactory +++ /dev/null @@ -1 +0,0 @@ -mock=org.apache.dubbo.registry.client.metadata.store.MockMetadataReportFactory