-
Notifications
You must be signed in to change notification settings - Fork 543
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
49467e4
commit 8ffa7e0
Showing
3 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
...iscovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulCatalogWatch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<BigInteger> 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<Map<String, List<String>>> 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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters