From 2e2f480c06d5cdc19f78821af6c90d5dd0d48981 Mon Sep 17 00:00:00 2001 From: pbting <314226532@qq.com> Date: Thu, 18 Jul 2019 16:28:12 +0800 Subject: [PATCH] add method for ServiceManager with putServiceIfAbsent --- .../nacos/naming/core/ServiceManager.java | 43 +++++++++++++++---- 1 file changed, 34 insertions(+), 9 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 564727132..dc3169f2d 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 @@ -144,11 +144,7 @@ public class ServiceManager implements RecordListener { if (oldDom != null) { oldDom.update(service); } else { - putService(service); - service.init(); - consistencyService.listen(KeyBuilder.buildInstanceListKey(service.getNamespaceId(), service.getName(), true), service); - consistencyService.listen(KeyBuilder.buildInstanceListKey(service.getNamespaceId(), service.getName(), false), service); - Loggers.SRV_LOG.info("[NEW-SERVICE] {}", service.toJSON()); + putServiceAndInit(service); } } catch (Throwable e) { Loggers.SRV_LOG.error("[NACOS-SERVICE] error while processing service update", e); @@ -409,16 +405,37 @@ public class ServiceManager implements RecordListener { } service.validate(); if (local) { - putService(service); - service.init(); - consistencyService.listen(KeyBuilder.buildInstanceListKey(service.getNamespaceId(), service.getName(), true), service); - consistencyService.listen(KeyBuilder.buildInstanceListKey(service.getNamespaceId(), service.getName(), false), service); + putServiceAndInit(service); } else { addOrReplaceService(service); } } } + public void putServiceIfAbsent(Service service, boolean local, Cluster cluster) throws NacosException { + final String namespaceId = service.getNamespaceId(); + final String serviceName = service.getName(); + + if (getService(namespaceId, serviceName) != null) { + return; + } + + Loggers.SRV_LOG.info("creating empty service {}:{}", namespaceId, 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(); + if (local) { + putServiceAndInit(service); + } else { + addOrReplaceService(service); + } + } + /** * Register an instance to a service in AP mode. *

@@ -601,6 +618,14 @@ public class ServiceManager implements RecordListener { serviceMap.get(service.getNamespaceId()).put(service.getName(), service); } + private void putServiceAndInit(Service service) throws NacosException { + putService(service); + service.init(); + consistencyService.listen(KeyBuilder.buildInstanceListKey(service.getNamespaceId(), service.getName(), true), service); + consistencyService.listen(KeyBuilder.buildInstanceListKey(service.getNamespaceId(), service.getName(), false), service); + Loggers.SRV_LOG.info("[NEW-SERVICE] {}", service.toJSON()); + } + public List searchServices(String namespaceId, String regex) { List result = new ArrayList<>(); for (Map.Entry entry : chooseServiceMap(namespaceId).entrySet()) {