diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/impl/PersistentServiceProcessor.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/impl/PersistentServiceProcessor.java index 028103a66..0e8f90d2b 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/impl/PersistentServiceProcessor.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/impl/PersistentServiceProcessor.java @@ -231,10 +231,6 @@ public class PersistentServiceProcessor extends LogProcessor4CP implements Persi final ValueChangeEvent event = ValueChangeEvent.builder().key(key).value(value) .action(Op.Delete.equals(op) ? DataOperation.DELETE : DataOperation.CHANGE).build(); NotifyCenter.publishEvent(event); - // remove listeners of key to avoid mem leak - if (Op.Delete.equals(op)) { - notifier.deregisterAllListener(key); - } } } 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 49d64e00c..2160e3a28 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 @@ -26,6 +26,7 @@ import com.alibaba.nacos.naming.consistency.ConsistencyService; import com.alibaba.nacos.naming.consistency.Datum; import com.alibaba.nacos.naming.consistency.KeyBuilder; import com.alibaba.nacos.naming.consistency.RecordListener; +import com.alibaba.nacos.naming.consistency.persistent.impl.PersistentServiceProcessor; import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer; import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeerSet; import com.alibaba.nacos.naming.misc.GlobalExecutor; @@ -229,10 +230,14 @@ public class ServiceManager implements RecordListener { if (service != null) { service.destroy(); - consistencyService.remove(KeyBuilder.buildInstanceListKey(namespace, name, true)); - - consistencyService.remove(KeyBuilder.buildInstanceListKey(namespace, name, false)); - + String ephemeralInstanceListKey = KeyBuilder.buildInstanceListKey(namespace, name, true); + String persistInstanceListKey = KeyBuilder.buildInstanceListKey(namespace, name, false); + consistencyService.remove(ephemeralInstanceListKey); + consistencyService.remove(persistInstanceListKey); + + // remove listeners of key to avoid mem leak + consistencyService.unListen(ephemeralInstanceListKey, service); + consistencyService.unListen(persistInstanceListKey, service); consistencyService.unListen(KeyBuilder.buildServiceMetaKey(namespace, name), service); Loggers.SRV_LOG.info("[DEAD-SERVICE] {}", service.toJson()); }