diff --git a/pom.xml b/pom.xml index cafae230..07e113be 100644 --- a/pom.xml +++ b/pom.xml @@ -107,6 +107,23 @@ spring-cloud-config-client 1.0.0.BUILD-SNAPSHOT + + com.ecwid.consul + consul-api + 0.1 + + + javax.servlet + servlet-api + + + + + + org.apache.httpcomponents + httpclient + 4.2.5 + org.springframework.cloud spring-cloud-starter-zuul @@ -132,21 +149,6 @@ spring-cloud-netflix-core 1.0.0.BUILD-SNAPSHOT - - com.netflix.feign - feign-core - ${feign.version} - - - com.netflix.feign - feign-jackson - ${feign.version} - - - com.netflix.feign - feign-slf4j - ${feign.version} - com.netflix.ribbon ribbon diff --git a/spring-cloud-consul-bus/src/main/java/org/springframework/cloud/consul/bus/ConsulBusAutoConfiguration.java b/spring-cloud-consul-bus/src/main/java/org/springframework/cloud/consul/bus/ConsulBusAutoConfiguration.java index 6fdd13e5..8abb03b1 100644 --- a/spring-cloud-consul-bus/src/main/java/org/springframework/cloud/consul/bus/ConsulBusAutoConfiguration.java +++ b/spring-cloud-consul-bus/src/main/java/org/springframework/cloud/consul/bus/ConsulBusAutoConfiguration.java @@ -1,5 +1,6 @@ package org.springframework.cloud.consul.bus; +import com.ecwid.consul.v1.ConsulClient; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; @@ -8,7 +9,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.cloud.bus.BusAutoConfiguration; import org.springframework.cloud.bus.event.RemoteApplicationEvent; -import org.springframework.cloud.consul.client.EventClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.integration.dsl.IntegrationFlow; @@ -22,7 +22,7 @@ import org.springframework.scheduling.annotation.EnableScheduling; * @author Spencer Gibb */ @Configuration -@ConditionalOnClass(EventClient.class) +@ConditionalOnClass(ConsulClient.class) @ConditionalOnExpression("${bus.consul.enabled:true}") @AutoConfigureAfter(BusAutoConfiguration.class) @EnableScheduling diff --git a/spring-cloud-consul-bus/src/main/java/org/springframework/cloud/consul/bus/ConsulInboundChannelAdapter.java b/spring-cloud-consul-bus/src/main/java/org/springframework/cloud/consul/bus/ConsulInboundChannelAdapter.java index a3a70c28..ee9f773f 100644 --- a/spring-cloud-consul-bus/src/main/java/org/springframework/cloud/consul/bus/ConsulInboundChannelAdapter.java +++ b/spring-cloud-consul-bus/src/main/java/org/springframework/cloud/consul/bus/ConsulInboundChannelAdapter.java @@ -2,8 +2,6 @@ package org.springframework.cloud.consul.bus; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cloud.consul.client.EventService; -import org.springframework.cloud.consul.model.Event; import org.springframework.integration.endpoint.MessageProducerSupport; import org.springframework.scheduling.annotation.Scheduled; @@ -18,8 +16,8 @@ import java.util.Map; * @author Spencer Gibb */ public class ConsulInboundChannelAdapter extends MessageProducerSupport { - @Autowired - private EventService eventService; + //@Autowired + //private EventService eventService; @Autowired private ObjectMapper objectMapper; @@ -47,7 +45,7 @@ public class ConsulInboundChannelAdapter extends MessageProducerSupport { @Scheduled(fixedDelayString = "10") public void getEvents() throws IOException { - List events = eventService.watch(); + /*FIXME: List events = eventService.watch(); for (Event event : events) { Map headers = new HashMap<>(); //headers.put(MessageHeaders.REPLY_CHANNEL, outputChannel.) @@ -56,7 +54,7 @@ public class ConsulInboundChannelAdapter extends MessageProducerSupport { .withPayload(objectMapper.readValue(event.getDecoded(), String.class)) //TODO: support headers .build()); - } + }*/ } @Override diff --git a/spring-cloud-consul-bus/src/main/java/org/springframework/cloud/consul/bus/ConsulOutboundEndpoint.java b/spring-cloud-consul-bus/src/main/java/org/springframework/cloud/consul/bus/ConsulOutboundEndpoint.java index ebe008c1..7b8d5d0f 100644 --- a/spring-cloud-consul-bus/src/main/java/org/springframework/cloud/consul/bus/ConsulOutboundEndpoint.java +++ b/spring-cloud-consul-bus/src/main/java/org/springframework/cloud/consul/bus/ConsulOutboundEndpoint.java @@ -1,8 +1,11 @@ package org.springframework.cloud.consul.bus; +import com.ecwid.consul.v1.ConsulClient; +import com.ecwid.consul.v1.QueryParams; +import com.ecwid.consul.v1.Response; +import com.ecwid.consul.v1.event.model.Event; +import com.ecwid.consul.v1.event.model.EventParams; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cloud.consul.client.EventService; -import org.springframework.cloud.consul.model.Event; import org.springframework.integration.handler.AbstractReplyProducingMessageHandler; import org.springframework.messaging.Message; @@ -13,14 +16,14 @@ import org.springframework.messaging.Message; public class ConsulOutboundEndpoint extends AbstractReplyProducingMessageHandler { @Autowired - protected EventService eventService; + protected ConsulClient consul; @Override protected Object handleRequestMessage(Message requestMessage) { Object payload = requestMessage.getPayload(); //TODO: support headers //TODO: support consul event filters: NodeFilter, ServiceFilter, TagFilter - Event event = eventService.fire("springCloudBus", (String) payload); + Response event = consul.eventFire("springCloudBus", (String) payload, new EventParams(), QueryParams.DEFAULT); //TODO: return event? return null; } diff --git a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySource.java b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySource.java index c648b335..9364c010 100644 --- a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySource.java +++ b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySource.java @@ -1,22 +1,26 @@ package org.springframework.cloud.consul.config; -import org.springframework.cloud.consul.client.KeyValueClient; -import org.springframework.cloud.consul.client.NotFoundException; -import org.springframework.cloud.consul.model.KeyValue; +import com.ecwid.consul.v1.ConsulClient; +import com.ecwid.consul.v1.QueryParams; +import com.ecwid.consul.v1.Response; +import com.ecwid.consul.v1.kv.model.GetValue; import org.springframework.core.env.EnumerablePropertySource; import java.util.*; +import static com.google.common.base.Charsets.UTF_8; +import static com.google.common.io.BaseEncoding.base64; + /** * @author Spencer Gibb */ -public class ConsulPropertySource extends EnumerablePropertySource { +public class ConsulPropertySource extends EnumerablePropertySource { private String context; private Map properties = new LinkedHashMap<>(); - public ConsulPropertySource(String context, KeyValueClient source) { + public ConsulPropertySource(String context, ConsulClient source) { super(context, source); this.context = context; @@ -26,21 +30,26 @@ public class ConsulPropertySource extends EnumerablePropertySource keyValues = source.getKeyValueRecurse(context); + Response> response = source.getKVValues(context, QueryParams.DEFAULT); + List values = response.getValue(); - for (KeyValue keyValue : keyValues) { - String key = keyValue.getKey() - .replace(context, "") - .replace('/', '.'); - String value = keyValue.getDecoded(); + if (values != null) { + for (GetValue getValue : values) { + String key = getValue.getKey() + .replace(context, "") + .replace('/', '.'); + String value = getDecoded(getValue.getValue()); properties.put(key, value); } - } catch (NotFoundException e) { - //not found, do nothing } } + public String getDecoded(String value) { + if (value == null) + return null; + return new String(base64().decode(value), UTF_8); + } + @Override public Object getProperty(String name) { return properties.get(name); diff --git a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocator.java b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocator.java index f3c949d4..d5862d2d 100644 --- a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocator.java +++ b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocator.java @@ -1,9 +1,9 @@ package org.springframework.cloud.consul.config; +import com.ecwid.consul.v1.ConsulClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.config.client.PropertySourceLocator; import org.springframework.cloud.consul.ConsulProperties; -import org.springframework.cloud.consul.client.KeyValueClient; import org.springframework.core.env.*; import java.util.ArrayList; @@ -16,7 +16,7 @@ import java.util.List; public class ConsulPropertySourceLocator implements PropertySourceLocator { @Autowired - private KeyValueClient keyValueClient; + private ConsulClient consul; @Autowired private ConsulProperties properties; @@ -53,7 +53,7 @@ public class ConsulPropertySourceLocator implements PropertySourceLocator { } private ConsulPropertySource create(String context) { - return new ConsulPropertySource(context, keyValueClient); + return new ConsulPropertySource(context, consul); } private void addProfiles(List contexts, String baseContext, List profiles) { diff --git a/spring-cloud-consul-core/pom.xml b/spring-cloud-consul-core/pom.xml index d1bb308f..7c28bb10 100644 --- a/spring-cloud-consul-core/pom.xml +++ b/spring-cloud-consul-core/pom.xml @@ -1,6 +1,7 @@ - + 4.0.0 spring-cloud-consul-core @@ -24,21 +25,13 @@ org.springframework.boot spring-boot-starter-actuator - - org.springframework.cloud - spring-cloud-commons - - com.netflix.feign - feign-core + org.springframework.cloud + spring-cloud-commons - - com.netflix.feign - feign-jackson - - com.netflix.feign - feign-slf4j + com.ecwid.consul + consul-api org.projectlombok @@ -51,10 +44,10 @@ spring-boot-starter-test test - - com.google.guava - guava - + + com.google.guava + guava + diff --git a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulAutoConfiguration.java b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulAutoConfiguration.java index 8a6f0b8a..a45ad28c 100644 --- a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulAutoConfiguration.java +++ b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulAutoConfiguration.java @@ -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 diff --git a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulEndpoint.java b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulEndpoint.java index 47912ee9..02b717b1 100644 --- a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulEndpoint.java +++ b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulEndpoint.java @@ -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 { @Autowired - KeyValueClient keyValueClient; - - @Autowired - CatalogClient catalogClient; - - @Autowired - AgentClient agentClient; + private ConsulClient consul; @Autowired public ConsulEndpoint() { @@ -39,25 +33,31 @@ public class ConsulEndpoint extends AbstractEndpoint public ConsulData invoke() { ConsulData data = new ConsulData(); //data.setKeyValues(kvClient.getKeyValueRecurse()); - data.setCatalogServices(catalogClient.getServices()); - Map services = agentClient.getServices(); - data.setAgentServices(services); + Response> agentServices = consul.getAgentServices(); + data.setAgentServices(agentServices.getValue()); - for (String serviceId : services.keySet()) { - data.getCatalogServiceNodes().put(serviceId, catalogClient.getServiceNodes(serviceId)); + Response>> catalogServices = consul.getCatalogServices(QueryParams.DEFAULT); + + + for (String serviceId : catalogServices.getValue().keySet()) { + Response> response = consul.getCatalogService(serviceId, QueryParams.DEFAULT); + data.getCatalogServices().put(serviceId, response.getValue()); } + Response> catalogNodes = consul.getCatalogNodes(QueryParams.DEFAULT); + data.setCatalogNodes(catalogNodes.getValue()); + return data; } @Data public static class ConsulData { - Map> catalogServices; - - Map> catalogServiceNodes = new LinkedHashMap<>(); + Map> catalogServices = new LinkedHashMap<>(); Map agentServices; - List keyValues; + List catalogNodes; + + //List keyValues; } } diff --git a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulHealthIndicator.java b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulHealthIndicator.java index 9c725911..a6620aab 100644 --- a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulHealthIndicator.java +++ b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulHealthIndicator.java @@ -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> services = catalogClient.getServices(); - builder.up().withDetail("services", services); + Response self = consul.getAgentSelf(); + Response>> services = consul.getCatalogServices(QueryParams.DEFAULT); + builder.up() + .withDetail("services", services.getValue()) + .withDetail("agent", self.getValue()); } catch (Exception e) { builder.down(e); } diff --git a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulProperties.java b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulProperties.java index 3a0d17d0..6634eeb0 100644 --- a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulProperties.java +++ b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulProperties.java @@ -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 tags = new ArrayList<>(); diff --git a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/client/AgentClient.java b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/client/AgentClient.java deleted file mode 100644 index 127c8b4f..00000000 --- a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/client/AgentClient.java +++ /dev/null @@ -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 getServices(); - - @RequestLine("GET /v1/agent/self") - //TODO change map to an object - Map getSelf(); - - @RequestLine("PUT /v1/agent/service/register") - void register(Service service); - - @RequestLine("PUT /v1/agent/service/deregister/{serviceId}") - void deregister(@Param("serviceId") String serviceId); -} diff --git a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/client/CatalogClient.java b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/client/CatalogClient.java deleted file mode 100644 index cea7746a..00000000 --- a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/client/CatalogClient.java +++ /dev/null @@ -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> getServices(); - - @RequestLine("GET /v1/catalog/service/{serviceId}") - List getServiceNodes(@Param("serviceId") String serviceId); -} diff --git a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/client/ConsulErrorDecoder.java b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/client/ConsulErrorDecoder.java deleted file mode 100644 index b4e42706..00000000 --- a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/client/ConsulErrorDecoder.java +++ /dev/null @@ -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); - } -} diff --git a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/client/EventClient.java b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/client/EventClient.java deleted file mode 100644 index e2d16d8d..00000000 --- a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/client/EventClient.java +++ /dev/null @@ -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=&index= - @RequestLine("GET /v1/event/list") - List 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); -} diff --git a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/client/EventService.java b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/client/EventService.java deleted file mode 100644 index c0aec0b1..00000000 --- a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/client/EventService.java +++ /dev/null @@ -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 lastIndex = new AtomicReference<>(); - - @PostConstruct - public void init() { - Response response = getEventsResponse(); - setLastIndex(response); - } - - private void setLastIndex(Response response) { - Collection 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 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 getEvents(BigInteger lastIndex) { - return filterEvents(readEvents(getEventsResponse()), lastIndex); - } - - public List watch() { - return watch(lastIndex.get()); - } - - public List watch(BigInteger lastIndex) { - //TODO: parameterized or configurable watch time - return filterEvents(readEvents(client.watch("2s", lastIndex.toString())), lastIndex); - } - - protected List 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 filterEvents(List toFilter, BigInteger lastIndex) { - List 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; - } - -} diff --git a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/client/KeyValueClient.java b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/client/KeyValueClient.java deleted file mode 100644 index 98cb5c72..00000000 --- a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/client/KeyValueClient.java +++ /dev/null @@ -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 getKeyValue(@Param("key") String key); - - @RequestLine("GET /v1/kv/?recurse=true") - List getKeyValueRecurse(); - - @RequestLine("GET /v1/kv/{key}?recurse=true") - List getKeyValueRecurse(@Param("key") String key); - - @RequestLine("GET /v1/kv/?keys=true") - List getKeys(); - - @RequestLine("GET /v1/kv/{key}?keys=true") - List 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); -} diff --git a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/client/NotFoundException.java b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/client/NotFoundException.java deleted file mode 100644 index 6e337b40..00000000 --- a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/client/NotFoundException.java +++ /dev/null @@ -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; -} diff --git a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/model/Check.java b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/model/Check.java deleted file mode 100644 index f5257901..00000000 --- a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/model/Check.java +++ /dev/null @@ -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; -} diff --git a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/model/Event.java b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/model/Event.java deleted file mode 100644 index f27e9188..00000000 --- a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/model/Event.java +++ /dev/null @@ -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(); - } -} diff --git a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/model/KeyValue.java b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/model/KeyValue.java deleted file mode 100644 index e527f255..00000000 --- a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/model/KeyValue.java +++ /dev/null @@ -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(); - } -} diff --git a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/model/Service.java b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/model/Service.java deleted file mode 100644 index bd7d9b68..00000000 --- a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/model/Service.java +++ /dev/null @@ -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 tags; - - @JsonProperty("Port") - private int port; - - @JsonProperty("Check") - private Check check; -} diff --git a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/model/ServiceNode.java b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/model/ServiceNode.java deleted file mode 100644 index f2210fa3..00000000 --- a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/model/ServiceNode.java +++ /dev/null @@ -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 serviceTags; - - @JsonProperty("ServicePort") - private int servicePort; -} diff --git a/spring-cloud-consul-core/src/test/java/org/springframework/cloud/consul/client/AgentClientIT.java b/spring-cloud-consul-core/src/test/java/org/springframework/cloud/consul/client/AgentClientIT.java deleted file mode 100644 index 3351081f..00000000 --- a/spring-cloud-consul-core/src/test/java/org/springframework/cloud/consul/client/AgentClientIT.java +++ /dev/null @@ -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 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 self = client.getSelf(); - assertNotNull("self was null", self); - } -} diff --git a/spring-cloud-consul-core/src/test/java/org/springframework/cloud/consul/client/CatalogClientIT.java b/spring-cloud-consul-core/src/test/java/org/springframework/cloud/consul/client/CatalogClientIT.java deleted file mode 100644 index b0407cac..00000000 --- a/spring-cloud-consul-core/src/test/java/org/springframework/cloud/consul/client/CatalogClientIT.java +++ /dev/null @@ -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> services = client.getServices(); - assertNotNull("services is null", services); - assertTrue("No consul key", services.containsKey("consul")); - } - - @Test - public void testGetService() { - List 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); - } -} diff --git a/spring-cloud-consul-core/src/test/java/org/springframework/cloud/consul/client/EventClientIT.java b/spring-cloud-consul-core/src/test/java/org/springframework/cloud/consul/client/EventClientIT.java deleted file mode 100644 index b2463491..00000000 --- a/spring-cloud-consul-core/src/test/java/org/springframework/cloud/consul/client/EventClientIT.java +++ /dev/null @@ -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 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);*/ - } -} \ No newline at end of file diff --git a/spring-cloud-consul-core/src/test/java/org/springframework/cloud/consul/client/EventServiceIT.java b/spring-cloud-consul-core/src/test/java/org/springframework/cloud/consul/client/EventServiceIT.java deleted file mode 100644 index 0d54f0bd..00000000 --- a/spring-cloud-consul-core/src/test/java/org/springframework/cloud/consul/client/EventServiceIT.java +++ /dev/null @@ -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 events = service.getEvents(service.getLastIndex()); - assertNotNull("events was null", events); - assertFalse("events was empty", events.isEmpty()); - } -} diff --git a/spring-cloud-consul-core/src/test/java/org/springframework/cloud/consul/client/KeyValueClientIT.java b/spring-cloud-consul-core/src/test/java/org/springframework/cloud/consul/client/KeyValueClientIT.java deleted file mode 100644 index a3c08029..00000000 --- a/spring-cloud-consul-core/src/test/java/org/springframework/cloud/consul/client/KeyValueClientIT.java +++ /dev/null @@ -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 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 values = keyValueClient.getKeyValueRecurse(KEY); - assertNotNull("values is null", values); - assertFalse("Values is null", values.isEmpty()); - } - - @Test - public void test004GetRecurse() { - List 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"); - } -} \ No newline at end of file diff --git a/spring-cloud-consul-core/src/test/java/org/springframework/cloud/consul/client/TestClientConfiguration.java b/spring-cloud-consul-core/src/test/java/org/springframework/cloud/consul/client/TestClientConfiguration.java deleted file mode 100644 index b68a1f5c..00000000 --- a/spring-cloud-consul-core/src/test/java/org/springframework/cloud/consul/client/TestClientConfiguration.java +++ /dev/null @@ -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 { -} diff --git a/spring-cloud-consul-discovery/pom.xml b/spring-cloud-consul-discovery/pom.xml index bf38c124..f337acae 100644 --- a/spring-cloud-consul-discovery/pom.xml +++ b/spring-cloud-consul-discovery/pom.xml @@ -47,6 +47,10 @@ spring-boot-starter-test test + + org.springframework.boot + spring-boot-starter-web + com.google.guava guava diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClient.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClient.java index 72849dad..a6d3c8aa 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClient.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClient.java @@ -1,15 +1,12 @@ package org.springframework.cloud.consul.discovery; +import com.ecwid.consul.v1.ConsulClient; import com.google.common.base.Function; import com.google.common.collect.Lists; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.DefaultServiceInstance; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; -import org.springframework.cloud.consul.client.AgentClient; -import org.springframework.cloud.consul.client.CatalogClient; -import org.springframework.cloud.consul.model.Service; -import org.springframework.cloud.consul.model.ServiceNode; import org.springframework.context.ApplicationContext; import javax.annotation.Nullable; @@ -27,10 +24,7 @@ public class ConsulDiscoveryClient implements DiscoveryClient { ApplicationContext context; @Autowired - AgentClient agentClient; - - @Autowired - CatalogClient catalogClient; + ConsulClient client; @Override public String description() { @@ -39,7 +33,7 @@ public class ConsulDiscoveryClient implements DiscoveryClient { @Override public ServiceInstance getLocalServiceInstance() { - Map services = agentClient.getServices(); + /*Map services = agentClient.getServices(); Service service = services.get(context.getId()); if (service == null) { throw new IllegalStateException("Unable to locate service in consul agent: "+context.getId()); @@ -52,12 +46,13 @@ public class ConsulDiscoveryClient implements DiscoveryClient { host = (String) member.get("Name"); } } - return new DefaultServiceInstance(service.getId(), host, service.getPort()); + return new DefaultServiceInstance(service.getId(), host, service.getPort());*/ + return null; } @Override public List getInstances(final String serviceId) { - List nodes = catalogClient.getServiceNodes(serviceId); + /*List nodes = catalogClient.getServiceNodes(serviceId); Iterable instances = transform(nodes, new Function() { @Nullable @Override @@ -66,12 +61,13 @@ public class ConsulDiscoveryClient implements DiscoveryClient { } }); - return Lists.newArrayList(instances); + return Lists.newArrayList(instances);*/ + return Lists.newArrayList(); } @Override public List getAllInstances() { - Iterable instances = transform(concat(transform(catalogClient.getServices().keySet(), new Function>() { + /*Iterable instances = transform(concat(transform(catalogClient.getServices().keySet(), new Function>() { @Nullable @Override public List apply(@Nullable String input) { @@ -85,11 +81,13 @@ public class ConsulDiscoveryClient implements DiscoveryClient { } }); - return Lists.newArrayList(instances); + return Lists.newArrayList(instances);*/ + return Lists.newArrayList(); } @Override public List getServices() { - return Lists.newArrayList(catalogClient.getServices().keySet()); + //return Lists.newArrayList(catalogClient.getServices().keySet()); + return Lists.newArrayList(); } } diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulLifecycle.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulLifecycle.java index 238121ab..b1909ca0 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulLifecycle.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulLifecycle.java @@ -1,11 +1,11 @@ package org.springframework.cloud.consul.discovery; +import com.ecwid.consul.v1.ConsulClient; +import com.ecwid.consul.v1.agent.model.NewService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.discovery.AbstractDiscoveryLifecycle; import org.springframework.cloud.consul.ConsulProperties; -import org.springframework.cloud.consul.client.AgentClient; -import org.springframework.cloud.consul.model.Service; /** * @author Spencer Gibb @@ -14,15 +14,16 @@ import org.springframework.cloud.consul.model.Service; public class ConsulLifecycle extends AbstractDiscoveryLifecycle { @Autowired - private AgentClient agentClient; + private ConsulClient client; @Autowired private ConsulProperties consulProperties; @Override protected void register() { - Service service = new Service(); + NewService service = new NewService(); String appName = getAppName(); + //TODO: move id to properties with context ID as default service.setId(getContext().getId()); service.setName(appName); //TODO: support port = 0 random assignment @@ -36,7 +37,7 @@ public class ConsulLifecycle extends AbstractDiscoveryLifecycle { @Override protected void registerManagement() { - Service management = new Service(); + NewService management = new NewService(); management.setId(getManagementServiceId()); management.setName(getManagementServiceName()); management.setPort(getManagementPort()); @@ -45,9 +46,9 @@ public class ConsulLifecycle extends AbstractDiscoveryLifecycle { register(management); } - protected void register(Service service) { + protected void register(NewService service) { log.info("Registering service with consul: {}", service.toString()); - agentClient.register(service); + client.agentServiceRegister(service); } @Override @@ -66,7 +67,7 @@ public class ConsulLifecycle extends AbstractDiscoveryLifecycle { } private void deregister(String serviceId) { - agentClient.deregister(serviceId); + client.agentServiceDeregister(serviceId); } @Override diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulLoadBalancerClient.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulLoadBalancerClient.java index 8f90b579..492fd754 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulLoadBalancerClient.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulLoadBalancerClient.java @@ -1,5 +1,6 @@ package org.springframework.cloud.consul.discovery; +import com.ecwid.consul.v1.ConsulClient; import com.google.common.base.Throwables; import com.netflix.client.config.DefaultClientConfigImpl; import com.netflix.client.config.IClientConfig; @@ -9,7 +10,6 @@ import org.springframework.cloud.client.DefaultServiceInstance; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.cloud.client.loadbalancer.LoadBalancerRequest; -import org.springframework.cloud.consul.client.CatalogClient; import org.springframework.web.util.UriComponentsBuilder; import java.net.URI; @@ -24,7 +24,7 @@ public class ConsulLoadBalancerClient implements LoadBalancerClient { private ConcurrentHashMap namedClientConfigs = new ConcurrentHashMap<>(); @Autowired - CatalogClient catalogClient; + ConsulClient client; @Override public ServiceInstance choose(String serviceId) { @@ -44,7 +44,7 @@ public class ConsulLoadBalancerClient implements LoadBalancerClient { .withRule(new AvailabilityFilteringRule()) //TODO: figure out ping //.withPing() - .withDynamicServerList(new ConsulServerList(catalogClient, serviceId)) + //FIXME: .withDynamicServerList(new ConsulServerList(catalogClient, serviceId)) .buildDynamicServerListLoadBalancer(); namedLoadBalancers.put(serviceId, lb); } diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulRibbonClientConfiguration.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulRibbonClientConfiguration.java index dd67fb91..3a9106f1 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulRibbonClientConfiguration.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulRibbonClientConfiguration.java @@ -20,11 +20,11 @@ import static com.netflix.client.config.CommonClientConfigKey.*; import javax.annotation.PostConstruct; +import com.ecwid.consul.v1.ConsulClient; import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.config.BeanPostProcessor; -import org.springframework.cloud.consul.client.CatalogClient; import org.springframework.cloud.netflix.ribbon.ZonePreferenceServerListFilter; import org.springframework.context.annotation.Configuration; @@ -46,7 +46,7 @@ import com.netflix.loadbalancer.ZoneAvoidanceRule; @Configuration public class ConsulRibbonClientConfiguration implements BeanPostProcessor { @Autowired - CatalogClient client; + private ConsulClient client; @Value("${ribbon.client.name}") private String serviceId = "client"; diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulServer.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulServer.java index c1282f29..9d1c2b82 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulServer.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulServer.java @@ -1,7 +1,7 @@ package org.springframework.cloud.consul.discovery; +import com.ecwid.consul.v1.catalog.model.CatalogService; import com.netflix.loadbalancer.Server; -import org.springframework.cloud.consul.model.ServiceNode; /** * @author Spencer Gibb @@ -10,12 +10,12 @@ public class ConsulServer extends Server { private final MetaInfo metaInfo; - public ConsulServer(final ServiceNode node) { - super(node.getNode(), node.getServicePort()); + public ConsulServer(final CatalogService service) { + super(service.getNode(), service.getServicePort()); metaInfo = new MetaInfo() { @Override public String getAppName() { - return node.getServiceName(); + return service.getServiceName(); } @Override @@ -30,7 +30,7 @@ public class ConsulServer extends Server { @Override public String getInstanceId() { - return node.getServiceID(); + return service.getServiceId(); } }; } diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulServerList.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulServerList.java index 52d8ec31..1a9bda2b 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulServerList.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulServerList.java @@ -1,10 +1,12 @@ package org.springframework.cloud.consul.discovery; +import com.ecwid.consul.v1.ConsulClient; +import com.ecwid.consul.v1.QueryParams; +import com.ecwid.consul.v1.Response; +import com.ecwid.consul.v1.catalog.model.CatalogService; import com.google.common.base.Function; import com.netflix.client.config.IClientConfig; import com.netflix.loadbalancer.AbstractServerList; -import org.springframework.cloud.consul.client.CatalogClient; -import org.springframework.cloud.consul.model.ServiceNode; import javax.annotation.Nullable; import java.util.Collection; @@ -19,19 +21,19 @@ import static com.google.common.collect.Lists.newArrayList; */ public class ConsulServerList extends AbstractServerList { - private CatalogClient client; + private ConsulClient client; private String serviceId; public ConsulServerList() { } - public ConsulServerList(CatalogClient client, String serviceId) { + public ConsulServerList(ConsulClient client, String serviceId) { this.client = client; this.serviceId = serviceId; } - public void setClient(CatalogClient client) { + public void setClient(ConsulClient client) { this.client = client; } @@ -54,15 +56,15 @@ public class ConsulServerList extends AbstractServerList { if (client == null) { return Collections.emptyList(); } - List nodes = client.getServiceNodes(serviceId); - if (nodes == null || nodes.isEmpty()) { + Response> response = client.getCatalogService(this.serviceId, QueryParams.DEFAULT); + if (response.getValue() == null || response.getValue().isEmpty()) { return Collections.EMPTY_LIST; } - Collection servers = transform(nodes, new Function() { + Collection servers = transform(response.getValue(), new Function() { @Nullable @Override - public ConsulServer apply(@Nullable ServiceNode node) { - ConsulServer server = new ConsulServer(node); + public ConsulServer apply(@Nullable CatalogService service) { + ConsulServer server = new ConsulServer(service); return server; } }); diff --git a/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulLifecycleTests.java b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulLifecycleTests.java new file mode 100644 index 00000000..145e9509 --- /dev/null +++ b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulLifecycleTests.java @@ -0,0 +1,60 @@ +package org.springframework.cloud.consul.discovery; + +import com.ecwid.consul.v1.ConsulClient; +import com.ecwid.consul.v1.Response; +import com.ecwid.consul.v1.agent.model.Service; +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.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.IntegrationTest; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.cloud.consul.ConsulAutoConfiguration; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; + +import java.util.Map; + +import static org.junit.Assert.*; + +/** + * @author Spencer Gibb + */ +@RunWith(SpringJUnit4ClassRunner.class) +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@SpringApplicationConfiguration(classes = TestConfig.class) +@IntegrationTest({"server.port=0", "spring.application.name=myTestService"}) +@WebAppConfiguration +public class ConsulLifecycleTests { + + @Autowired + ConsulLifecycle lifecycle; + + @Autowired + ConsulClient consul; + + @Autowired + ApplicationContext context; + + @Test + public void contextLoads() { + Response> response = consul.getAgentServices(); + Map services = response.getValue(); + Service service = services.get(context.getId()); + assertNotNull("service was null", service); + assertEquals("service id was wrong", service.getId(), context.getId()); + assertEquals("service name was wrong", service.getService(), "myTestService"); + } +} + +@Configuration +@EnableAutoConfiguration +@Import({ConsulAutoConfiguration.class, ConsulDiscoveryClientConfiguration.class}) +class TestConfig { + +} diff --git a/spring-cloud-consul-tests/src/test/java/org/springframework/cloud/consul/sidecar/SidecarApplicationTests.java b/spring-cloud-consul-tests/src/test/java/org/springframework/cloud/consul/sidecar/SidecarApplicationTests.java index f66eb31a..eab8945e 100644 --- a/spring-cloud-consul-tests/src/test/java/org/springframework/cloud/consul/sidecar/SidecarApplicationTests.java +++ b/spring-cloud-consul-tests/src/test/java/org/springframework/cloud/consul/sidecar/SidecarApplicationTests.java @@ -30,6 +30,7 @@ import org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Import; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.web.bind.annotation.RestController; import java.lang.annotation.*; @@ -37,6 +38,7 @@ import java.lang.annotation.*; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SidecarApplicationTests.SidecarApplication.class) @IntegrationTest("server.port=0") +@WebAppConfiguration public class SidecarApplicationTests { @Test diff --git a/spring-cloud-consul-tests/src/test/java/org/springframework/cloud/consul/sidecar/ZuulApplicationTests.java b/spring-cloud-consul-tests/src/test/java/org/springframework/cloud/consul/sidecar/ZuulApplicationTests.java index a45a2252..d7516083 100644 --- a/spring-cloud-consul-tests/src/test/java/org/springframework/cloud/consul/sidecar/ZuulApplicationTests.java +++ b/spring-cloud-consul-tests/src/test/java/org/springframework/cloud/consul/sidecar/ZuulApplicationTests.java @@ -28,10 +28,12 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.context.annotation.Bean; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = ZuulApplicationTests.ZuulApplication.class) @IntegrationTest("server.port=0") +@WebAppConfiguration public class ZuulApplicationTests { @Test