From 8d43d78959a262efb6c0c68389760beddf62e9c0 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Wed, 6 Apr 2016 11:58:13 -0600 Subject: [PATCH] Make dealing with catalog watch errors easier. Uses logger to log errors. Adds ability to disable watch. see gh-169 --- .../consul/discovery/ConsulCatalogWatch.java | 40 +++++++++++-------- .../ConsulDiscoveryClientConfiguration.java | 1 + 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulCatalogWatch.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulCatalogWatch.java index 9009842b..c184f4c9 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulCatalogWatch.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulCatalogWatch.java @@ -1,4 +1,4 @@ -package org.springframework.cloud.consul.discovery;/* +/* * Copyright 2013-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,13 +14,13 @@ package org.springframework.cloud.consul.discovery;/* * limitations under the License. */ +package org.springframework.cloud.consul.discovery; + import java.math.BigInteger; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicReference; -import lombok.extern.slf4j.Slf4j; - import org.springframework.cloud.client.discovery.event.HeartbeatEvent; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisherAware; @@ -30,6 +30,8 @@ import com.ecwid.consul.v1.ConsulClient; import com.ecwid.consul.v1.QueryParams; import com.ecwid.consul.v1.Response; +import lombok.extern.slf4j.Slf4j; + /** * @author Spencer Gibb */ @@ -53,20 +55,26 @@ public class ConsulCatalogWatch implements ApplicationEventPublisherAware { @Scheduled(fixedDelayString = "${spring.cloud.consul.discovery.catalogServicesWatchDelay:30000}") public void catalogServicesWatch() { - long index = -1; - if (catalogServicesIndex.get() != null) { - index = catalogServicesIndex.get().longValue(); - } + try { + long index = -1; + if (catalogServicesIndex.get() != null) { + index = catalogServicesIndex.get().longValue(); + } - Response>> response = consul - .getCatalogServices(new QueryParams(properties - .getCatalogServicesWatchTimeout(), index)); - Long consulIndex = response.getConsulIndex(); - if (consulIndex != null) { - catalogServicesIndex.set(BigInteger.valueOf(consulIndex)); - } + Response>> response = consul + .getCatalogServices(new QueryParams(properties + .getCatalogServicesWatchTimeout(), index)); + Long consulIndex = response.getConsulIndex(); + if (consulIndex != null) { + catalogServicesIndex.set(BigInteger.valueOf(consulIndex)); + } - log.trace("Received services update from consul: {}, index: {}", response.getValue(), consulIndex); - publisher.publishEvent(new HeartbeatEvent(this, consulIndex)); + log.trace("Received services update from consul: {}, index: {}", + response.getValue(), consulIndex); + publisher.publishEvent(new HeartbeatEvent(this, consulIndex)); + } + catch (Exception e) { + log.error("Error watching Consul CatalogServices", e); + } } } diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClientConfiguration.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClientConfiguration.java index 2082685e..6c5cb312 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClientConfiguration.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClientConfiguration.java @@ -79,6 +79,7 @@ public class ConsulDiscoveryClientConfiguration { @Bean @ConditionalOnMissingBean + @ConditionalOnProperty(name = "spring.cloud.consul.discovery.catalogServicesWatch.enabled", matchIfMissing = true) public ConsulCatalogWatch consulCatalogWatch( ConsulDiscoveryProperties discoveryProperties) { return new ConsulCatalogWatch(discoveryProperties, consulClient);