From 8ffa7e04eadb7ccdd44749a10c62f07b4cdc17f2 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Fri, 22 May 2015 14:08:06 -0600 Subject: [PATCH] add catalog services watch that sends a HeartbeatEvent. uses consul last index as the value to compare in the HeartbeatEvent. Sends HeartbeatEvent so components such as Zuul can respond to changes in the services. fixes gh-1 --- .../consul/discovery/ConsulCatalogWatch.java | 72 +++++++++++++++++++ .../ConsulDiscoveryClientConfiguration.java | 5 ++ .../discovery/ConsulDiscoveryProperties.java | 4 ++ 3 files changed, 81 insertions(+) create mode 100644 spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulCatalogWatch.java 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 new file mode 100644 index 00000000..152bac82 --- /dev/null +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulCatalogWatch.java @@ -0,0 +1,72 @@ +package org.springframework.cloud.consul.discovery;/* + * Copyright 2013-2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +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; +import org.springframework.scheduling.annotation.Scheduled; + +import com.ecwid.consul.v1.ConsulClient; +import com.ecwid.consul.v1.QueryParams; +import com.ecwid.consul.v1.Response; + +/** + * @author Spencer Gibb + */ +@Slf4j +public class ConsulCatalogWatch implements ApplicationEventPublisherAware { + + private final ConsulDiscoveryProperties properties; + private final ConsulClient consul; + private final AtomicReference catalogServicesIndex = new AtomicReference<>(); + private ApplicationEventPublisher publisher; + + public ConsulCatalogWatch(ConsulDiscoveryProperties properties, ConsulClient consul) { + this.properties = properties; + this.consul = consul; + } + + @Override + public void setApplicationEventPublisher(ApplicationEventPublisher publisher) { + this.publisher = publisher; + } + + @Scheduled(fixedDelayString = "${spring.cloud.consul.discovery.catalogServicesWatchDelay:10}") + public void catalogServicesWatch() { + 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)); + } + + log.debug("Received services update from consul: {}", response.getValue()); + publisher.publishEvent(new HeartbeatEvent(this, consulIndex)); + } +} 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 7413da9d..51654633 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 @@ -59,4 +59,9 @@ public class ConsulDiscoveryClientConfiguration { public ConsulDiscoveryClient consulDiscoveryClient() { return new ConsulDiscoveryClient(); } + + @Bean + public ConsulCatalogWatch consulCatalogWatch() { + return new ConsulCatalogWatch(consulDiscoveryProperties(), consulClient); + } } diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryProperties.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryProperties.java index 1b16110a..e9bf10d8 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryProperties.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryProperties.java @@ -60,6 +60,10 @@ public class ConsulDiscoveryProperties { private boolean preferIpAddress = false; + private int catalogServicesWatchDelay = 10; + + private int catalogServicesWatchTimeout = 2; + public String getHostname() { return this.preferIpAddress ? this.ipAddress : this.hostname; }