diff --git a/README.md b/README.md
index a3225314..acf37790 100644
--- a/README.md
+++ b/README.md
@@ -2,6 +2,19 @@
Preview of Spring Cloud Consul implementation
+### Short consul overview
+
+consul does
+
+* distributed configuration
+* service registration and discovery
+* messaging
+* distributed locking and sessions
+* supports multiple data centers
+* has a slick ui
+
+See the [intro](https://consul.io/intro/index.html) for more information.
+
### Running the sample
1. [Install consul](https://consul.io/downloads.html)
diff --git a/docs/pom.xml b/docs/pom.xml
index 8cba1949..d1065b90 100644
--- a/docs/pom.xml
+++ b/docs/pom.xml
@@ -5,7 +5,7 @@
org.springframework.cloud
spring-cloud-build
- 1.0.0.BUILD-SNAPSHOT
+ 1.0.1.BUILD-SNAPSHOT
spring-cloud-consul-docs
diff --git a/pom.xml b/pom.xml
index 5b5717dd..a27e278e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
org.springframework.cloud
spring-cloud-consul
- 1.0.0.BUILD-SNAPSHOT
+ 1.0.1.BUILD-SNAPSHOT
pom
Spring Cloud Consul
Spring Cloud Consul
@@ -13,7 +13,7 @@
org.springframework.cloud
spring-cloud-build
- 1.0.0.BUILD-SNAPSHOT
+ 1.0.1.BUILD-SNAPSHOT
@@ -61,32 +61,32 @@
org.springframework.cloud
spring-cloud-commons
- 1.0.0.BUILD-SNAPSHOT
+ 1.0.1.BUILD-SNAPSHOT
org.springframework.cloud
spring-cloud-consul-core
- 1.0.0.BUILD-SNAPSHOT
+ 1.0.1.BUILD-SNAPSHOT
org.springframework.cloud
spring-cloud-consul-bus
- 1.0.0.BUILD-SNAPSHOT
+ 1.0.1.BUILD-SNAPSHOT
org.springframework.cloud
spring-cloud-consul-config
- 1.0.0.BUILD-SNAPSHOT
+ 1.0.1.BUILD-SNAPSHOT
org.springframework.cloud
spring-cloud-consul-discovery
- 1.0.0.BUILD-SNAPSHOT
+ 1.0.1.BUILD-SNAPSHOT
org.springframework.cloud
spring-cloud-bus
- 1.0.0.BUILD-SNAPSHOT
+ 1.0.1.BUILD-SNAPSHOT
org.springframework.boot
@@ -112,8 +112,8 @@
org.springframework.cloud
- spring-cloud-config-client
- 1.0.0.BUILD-SNAPSHOT
+ spring-cloud-context
+ 1.0.1.BUILD-SNAPSHOT
com.ecwid.consul
@@ -141,12 +141,12 @@
org.springframework.cloud
spring-cloud-starter-zuul
- 1.0.0.BUILD-SNAPSHOT
+ 1.0.1.BUILD-SNAPSHOT
org.springframework.cloud
spring-cloud-netflix-sidecar
- 1.0.0.BUILD-SNAPSHOT
+ 1.0.1.BUILD-SNAPSHOT
com.netflix.eureka
@@ -161,7 +161,7 @@
org.springframework.cloud
spring-cloud-netflix-core
- 1.0.0.BUILD-SNAPSHOT
+ 1.0.1.BUILD-SNAPSHOT
com.netflix.ribbon
diff --git a/spring-cloud-consul-bus/pom.xml b/spring-cloud-consul-bus/pom.xml
index bd09281e..0179211f 100644
--- a/spring-cloud-consul-bus/pom.xml
+++ b/spring-cloud-consul-bus/pom.xml
@@ -11,7 +11,7 @@
org.springframework.cloud
spring-cloud-consul
- 1.0.0.BUILD-SNAPSHOT
+ 1.0.1.BUILD-SNAPSHOT
..
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 3dc0aa4a..93c90d9a 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
@@ -16,8 +16,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;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@@ -34,6 +32,9 @@ import org.springframework.integration.support.json.Jackson2JsonObjectMapper;
import org.springframework.messaging.MessageChannel;
import org.springframework.scheduling.annotation.EnableScheduling;
+import com.ecwid.consul.v1.ConsulClient;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
/**
* @author Spencer Gibb
*/
@@ -43,47 +44,48 @@ import org.springframework.scheduling.annotation.EnableScheduling;
@AutoConfigureAfter(BusAutoConfiguration.class)
@EnableScheduling
public class ConsulBusAutoConfiguration {
- @Autowired
- @Qualifier("cloudBusInboundChannel") MessageChannel cloudBusInboundChannel;
+ @Autowired
+ @Qualifier("cloudBusInboundChannel")
+ MessageChannel cloudBusInboundChannel;
- @Autowired
- ObjectMapper objectMapper;
+ @Autowired
+ ObjectMapper objectMapper;
- @Bean
- public EventService eventService() {
- return new EventService();
- }
+ @Bean
+ public EventService eventService() {
+ return new EventService();
+ }
- @Bean
- public ConsulOutboundEndpoint consulOutboundEndpoint() {
- return new ConsulOutboundEndpoint();
- }
+ @Bean
+ public ConsulOutboundEndpoint consulOutboundEndpoint() {
+ return new ConsulOutboundEndpoint();
+ }
- @Bean
- public IntegrationFlow cloudBusConsulOutboundFlow(
- @Qualifier("cloudBusOutboundChannel") MessageChannel cloudBusOutboundChannel) {
- return IntegrationFlows
- .from(cloudBusOutboundChannel)
- //TODO: put the json headers as part of the message, here?
- .transform(Transformers.toJson())
- .handle(consulOutboundEndpoint())
- .get();
- }
+ @Bean
+ public IntegrationFlow cloudBusConsulOutboundFlow(
+ @Qualifier("cloudBusOutboundChannel") MessageChannel cloudBusOutboundChannel) {
+ return IntegrationFlows.from(cloudBusOutboundChannel)
+ // TODO: put the json headers as part of the message, here?
+ .transform(Transformers.toJson()).handle(consulOutboundEndpoint()).get();
+ }
- @Bean
- public IntegrationFlow cloudBusConsulInboundFlow() {
- return IntegrationFlows
- .from(consulInboundChannelAdapter())
- .transform(Transformers.fromJson(RemoteApplicationEvent.class, new Jackson2JsonObjectMapper(objectMapper)))
- .channel(cloudBusInboundChannel) // now set in consulInboundChannelAdapter bean
- .get();
- }
+ @Bean
+ public IntegrationFlow cloudBusConsulInboundFlow() {
+ return IntegrationFlows
+ .from(consulInboundChannelAdapter())
+ .transform(
+ Transformers.fromJson(RemoteApplicationEvent.class,
+ new Jackson2JsonObjectMapper(objectMapper)))
+ .channel(cloudBusInboundChannel) // now set in consulInboundChannelAdapter
+ // bean
+ .get();
+ }
- @Bean
- public ConsulInboundChannelAdapter consulInboundChannelAdapter() {
- ConsulInboundChannelAdapter adapter = new ConsulInboundChannelAdapter();
- adapter.setOutputChannel(cloudBusInboundChannel);
- return adapter;
- }
+ @Bean
+ public ConsulInboundChannelAdapter consulInboundChannelAdapter() {
+ ConsulInboundChannelAdapter adapter = new ConsulInboundChannelAdapter();
+ adapter.setOutputChannel(cloudBusInboundChannel);
+ return adapter;
+ }
}
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 c360a345..2e15d3f4 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
@@ -28,50 +28,51 @@ import org.springframework.scheduling.annotation.Scheduled;
import com.ecwid.consul.v1.event.model.Event;
/**
- * Adapter that receives Messages from Consul Events, converts them into
- * Spring Integration Messages, and sends the results to a Message Channel.
+ * Adapter that receives Messages from Consul Events, converts them into Spring
+ * Integration Messages, and sends the results to a Message Channel.
* @author Spencer Gibb
*/
public class ConsulInboundChannelAdapter extends MessageProducerSupport {
- @Autowired
- private EventService eventService;
+ @Autowired
+ private EventService eventService;
- public ConsulInboundChannelAdapter() {
- }
+ public ConsulInboundChannelAdapter() {
+ }
- //link eventService to sendMessage
- /*
- Map headers = headerMapper.toHeadersFromRequest(message.getMessageProperties());
- if (messageListenerContainer.getAcknowledgeMode() == AcknowledgeMode.MANUAL) {
- headers.put(AmqpHeaders.DELIVERY_TAG, message.getMessageProperties().getDeliveryTag());
- headers.put(AmqpHeaders.CHANNEL, channel);
- }
- sendMessage(AmqpInboundChannelAdapter.this.getMessageBuilderFactory().withPayload(payload).copyHeaders(headers).build());*/
+ // link eventService to sendMessage
+ /*
+ * Map headers =
+ * headerMapper.toHeadersFromRequest(message.getMessageProperties()); if
+ * (messageListenerContainer.getAcknowledgeMode() == AcknowledgeMode.MANUAL) {
+ * headers.put(AmqpHeaders.DELIVERY_TAG,
+ * message.getMessageProperties().getDeliveryTag()); headers.put(AmqpHeaders.CHANNEL,
+ * channel); }
+ * sendMessage(AmqpInboundChannelAdapter.this.getMessageBuilderFactory().withPayload
+ * (payload).copyHeaders(headers).build());
+ */
- //start thread
- //make blocking calls
- //foreach event -> send message
+ // start thread
+ // make blocking calls
+ // foreach event -> send message
+ @Override
+ protected void doStart() {
+ }
- @Override
- protected void doStart() {
- }
+ @Scheduled(fixedDelayString = "10")
+ public void getEvents() throws IOException {
+ List events = eventService.watch();
+ for (Event event : events) {
+ // Map headers = new HashMap<>();
+ // headers.put(MessageHeaders.REPLY_CHANNEL, outputChannel.)
+ String decoded = new String(decodeFromString(event.getPayload()));
+ sendMessage(getMessageBuilderFactory().withPayload(decoded)
+ // TODO: support headers
+ .build());
+ }
+ }
- @Scheduled(fixedDelayString = "10")
- public void getEvents() throws IOException {
- List events = eventService.watch();
- for (Event event : events) {
- //Map headers = new HashMap<>();
- //headers.put(MessageHeaders.REPLY_CHANNEL, outputChannel.)
- String decoded = new String(decodeFromString(event.getPayload()));
- sendMessage(getMessageBuilderFactory()
- .withPayload(decoded)
- //TODO: support headers
- .build());
- }
- }
-
- @Override
- protected void doStop() {
- }
+ @Override
+ protected void doStop() {
+ }
}
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 78208f2f..a77ad9b1 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
@@ -16,14 +16,15 @@
package org.springframework.cloud.consul.bus;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
+import org.springframework.messaging.Message;
+
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.integration.handler.AbstractReplyProducingMessageHandler;
-import org.springframework.messaging.Message;
/**
* Adapter that converts and sends Messages as Consul events
@@ -31,16 +32,17 @@ import org.springframework.messaging.Message;
*/
public class ConsulOutboundEndpoint extends AbstractReplyProducingMessageHandler {
- @Autowired
- protected ConsulClient consul;
+ @Autowired
+ protected ConsulClient consul;
- @Override
- protected Object handleRequestMessage(Message> requestMessage) {
- Object payload = requestMessage.getPayload();
- //TODO: support headers
- //TODO: support consul event filters: NodeFilter, ServiceFilter, TagFilter
- Response event = consul.eventFire("springCloudBus", (String) payload, new EventParams(), QueryParams.DEFAULT);
- //TODO: return event?
- return null;
- }
+ @Override
+ protected Object handleRequestMessage(Message> requestMessage) {
+ Object payload = requestMessage.getPayload();
+ // TODO: support headers
+ // TODO: support consul event filters: NodeFilter, ServiceFilter, TagFilter
+ Response event = consul.eventFire("springCloudBus", (String) payload,
+ new EventParams(), QueryParams.DEFAULT);
+ // TODO: return event?
+ return null;
+ }
}
diff --git a/spring-cloud-consul-bus/src/main/java/org/springframework/cloud/consul/bus/EventService.java b/spring-cloud-consul-bus/src/main/java/org/springframework/cloud/consul/bus/EventService.java
index 2585ccd6..800c524e 100644
--- a/spring-cloud-consul-bus/src/main/java/org/springframework/cloud/consul/bus/EventService.java
+++ b/spring-cloud-consul-bus/src/main/java/org/springframework/cloud/consul/bus/EventService.java
@@ -30,116 +30,109 @@ import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.event.model.Event;
import com.ecwid.consul.v1.event.model.EventParams;
import com.fasterxml.jackson.databind.ObjectMapper;
+
/**
* @author Spencer Gibb
*/
public class EventService {
- @Autowired
- protected ConsulClient consul;
+ @Autowired
+ protected ConsulClient consul;
- @Autowired(required = false)
- protected ObjectMapper objectMapper = new ObjectMapper();
+ @Autowired(required = false)
+ protected ObjectMapper objectMapper = new ObjectMapper();
- private AtomicReference lastIndex = new AtomicReference<>();
+ private AtomicReference lastIndex = new AtomicReference<>();
- @PostConstruct
- public void init() {
- setLastIndex(getEventsResponse());
- }
+ @PostConstruct
+ public void init() {
+ setLastIndex(getEventsResponse());
+ }
- private void setLastIndex(Response> response) {
- Long consulIndex = response.getConsulIndex();
- if (consulIndex != null) {
- lastIndex.set(BigInteger.valueOf(consulIndex));
- }
- }
+ private void setLastIndex(Response> response) {
+ Long consulIndex = response.getConsulIndex();
+ if (consulIndex != null) {
+ lastIndex.set(BigInteger.valueOf(consulIndex));
+ }
+ }
- public BigInteger getLastIndex() {
- return lastIndex.get();
- }
+ public BigInteger getLastIndex() {
+ return lastIndex.get();
+ }
- public Event fire(String name, String payload) {
- Response response = consul.eventFire(name, payload, new EventParams(), QueryParams.DEFAULT);
- return response.getValue();
- }
+ public Event fire(String name, String payload) {
+ Response response = consul.eventFire(name, payload, new EventParams(),
+ QueryParams.DEFAULT);
+ return response.getValue();
+ }
- public Response> getEventsResponse() {
- return consul.eventList(QueryParams.DEFAULT);
- }
+ public Response> getEventsResponse() {
+ return consul.eventList(QueryParams.DEFAULT);
+ }
- public List getEvents() {
- return getEventsResponse().getValue();
- }
+ public List getEvents() {
+ return getEventsResponse().getValue();
+ }
- /**
- * from https://github.com/hashicorp/consul/blob/master/api/event.go#L90-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;
- }
+ /**
+ * from https://github.com/hashicorp/consul/blob/master/api/event.go#L90-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 getEvents(BigInteger lastIndex) {
+ return filterEvents(readEvents(getEventsResponse()), lastIndex);
+ }
- public List watch() {
- return watch(lastIndex.get());
- }
+ public List watch() {
+ return watch(lastIndex.get());
+ }
- public List watch(BigInteger lastIndex) {
- //TODO: parameterized or configurable watch time
- long index = -1;
- if (lastIndex != null) {
- index = lastIndex.longValue();
- }
- Response> watch = consul.eventList(new QueryParams(2, index));
- return filterEvents(readEvents(watch), lastIndex);
- }
+ public List watch(BigInteger lastIndex) {
+ // TODO: parameterized or configurable watch time
+ long index = -1;
+ if (lastIndex != null) {
+ index = lastIndex.longValue();
+ }
+ Response> watch = consul.eventList(new QueryParams(2, index));
+ return filterEvents(readEvents(watch), lastIndex);
+ }
- protected List readEvents(Response> response) {
- setLastIndex(response);
- return response.getValue();
- }
+ protected List readEvents(Response> response) {
+ setLastIndex(response);
+ return response.getValue();
+ }
- /**
- * 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;
- }
+ /**
+ * 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-bus/src/main/java/org/springframework/cloud/consul/bus/SimpleRemoteEvent.java b/spring-cloud-consul-bus/src/main/java/org/springframework/cloud/consul/bus/SimpleRemoteEvent.java
index 1bdc6b4b..8e44ec33 100644
--- a/spring-cloud-consul-bus/src/main/java/org/springframework/cloud/consul/bus/SimpleRemoteEvent.java
+++ b/spring-cloud-consul-bus/src/main/java/org/springframework/cloud/consul/bus/SimpleRemoteEvent.java
@@ -16,10 +16,12 @@
package org.springframework.cloud.consul.bus;
-import com.fasterxml.jackson.annotation.JsonTypeName;
import lombok.Data;
+
import org.springframework.cloud.bus.event.RemoteApplicationEvent;
+import com.fasterxml.jackson.annotation.JsonTypeName;
+
/**
* @author Spencer Gibb
*/
@@ -27,17 +29,19 @@ import org.springframework.cloud.bus.event.RemoteApplicationEvent;
@Data
public class SimpleRemoteEvent extends RemoteApplicationEvent {
- private String message;
+ private String message;
- private SimpleRemoteEvent(){}
+ private SimpleRemoteEvent() {
+ }
- public SimpleRemoteEvent(Object source, String originService, String destinationService, String message) {
- super(source, originService, destinationService);
- this.message = message;
- }
+ public SimpleRemoteEvent(Object source, String originService,
+ String destinationService, String message) {
+ super(source, originService, destinationService);
+ this.message = message;
+ }
- public SimpleRemoteEvent(Object source, String originService, String message) {
- super(source, originService);
- this.message = message;
- }
+ public SimpleRemoteEvent(Object source, String originService, String message) {
+ super(source, originService);
+ this.message = message;
+ }
}
diff --git a/spring-cloud-consul-bus/src/test/java/org/springframework/cloud/consul/bus/ConsulBusIT.java b/spring-cloud-consul-bus/src/test/java/org/springframework/cloud/consul/bus/ConsulBusIT.java
index ab6aa327..aaa4bb8c 100644
--- a/spring-cloud-consul-bus/src/test/java/org/springframework/cloud/consul/bus/ConsulBusIT.java
+++ b/spring-cloud-consul-bus/src/test/java/org/springframework/cloud/consul/bus/ConsulBusIT.java
@@ -16,7 +16,9 @@
package org.springframework.cloud.consul.bus;
-import com.fasterxml.jackson.databind.ObjectMapper;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
@@ -37,88 +39,90 @@ import org.springframework.integration.support.json.Jackson2JsonObjectMapper;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.GenericMessage;
-import static org.junit.Assert.*;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
/**
* @author Spencer Gibb
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ConsulBusIT {
- @Test
- public void test001ConsulOutboundEndpoint_HandleRequestMessage() {
- ConfigurableApplicationContext context = getOutboundContext();
- context.publishEvent(new SimpleRemoteEvent(this, "testService", "testMessage"));
- }
+ @Test
+ public void test001ConsulOutboundEndpoint_HandleRequestMessage() {
+ ConfigurableApplicationContext context = getOutboundContext();
+ context.publishEvent(new SimpleRemoteEvent(this, "testService", "testMessage"));
+ }
- private ConfigurableApplicationContext getOutboundContext() {
- System.setProperty("spring.cloud.config.enabled", "false");
- ConfigurableApplicationContext context = new SpringApplicationBuilder()
- .web(false)
- .sources(OutboundConfig.class)
- .run();
- context.setId("testService");
- return context;
- }
+ private ConfigurableApplicationContext getOutboundContext() {
+ System.setProperty("spring.cloud.config.enabled", "false");
+ ConfigurableApplicationContext context = new SpringApplicationBuilder()
+ .web(false).sources(OutboundConfig.class).run();
+ context.setId("testService");
+ return context;
+ }
- /*@Test
- public void test002ConsulInboundChannelAdapter() {
- ConfigurableApplicationContext inbound = getInboundContext();
- ConfigurableApplicationContext outbound = getOutboundContext();
- outbound.publishEvent(new TestMessage(this, "testService", "inboundTestService", "testMessage"));
+ /*
+ * @Test public void test002ConsulInboundChannelAdapter() {
+ * ConfigurableApplicationContext inbound = getInboundContext();
+ * ConfigurableApplicationContext outbound = getOutboundContext();
+ * outbound.publishEvent(new TestMessage(this, "testService", "inboundTestService",
+ * "testMessage"));
+ *
+ * InboundConfig inboundConfig = inbound.getBean(InboundConfig.class);
+ * assertNotNull("message was null", inboundConfig.message); }
+ *
+ * private ConfigurableApplicationContext getInboundContext() {
+ * System.setProperty("spring.cloud.config.enabled", "false");
+ * ConfigurableApplicationContext context = new SpringApplicationBuilder() .web(false)
+ * .sources(InboundConfig.class) .run(); context.setId("inboundTestService"); return
+ * context; }
+ */
- InboundConfig inboundConfig = inbound.getBean(InboundConfig.class);
- assertNotNull("message was null", inboundConfig.message);
- }
+ protected static final String JSON_PAYLOAD = "{\"type\":\"simple\",\"timestamp\":1416349427372,\"originService\":\"testService\",\"destinationService\":null,\"message\":\"testMessage\"}";
- private ConfigurableApplicationContext getInboundContext() {
- System.setProperty("spring.cloud.config.enabled", "false");
- ConfigurableApplicationContext context = new SpringApplicationBuilder()
- .web(false)
- .sources(InboundConfig.class)
- .run();
- context.setId("inboundTestService");
- return context;
- }*/
+ @Test
+ public void test003JsonToObject() {
+ ObjectMapper objectMapper = new ObjectMapper();
+ objectMapper.registerModule(new SubtypeModule(SimpleRemoteEvent.class));
+ JsonToObjectTransformer transformer = Transformers.fromJson(
+ RemoteApplicationEvent.class, new Jackson2JsonObjectMapper(objectMapper));
+ /*
+ * HashMap map = new HashMap<>(); map.put(JsonHeaders.TYPE_ID,
+ * RemoteApplicationEvent.class);
+ */
+ Message> message = transformer.transform(new GenericMessage<>(JSON_PAYLOAD));
+ Object payload = message.getPayload();
+ assertTrue("payload is of wrong type", payload instanceof RemoteApplicationEvent);
+ assertTrue("payload is of wrong type", payload instanceof SimpleRemoteEvent);
+ SimpleRemoteEvent event = (SimpleRemoteEvent) payload;
+ assertEquals("payload is wrong", "testMessage", event.getMessage());
+ }
- protected static final String JSON_PAYLOAD = "{\"type\":\"simple\",\"timestamp\":1416349427372,\"originService\":\"testService\",\"destinationService\":null,\"message\":\"testMessage\"}";
+ @Configuration
+ @Import({ ConsulAutoConfiguration.class, BusAutoConfiguration.class,
+ ConsulBusAutoConfiguration.class })
+ @EnableIntegration
+ public static class OutboundConfig {
- @Test
- public void test003JsonToObject() {
- ObjectMapper objectMapper = new ObjectMapper();
- objectMapper.registerModule(new SubtypeModule(SimpleRemoteEvent.class));
- JsonToObjectTransformer transformer = Transformers.fromJson(RemoteApplicationEvent.class, new Jackson2JsonObjectMapper(objectMapper));
- /*HashMap map = new HashMap<>();
- map.put(JsonHeaders.TYPE_ID, RemoteApplicationEvent.class);*/
- Message> message = transformer.transform(new GenericMessage<>(JSON_PAYLOAD));
- Object payload = message.getPayload();
- assertTrue("payload is of wrong type", payload instanceof RemoteApplicationEvent);
- assertTrue("payload is of wrong type", payload instanceof SimpleRemoteEvent);
- SimpleRemoteEvent event = (SimpleRemoteEvent) payload;
- assertEquals("payload is wrong", "testMessage", event.getMessage());
- }
+ @Bean
+ public ObjectMapper objectMapper() {
+ ObjectMapper objectMapper = new ObjectMapper();
+ objectMapper.registerModule(new SubtypeModule(SimpleRemoteEvent.class));
+ return objectMapper;
+ }
+ }
- @Configuration
- @Import({ConsulAutoConfiguration.class, BusAutoConfiguration.class, ConsulBusAutoConfiguration.class})
- @EnableIntegration
- public static class OutboundConfig {
+ @Configuration
+ @Import({ ConsulAutoConfiguration.class, BusAutoConfiguration.class,
+ ConsulBusAutoConfiguration.class })
+ @EnableIntegration
+ public static class InboundConfig implements
+ ApplicationListener {
+ RemoteApplicationEvent message;
- @Bean
- public ObjectMapper objectMapper() {
- ObjectMapper objectMapper = new ObjectMapper();
- objectMapper.registerModule(new SubtypeModule(SimpleRemoteEvent.class));
- return objectMapper;
- }
- }
-
- @Configuration
- @Import({ConsulAutoConfiguration.class, BusAutoConfiguration.class, ConsulBusAutoConfiguration.class})
- @EnableIntegration
- public static class InboundConfig implements ApplicationListener {
- RemoteApplicationEvent message;
-
- @Override
- public void onApplicationEvent(RemoteApplicationEvent event) {
- this.message = event;
- }
- }
+ @Override
+ public void onApplicationEvent(RemoteApplicationEvent event) {
+ this.message = event;
+ }
+ }
}
diff --git a/spring-cloud-consul-config/pom.xml b/spring-cloud-consul-config/pom.xml
index d7b04401..081ee83b 100644
--- a/spring-cloud-consul-config/pom.xml
+++ b/spring-cloud-consul-config/pom.xml
@@ -11,7 +11,7 @@
org.springframework.cloud
spring-cloud-consul
- 1.0.0.BUILD-SNAPSHOT
+ 1.0.1.BUILD-SNAPSHOT
..
@@ -22,7 +22,7 @@
org.springframework.cloud
- spring-cloud-config-client
+ spring-cloud-context
org.projectlombok
diff --git a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigBootstrapConfiguration.java b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigBootstrapConfiguration.java
index 984d16cd..adb52ce7 100644
--- a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigBootstrapConfiguration.java
+++ b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigBootstrapConfiguration.java
@@ -17,7 +17,6 @@
package org.springframework.cloud.consul.config;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.cloud.config.client.ConfigClientProperties;
import org.springframework.cloud.consul.ConsulAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -35,13 +34,7 @@ public class ConsulConfigBootstrapConfiguration {
private ConfigurableEnvironment environment;
@Bean
- public ConfigClientProperties configClientProperties() {
- ConfigClientProperties client = new ConfigClientProperties(environment);
- return client;
+ public ConsulPropertySourceLocator consulPropertySourceLocator() {
+ return new ConsulPropertySourceLocator();
}
-
- @Bean
- public ConsulPropertySourceLocator consulPropertySourceLocator() {
- return new ConsulPropertySourceLocator();
- }
}
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 e18ce263..9934ac68 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
@@ -16,62 +16,64 @@
package org.springframework.cloud.consul.config;
+import static org.springframework.util.Base64Utils.decodeFromString;
+
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.springframework.core.env.EnumerablePropertySource;
+
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 org.springframework.util.Base64Utils.*;
/**
* @author Spencer Gibb
*/
public class ConsulPropertySource extends EnumerablePropertySource {
- private String context;
+ private String context;
- private Map properties = new LinkedHashMap<>();
+ private Map properties = new LinkedHashMap<>();
- public ConsulPropertySource(String context, ConsulClient source) {
- super(context, source);
- this.context = context;
+ public ConsulPropertySource(String context, ConsulClient source) {
+ super(context, source);
+ this.context = context;
- if (!this.context.endsWith("/")) {
- this.context = this.context + "/";
- }
- }
+ if (!this.context.endsWith("/")) {
+ this.context = this.context + "/";
+ }
+ }
- public void init() {
- Response> response = source.getKVValues(context, QueryParams.DEFAULT);
+ public void init() {
+ Response> response = source.getKVValues(context,
+ QueryParams.DEFAULT);
List values = response.getValue();
- if (values != null) {
- for (GetValue getValue : values) {
- String key = getValue.getKey()
- .replace(context, "")
- .replace('/', '.');
- String value = getDecoded(getValue.getValue());
- properties.put(key, value);
- }
- }
- }
+ if (values != null) {
+ for (GetValue getValue : values) {
+ String key = getValue.getKey().replace(context, "").replace('/', '.');
+ String value = getDecoded(getValue.getValue());
+ properties.put(key, value);
+ }
+ }
+ }
- public String getDecoded(String value) {
- if (value == null)
- return null;
+ public String getDecoded(String value) {
+ if (value == null)
+ return null;
return new String(decodeFromString(value));
- }
+ }
- @Override
- public Object getProperty(String name) {
- return properties.get(name);
- }
+ @Override
+ public Object getProperty(String name) {
+ return properties.get(name);
+ }
- @Override
- public String[] getPropertyNames() {
- return properties.keySet().toArray(new String[0]);
- }
+ @Override
+ public String[] getPropertyNames() {
+ return properties.keySet().toArray(new String[0]);
+ }
}
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 b4bc8493..b35a2215 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
@@ -16,65 +16,70 @@
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.core.env.*;
-
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
+import org.springframework.cloud.consul.ConsulProperties;
+import org.springframework.core.env.CompositePropertySource;
+import org.springframework.core.env.ConfigurableEnvironment;
+import org.springframework.core.env.Environment;
+import org.springframework.core.env.PropertySource;
+
+import com.ecwid.consul.v1.ConsulClient;
+
/**
* @author Spencer Gibb
*/
public class ConsulPropertySourceLocator implements PropertySourceLocator {
- @Autowired
- private ConsulClient consul;
+ @Autowired
+ private ConsulClient consul;
- @Autowired
- private ConsulProperties properties;
+ @Autowired
+ private ConsulProperties properties;
- @Override
- public PropertySource> locate(Environment environment) {
- if (environment instanceof ConfigurableEnvironment) {
- ConfigurableEnvironment env = (ConfigurableEnvironment) environment;
- String appName = env.getProperty("spring.application.name");
- List profiles = Arrays.asList(env.getActiveProfiles());
+ @Override
+ public PropertySource> locate(Environment environment) {
+ if (environment instanceof ConfigurableEnvironment) {
+ ConfigurableEnvironment env = (ConfigurableEnvironment) environment;
+ String appName = env.getProperty("spring.application.name");
+ List profiles = Arrays.asList(env.getActiveProfiles());
- String prefix = properties.getPrefix();
- List contexts = new ArrayList<>();
+ String prefix = properties.getPrefix();
+ List contexts = new ArrayList<>();
- String defaultContext = prefix + "/application";
- contexts.add(defaultContext + "/");
- addProfiles(contexts, defaultContext, profiles);
+ String defaultContext = prefix + "/application";
+ contexts.add(defaultContext + "/");
+ addProfiles(contexts, defaultContext, profiles);
- String baseContext = prefix + "/" + appName;
- contexts.add(baseContext + "/");
- addProfiles(contexts, baseContext, profiles);
+ String baseContext = prefix + "/" + appName;
+ contexts.add(baseContext + "/");
+ addProfiles(contexts, baseContext, profiles);
- CompositePropertySource composite = new CompositePropertySource("consul");
+ CompositePropertySource composite = new CompositePropertySource("consul");
- for (String propertySourceContext : contexts) {
- ConsulPropertySource propertySource = create(propertySourceContext);
- propertySource.init();
- composite.addPropertySource(propertySource);
- }
+ for (String propertySourceContext : contexts) {
+ ConsulPropertySource propertySource = create(propertySourceContext);
+ propertySource.init();
+ composite.addPropertySource(propertySource);
+ }
- return composite;
- }
- return null;
- }
+ return composite;
+ }
+ return null;
+ }
- private ConsulPropertySource create(String context) {
- return new ConsulPropertySource(context, consul);
- }
+ private ConsulPropertySource create(String context) {
+ return new ConsulPropertySource(context, consul);
+ }
- private void addProfiles(List contexts, String baseContext, List profiles) {
- for (String profile : profiles) {
- contexts.add(baseContext + "::" + profile + "/");
- }
- }
+ private void addProfiles(List contexts, String baseContext,
+ List profiles) {
+ for (String profile : profiles) {
+ contexts.add(baseContext + "::" + profile + "/");
+ }
+ }
}
diff --git a/spring-cloud-consul-core/pom.xml b/spring-cloud-consul-core/pom.xml
index 05052729..65f81b8a 100644
--- a/spring-cloud-consul-core/pom.xml
+++ b/spring-cloud-consul-core/pom.xml
@@ -12,7 +12,7 @@
org.springframework.cloud
spring-cloud-consul
- 1.0.0.BUILD-SNAPSHOT
+ 1.0.1.BUILD-SNAPSHOT
..
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 fce0f698..b1346170 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
@@ -16,12 +16,13 @@
package org.springframework.cloud.consul;
-import com.ecwid.consul.v1.ConsulClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
+import com.ecwid.consul.v1.ConsulClient;
+
/**
* @author Spencer Gibb
*/
@@ -29,27 +30,28 @@ import org.springframework.context.annotation.Configuration;
@EnableConfigurationProperties
public class ConsulAutoConfiguration {
- @Bean
- @ConditionalOnMissingBean
- public ConsulProperties consulProperties() {
- return new ConsulProperties();
- }
+ @Bean
+ @ConditionalOnMissingBean
+ public ConsulProperties consulProperties() {
+ return new ConsulProperties();
+ }
- @Bean
- @ConditionalOnMissingBean
- public ConsulClient consulClient() {
- return new ConsulClient(consulProperties().getHost(), consulProperties().getPort());
- }
+ @Bean
+ @ConditionalOnMissingBean
+ public ConsulClient consulClient() {
+ return new ConsulClient(consulProperties().getHost(), consulProperties()
+ .getPort());
+ }
- @Bean
- @ConditionalOnMissingBean
- public ConsulEndpoint consulEndpoint() {
- return new ConsulEndpoint();
- }
+ @Bean
+ @ConditionalOnMissingBean
+ public ConsulEndpoint consulEndpoint() {
+ return new ConsulEndpoint();
+ }
- @Bean
- @ConditionalOnMissingBean
- public ConsulHealthIndicator consulHealthIndicator() {
- return new ConsulHealthIndicator();
- }
+ @Bean
+ @ConditionalOnMissingBean
+ public ConsulHealthIndicator consulHealthIndicator() {
+ return new ConsulHealthIndicator();
+ }
}
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 1b53b1c1..19418478 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
@@ -16,20 +16,22 @@
package org.springframework.cloud.consul;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+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 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 java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
/**
* @author Spencer Gibb
@@ -37,43 +39,44 @@ import java.util.Map;
@ConfigurationProperties(prefix = "endpoints.consul", ignoreUnknownFields = false)
public class ConsulEndpoint extends AbstractEndpoint {
- @Autowired
- private ConsulClient consul;
+ @Autowired
+ private ConsulClient consul;
- @Autowired
- public ConsulEndpoint() {
- super("consul", false, true);
- }
+ @Autowired
+ public ConsulEndpoint() {
+ super("consul", false, true);
+ }
- @Override
- public ConsulData invoke() {
- ConsulData data = new ConsulData();
- //data.setKeyValues(kvClient.getKeyValueRecurse());
- Response