From 2546f052e4e23fa4d49b6b398628c441af0c6a10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E5=BB=B6?= <1060026287@qq.com> Date: Tue, 15 Dec 2020 12:56:05 +0800 Subject: [PATCH] [ISSUE-#4467] Fix snowflake id problem (#4468) * fix snowflake id problem * revert code format --- .../com/alibaba/nacos/naming/core/ServiceManager.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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 d125f7929..fda5b5d6b 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 @@ -811,7 +811,12 @@ public class ServiceManager implements RecordListener { if (UtilsAndCommons.UPDATE_INSTANCE_ACTION_REMOVE.equals(action)) { instanceMap.remove(instance.getDatumKey()); } else { - instance.setInstanceId(instance.generateInstanceId(currentInstanceIds)); + Instance oldInstance = instanceMap.get(instance.getDatumKey()); + if (oldInstance != null) { + instance.setInstanceId(oldInstance.getInstanceId()); + } else { + instance.setInstanceId(instance.generateInstanceId(currentInstanceIds)); + } instanceMap.put(instance.getDatumKey(), instance); } @@ -958,7 +963,7 @@ public class ServiceManager implements RecordListener { List instances = service.allIPs(); for (Instance instance : instances) { if (IPUtil.containsPort(containedInstance)) { - if (StringUtils.equals(instance.getIp() + IPUtil.IP_PORT_SPLITER + instance.getPort(), containedInstance)) { + if (StringUtils.equals(instance.getIp() + IPUtil.IP_PORT_SPLITER + instance.getPort(), containedInstance)) { contained = true; break; }