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 6e0fefcfafa..adcb7790793 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 @@ -446,26 +446,28 @@ public void createEmptyService(String namespaceId, String serviceName, boolean l public void createServiceIfAbsent(String namespaceId, String serviceName, boolean local, Cluster cluster) throws NacosException { Service service = getService(namespaceId, serviceName); - if (service == null) { - - Loggers.SRV_LOG.info("creating empty service {}:{}", namespaceId, serviceName); - service = new Service(); - service.setName(serviceName); - service.setNamespaceId(namespaceId); - service.setGroupName(NamingUtils.getGroupName(serviceName)); - // now validate the service. if failed, exception will be thrown - service.setLastModifiedMillis(System.currentTimeMillis()); - service.recalculateChecksum(); - if (cluster != null) { - cluster.setService(service); - service.getClusterMap().put(cluster.getName(), cluster); - } - service.validate(); - - putServiceAndInit(service); - if (!local) { - addOrReplaceService(service); - } + //return if service already exists + if (service != null) { + return; + } + + Loggers.SRV_LOG.info("creating empty service {}:{}", namespaceId, serviceName); + service = new Service(); + service.setName(serviceName); + service.setNamespaceId(namespaceId); + service.setGroupName(NamingUtils.getGroupName(serviceName)); + // now validate the service. if failed, exception will be thrown + service.setLastModifiedMillis(System.currentTimeMillis()); + service.recalculateChecksum(); + if (cluster != null) { + cluster.setService(service); + service.getClusterMap().put(cluster.getName(), cluster); + } + service.validate(); + + putServiceAndInit(service); + if (!local) { + addOrReplaceService(service); } }