Support nacos disable subscribe legacy service name (#11887)

This commit is contained in:
Albumen Kevin 2023-04-10 09:34:26 +08:00 committed by GitHub
parent 5cd5b32b8b
commit d11616c4bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 5 deletions

View File

@ -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";

View File

@ -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<String> serviceNames = new ConcurrentHashSet<>();
private final Map<String, List<Instance>> 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;
}

View File

@ -136,10 +136,12 @@ public class NacosRegistry extends FailbackRegistry {
private final Map<URL, Map<NotifyListener, NacosAggregateListener>> originToAggregateListener = new ConcurrentHashMap<>();
private final Map<URL, Map<NacosAggregateListener, Map<String, EventListener>>> 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);