From 723ab69740bf986b9abc6ee3b920280532ab8c1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=88=E9=93=AD?= Date: Mon, 22 Apr 2024 11:36:21 +0800 Subject: [PATCH] compatible service discovery, export noting suffix servicename, eg: {interface}:${version} --- .../java/org/apache/dubbo/common/URL.java | 8 +++---- .../common/constants/RegistryConstants.java | 7 ++++++ .../dubbo/registry/nacos/NacosRegistry.java | 23 +++++++------------ 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java index 6a1ccbfab4c2..eaf8ffd4011a 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java @@ -1300,11 +1300,11 @@ public String getColonSeparatedKey() { * * @return */ - public String getColonSeparatedKey0() { + public String getCompatibleColonSeparatedKey() { StringBuilder serviceNameBuilder = new StringBuilder(); serviceNameBuilder.append(this.getServiceInterface()); - append0(serviceNameBuilder, VERSION_KEY); - append0(serviceNameBuilder, GROUP_KEY); + compatibleAppend(serviceNameBuilder, VERSION_KEY); + compatibleAppend(serviceNameBuilder, GROUP_KEY); return serviceNameBuilder.toString(); } @@ -1320,7 +1320,7 @@ private void append(StringBuilder target, String parameterName, boolean first) { } } - private void append0(StringBuilder target, String parameterName) { + private void compatibleAppend(StringBuilder target, String parameterName) { String parameterValue = this.getParameter(parameterName); if (!isBlank(parameterValue)) { target.append(':'); diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/RegistryConstants.java b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/RegistryConstants.java index 74a8f0b4b21a..18cb945aeec6 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/RegistryConstants.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/RegistryConstants.java @@ -141,4 +141,11 @@ public interface RegistryConstants { String ENABLE_EMPTY_PROTECTION_KEY = "enable-empty-protection"; boolean DEFAULT_ENABLE_EMPTY_PROTECTION = false; String REGISTER_CONSUMER_URL_KEY = "register-consumer-url"; + + /** + * export noting suffix servicename + * by default, dubbo export servicename is "${interface}:${version}:", this servicename with ':' suffix + * for compatible, we should export noting suffix servicename, eg: ${interface}:${version} + */ + String NACOE_REGISTER_COMPATIBLE = "nacos.register-compatible"; } diff --git a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistry.java b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistry.java index 016da30dbe72..27f6608b8104 100644 --- a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistry.java +++ b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistry.java @@ -72,6 +72,7 @@ import static org.apache.dubbo.common.constants.RegistryConstants.DEFAULT_ENABLE_EMPTY_PROTECTION; import static org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL; import static org.apache.dubbo.common.constants.RegistryConstants.ENABLE_EMPTY_PROTECTION_KEY; +import static org.apache.dubbo.common.constants.RegistryConstants.NACOE_REGISTER_COMPATIBLE; import static org.apache.dubbo.common.constants.RegistryConstants.PROVIDERS_CATEGORY; import static org.apache.dubbo.common.constants.RegistryConstants.REGISTER_CONSUMER_URL_KEY; import static org.apache.dubbo.common.constants.RegistryConstants.ROUTERS_CATEGORY; @@ -123,14 +124,6 @@ public class NacosRegistry extends FailbackRegistry { */ private static final long LOOKUP_INTERVAL = Long.getLong("nacos.service.names.lookup.interval", 30); - /** - * export noting suffix servicename - * by default, dubbo export servicename is "${interface}:${version}:", this servicename with ':' suffix - * for compatible, we should export noting suffix servicename, eg: ${interface}:${version} - */ - private static final String DUUBO_NACOS_EXPORT_NOTING_SUFFIX_SERVICENAME = - System.getProperty("dubbo.nacos.export.noting.suffix.servicename", "false"); - private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(NacosRegistry.class); private final NacosNamingServiceWrapper namingService; /** @@ -190,10 +183,10 @@ public void doRegister(URL url) { serviceNames.add(serviceName); // in https://github.com/apache/dubbo/issues/14075 - if (Boolean.parseBoolean(DUUBO_NACOS_EXPORT_NOTING_SUFFIX_SERVICENAME)) { + if (getUrl().getParameter(NACOE_REGISTER_COMPATIBLE, false)) { // servicename is "org.apache.dubbo.xxService:1.0.0" - String serviceName1 = getServiceName(url, true); - serviceNames.add(serviceName1); + String compatibleServiceName = getServiceName(url, true); + serviceNames.add(compatibleServiceName); } /** @@ -229,7 +222,7 @@ public void doUnregister(final URL url) { serviceNames.add(serviceName); // in https://github.com/apache/dubbo/issues/14075 - if (Boolean.parseBoolean(DUUBO_NACOS_EXPORT_NOTING_SUFFIX_SERVICENAME)) { + if (getUrl().getParameter(NACOE_REGISTER_COMPATIBLE, false)) { // servicename is "org.apache.dubbo.xxService:1.0.0" String serviceName1 = getServiceName(url, true); serviceNames.add(serviceName1); @@ -739,7 +732,7 @@ private NacosServiceName createServiceName(URL url) { private String getServiceName(URL url, boolean needCompatible) { if (needCompatible) { - return getServiceName0(url, url.getCategory(DEFAULT_CATEGORY)); + return getCompatibleServiceName(url, url.getCategory(DEFAULT_CATEGORY)); } return getServiceName(url, url.getCategory(DEFAULT_CATEGORY)); } @@ -748,8 +741,8 @@ private String getServiceName(URL url, String category) { return category + SERVICE_NAME_SEPARATOR + url.getColonSeparatedKey(); } - private String getServiceName0(URL url, String category) { - return category + SERVICE_NAME_SEPARATOR + url.getColonSeparatedKey0(); + private String getCompatibleServiceName(URL url, String category) { + return category + SERVICE_NAME_SEPARATOR + url.getCompatibleColonSeparatedKey(); } private void filterEnabledInstances(Collection instances) {