update to use Ecwid/consul-api, all working except events
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-cloud-consul-core</artifactId>
|
||||
@@ -24,21 +25,13 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-commons</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.netflix.feign</groupId>
|
||||
<artifactId>feign-core</artifactId>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-commons</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.netflix.feign</groupId>
|
||||
<artifactId>feign-jackson</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.netflix.feign</groupId>
|
||||
<artifactId>feign-slf4j</artifactId>
|
||||
<groupId>com.ecwid.consul</groupId>
|
||||
<artifactId>consul-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
@@ -51,10 +44,10 @@
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
package org.springframework.cloud.consul;
|
||||
|
||||
import feign.Feign;
|
||||
import feign.Logger;
|
||||
import feign.jackson.JacksonDecoder;
|
||||
import feign.jackson.JacksonEncoder;
|
||||
import com.ecwid.consul.v1.ConsulClient;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.consul.client.*;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@@ -16,11 +12,6 @@ import org.springframework.context.annotation.Configuration;
|
||||
@Configuration
|
||||
@EnableConfigurationProperties
|
||||
public class ConsulAutoConfiguration {
|
||||
protected Feign.Builder builder = Feign.builder()
|
||||
.logger(new Logger.JavaLogger())
|
||||
.errorDecoder(new ConsulErrorDecoder())
|
||||
.decoder(new JacksonDecoder())
|
||||
.encoder(new JacksonEncoder());
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@@ -30,32 +21,8 @@ public class ConsulAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public AgentClient agentClient() {
|
||||
return builder.target(AgentClient.class, consulProperties().getUrl());
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public CatalogClient catalogClient() {
|
||||
return builder.target(CatalogClient.class, consulProperties().getUrl());
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public KeyValueClient kvClient() {
|
||||
return builder.target(KeyValueClient.class, consulProperties().getUrl());
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public EventClient eventClient() {
|
||||
return builder.target(EventClient.class, consulProperties().getUrl());
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public EventService eventService() {
|
||||
return new EventService();
|
||||
public ConsulClient consulClient() {
|
||||
return new ConsulClient(consulProperties().getHost(), consulProperties().getPort());
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
package org.springframework.cloud.consul;
|
||||
|
||||
import com.ecwid.consul.v1.ConsulClient;
|
||||
import com.ecwid.consul.v1.QueryParams;
|
||||
import com.ecwid.consul.v1.Response;
|
||||
import com.ecwid.consul.v1.agent.model.Service;
|
||||
import com.ecwid.consul.v1.catalog.model.CatalogService;
|
||||
import com.ecwid.consul.v1.catalog.model.Node;
|
||||
import lombok.Data;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.endpoint.AbstractEndpoint;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.consul.client.AgentClient;
|
||||
import org.springframework.cloud.consul.client.CatalogClient;
|
||||
import org.springframework.cloud.consul.client.KeyValueClient;
|
||||
import org.springframework.cloud.consul.model.KeyValue;
|
||||
import org.springframework.cloud.consul.model.Service;
|
||||
import org.springframework.cloud.consul.model.ServiceNode;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -22,13 +22,7 @@ import java.util.Map;
|
||||
public class ConsulEndpoint extends AbstractEndpoint<ConsulEndpoint.ConsulData> {
|
||||
|
||||
@Autowired
|
||||
KeyValueClient keyValueClient;
|
||||
|
||||
@Autowired
|
||||
CatalogClient catalogClient;
|
||||
|
||||
@Autowired
|
||||
AgentClient agentClient;
|
||||
private ConsulClient consul;
|
||||
|
||||
@Autowired
|
||||
public ConsulEndpoint() {
|
||||
@@ -39,25 +33,31 @@ public class ConsulEndpoint extends AbstractEndpoint<ConsulEndpoint.ConsulData>
|
||||
public ConsulData invoke() {
|
||||
ConsulData data = new ConsulData();
|
||||
//data.setKeyValues(kvClient.getKeyValueRecurse());
|
||||
data.setCatalogServices(catalogClient.getServices());
|
||||
Map<String, Service> services = agentClient.getServices();
|
||||
data.setAgentServices(services);
|
||||
Response<Map<String, Service>> agentServices = consul.getAgentServices();
|
||||
data.setAgentServices(agentServices.getValue());
|
||||
|
||||
for (String serviceId : services.keySet()) {
|
||||
data.getCatalogServiceNodes().put(serviceId, catalogClient.getServiceNodes(serviceId));
|
||||
Response<Map<String, List<String>>> catalogServices = consul.getCatalogServices(QueryParams.DEFAULT);
|
||||
|
||||
|
||||
for (String serviceId : catalogServices.getValue().keySet()) {
|
||||
Response<List<CatalogService>> response = consul.getCatalogService(serviceId, QueryParams.DEFAULT);
|
||||
data.getCatalogServices().put(serviceId, response.getValue());
|
||||
}
|
||||
|
||||
Response<List<Node>> catalogNodes = consul.getCatalogNodes(QueryParams.DEFAULT);
|
||||
data.setCatalogNodes(catalogNodes.getValue());
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class ConsulData {
|
||||
Map<String, List<String>> catalogServices;
|
||||
|
||||
Map<String, List<ServiceNode>> catalogServiceNodes = new LinkedHashMap<>();
|
||||
Map<String, List<CatalogService>> catalogServices = new LinkedHashMap<>();
|
||||
|
||||
Map<String, Service> agentServices;
|
||||
|
||||
List<KeyValue> keyValues;
|
||||
List<Node> catalogNodes;
|
||||
|
||||
//List<KeyValue> keyValues;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package org.springframework.cloud.consul;
|
||||
|
||||
import com.ecwid.consul.v1.ConsulClient;
|
||||
import com.ecwid.consul.v1.QueryParams;
|
||||
import com.ecwid.consul.v1.Response;
|
||||
import com.ecwid.consul.v1.agent.model.Self;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.cloud.consul.client.CatalogClient;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -14,13 +17,16 @@ import java.util.Map;
|
||||
public class ConsulHealthIndicator extends AbstractHealthIndicator {
|
||||
|
||||
@Autowired
|
||||
private CatalogClient catalogClient;
|
||||
private ConsulClient consul;
|
||||
|
||||
@Override
|
||||
protected void doHealthCheck(Health.Builder builder) throws Exception {
|
||||
try {
|
||||
Map<String, List<String>> services = catalogClient.getServices();
|
||||
builder.up().withDetail("services", services);
|
||||
Response<Self> self = consul.getAgentSelf();
|
||||
Response<Map<String, List<String>>> services = consul.getCatalogServices(QueryParams.DEFAULT);
|
||||
builder.up()
|
||||
.withDetail("services", services.getValue())
|
||||
.withDetail("agent", self.getValue());
|
||||
} catch (Exception e) {
|
||||
builder.down(e);
|
||||
}
|
||||
|
||||
@@ -15,7 +15,10 @@ import java.util.List;
|
||||
@Data
|
||||
public class ConsulProperties {
|
||||
@NotNull
|
||||
private String url = "http://localhost:8500";
|
||||
private String host = "localhost";
|
||||
|
||||
@NotNull
|
||||
private int port = 8500;
|
||||
|
||||
private List<String> tags = new ArrayList<>();
|
||||
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
package org.springframework.cloud.consul.client;
|
||||
|
||||
import feign.Param;
|
||||
import feign.RequestLine;
|
||||
import org.springframework.cloud.consul.model.Service;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public interface AgentClient {
|
||||
@RequestLine("GET /v1/agent/services")
|
||||
Map<String, Service> getServices();
|
||||
|
||||
@RequestLine("GET /v1/agent/self")
|
||||
//TODO change map to an object
|
||||
Map<String, Object> getSelf();
|
||||
|
||||
@RequestLine("PUT /v1/agent/service/register")
|
||||
void register(Service service);
|
||||
|
||||
@RequestLine("PUT /v1/agent/service/deregister/{serviceId}")
|
||||
void deregister(@Param("serviceId") String serviceId);
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package org.springframework.cloud.consul.client;
|
||||
|
||||
import feign.Param;
|
||||
import feign.RequestLine;
|
||||
import org.springframework.cloud.consul.model.ServiceNode;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public interface CatalogClient {
|
||||
@RequestLine("GET /v1/catalog/services")
|
||||
Map<String, List<String>> getServices();
|
||||
|
||||
@RequestLine("GET /v1/catalog/service/{serviceId}")
|
||||
List<ServiceNode> getServiceNodes(@Param("serviceId") String serviceId);
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package org.springframework.cloud.consul.client;
|
||||
|
||||
import feign.Response;
|
||||
import feign.codec.ErrorDecoder;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public class ConsulErrorDecoder extends ErrorDecoder.Default {
|
||||
@Override
|
||||
public Exception decode(String methodKey, Response response) {
|
||||
if (response.status() == 404) {
|
||||
throw new NotFoundException(response);
|
||||
}
|
||||
return super.decode(methodKey, response);
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package org.springframework.cloud.consul.client;
|
||||
|
||||
import feign.Param;
|
||||
import feign.RequestLine;
|
||||
import feign.Response;
|
||||
import org.springframework.cloud.consul.model.Event;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public interface EventClient {
|
||||
//?node=, ?service=, and ?tag= ?dc=
|
||||
@RequestLine("PUT /v1/event/fire/{name}")
|
||||
Event fire(@Param("name") String name, String payload);
|
||||
|
||||
//?name=
|
||||
//?wait=<interval>&index=<idx>
|
||||
@RequestLine("GET /v1/event/list")
|
||||
List<Event> getEvents();
|
||||
|
||||
@RequestLine("GET /v1/event/list")
|
||||
Response getEventsResponse();
|
||||
|
||||
@RequestLine("GET /v1/event/list?wait={wait}&index={index}")
|
||||
Response watch(@Param("wait") String wait, @Param("index") String index);
|
||||
}
|
||||
@@ -1,131 +0,0 @@
|
||||
package org.springframework.cloud.consul.client;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.type.TypeFactory;
|
||||
import com.google.common.base.Throwables;
|
||||
import feign.Param;
|
||||
import feign.Response;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.consul.model.Event;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public class EventService {
|
||||
|
||||
@Autowired
|
||||
protected EventClient client;
|
||||
|
||||
@Autowired(required = false)
|
||||
protected ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
private AtomicReference<BigInteger> lastIndex = new AtomicReference<>();
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
Response response = getEventsResponse();
|
||||
setLastIndex(response);
|
||||
}
|
||||
|
||||
private void setLastIndex(Response response) {
|
||||
Collection<String> header = response.headers().get("X-Consul-Index");
|
||||
if (header != null && header.iterator().hasNext()) {
|
||||
lastIndex.set(new BigInteger(header.iterator().next()));
|
||||
}
|
||||
}
|
||||
|
||||
public BigInteger getLastIndex() {
|
||||
return lastIndex.get();
|
||||
}
|
||||
|
||||
public Event fire(@Param("name") String name, String payload) {
|
||||
return client.fire(name, payload);
|
||||
}
|
||||
|
||||
public Response getEventsResponse() {
|
||||
return client.getEventsResponse();
|
||||
}
|
||||
|
||||
public List<Event> getEvents() {
|
||||
return client.getEvents();
|
||||
}
|
||||
|
||||
/**
|
||||
* from https://github.com/armon/consul-api/blob/master/event.go#L92-L104
|
||||
// IDToIndex is a bit of a hack. This simulates the index generation to
|
||||
// convert an event ID into a WaitIndex.
|
||||
func (e *Event) IDToIndex(uuid string) uint64 {
|
||||
lower := uuid[0:8] + uuid[9:13] + uuid[14:18]
|
||||
upper := uuid[19:23] + uuid[24:36]
|
||||
lowVal, err := strconv.ParseUint(lower, 16, 64)
|
||||
if err != nil {
|
||||
panic("Failed to convert " + lower)
|
||||
}
|
||||
highVal, err := strconv.ParseUint(upper, 16, 64)
|
||||
if err != nil {
|
||||
panic("Failed to convert " + upper)
|
||||
}
|
||||
return lowVal ^ highVal
|
||||
//^ bitwise XOR integers
|
||||
}
|
||||
*/
|
||||
public BigInteger toIndex(String eventId) {
|
||||
String lower = eventId.substring(0, 8) + eventId.substring(9, 13) + eventId.substring(14, 18);
|
||||
String upper = eventId.substring(19, 23) + eventId.substring(24, 36);
|
||||
BigInteger lowVal = new BigInteger(lower, 16);
|
||||
BigInteger highVal = new BigInteger(upper, 16);
|
||||
BigInteger index = lowVal.xor(highVal);
|
||||
return index;
|
||||
}
|
||||
|
||||
public List<Event> getEvents(BigInteger lastIndex) {
|
||||
return filterEvents(readEvents(getEventsResponse()), lastIndex);
|
||||
}
|
||||
|
||||
public List<Event> watch() {
|
||||
return watch(lastIndex.get());
|
||||
}
|
||||
|
||||
public List<Event> watch(BigInteger lastIndex) {
|
||||
//TODO: parameterized or configurable watch time
|
||||
return filterEvents(readEvents(client.watch("2s", lastIndex.toString())), lastIndex);
|
||||
}
|
||||
|
||||
protected List<Event> readEvents(Response response) {
|
||||
try {
|
||||
setLastIndex(response);
|
||||
return objectMapper.readValue(response.body().asInputStream(),
|
||||
TypeFactory.defaultInstance().constructCollectionType(ArrayList.class, Event.class));
|
||||
} catch (IOException e) {
|
||||
Throwables.propagate(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* from https://github.com/hashicorp/consul/blob/master/watch/funcs.go#L169-L194
|
||||
*/
|
||||
protected List<Event> filterEvents(List<Event> toFilter, BigInteger lastIndex) {
|
||||
List<Event> events = toFilter;
|
||||
if (lastIndex != null) {
|
||||
for (int i = 0; i < events.size(); i++) {
|
||||
Event event = events.get(i);
|
||||
BigInteger eventIndex = toIndex(event.getId());
|
||||
if (eventIndex.equals(lastIndex)) {
|
||||
events = events.subList(i + 1, events.size());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return events;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package org.springframework.cloud.consul.client;
|
||||
|
||||
import feign.Param;
|
||||
import feign.RequestLine;
|
||||
import org.springframework.cloud.consul.model.KeyValue;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public interface KeyValueClient {
|
||||
@RequestLine("GET /v1/kv/{key}")
|
||||
List<KeyValue> getKeyValue(@Param("key") String key);
|
||||
|
||||
@RequestLine("GET /v1/kv/?recurse=true")
|
||||
List<KeyValue> getKeyValueRecurse();
|
||||
|
||||
@RequestLine("GET /v1/kv/{key}?recurse=true")
|
||||
List<KeyValue> getKeyValueRecurse(@Param("key") String key);
|
||||
|
||||
@RequestLine("GET /v1/kv/?keys=true")
|
||||
List<String> getKeys();
|
||||
|
||||
@RequestLine("GET /v1/kv/{key}?keys=true")
|
||||
List<String> getKeys(@Param("key") String key);
|
||||
|
||||
@RequestLine("PUT /v1/kv/{key}")
|
||||
boolean put(@Param("key") String key, Object value);
|
||||
|
||||
@RequestLine("DELETE /v1/kv/{key}")
|
||||
void delete(@Param("key") String key);
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package org.springframework.cloud.consul.client;
|
||||
|
||||
import feign.Response;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@Data
|
||||
public class NotFoundException extends RuntimeException {
|
||||
private final Response response;
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package org.springframework.cloud.consul.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@Data
|
||||
public class Check {
|
||||
@JsonProperty("Script")
|
||||
private String script;
|
||||
|
||||
@JsonProperty("Interval")
|
||||
private int interval;
|
||||
|
||||
@JsonProperty("TTL")
|
||||
private int ttl;
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
package org.springframework.cloud.consul.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import static com.google.common.base.Charsets.UTF_8;
|
||||
import static com.google.common.base.MoreObjects.toStringHelper;
|
||||
import static com.google.common.io.BaseEncoding.base64;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
* Example:
|
||||
* "ID": "b54fe110-7af5-cafc-d1fb-afc8ba432b1c",
|
||||
"Name": "deploy",
|
||||
"Payload": null,
|
||||
"NodeFilter": "",
|
||||
"ServiceFilter": "",
|
||||
"TagFilter": "",
|
||||
"Version": 1,
|
||||
"LTime": 0
|
||||
*/
|
||||
@Data
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class Event {
|
||||
@JsonProperty("ID")
|
||||
private String id;
|
||||
|
||||
@JsonProperty("Name")
|
||||
private String name;
|
||||
|
||||
@JsonProperty("NodeFilter")
|
||||
private String nodeFilter;
|
||||
|
||||
@JsonProperty("ServiceFilter")
|
||||
private String serviceFilter;
|
||||
|
||||
@JsonProperty("TagFilter")
|
||||
private String tagFilter;
|
||||
|
||||
@JsonProperty("Version")
|
||||
private Long version;
|
||||
|
||||
@JsonProperty("LTime")
|
||||
private Long lTime;
|
||||
|
||||
@JsonProperty("Payload")
|
||||
private String payload;
|
||||
|
||||
public String getDecoded() {
|
||||
if (payload == null)
|
||||
return null;
|
||||
return new String(base64().decode(payload), UTF_8);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return toStringHelper(this)
|
||||
.add("id", id)
|
||||
.add("name", name)
|
||||
.add("nodeFilter", nodeFilter)
|
||||
.add("serviceFilter", serviceFilter)
|
||||
.add("tagFilter", tagFilter)
|
||||
.add("version", version)
|
||||
.add("lTime", lTime)
|
||||
.add("payload", payload)
|
||||
.add("decodedPayload", getDecoded())
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
package org.springframework.cloud.consul.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import static com.google.common.base.Charsets.UTF_8;
|
||||
import static com.google.common.base.MoreObjects.toStringHelper;
|
||||
import static com.google.common.io.BaseEncoding.base64;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@Data
|
||||
public class KeyValue {
|
||||
@JsonProperty("Key")
|
||||
private String key;
|
||||
|
||||
@JsonProperty("Value")
|
||||
private String value;
|
||||
|
||||
@JsonProperty("CreateIndex")
|
||||
private Long createIndex;
|
||||
|
||||
@JsonProperty("ModifyIndex")
|
||||
private Long modifyIndex;
|
||||
|
||||
@JsonProperty("Flags")
|
||||
private Long flags;
|
||||
|
||||
//TODO: use jackson to do the encoded/decoding
|
||||
public String getDecoded() {
|
||||
if (value == null)
|
||||
return null;
|
||||
return new String(base64().decode(value), UTF_8);
|
||||
}
|
||||
|
||||
public void setUnencoded(String unencoded) {
|
||||
setValue(base64().encode(unencoded.getBytes(UTF_8)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return toStringHelper(this)
|
||||
.add("key", key)
|
||||
.add("value", value)
|
||||
.add("decodedValue", getDecoded())
|
||||
.add("createIndex", createIndex)
|
||||
.add("modifyIndex", modifyIndex)
|
||||
.add("flags", flags)
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package org.springframework.cloud.consul.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@Data
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class Service {
|
||||
@JsonProperty("ID")
|
||||
private String id;
|
||||
|
||||
@JsonProperty("Name")
|
||||
private String name;
|
||||
|
||||
@JsonProperty("Tags")
|
||||
private List<String> tags;
|
||||
|
||||
@JsonProperty("Port")
|
||||
private int port;
|
||||
|
||||
@JsonProperty("Check")
|
||||
private Check check;
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package org.springframework.cloud.consul.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@Data
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class ServiceNode {
|
||||
@JsonProperty("Node")
|
||||
private String node;
|
||||
|
||||
@JsonProperty("Address")
|
||||
private String address;
|
||||
|
||||
@JsonProperty("ServiceID")
|
||||
private String serviceID;
|
||||
|
||||
@JsonProperty("ServiceName")
|
||||
private String serviceName;
|
||||
|
||||
@JsonProperty("ServiceTags")
|
||||
private List<String> serviceTags;
|
||||
|
||||
@JsonProperty("ServicePort")
|
||||
private int servicePort;
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package org.springframework.cloud.consul.client;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.MethodSorters;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.cloud.consul.model.Check;
|
||||
import org.springframework.cloud.consul.model.Service;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
* Date: 4/18/14
|
||||
* Time: 11:04 AM
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
@SpringApplicationConfiguration(classes = TestClientConfiguration.class)
|
||||
public class AgentClientIT {
|
||||
|
||||
@Autowired
|
||||
AgentClient client;
|
||||
|
||||
@Test
|
||||
public void test001RegisterService() {
|
||||
Service service = new Service();
|
||||
service.setId("test1id");
|
||||
service.setName("test1Name");
|
||||
service.setPort(9999);
|
||||
service.setTags(Lists.newArrayList("test1tag1", "test1tag2"));
|
||||
Check check = new Check();
|
||||
check.setScript("/usr/local/bin/gtrue");
|
||||
check.setInterval(60);
|
||||
service.setCheck(check);
|
||||
client.register(service);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test002GetServices() {
|
||||
Map<String, Service> services = client.getServices();
|
||||
assertNotNull("services was null", services);
|
||||
assertFalse("services was empty", services.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test003DeregisterService() {
|
||||
client.deregister("test1id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test004GetSelf() {
|
||||
Map<String, Object> self = client.getSelf();
|
||||
assertNotNull("self was null", self);
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
package org.springframework.cloud.consul.client;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.cloud.consul.model.ServiceNode;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
* Date: 4/18/14
|
||||
* Time: 11:04 AM
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = TestClientConfiguration.class)
|
||||
public class CatalogClientIT {
|
||||
|
||||
@Autowired
|
||||
CatalogClient client;
|
||||
|
||||
@Test
|
||||
public void testGetServices() {
|
||||
Map<String, List<String>> services = client.getServices();
|
||||
assertNotNull("services is null", services);
|
||||
assertTrue("No consul key", services.containsKey("consul"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetService() {
|
||||
List<ServiceNode> serviceNodes = client.getServiceNodes("consul");
|
||||
assertNotNull("serviceNodes is null", serviceNodes);
|
||||
assertFalse("serviceNodes is empty", serviceNodes.isEmpty());
|
||||
|
||||
ServiceNode node = serviceNodes.get(0);
|
||||
|
||||
assertNotNull("address is null", node.getAddress());
|
||||
assertNotNull("node is null", node.getNode());
|
||||
assertNotNull("serviceId is null", node.getServiceID());
|
||||
assertNotNull("serviceName is null", node.getServiceName());
|
||||
assertTrue("servicePort is wrong", node.getServicePort() > 0);
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
package org.springframework.cloud.consul.client;
|
||||
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.MethodSorters;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.cloud.consul.model.Event;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = TestClientConfiguration.class)
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class EventClientIT {
|
||||
|
||||
public static final String NAME = "testEvent";
|
||||
public static final String PAYLOAD = "TestPayload." + System.currentTimeMillis();
|
||||
|
||||
@Autowired
|
||||
private EventClient client;
|
||||
|
||||
@Test
|
||||
public void test001Fire() {
|
||||
Event event = client.fire(NAME, PAYLOAD);
|
||||
assertNotNull("Event was null", event);
|
||||
assertNotNull("Event Id was null", event.getId());
|
||||
assertEquals("Event name was wrong", NAME, event.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test002Get() {
|
||||
List<Event> values = client.getEvents();
|
||||
assertNotNull("events is null", values);
|
||||
assertFalse("Values is empty", values.isEmpty());
|
||||
/*assertTrue("Values is not size 1", values.size() == 1);
|
||||
KeyValue keyValue = values.get(0);
|
||||
//TODO: how to deal with this?
|
||||
String decoded = objectMapper.readValue(keyValue.getDecoded(), String.class);
|
||||
|
||||
assertEquals(decoded, VALUE);*/
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package org.springframework.cloud.consul.client;
|
||||
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.MethodSorters;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.cloud.consul.model.Event;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = TestClientConfiguration.class)
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class EventServiceIT {
|
||||
|
||||
@Autowired
|
||||
private EventService service;
|
||||
|
||||
@Test
|
||||
public void test001InitialIndex() {
|
||||
assertNotNull("initialIndex was null", service.getLastIndex());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test002ToIndex() {
|
||||
String eventId = "bf24ae36-d240-9666-7343-1a87346d2f94";
|
||||
BigInteger index = service.toIndex(eventId);
|
||||
assertEquals("wrong index generated", new BigInteger("14728939782502463986"), index);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test003GetEvents() {
|
||||
Event event = service.fire("testEvent", "test003GetEvents" + System.currentTimeMillis());
|
||||
assertNotNull("event was null", event);
|
||||
|
||||
List<Event> events = service.getEvents(service.getLastIndex());
|
||||
assertNotNull("events was null", events);
|
||||
assertFalse("events was empty", events.isEmpty());
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
package org.springframework.cloud.consul.client;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.MethodSorters;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.cloud.consul.model.KeyValue;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = TestClientConfiguration.class)
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class KeyValueClientIT {
|
||||
|
||||
public static final String KEY = "test/testkey";
|
||||
public static final String VALUE = "TestPut." + System.currentTimeMillis();
|
||||
|
||||
@Autowired
|
||||
KeyValueClient keyValueClient;
|
||||
|
||||
@Autowired(required = false)
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Test
|
||||
public void test001Put() {
|
||||
|
||||
boolean actual = keyValueClient.put(KEY, VALUE);
|
||||
assertTrue("Invalid resposne", actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test002Get() throws IOException {
|
||||
List<KeyValue> values = keyValueClient.getKeyValue(KEY);
|
||||
assertNotNull("values is null", values);
|
||||
assertFalse("Values is null", values.isEmpty());
|
||||
assertTrue("Values is not size 1", values.size() == 1);
|
||||
KeyValue keyValue = values.get(0);
|
||||
//TODO: how to deal with this?
|
||||
String decoded = objectMapper.readValue(keyValue.getDecoded(), String.class);
|
||||
|
||||
assertEquals(decoded, VALUE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test003GetKeyRecurse() {
|
||||
List<KeyValue> values = keyValueClient.getKeyValueRecurse(KEY);
|
||||
assertNotNull("values is null", values);
|
||||
assertFalse("Values is null", values.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test004GetRecurse() {
|
||||
List<KeyValue> values = keyValueClient.getKeyValueRecurse();
|
||||
assertNotNull("values is null", values);
|
||||
assertFalse("Values is null", values.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test005Delete() {
|
||||
keyValueClient.delete(KEY);
|
||||
}
|
||||
|
||||
@Test(expected = NotFoundException.class)
|
||||
public void test006KeyNotFound() {
|
||||
keyValueClient.getKeyValue(System.currentTimeMillis()+"a123");
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package org.springframework.cloud.consul.client;
|
||||
|
||||
import org.springframework.cloud.consul.ConsulAutoConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@Configuration
|
||||
@Import(ConsulAutoConfiguration.class)
|
||||
public class TestClientConfiguration {
|
||||
}
|
||||
Reference in New Issue
Block a user