From dd360f4f858922f9ca4c068ce8c07b9ba823a56b Mon Sep 17 00:00:00 2001 From: cvictory Date: Thu, 15 Aug 2019 11:23:30 +0800 Subject: [PATCH] metadata support for nacos, consul, etcd --- .../BaseApplicationMetadataIdentifier.java | 4 +- .../identifier/BaseMetadataIdentifier.java | 12 +++ .../BaseServiceMetadataIdentifier.java | 4 +- .../report/identifier/KeyTypeEnum.java | 8 ++ .../report/identifier/MetadataIdentifier.java | 8 +- .../identifier/ServiceMetadataIdentifier.java | 5 +- .../SubscriberMetadataIdentifier.java | 5 +- .../support/AbstractMetadataReport.java | 5 +- ...RemoteWritableMetadataServiceDelegate.java | 42 ++++------ .../identifier/MetadataIdentifierTest.java | 4 +- .../support/AbstractMetadataReportTest.java | 21 ++--- .../test/JTestMetadataReport4Test.java | 9 +- .../store/consul/ConsulMetadataReport.java | 55 ++++++++++-- .../store/etcd/EtcdMetadataReport.java | 28 +++++-- .../store/nacos/NacosMetadataReport.java | 48 +++++++++-- .../store/nacos/NacosMetadataReportTest.java | 5 +- .../store/redis/RedisMetadataReport.java | 84 ++++++++++++++++--- .../store/redis/RedisMetadataReportTest.java | 9 +- .../zookeeper/ZookeeperMetadataReport.java | 23 +++-- .../ZookeeperMetadataReportTest.java | 3 +- 20 files changed, 270 insertions(+), 112 deletions(-) create mode 100644 dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/BaseMetadataIdentifier.java create mode 100644 dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/KeyTypeEnum.java diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/BaseApplicationMetadataIdentifier.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/BaseApplicationMetadataIdentifier.java index 7844e6478ac..741d4dab3f1 100644 --- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/BaseApplicationMetadataIdentifier.java +++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/BaseApplicationMetadataIdentifier.java @@ -12,8 +12,8 @@ public class BaseApplicationMetadataIdentifier { String application; - String getUniqueKey(MetadataIdentifier.KeyTypeEnum keyType, String... params) { - if (keyType == MetadataIdentifier.KeyTypeEnum.PATH) { + String getUniqueKey(KeyTypeEnum keyType, String... params) { + if (keyType == KeyTypeEnum.PATH) { return getFilePathKey(params); } return getIdentifierKey(params); diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/BaseMetadataIdentifier.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/BaseMetadataIdentifier.java new file mode 100644 index 00000000000..5cb1b782b6b --- /dev/null +++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/BaseMetadataIdentifier.java @@ -0,0 +1,12 @@ +package org.apache.dubbo.metadata.report.identifier; + +/** + * @author cvictory ON 2019-08-15 + */ +public interface BaseMetadataIdentifier { + + String getUniqueKey(KeyTypeEnum keyType); + + String getIdentifierKey(); + +} diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/BaseServiceMetadataIdentifier.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/BaseServiceMetadataIdentifier.java index f4d9999549d..81edc1364d9 100644 --- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/BaseServiceMetadataIdentifier.java +++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/BaseServiceMetadataIdentifier.java @@ -19,8 +19,8 @@ public class BaseServiceMetadataIdentifier { String group; String side; - String getUniqueKey(MetadataIdentifier.KeyTypeEnum keyType, String... params) { - if (keyType == MetadataIdentifier.KeyTypeEnum.PATH) { + String getUniqueKey(KeyTypeEnum keyType, String... params) { + if (keyType == KeyTypeEnum.PATH) { return getFilePathKey(params); } return getIdentifierKey(params); diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/KeyTypeEnum.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/KeyTypeEnum.java new file mode 100644 index 00000000000..41069aadad1 --- /dev/null +++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/KeyTypeEnum.java @@ -0,0 +1,8 @@ +package org.apache.dubbo.metadata.report.identifier; + +/** + * 2019-08-15 + */ +public enum KeyTypeEnum { + PATH, UNIQUE_KEY +} diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/MetadataIdentifier.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/MetadataIdentifier.java index 335e3f07e1b..ce7542f2ec1 100644 --- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/MetadataIdentifier.java +++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/MetadataIdentifier.java @@ -22,14 +22,15 @@ import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY; import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY; -import static org.apache.dubbo.metadata.MetadataConstants.KEY_SEPARATOR; /** * The MetadataIdentifier is used to store method descriptor. *

+ * The name of class is reserved because of it has been used in the previous version. + *

* 2018/10/25 */ -public class MetadataIdentifier extends BaseServiceMetadataIdentifier { +public class MetadataIdentifier extends BaseServiceMetadataIdentifier implements BaseMetadataIdentifier { private String application; @@ -101,7 +102,4 @@ public void setApplication(String application) { this.application = application; } - public enum KeyTypeEnum { - PATH, UNIQUE_KEY - } } diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/ServiceMetadataIdentifier.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/ServiceMetadataIdentifier.java index 65c7b4eebd3..a8cd55b8293 100644 --- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/ServiceMetadataIdentifier.java +++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/ServiceMetadataIdentifier.java @@ -3,7 +3,6 @@ import org.apache.dubbo.common.URL; import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; -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.CommonConstants.VERSION_KEY; import static org.apache.dubbo.metadata.MetadataConstants.KEY_REVISON_PREFIX; @@ -14,7 +13,7 @@ *

* 2019-08-09 */ -public class ServiceMetadataIdentifier extends BaseServiceMetadataIdentifier { +public class ServiceMetadataIdentifier extends BaseServiceMetadataIdentifier implements BaseMetadataIdentifier { private String revision; private String protocol; @@ -40,7 +39,7 @@ public ServiceMetadataIdentifier(URL url) { this.protocol = url.getProtocol(); } - public String getUniqueKey(MetadataIdentifier.KeyTypeEnum keyType) { + public String getUniqueKey(KeyTypeEnum keyType) { return super.getUniqueKey(keyType, protocol, KEY_REVISON_PREFIX + revision); } diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/SubscriberMetadataIdentifier.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/SubscriberMetadataIdentifier.java index a227f1e590a..69bed30c067 100644 --- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/SubscriberMetadataIdentifier.java +++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/SubscriberMetadataIdentifier.java @@ -6,10 +6,9 @@ import static org.apache.dubbo.common.constants.CommonConstants.REVISION_KEY; /** - * * 2019-08-12 */ -public class SubscriberMetadataIdentifier extends BaseApplicationMetadataIdentifier{ +public class SubscriberMetadataIdentifier extends BaseApplicationMetadataIdentifier implements BaseMetadataIdentifier { private String revision; @@ -27,7 +26,7 @@ public SubscriberMetadataIdentifier(URL url) { this.revision = url.getParameter(REVISION_KEY, ""); } - public String getUniqueKey(MetadataIdentifier.KeyTypeEnum keyType) { + public String getUniqueKey(KeyTypeEnum keyType) { return super.getUniqueKey(keyType, revision); } 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 7f46eefa8ef..71f75321cf1 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 @@ -25,6 +25,7 @@ import org.apache.dubbo.metadata.definition.model.FullServiceDefinition; import org.apache.dubbo.metadata.definition.model.ServiceDefinition; import org.apache.dubbo.metadata.report.MetadataReport; +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; @@ -200,9 +201,9 @@ private void saveProperties(MetadataIdentifier metadataIdentifier, String value, try { if (add) { - properties.setProperty(metadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), value); + properties.setProperty(metadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), value); } else { - properties.remove(metadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY)); + properties.remove(metadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY)); } long version = lastCacheChanged.incrementAndGet(); if (sync) { diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/store/RemoteWritableMetadataServiceDelegate.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/store/RemoteWritableMetadataServiceDelegate.java index 388542952fb..f46bf2174e6 100644 --- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/store/RemoteWritableMetadataServiceDelegate.java +++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/store/RemoteWritableMetadataServiceDelegate.java @@ -3,8 +3,6 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.metadata.WritableMetadataService; -import java.util.ArrayList; -import java.util.List; import java.util.SortedSet; import java.util.function.BiFunction; @@ -14,20 +12,16 @@ * @since 2.7.5 */ public class RemoteWritableMetadataServiceDelegate implements WritableMetadataService { - List metadataServiceList = new ArrayList<>(2); + private WritableMetadataService defaultWritableMetadataService; + private RemoteWritableMetadataService remoteWritableMetadataService; public RemoteWritableMetadataServiceDelegate() { - metadataServiceList.add(WritableMetadataService.getDefaultExtension()); - metadataServiceList.add(new RemoteWritableMetadataService()); + defaultWritableMetadataService = WritableMetadataService.getDefaultExtension(); + remoteWritableMetadataService = new RemoteWritableMetadataService(); } - private WritableMetadataService getInMemoryWritableMetadataService() { - for (WritableMetadataService writableMetadataService : metadataServiceList) { - if (writableMetadataService instanceof InMemoryWritableMetadataService) { - return writableMetadataService; - } - } - return metadataServiceList.get(0); + private WritableMetadataService getDefaultWritableMetadataService() { + return defaultWritableMetadataService; } @Override @@ -53,43 +47,41 @@ public boolean unsubscribeURL(URL url) { @Override public boolean refreshMetadata(String exportedRevision, String subscribedRevision) { boolean result = true; - for (WritableMetadataService writableMetadataService : metadataServiceList) { - result &= writableMetadataService.refreshMetadata(exportedRevision, subscribedRevision); - } + result &= defaultWritableMetadataService.refreshMetadata(exportedRevision, subscribedRevision); + result &= remoteWritableMetadataService.refreshMetadata(exportedRevision, subscribedRevision); return result; } @Override public void publishServiceDefinition(URL providerUrl) { - for (WritableMetadataService writableMetadataService : metadataServiceList) { - writableMetadataService.publishServiceDefinition(providerUrl); - } + defaultWritableMetadataService.publishServiceDefinition(providerUrl); + remoteWritableMetadataService.publishServiceDefinition(providerUrl); } @Override public SortedSet getExportedURLs(String serviceInterface, String group, String version, String protocol) { - return getInMemoryWritableMetadataService().getExportedURLs(serviceInterface, group, version, protocol); + return getDefaultWritableMetadataService().getExportedURLs(serviceInterface, group, version, protocol); } + @Override public SortedSet getSubscribedURLs() { - return getInMemoryWritableMetadataService().getSubscribedURLs(); + return getDefaultWritableMetadataService().getSubscribedURLs(); } @Override public String getServiceDefinition(String interfaceName, String version, String group) { - return getInMemoryWritableMetadataService().getServiceDefinition(interfaceName, version, group); + return getDefaultWritableMetadataService().getServiceDefinition(interfaceName, version, group); } @Override public String getServiceDefinition(String serviceKey) { - return getInMemoryWritableMetadataService().getServiceDefinition(serviceKey); + return getDefaultWritableMetadataService().getServiceDefinition(serviceKey); } private boolean doFunction(BiFunction func, URL url) { boolean result = true; - for (WritableMetadataService writableMetadataService : metadataServiceList) { - result &= func.apply(writableMetadataService, url); - } + result &= func.apply(defaultWritableMetadataService, url); + result &= func.apply(remoteWritableMetadataService, url); return result; } } diff --git a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/report/identifier/MetadataIdentifierTest.java b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/report/identifier/MetadataIdentifierTest.java index cb19880d6bc..90df3dbc80a 100644 --- a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/report/identifier/MetadataIdentifierTest.java +++ b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/report/identifier/MetadataIdentifierTest.java @@ -17,7 +17,7 @@ package org.apache.dubbo.metadata.report.identifier; import org.apache.dubbo.metadata.MetadataConstants; -import org.apache.dubbo.metadata.report.identifier.MetadataIdentifier.KeyTypeEnum; +import org.apache.dubbo.metadata.report.identifier.KeyTypeEnum; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -42,7 +42,7 @@ public void testGetUniqueKey() { (version == null ? "" : (version + PATH_SEPARATOR)) + (group == null ? "" : (group + PATH_SEPARATOR)) + PROVIDER_SIDE + PATH_SEPARATOR + application); - Assertions.assertEquals(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), + Assertions.assertEquals(providerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), interfaceName + MetadataConstants.KEY_SEPARATOR + (version == null ? "" : version) + MetadataConstants.KEY_SEPARATOR + (group == null ? "" : group) + MetadataConstants.KEY_SEPARATOR 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 24239e5e5d7..9d4267f7e3d 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 @@ -20,6 +20,7 @@ import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.metadata.definition.ServiceDefinitionBuilder; import org.apache.dubbo.metadata.definition.model.FullServiceDefinition; +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; @@ -72,7 +73,7 @@ public void testStoreProviderUsual() throws ClassNotFoundException, InterruptedE String application = "vic"; MetadataIdentifier providerMetadataIdentifier = storePrivider(abstractMetadataReport, interfaceName, version, group, application); Thread.sleep(1500); - Assertions.assertNotNull(abstractMetadataReport.store.get(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY))); + Assertions.assertNotNull(abstractMetadataReport.store.get(providerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY))); } @Test @@ -83,7 +84,7 @@ public void testStoreProviderSync() throws ClassNotFoundException, InterruptedEx String application = "vic"; abstractMetadataReport.syncReport = true; MetadataIdentifier providerMetadataIdentifier = storePrivider(abstractMetadataReport, interfaceName, version, group, application); - Assertions.assertNotNull(abstractMetadataReport.store.get(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY))); + Assertions.assertNotNull(abstractMetadataReport.store.get(providerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY))); } @Test @@ -102,7 +103,7 @@ public void testFileExistAfterPut() throws InterruptedException, ClassNotFoundEx Thread.sleep(2000); Assertions.assertTrue(singleMetadataReport.file.exists()); - Assertions.assertTrue(singleMetadataReport.properties.containsKey(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY))); + Assertions.assertTrue(singleMetadataReport.properties.containsKey(providerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY))); } @Test @@ -221,17 +222,17 @@ public void testPublishAll() throws ClassNotFoundException, InterruptedException Assertions.assertEquals(3, abstractMetadataReport.store.size()); - String v = abstractMetadataReport.store.get(providerMetadataIdentifier1.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY)); + String v = abstractMetadataReport.store.get(providerMetadataIdentifier1.getUniqueKey(KeyTypeEnum.UNIQUE_KEY)); Gson gson = new Gson(); FullServiceDefinition data = gson.fromJson(v, FullServiceDefinition.class); checkParam(data.getParameters(), application, version); - String v2 = abstractMetadataReport.store.get(providerMetadataIdentifier2.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY)); + String v2 = abstractMetadataReport.store.get(providerMetadataIdentifier2.getUniqueKey(KeyTypeEnum.UNIQUE_KEY)); gson = new Gson(); data = gson.fromJson(v2, FullServiceDefinition.class); checkParam(data.getParameters(), application, version + "_2"); - String v3 = abstractMetadataReport.store.get(consumerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY)); + String v3 = abstractMetadataReport.store.get(consumerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY)); gson = new Gson(); Map v3Map = gson.fromJson(v3, Map.class); checkParam(v3Map, application, version + "_3"); @@ -283,12 +284,12 @@ public NewMetadataReport(URL metadataReportURL) { @Override protected void doStoreProviderMetadata(MetadataIdentifier providerMetadataIdentifier, String serviceDefinitions) { - store.put(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), serviceDefinitions); + store.put(providerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), serviceDefinitions); } @Override protected void doStoreConsumerMetadata(MetadataIdentifier consumerMetadataIdentifier, String serviceParameterString) { - store.put(consumerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), serviceParameterString); + store.put(consumerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), serviceParameterString); } @Override @@ -340,7 +341,7 @@ protected void doStoreProviderMetadata(MetadataIdentifier providerMetadataIdenti if (executeTimes <= needRetryTimes) { throw new RuntimeException("must retry:" + executeTimes); } - store.put(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), serviceDefinitions); + store.put(providerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), serviceDefinitions); } @Override @@ -349,7 +350,7 @@ protected void doStoreConsumerMetadata(MetadataIdentifier consumerMetadataIdenti if (executeTimes <= needRetryTimes) { throw new RuntimeException("must retry:" + executeTimes); } - store.put(consumerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), serviceParameterString); + store.put(consumerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), serviceParameterString); } @Override diff --git a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/test/JTestMetadataReport4Test.java b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/test/JTestMetadataReport4Test.java index 57d41eed08e..dc1571f5d9c 100644 --- a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/test/JTestMetadataReport4Test.java +++ b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/test/JTestMetadataReport4Test.java @@ -19,6 +19,7 @@ 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.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; @@ -53,12 +54,12 @@ private static String getProtocol(URL url) { @Override protected void doStoreProviderMetadata(MetadataIdentifier providerMetadataIdentifier, String serviceDefinitions) { - store.put(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), serviceDefinitions); + store.put(providerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), serviceDefinitions); } @Override protected void doStoreConsumerMetadata(MetadataIdentifier consumerMetadataIdentifier, String serviceParameterString) { - store.put(consumerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), serviceParameterString); + store.put(consumerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), serviceParameterString); } @Override @@ -87,11 +88,11 @@ protected String doGetSubscribedURLs(SubscriberMetadataIdentifier metadataIdenti } public static String getProviderKey(URL url) { - return new MetadataIdentifier(url).getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY); + return new MetadataIdentifier(url).getUniqueKey(KeyTypeEnum.UNIQUE_KEY); } public static String getConsumerKey(URL url) { - return new MetadataIdentifier(url).getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY); + return new MetadataIdentifier(url).getUniqueKey(KeyTypeEnum.UNIQUE_KEY); } @Override diff --git a/dubbo-metadata/dubbo-metadata-report-consul/src/main/java/org/apache/dubbo/metadata/store/consul/ConsulMetadataReport.java b/dubbo-metadata/dubbo-metadata-report-consul/src/main/java/org/apache/dubbo/metadata/store/consul/ConsulMetadataReport.java index ba96a08aefb..c1e0a309bef 100644 --- a/dubbo-metadata/dubbo-metadata-report-consul/src/main/java/org/apache/dubbo/metadata/store/consul/ConsulMetadataReport.java +++ b/dubbo-metadata/dubbo-metadata-report-consul/src/main/java/org/apache/dubbo/metadata/store/consul/ConsulMetadataReport.java @@ -20,6 +20,9 @@ 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.StringUtils; +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; @@ -27,7 +30,12 @@ import org.apache.dubbo.rpc.RpcException; import com.ecwid.consul.v1.ConsulClient; +import com.ecwid.consul.v1.Response; +import com.ecwid.consul.v1.kv.model.GetValue; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.List; /** @@ -59,40 +67,69 @@ protected void doStoreConsumerMetadata(MetadataIdentifier consumerMetadataIdenti @Override protected void doSaveMetadata(ServiceMetadataIdentifier serviceMetadataIdentifier, URL url) { - throw new UnsupportedOperationException("This extension does not support working as a remote metadata center."); + this.storeMetadata(serviceMetadataIdentifier, URL.encode(url.toFullString())); } @Override protected void doRemoveMetadata(ServiceMetadataIdentifier serviceMetadataIdentifier) { - throw new UnsupportedOperationException("This extension does not support working as a remote metadata center."); + this.deleteMetadata(serviceMetadataIdentifier); } @Override protected List doGetExportedURLs(ServiceMetadataIdentifier metadataIdentifier) { - throw new UnsupportedOperationException("This extension does not support working as a remote metadata center."); + //todo encode and decode + String content = getMetadata(metadataIdentifier); + if (StringUtils.isEmpty(content)) { + return Collections.emptyList(); + } + return new ArrayList(Arrays.asList(URL.decode(content))); } @Override protected void doSaveSubscriberData(SubscriberMetadataIdentifier subscriberMetadataIdentifier, String urlListStr) { - + this.storeMetadata(subscriberMetadataIdentifier, urlListStr); } @Override protected String doGetSubscribedURLs(SubscriberMetadataIdentifier subscriberMetadataIdentifier) { - throw new UnsupportedOperationException("This extension does not support working as a remote metadata center."); + return getMetadata(subscriberMetadataIdentifier); } - private void storeMetadata(MetadataIdentifier identifier, String v) { + private void storeMetadata(BaseMetadataIdentifier identifier, String v) { try { - client.setKVValue(identifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), v); + client.setKVValue(identifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), v); } catch (Throwable t) { logger.error("Failed to put " + identifier + " to consul " + v + ", cause: " + t.getMessage(), t); throw new RpcException("Failed to put " + identifier + " to consul " + v + ", cause: " + t.getMessage(), t); } } + private void deleteMetadata(BaseMetadataIdentifier identifier) { + try { + client.deleteKVValue(identifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY)); + } catch (Throwable t) { + logger.error("Failed to delete " + identifier + " from consul , cause: " + t.getMessage(), t); + throw new RpcException("Failed to delete " + identifier + " from consul , cause: " + t.getMessage(), t); + } + } + + private String getMetadata(BaseMetadataIdentifier identifier) { + try { + Response value = client.getKVValue(identifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY)); + //FIXME CHECK + if (value != null && value.getValue() != null) { + //todo check decode value and value diff + return value.getValue().getValue(); + } + return null; + } catch (Throwable t) { + logger.error("Failed to get " + identifier + " from consul , cause: " + t.getMessage(), t); + throw new RpcException("Failed to get " + identifier + " from consul , cause: " + t.getMessage(), t); + } + } + @Override - public String getServiceDefinition(MetadataIdentifier consumerMetadataIdentifier) { - throw new UnsupportedOperationException("This extension does not support working as a remote metadata center."); + public String getServiceDefinition(MetadataIdentifier metadataIdentifier) { + return getMetadata(metadataIdentifier); } } diff --git a/dubbo-metadata/dubbo-metadata-report-etcd/src/main/java/org/apache/dubbo/metadata/store/etcd/EtcdMetadataReport.java b/dubbo-metadata/dubbo-metadata-report-etcd/src/main/java/org/apache/dubbo/metadata/store/etcd/EtcdMetadataReport.java index 507cd5345d5..1e97927dbb0 100644 --- a/dubbo-metadata/dubbo-metadata-report-etcd/src/main/java/org/apache/dubbo/metadata/store/etcd/EtcdMetadataReport.java +++ b/dubbo-metadata/dubbo-metadata-report-etcd/src/main/java/org/apache/dubbo/metadata/store/etcd/EtcdMetadataReport.java @@ -36,12 +36,18 @@ 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.StringUtils; +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 org.apache.dubbo.metadata.report.support.AbstractMetadataReport; import org.apache.dubbo.remoting.etcd.jetcd.JEtcdClient; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.List; import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; @@ -86,22 +92,32 @@ protected void doStoreConsumerMetadata(MetadataIdentifier consumerMetadataIdenti @Override protected void doSaveMetadata(ServiceMetadataIdentifier serviceMetadataIdentifier, URL url) { - throw new UnsupportedOperationException("This extension does not support working as a remote metadata center."); + String key = getNodeKey(serviceMetadataIdentifier); + if (!etcdClient.put(key, URL.encode(url.toFullString()))) { + logger.error("Failed to put " + serviceMetadataIdentifier + " to etcd, value: " + url); + } } @Override protected void doRemoveMetadata(ServiceMetadataIdentifier serviceMetadataIdentifier) { - throw new UnsupportedOperationException("This extension does not support working as a remote metadata center."); + etcdClient.delete(getNodeKey(serviceMetadataIdentifier)); } @Override protected List doGetExportedURLs(ServiceMetadataIdentifier metadataIdentifier) { - throw new UnsupportedOperationException("This extension does not support working as a remote metadata center."); + String content = etcdClient.getKVValue(getNodeKey(metadataIdentifier)); + if (StringUtils.isEmpty(content)) { + return Collections.emptyList(); + } + return new ArrayList(Arrays.asList(URL.decode(content))); } @Override protected void doSaveSubscriberData(SubscriberMetadataIdentifier subscriberMetadataIdentifier, String urlListStr) { - + String key = getNodeKey(subscriberMetadataIdentifier); + if (!etcdClient.put(key, urlListStr)) { + logger.error("Failed to put " + subscriberMetadataIdentifier + " to etcd, value: " + urlListStr); + } } @Override @@ -121,8 +137,8 @@ private void storeMetadata(MetadataIdentifier identifier, String v) { } } - String getNodeKey(MetadataIdentifier identifier) { - return toRootDir() + identifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.PATH); + String getNodeKey(BaseMetadataIdentifier identifier) { + return toRootDir() + identifier.getUniqueKey(KeyTypeEnum.PATH); } String toRootDir() { diff --git a/dubbo-metadata/dubbo-metadata-report-nacos/src/main/java/org/apache/dubbo/metadata/store/nacos/NacosMetadataReport.java b/dubbo-metadata/dubbo-metadata-report-nacos/src/main/java/org/apache/dubbo/metadata/store/nacos/NacosMetadataReport.java index bdbe759f280..6cd4bd2dc5d 100644 --- a/dubbo-metadata/dubbo-metadata-report-nacos/src/main/java/org/apache/dubbo/metadata/store/nacos/NacosMetadataReport.java +++ b/dubbo-metadata/dubbo-metadata-report-nacos/src/main/java/org/apache/dubbo/metadata/store/nacos/NacosMetadataReport.java @@ -21,6 +21,8 @@ import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.StringUtils; +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; @@ -31,6 +33,9 @@ import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.exception.NacosException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Properties; @@ -127,37 +132,41 @@ protected void doStoreConsumerMetadata(MetadataIdentifier consumerMetadataIdenti @Override protected void doSaveMetadata(ServiceMetadataIdentifier serviceMetadataIdentifier, URL url) { - throw new UnsupportedOperationException("This extension does not support working as a remote metadata center."); + storeMetadata(serviceMetadataIdentifier, URL.encode(url.toFullString())); } @Override protected void doRemoveMetadata(ServiceMetadataIdentifier serviceMetadataIdentifier) { - throw new UnsupportedOperationException("This extension does not support working as a remote metadata center."); + deleteMetadata(serviceMetadataIdentifier); } @Override protected List doGetExportedURLs(ServiceMetadataIdentifier metadataIdentifier) { - throw new UnsupportedOperationException("This extension does not support working as a remote metadata center."); + String content = getConfig(metadataIdentifier); + if (StringUtils.isEmpty(content)) { + return Collections.emptyList(); + } + return new ArrayList(Arrays.asList(URL.decode(content))); } @Override protected void doSaveSubscriberData(SubscriberMetadataIdentifier subscriberMetadataIdentifier, String urlListStr) { - throw new UnsupportedOperationException("This extension does not support working as a remote metadata center."); + storeMetadata(subscriberMetadataIdentifier, urlListStr); } @Override protected String doGetSubscribedURLs(SubscriberMetadataIdentifier subscriberMetadataIdentifier) { - throw new UnsupportedOperationException("This extension does not support working as a remote metadata center."); + return getConfig(subscriberMetadataIdentifier); } @Override - public String getServiceDefinition(MetadataIdentifier consumerMetadataIdentifier) { - throw new UnsupportedOperationException("This extension does not support working as a remote metadata center."); + public String getServiceDefinition(MetadataIdentifier metadataIdentifier) { + return getConfig(metadataIdentifier); } - private void storeMetadata(MetadataIdentifier identifier, String value) { + private void storeMetadata(BaseMetadataIdentifier identifier, String value) { try { - boolean publishResult = configService.publishConfig(identifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), group, value); + boolean publishResult = configService.publishConfig(identifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), group, value); if (!publishResult) { throw new RuntimeException("publish nacos metadata failed"); } @@ -166,4 +175,25 @@ private void storeMetadata(MetadataIdentifier identifier, String value) { throw new RpcException("Failed to put " + identifier + " to nacos " + value + ", cause: " + t.getMessage(), t); } } + + private void deleteMetadata(BaseMetadataIdentifier identifier) { + try { + boolean publishResult = configService.removeConfig(identifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), group); + if (!publishResult) { + throw new RuntimeException("remove nacos metadata failed"); + } + } catch (Throwable t) { + logger.error("Failed to remove " + identifier + " from nacos , cause: " + t.getMessage(), t); + throw new RpcException("Failed to remove " + identifier + " from nacos , cause: " + t.getMessage(), t); + } + } + + private String getConfig(BaseMetadataIdentifier identifier) { + try { + return configService.getConfig(identifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), group, 300); + } catch (Throwable t) { + logger.error("Failed to get " + identifier + " from nacos , cause: " + t.getMessage(), t); + throw new RpcException("Failed to get " + identifier + " from nacos , cause: " + t.getMessage(), t); + } + } } diff --git a/dubbo-metadata/dubbo-metadata-report-nacos/src/test/java/org/apache/dubbo/metadata/store/nacos/NacosMetadataReportTest.java b/dubbo-metadata/dubbo-metadata-report-nacos/src/test/java/org/apache/dubbo/metadata/store/nacos/NacosMetadataReportTest.java index 666ec124be6..c4416353d25 100644 --- a/dubbo-metadata/dubbo-metadata-report-nacos/src/test/java/org/apache/dubbo/metadata/store/nacos/NacosMetadataReportTest.java +++ b/dubbo-metadata/dubbo-metadata-report-nacos/src/test/java/org/apache/dubbo/metadata/store/nacos/NacosMetadataReportTest.java @@ -20,6 +20,7 @@ import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.metadata.definition.ServiceDefinitionBuilder; import org.apache.dubbo.metadata.definition.model.FullServiceDefinition; +import org.apache.dubbo.metadata.report.identifier.KeyTypeEnum; import org.apache.dubbo.metadata.report.identifier.MetadataIdentifier; import com.alibaba.nacos.api.config.ConfigService; @@ -68,7 +69,7 @@ public void testStoreProvider() throws Exception { String application = "nacos-metdata-report-test"; MetadataIdentifier providerIdentifier = storeProvider(nacosMetadataReport, TEST_SERVICE, version, group, application); - String serverContent = configService.getConfig(providerIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), NACOS_GROUP, 5000L); + String serverContent = configService.getConfig(providerIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), NACOS_GROUP, 5000L); Assertions.assertNotNull(serverContent); Gson gson = new Gson(); @@ -83,7 +84,7 @@ public void testStoreConsumer() throws Exception { String application = "nacos-metadata-report-consumer-test"; MetadataIdentifier consumerIdentifier = storeConsumer(nacosMetadataReport, TEST_SERVICE, version, group, application); - String serverContent = configService.getConfig(consumerIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), NACOS_GROUP, 5000L); + String serverContent = configService.getConfig(consumerIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), NACOS_GROUP, 5000L); Assertions.assertNotNull(serverContent); Assertions.assertEquals(serverContent, "{\"paramConsumerTest\":\"nacosConsumer\"}"); } diff --git a/dubbo-metadata/dubbo-metadata-report-redis/src/main/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReport.java b/dubbo-metadata/dubbo-metadata-report-redis/src/main/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReport.java index c9b3bb4cf66..c00be049b72 100644 --- a/dubbo-metadata/dubbo-metadata-report-redis/src/main/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReport.java +++ b/dubbo-metadata/dubbo-metadata-report-redis/src/main/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReport.java @@ -19,6 +19,9 @@ 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.StringUtils; +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; @@ -32,6 +35,9 @@ import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -82,35 +88,39 @@ protected void doStoreConsumerMetadata(MetadataIdentifier consumerMetadataIdenti @Override protected void doSaveMetadata(ServiceMetadataIdentifier serviceMetadataIdentifier, URL url) { - + this.storeMetadata(serviceMetadataIdentifier, URL.encode(url.toFullString())); } @Override protected void doRemoveMetadata(ServiceMetadataIdentifier serviceMetadataIdentifier) { - + this.deleteMetadata(serviceMetadataIdentifier); } @Override protected List doGetExportedURLs(ServiceMetadataIdentifier metadataIdentifier) { - return null; + String content = getMetadata(metadataIdentifier); + if (StringUtils.isEmpty(content)) { + return Collections.emptyList(); + } + return new ArrayList(Arrays.asList(URL.decode(content))); } @Override protected void doSaveSubscriberData(SubscriberMetadataIdentifier subscriberMetadataIdentifier, String urlListStr) { - throw new UnsupportedOperationException("This extension does not support working as a remote metadata center."); + this.storeMetadata(subscriberMetadataIdentifier, urlListStr); } @Override protected String doGetSubscribedURLs(SubscriberMetadataIdentifier subscriberMetadataIdentifier) { - throw new UnsupportedOperationException("This extension does not support working as a remote metadata center."); + return this.getMetadata(subscriberMetadataIdentifier); } @Override - public String getServiceDefinition(MetadataIdentifier consumerMetadataIdentifier) { - throw new UnsupportedOperationException("This extension does not support working as a remote metadata center."); + public String getServiceDefinition(MetadataIdentifier metadataIdentifier) { + return this.getMetadata(metadataIdentifier); } - private void storeMetadata(MetadataIdentifier metadataIdentifier, String v) { + private void storeMetadata(BaseMetadataIdentifier metadataIdentifier, String v) { if (pool != null) { storeMetadataStandalone(metadataIdentifier, v); } else { @@ -118,7 +128,7 @@ private void storeMetadata(MetadataIdentifier metadataIdentifier, String v) { } } - private void storeMetadataInCluster(MetadataIdentifier metadataIdentifier, String v) { + private void storeMetadataInCluster(BaseMetadataIdentifier metadataIdentifier, String v) { try (JedisCluster jedisCluster = new JedisCluster(jedisClusterNodes, timeout, timeout, 2, password, new GenericObjectPoolConfig())) { jedisCluster.set(metadataIdentifier.getIdentifierKey() + META_DATA_STORE_TAG, v); } catch (Throwable e) { @@ -127,13 +137,65 @@ private void storeMetadataInCluster(MetadataIdentifier metadataIdentifier, Strin } } - private void storeMetadataStandalone(MetadataIdentifier metadataIdentifier, String v) { + private void storeMetadataStandalone(BaseMetadataIdentifier metadataIdentifier, String v) { try (Jedis jedis = pool.getResource()) { - jedis.set(metadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), v); + jedis.set(metadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), v); } catch (Throwable e) { logger.error("Failed to put " + metadataIdentifier + " to redis " + v + ", cause: " + e.getMessage(), e); throw new RpcException("Failed to put " + metadataIdentifier + " to redis " + v + ", cause: " + e.getMessage(), e); } } + private void deleteMetadata(BaseMetadataIdentifier metadataIdentifier) { + if (pool != null) { + deleteMetadataStandalone(metadataIdentifier); + } else { + deleteMetadataInCluster(metadataIdentifier); + } + } + + private void deleteMetadataInCluster(BaseMetadataIdentifier metadataIdentifier) { + try (JedisCluster jedisCluster = new JedisCluster(jedisClusterNodes, timeout, timeout, 2, password, new GenericObjectPoolConfig())) { + jedisCluster.del(metadataIdentifier.getIdentifierKey() + META_DATA_STORE_TAG); + } catch (Throwable e) { + logger.error("Failed to delete " + metadataIdentifier + " from redis cluster , cause: " + e.getMessage(), e); + throw new RpcException("Failed to delete " + metadataIdentifier + " from redis cluster , cause: " + e.getMessage(), e); + } + } + + private void deleteMetadataStandalone(BaseMetadataIdentifier metadataIdentifier) { + try (Jedis jedis = pool.getResource()) { + jedis.del(metadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY)); + } catch (Throwable e) { + logger.error("Failed to delete " + metadataIdentifier + " from redis , cause: " + e.getMessage(), e); + throw new RpcException("Failed to delete " + metadataIdentifier + " from redis , cause: " + e.getMessage(), e); + } + } + + private String getMetadata(BaseMetadataIdentifier metadataIdentifier) { + if (pool != null) { + return getMetadataStandalone(metadataIdentifier); + } else { + return getMetadataInCluster(metadataIdentifier); + } + } + + private String getMetadataInCluster(BaseMetadataIdentifier metadataIdentifier) { + try (JedisCluster jedisCluster = new JedisCluster(jedisClusterNodes, timeout, timeout, 2, password, new GenericObjectPoolConfig())) { + return jedisCluster.get(metadataIdentifier.getIdentifierKey() + META_DATA_STORE_TAG); + } catch (Throwable e) { + logger.error("Failed to get " + metadataIdentifier + " from redis cluster , cause: " + e.getMessage(), e); + throw new RpcException("Failed to get " + metadataIdentifier + " from redis cluster , cause: " + e.getMessage(), e); + } + } + + private String getMetadataStandalone(BaseMetadataIdentifier metadataIdentifier) { + try (Jedis jedis = pool.getResource()) { + return jedis.get(metadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY)); + } catch (Throwable e) { + logger.error("Failed to get " + metadataIdentifier + " from redis , cause: " + e.getMessage(), e); + throw new RpcException("Failed to get " + metadataIdentifier + " from redis , cause: " + e.getMessage(), e); + } + } + } diff --git a/dubbo-metadata/dubbo-metadata-report-redis/src/test/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReportTest.java b/dubbo-metadata/dubbo-metadata-report-redis/src/test/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReportTest.java index 88105e276b9..61dd37635b1 100644 --- a/dubbo-metadata/dubbo-metadata-report-redis/src/test/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReportTest.java +++ b/dubbo-metadata/dubbo-metadata-report-redis/src/test/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReportTest.java @@ -20,6 +20,7 @@ import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.metadata.definition.ServiceDefinitionBuilder; import org.apache.dubbo.metadata.definition.model.FullServiceDefinition; +import org.apache.dubbo.metadata.report.identifier.KeyTypeEnum; import org.apache.dubbo.metadata.report.identifier.MetadataIdentifier; import org.apache.dubbo.rpc.RpcException; @@ -94,7 +95,7 @@ private void testStoreProvider(RedisMetadataReport redisMetadataReport, String v Jedis jedis = null; try { jedis = redisMetadataReport.pool.getResource(); - String keyTmp = providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY); + String keyTmp = providerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY); String value = jedis.get(keyTmp); if (value == null) { Thread.sleep(moreTime); @@ -110,7 +111,7 @@ private void testStoreProvider(RedisMetadataReport redisMetadataReport, String v throw new RpcException("Failed to put to redis . cause: " + e.getMessage(), e); } finally { if (jedis != null) { - jedis.del(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY)); + jedis.del(providerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY)); } redisMetadataReport.pool.close(); } @@ -134,7 +135,7 @@ private void testStoreConsumer(RedisMetadataReport redisMetadataReport, String v Jedis jedis = null; try { jedis = redisMetadataReport.pool.getResource(); - String keyTmp = consumerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY); + String keyTmp = consumerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY); String value = jedis.get(keyTmp); if (value == null) { Thread.sleep(moreTime); @@ -145,7 +146,7 @@ private void testStoreConsumer(RedisMetadataReport redisMetadataReport, String v throw new RpcException("Failed to put to redis . cause: " + e.getMessage(), e); } finally { if (jedis != null) { - jedis.del(consumerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY)); + jedis.del(consumerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY)); } redisMetadataReport.pool.close(); } 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 b370a620b90..9ca2e42024a 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,6 +19,9 @@ 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.StringUtils; +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; @@ -28,6 +31,7 @@ import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; @@ -86,7 +90,11 @@ protected void doRemoveMetadata(ServiceMetadataIdentifier metadataIdentifier) { @Override protected List doGetExportedURLs(ServiceMetadataIdentifier metadataIdentifier) { - return new ArrayList(Arrays.asList(URL.decode(zkClient.getContent(getNodePath(metadataIdentifier))))); + String content = zkClient.getContent(getNodePath(metadataIdentifier)); + if (StringUtils.isEmpty(content)) { + return Collections.emptyList(); + } + return new ArrayList(Arrays.asList(URL.decode(content))); } @Override @@ -108,17 +116,8 @@ private void storeMetadata(MetadataIdentifier metadataIdentifier, String v) { zkClient.create(getNodePath(metadataIdentifier), v, false); } - String getNodePath(MetadataIdentifier metadataIdentifier) { - return toRootDir() + metadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.PATH); - } - - String getNodePath(ServiceMetadataIdentifier metadataIdentifier) { - return toRootDir() + metadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.PATH); + String getNodePath(BaseMetadataIdentifier metadataIdentifier) { + return toRootDir() + metadataIdentifier.getUniqueKey(KeyTypeEnum.PATH); } - String getNodePath(SubscriberMetadataIdentifier metadataIdentifier) { - return toRootDir() + metadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.PATH); - } - - } diff --git a/dubbo-metadata/dubbo-metadata-report-zookeeper/src/test/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReportTest.java b/dubbo-metadata/dubbo-metadata-report-zookeeper/src/test/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReportTest.java index 55912f0e352..6b5d3b59216 100644 --- a/dubbo-metadata/dubbo-metadata-report-zookeeper/src/test/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReportTest.java +++ b/dubbo-metadata/dubbo-metadata-report-zookeeper/src/test/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReportTest.java @@ -20,6 +20,7 @@ import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.metadata.definition.ServiceDefinitionBuilder; import org.apache.dubbo.metadata.definition.model.FullServiceDefinition; +import org.apache.dubbo.metadata.report.identifier.KeyTypeEnum; import org.apache.dubbo.metadata.report.identifier.MetadataIdentifier; import org.apache.dubbo.remoting.zookeeper.curator.CuratorZookeeperTransporter; @@ -62,7 +63,7 @@ public void tearDown() throws Exception { } private void deletePath(MetadataIdentifier metadataIdentifier, ZookeeperMetadataReport zookeeperMetadataReport) { - String category = zookeeperMetadataReport.toRootDir() + metadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.PATH); + String category = zookeeperMetadataReport.toRootDir() + metadataIdentifier.getUniqueKey(KeyTypeEnum.PATH); zookeeperMetadataReport.zkClient.delete(category); }