diff --git a/consul/src/main/java/com/networknt/consul/ConsulRegistry.java b/consul/src/main/java/com/networknt/consul/ConsulRegistry.java index d3a829c409..56d3082ca3 100644 --- a/consul/src/main/java/com/networknt/consul/ConsulRegistry.java +++ b/consul/src/main/java/com/networknt/consul/ConsulRegistry.java @@ -180,7 +180,7 @@ protected List discoverService(URL url) { synchronized (serviceName.intern()) { urls = serviceCache.get(serviceName); if (urls == null || urls .isEmpty()) { - ConcurrentHashMap> serviceUrls = lookupServiceUpdate(protocol, serviceName); + ConcurrentHashMap> serviceUrls = lookupServiceUpdate(protocol, serviceName, false); updateServiceCache(serviceName, serviceUrls, false); urls = serviceCache.get(serviceName); } @@ -190,7 +190,15 @@ protected List discoverService(URL url) { } private ConcurrentHashMap> lookupServiceUpdate(String protocol, String serviceName) { - Long lastConsulIndexId = lookupServices.get(serviceName) == null ? 0L : lookupServices.get(serviceName); + return lookupServiceUpdate(protocol, serviceName, true); + } + + private ConcurrentHashMap> lookupServiceUpdate(String protocol, String serviceName, boolean isBlockQuery) { + Long lastConsulIndexId = 0L; + if (isBlockQuery) { + lastConsulIndexId = lookupServices.get(serviceName) == null ? 0L : lookupServices.get(serviceName); + } + if(logger.isTraceEnabled()) logger.trace("serviceName = " + serviceName + " lastConsulIndexId = " + lastConsulIndexId); ConsulResponse> response = lookupConsulService(serviceName, lastConsulIndexId); if(logger.isTraceEnabled()) { @@ -220,6 +228,9 @@ private ConcurrentHashMap> lookupServiceUpdate(String protocol } lookupServices.put(serviceName, response.getConsulIndex()); return serviceUrls; + } else if (response.getConsulIndex() <= lastConsulIndexId) { + logger.info(serviceName + " lastIndex:" + lastConsulIndexId + "; response consul Index:" + response.getConsulIndex()); + lookupServices.put(serviceName, 0L); } else { logger.info(serviceName + " no need update, lastIndex:" + lastConsulIndexId); }