diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroConsistencyServiceImpl.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroConsistencyServiceImpl.java index 65448e8a0..145ee6b5e 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroConsistencyServiceImpl.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroConsistencyServiceImpl.java @@ -322,6 +322,11 @@ public class DistroConsistencyServiceImpl implements EphemeralConsistencyService if (!listeners.containsKey(key)) { listeners.put(key, new CopyOnWriteArrayList<>()); } + + if (listeners.get(key).contains(listener)) { + return; + } + listeners.get(key).add(listener); } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java index e45ec2b12..7daaf5a27 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java @@ -143,6 +143,9 @@ public class ServiceManager implements RecordListener { if (oldDom != null) { oldDom.update(service); + // re-listen to handle the situation when the underlying listener is removed: + consistencyService.listen(KeyBuilder.buildInstanceListKey(service.getNamespaceId(), service.getName(), true), oldDom); + consistencyService.listen(KeyBuilder.buildInstanceListKey(service.getNamespaceId(), service.getName(), false), oldDom); } else { putService(service); service.init();