From d11616c4bf5f164b7773dede501cef93e2bfa4cb Mon Sep 17 00:00:00 2001 From: Albumen Kevin Date: Mon, 10 Apr 2023 09:34:26 +0800 Subject: [PATCH] Support nacos disable subscribe legacy service name (#11887) --- .../common/constants/LoggerCodeConstants.java | 2 ++ .../nacos/NacosAggregateListener.java | 21 +++++++++++++++++++ .../dubbo/registry/nacos/NacosRegistry.java | 14 ++++++++----- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/LoggerCodeConstants.java b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/LoggerCodeConstants.java index 0682c5233d..0a3bfa2125 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/LoggerCodeConstants.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/LoggerCodeConstants.java @@ -192,6 +192,8 @@ public interface LoggerCodeConstants { String REGISTRY_ISTIO_EXCEPTION = "1-41"; + String REGISTRY_NACOS_SUB_LEGACY = "1-42"; + // Cluster module 2-x String CLUSTER_FAILED_SITE_SELECTION = "2-1"; diff --git a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosAggregateListener.java b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosAggregateListener.java index 4eafb9b6f5..b8dcaad92f 100644 --- a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosAggregateListener.java +++ b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosAggregateListener.java @@ -16,6 +16,8 @@ */ package org.apache.dubbo.registry.nacos; +import org.apache.dubbo.common.logger.ErrorTypeAwareLogger; +import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.ConcurrentHashSet; import org.apache.dubbo.registry.NotifyListener; @@ -26,12 +28,19 @@ import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.regex.Pattern; import java.util.stream.Collectors; +import static org.apache.dubbo.common.constants.LoggerCodeConstants.REGISTRY_NACOS_SUB_LEGACY; + public class NacosAggregateListener { + private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(NacosAggregateListener.class); private final NotifyListener notifyListener; private final Set serviceNames = new ConcurrentHashSet<>(); private final Map> serviceInstances = new ConcurrentHashMap<>(); + private final AtomicBoolean warned = new AtomicBoolean(false); + private static final Pattern SPLITTED_PATTERN = Pattern.compile(".*:.*:.*:.*"); public NacosAggregateListener(NotifyListener notifyListener) { this.notifyListener = notifyListener; @@ -44,9 +53,21 @@ public class NacosAggregateListener { } else { serviceInstances.put(serviceName, instances); } + if (isLegacyName(serviceName) && instances != null && + !instances.isEmpty() && warned.compareAndSet(false, true)) { + logger.error(REGISTRY_NACOS_SUB_LEGACY, "", "", + "Received not empty notification for legacy service name: " + serviceName + ", " + + "instances: [" + instances.stream().map(Instance::getIp).collect(Collectors.joining(" ,")) + "]. " + + "Please upgrade these Dubbo client(lower than 2.7.3) to the latest version. " + + "Dubbo will remove the support for legacy service name in the future."); + } return serviceInstances.values().stream().flatMap(List::stream).collect(Collectors.toList()); } + private static boolean isLegacyName(String serviceName) { + return !SPLITTED_PATTERN.matcher(serviceName).matches(); + } + public NotifyListener getNotifyListener() { return notifyListener; } 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 269aac4b44..baaa2bf437 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 @@ -136,10 +136,12 @@ public class NacosRegistry extends FailbackRegistry { private final Map> originToAggregateListener = new ConcurrentHashMap<>(); private final Map>> nacosListeners = new ConcurrentHashMap<>(); + private final boolean supportLegacyServiceName; public NacosRegistry(URL url, NacosNamingServiceWrapper namingService) { super(url); this.namingService = namingService; + this.supportLegacyServiceName = url.getParameter("nacos.subscribe.legacy-name", true); } @Override @@ -326,11 +328,13 @@ public class NacosRegistry extends FailbackRegistry { if (serviceName.isConcrete()) { // is the concrete service name serviceNames = new LinkedHashSet<>(); serviceNames.add(serviceName.toString()); - // Add the legacy service name since 2.7.6 - String legacySubscribedServiceName = getLegacySubscribedServiceName(url); - if (!serviceName.toString().equals(legacySubscribedServiceName)) { - //avoid duplicated service names - serviceNames.add(legacySubscribedServiceName); + if (supportLegacyServiceName) { + // Add the legacy service name since 2.7.6 + String legacySubscribedServiceName = getLegacySubscribedServiceName(url); + if (!serviceName.toString().equals(legacySubscribedServiceName)) { + //avoid duplicated service names + serviceNames.add(legacySubscribedServiceName); + } } } else { serviceNames = filterServiceNames(serviceName);