From 8504e7e74f36c270179d3189d78fd4c1d306dbcf Mon Sep 17 00:00:00 2001 From: OlgaMaciaszek Date: Fri, 18 May 2018 13:51:09 +0200 Subject: [PATCH 1/7] Make CompositeDiscoveryClient use ordered DiscoveryClient instances. Fixes gh-304. --- .../client/discovery/DiscoveryClient.java | 8 ++- .../composite/CompositeDiscoveryClient.java | 5 +- .../simple/SimpleDiscoveryClient.java | 6 ++ .../simple/SimpleDiscoveryProperties.java | 21 +++++- .../CompositeDiscoveryClientOrderTest.java | 57 ++++++++++++++++ .../CompositeDiscoveryClientTests.java | 65 +++++------------- .../CompositeDiscoveryClientTestsConfig.java | 66 +++++++++++++++++++ 7 files changed, 173 insertions(+), 55 deletions(-) create mode 100644 spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientOrderTest.java create mode 100644 spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTestsConfig.java diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/DiscoveryClient.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/DiscoveryClient.java index a6d0f93c..28322b2f 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/DiscoveryClient.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/DiscoveryClient.java @@ -19,13 +19,15 @@ package org.springframework.cloud.client.discovery; import java.util.List; import org.springframework.cloud.client.ServiceInstance; +import org.springframework.core.Ordered; /** * DiscoveryClient represents read operations commonly available to Discovery service such as * Netflix Eureka or consul.io * @author Spencer Gibb + * @author Olga Maciaszek-Sharma */ -public interface DiscoveryClient { +public interface DiscoveryClient extends Ordered { /** * A human readable description of the implementation, used in HealthIndicator @@ -45,4 +47,8 @@ public interface DiscoveryClient { */ List getServices(); + @Override + default int getOrder() { + return Ordered.LOWEST_PRECEDENCE; + } } diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClient.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClient.java index 21a328de..b653890d 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClient.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClient.java @@ -7,18 +7,21 @@ import java.util.List; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.core.annotation.AnnotationAwareOrderComparator; /** - * A {@link DiscoveryClient} composed of other Discovery Client's and will delegate the + * A {@link DiscoveryClient} composed of other Discovery Clients that will delegate the * calls to each of them in order * * @author Biju Kunjummen + * @author Olga Maciaszek-Sharma */ public class CompositeDiscoveryClient implements DiscoveryClient { private final List discoveryClients; public CompositeDiscoveryClient(List discoveryClients) { + AnnotationAwareOrderComparator.sort(discoveryClients); this.discoveryClients = discoveryClients; } diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClient.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClient.java index f9b3d4fb..c81b7317 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClient.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClient.java @@ -12,6 +12,7 @@ import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryProperti * properties file as a source of service instances * * @author Biju Kunjummen + * @author Olga Maciaszek-Sharma */ public class SimpleDiscoveryClient implements DiscoveryClient { @@ -42,4 +43,9 @@ public class SimpleDiscoveryClient implements DiscoveryClient { public List getServices() { return new ArrayList<>(this.simpleDiscoveryProperties.getInstances().keySet()); } + + @Override + public int getOrder() { + return simpleDiscoveryProperties.getOrder(); + } } diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryProperties.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryProperties.java index e9273b52..0abe74ea 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryProperties.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryProperties.java @@ -10,13 +10,18 @@ import javax.annotation.PostConstruct; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cloud.client.ServiceInstance; +import org.springframework.core.Ordered; /** * Properties to hold the details of a * {@link org.springframework.cloud.client.discovery.DiscoveryClient} service instances - * for a given service + * for a given service. + * It also holds the user-configurable order that will be used to establish the + * precedence of this client in the list of clients + * used by {@link org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClient}. * * @author Biju Kunjummen + * @author Olga Maciaszek-Sharma */ @ConfigurationProperties(prefix = "spring.cloud.discovery.client.simple") @@ -30,8 +35,10 @@ public class SimpleDiscoveryProperties { */ private SimpleServiceInstance local = new SimpleServiceInstance(); + private int order = Ordered.LOWEST_PRECEDENCE; + public Map> getInstances() { - return this.instances; + return instances; } public void setInstances(Map> instances) { @@ -39,7 +46,15 @@ public class SimpleDiscoveryProperties { } public SimpleServiceInstance getLocal() { - return this.local; + return local; + } + + public int getOrder() { + return order; + } + + public void setOrder(int order) { + this.order = order; } @PostConstruct diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientOrderTest.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientOrderTest.java new file mode 100644 index 00000000..d30fcd33 --- /dev/null +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientOrderTest.java @@ -0,0 +1,57 @@ +package org.springframework.cloud.client.discovery.composite; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientTestsConfig.A_CUSTOM_DISCOVERY_CLIENT; +import static org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientTestsConfig.CUSTOM_SERVICE_ID; +import static org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientTestsConfig.THIRD_DISCOVERY_CLIENT; + +import java.util.List; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * Tests for the support of ordered {@link DiscoveryClient} instances in {@link CompositeDiscoveryClient} + * + * @author Olga Maciaszek-Sharma + */ +@RunWith(SpringRunner.class) +@SpringBootTest(properties = "spring.cloud.discovery.client.simple.order:2", classes = { + CompositeDiscoveryClientTestsConfig.class }) +public class CompositeDiscoveryClientOrderTest { + + @Autowired + DiscoveryClient discoveryClient; + + @Test + public void shouldGetOrderedDiscoveryClients() { + // when: + List discoveryClients = ((CompositeDiscoveryClient) discoveryClient) + .getDiscoveryClients(); + + // then: + assertThat(discoveryClients.get(0).description()) + .isEqualTo(A_CUSTOM_DISCOVERY_CLIENT); + assertThat(discoveryClients.get(1).description()) + .isEqualTo("Simple Discovery Client"); + assertThat(discoveryClients.get(2).description()) + .isEqualTo(THIRD_DISCOVERY_CLIENT); + } + + @Test + public void shouldOnlyReturnServiceInstancesForTheHighestPrecedenceDiscoveryClient() { + // when: + List serviceInstances = discoveryClient + .getInstances(CUSTOM_SERVICE_ID); + + // then: + assertThat(serviceInstances).hasSize(1); + assertThat(serviceInstances.get(0).getPort()).isEqualTo(123); + } +} diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTests.java index 841ab3cd..8eae5829 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTests.java @@ -1,25 +1,19 @@ package org.springframework.cloud.client.discovery.composite; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientTestsConfig.CUSTOM_SERVICE_ID; + import java.net.URI; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; + import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.client.DefaultServiceInstance; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.annotation.Order; import org.springframework.test.context.junit4.SpringRunner; -import static org.assertj.core.api.Assertions.assertThat; - /** * Tests for behavior of Composite Discovery Client * @@ -32,7 +26,8 @@ import static org.assertj.core.api.Assertions.assertThat; "spring.cloud.discovery.client.simple.instances.service1[0].uri=http://s1-1:8080", "spring.cloud.discovery.client.simple.instances.service1[1].uri=https://s1-2:8443", "spring.cloud.discovery.client.simple.instances.service2[0].uri=https://s2-1:8080", - "spring.cloud.discovery.client.simple.instances.service2[1].uri=https://s2-2:443" }) + "spring.cloud.discovery.client.simple.instances.service2[1].uri=https://s2-2:443", }, classes = { + CompositeDiscoveryClientTestsConfig.class }) public class CompositeDiscoveryClientTests { @Autowired @@ -40,11 +35,11 @@ public class CompositeDiscoveryClientTests { @Test public void getInstancesByServiceIdShouldDelegateCall() { - assertThat(this.discoveryClient).isInstanceOf(CompositeDiscoveryClient.class); + assertThat(discoveryClient).isInstanceOf(CompositeDiscoveryClient.class); - assertThat(this.discoveryClient.getInstances("service1")).hasSize(2); + assertThat(discoveryClient.getInstances("service1")).hasSize(2); - ServiceInstance s1 = this.discoveryClient.getInstances("service1").get(0); + ServiceInstance s1 = discoveryClient.getInstances("service1").get(0); assertThat(s1.getHost()).isEqualTo("s1-1"); assertThat(s1.getPort()).isEqualTo(8080); assertThat(s1.getUri()).isEqualTo(URI.create("http://s1-1:8080")); @@ -53,53 +48,23 @@ public class CompositeDiscoveryClientTests { @Test public void getServicesShouldAggregateAllServiceNames() { - assertThat(this.discoveryClient.getServices()).containsOnlyOnce("service1", "service2", "custom"); + assertThat(discoveryClient.getServices()).containsOnlyOnce("service1", "service2", + CUSTOM_SERVICE_ID); } @Test public void getDescriptionShouldBeComposite() { - assertThat(this.discoveryClient.description()).isEqualTo("Composite Discovery Client"); + assertThat(discoveryClient.description()).isEqualTo("Composite Discovery Client"); } @Test public void getInstancesShouldRespectOrder() { - assertThat(this.discoveryClient.getInstances("custom")).hasSize(1); - assertThat(this.discoveryClient.getInstances("custom")).hasSize(1); + assertThat(discoveryClient.getInstances(CUSTOM_SERVICE_ID)).hasSize(1); + assertThat(discoveryClient.getInstances(CUSTOM_SERVICE_ID)).hasSize(1); } @Test public void getInstancesByUnknownServiceIdShouldReturnAnEmptyList() { - assertThat(this.discoveryClient.getInstances("unknown")).hasSize(0); - } - - @EnableAutoConfiguration - @Configuration - public static class Config { - - @Bean - @Order(1) - public DiscoveryClient customDiscoveryClient() { - return new DiscoveryClient() { - @Override - public String description() { - return "A custom discovery client"; - } - - @Override - public List getInstances(String serviceId) { - if (serviceId.equals("custom")) { - ServiceInstance s1 = new DefaultServiceInstance("custom", "host", - 123, false); - return Arrays.asList(s1); - } - return Collections.emptyList(); - } - - @Override - public List getServices() { - return Arrays.asList("custom"); - } - }; - } + assertThat(discoveryClient.getInstances("unknown")).hasSize(0); } } diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTestsConfig.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTestsConfig.java new file mode 100644 index 00000000..b332602e --- /dev/null +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTestsConfig.java @@ -0,0 +1,66 @@ +package org.springframework.cloud.client.discovery.composite; + +import static java.util.Collections.singletonList; + +import java.util.Collections; +import java.util.List; + +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.cloud.client.DefaultServiceInstance; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Test configuration for {@link CompositeDiscoveryClient} tests. + * + * @author Olga Maciaszek-Sharma + */ +@Configuration +@EnableAutoConfiguration +public class CompositeDiscoveryClientTestsConfig { + + static final String A_CUSTOM_DISCOVERY_CLIENT = "A custom discovery client"; + static final String THIRD_DISCOVERY_CLIENT = "Third discovery client"; + static final String CUSTOM_SERVICE_ID = "custom"; + + @Bean + public DiscoveryClient customDiscoveryClient() { + return aDiscoveryClient(1, A_CUSTOM_DISCOVERY_CLIENT); + } + + @Bean + public DiscoveryClient thirdOrderCustomDiscoveryClient() { + return aDiscoveryClient(3, THIRD_DISCOVERY_CLIENT); + } + + private DiscoveryClient aDiscoveryClient(int order, String description) { + return new DiscoveryClient() { + @Override + public String description() { + return description; + } + + @Override + public List getInstances(String serviceId) { + if (serviceId.equals(CUSTOM_SERVICE_ID)) { + ServiceInstance s1 = new DefaultServiceInstance(CUSTOM_SERVICE_ID, + "host", 123, false); + return singletonList(s1); + } + return Collections.emptyList(); + } + + @Override + public List getServices() { + return singletonList(CUSTOM_SERVICE_ID); + } + + @Override + public int getOrder() { + return order; + } + }; + } +} From 3534e1601351b9def52a942e0767a64a87d545ec Mon Sep 17 00:00:00 2001 From: OlgaMaciaszek Date: Fri, 18 May 2018 15:44:44 +0200 Subject: [PATCH 2/7] Adjust changes to existing convention. --- .../discovery/simple/SimpleDiscoveryClient.java | 2 +- .../simple/SimpleDiscoveryProperties.java | 6 +++--- .../CompositeDiscoveryClientOrderTest.java | 4 ++-- .../CompositeDiscoveryClientTests.java | 17 ++++++++--------- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClient.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClient.java index c81b7317..636de6e0 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClient.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClient.java @@ -46,6 +46,6 @@ public class SimpleDiscoveryClient implements DiscoveryClient { @Override public int getOrder() { - return simpleDiscoveryProperties.getOrder(); + return this.simpleDiscoveryProperties.getOrder(); } } diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryProperties.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryProperties.java index 0abe74ea..19898fd7 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryProperties.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryProperties.java @@ -38,7 +38,7 @@ public class SimpleDiscoveryProperties { private int order = Ordered.LOWEST_PRECEDENCE; public Map> getInstances() { - return instances; + return this.instances; } public void setInstances(Map> instances) { @@ -46,11 +46,11 @@ public class SimpleDiscoveryProperties { } public SimpleServiceInstance getLocal() { - return local; + return this.local; } public int getOrder() { - return order; + return this.order; } public void setOrder(int order) { diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientOrderTest.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientOrderTest.java index d30fcd33..24148fa8 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientOrderTest.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientOrderTest.java @@ -32,7 +32,7 @@ public class CompositeDiscoveryClientOrderTest { @Test public void shouldGetOrderedDiscoveryClients() { // when: - List discoveryClients = ((CompositeDiscoveryClient) discoveryClient) + List discoveryClients = ((CompositeDiscoveryClient) this.discoveryClient) .getDiscoveryClients(); // then: @@ -47,7 +47,7 @@ public class CompositeDiscoveryClientOrderTest { @Test public void shouldOnlyReturnServiceInstancesForTheHighestPrecedenceDiscoveryClient() { // when: - List serviceInstances = discoveryClient + List serviceInstances = this.discoveryClient .getInstances(CUSTOM_SERVICE_ID); // then: diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTests.java index 8eae5829..0d6330a1 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTests.java @@ -35,11 +35,11 @@ public class CompositeDiscoveryClientTests { @Test public void getInstancesByServiceIdShouldDelegateCall() { - assertThat(discoveryClient).isInstanceOf(CompositeDiscoveryClient.class); + assertThat(this.discoveryClient).isInstanceOf(CompositeDiscoveryClient.class); - assertThat(discoveryClient.getInstances("service1")).hasSize(2); + assertThat(this.discoveryClient.getInstances("service1")).hasSize(2); - ServiceInstance s1 = discoveryClient.getInstances("service1").get(0); + ServiceInstance s1 = this.discoveryClient.getInstances("service1").get(0); assertThat(s1.getHost()).isEqualTo("s1-1"); assertThat(s1.getPort()).isEqualTo(8080); assertThat(s1.getUri()).isEqualTo(URI.create("http://s1-1:8080")); @@ -48,23 +48,22 @@ public class CompositeDiscoveryClientTests { @Test public void getServicesShouldAggregateAllServiceNames() { - assertThat(discoveryClient.getServices()).containsOnlyOnce("service1", "service2", - CUSTOM_SERVICE_ID); + assertThat(this.discoveryClient.getServices()).containsOnlyOnce("service1", "service2", "custom"); } @Test public void getDescriptionShouldBeComposite() { - assertThat(discoveryClient.description()).isEqualTo("Composite Discovery Client"); + assertThat(this.discoveryClient.description()).isEqualTo("Composite Discovery Client"); } @Test public void getInstancesShouldRespectOrder() { - assertThat(discoveryClient.getInstances(CUSTOM_SERVICE_ID)).hasSize(1); - assertThat(discoveryClient.getInstances(CUSTOM_SERVICE_ID)).hasSize(1); + assertThat(this.discoveryClient.getInstances(CUSTOM_SERVICE_ID)).hasSize(1); + assertThat(this.discoveryClient.getInstances(CUSTOM_SERVICE_ID)).hasSize(1); } @Test public void getInstancesByUnknownServiceIdShouldReturnAnEmptyList() { - assertThat(discoveryClient.getInstances("unknown")).hasSize(0); + assertThat(this.discoveryClient.getInstances("unknown")).hasSize(0); } } From eb08c8c8429edddeedc8402d74b7464c2f276231 Mon Sep 17 00:00:00 2001 From: OlgaMaciaszek Date: Fri, 18 May 2018 17:22:13 +0200 Subject: [PATCH 3/7] Add documentation for the new functionality. --- .../main/asciidoc/spring-cloud-commons.adoc | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/docs/src/main/asciidoc/spring-cloud-commons.adoc b/docs/src/main/asciidoc/spring-cloud-commons.adoc index a028c198..573278e2 100644 --- a/docs/src/main/asciidoc/spring-cloud-commons.adoc +++ b/docs/src/main/asciidoc/spring-cloud-commons.adoc @@ -235,6 +235,30 @@ To disable it, set `spring.cloud.discovery.client.health-indicator.enabled=false To disable the description field of the `DiscoveryClientHealthIndicator`, set `spring.cloud.discovery.client.health-indicator.include-description=false`. Otherwise, it can bubble up as the `description` of the rolled up `HealthIndicator`. +==== Ordering `DiscoveryClient` instances +`DiscoveryClient` interface extends `Ordered` to let you define the order of the returned discovery clients, similar to +how you can order the beans loaded by a Spring application. By default, the order of any `DiscoveryClient` is set to +`Ordered.LOWEST_PRECEDENCE`, which equals `Integer.MAX_VALUE`. If you want to set a different order for your custom +`DiscoveryClient` implementations, you just need to override the `getOrder()` method so that it returns the value that +is suitable for your setup. Apart from this, you can use properties to set the order of the `DiscoveryClient` +implementations provided by Spring Cloud, among others `ConsulDiscoveryClient`, `EurekaDiscoveryClient` and +`ZookeeperDiscoveryClient`. In order to do it, you just need to set the +`spring.cloud.discovery.client.{clientIdentifier}.order` property to the desired value. + +The following example shows how to set order value for Spring Cloud `DiscoveryClient` implementations: + +[source,yaml,indent=0] +---- +spring: + cloud: + discovery: + client: + eureka: 1 +---- + +The following client identifiers are currently supported with this syntax: `eureka`, `consul`, `zookeeper`, +`cloudfoundry`, `simple`. + === ServiceRegistry Commons now provides a `ServiceRegistry` interface that provides methods such as `register(Registration)` and `deregister(Registration)`, which let you provide custom registered services. From 40f8603e4ca28c9e79ac83e092f5ab7616b637fd Mon Sep 17 00:00:00 2001 From: Olga Maciaszek-Sharma Date: Fri, 7 Sep 2018 10:08:30 +0200 Subject: [PATCH 4/7] Add changes after code review. Modify # Conflicts: # spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClient.java # spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryProperties.java --- .../main/asciidoc/spring-cloud-commons.adoc | 6 +++--- .../client/discovery/DiscoveryClient.java | 12 +++++++++++- .../CompositeDiscoveryClientOrderTest.java | 13 ++++++++----- .../CompositeDiscoveryClientTestsConfig.java | 18 ++++++++++++------ 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/docs/src/main/asciidoc/spring-cloud-commons.adoc b/docs/src/main/asciidoc/spring-cloud-commons.adoc index 6f6e5dc4..40ef3984 100644 --- a/docs/src/main/asciidoc/spring-cloud-commons.adoc +++ b/docs/src/main/asciidoc/spring-cloud-commons.adoc @@ -257,9 +257,9 @@ Otherwise, it can bubble up as the `description` of the rolled up `HealthIndicat ==== Ordering `DiscoveryClient` instances `DiscoveryClient` interface extends `Ordered` to let you define the order of the returned discovery clients, similar to how you can order the beans loaded by a Spring application. By default, the order of any `DiscoveryClient` is set to -`Ordered.LOWEST_PRECEDENCE`, which equals `Integer.MAX_VALUE`. If you want to set a different order for your custom -`DiscoveryClient` implementations, you just need to override the `getOrder()` method so that it returns the value that -is suitable for your setup. Apart from this, you can use properties to set the order of the `DiscoveryClient` +`0`. If you want to set a different order for your custom `DiscoveryClient` implementations, you just need to override +the `getOrder()` method so that it returns the value that is suitable for your setup. Apart from this, you can use +properties to set the order of the `DiscoveryClient` implementations provided by Spring Cloud, among others `ConsulDiscoveryClient`, `EurekaDiscoveryClient` and `ZookeeperDiscoveryClient`. In order to do it, you just need to set the `spring.cloud.discovery.client.{clientIdentifier}.order` property to the desired value. diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/DiscoveryClient.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/DiscoveryClient.java index cce4044a..0b7cb1ca 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/DiscoveryClient.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/DiscoveryClient.java @@ -24,19 +24,24 @@ import org.springframework.core.Ordered; /** * Represents read operations commonly available to discovery services such as Netflix * Eureka or consul.io. + * * @author Spencer Gibb * @author Olga Maciaszek-Sharma */ public interface DiscoveryClient extends Ordered { + int DEFAULT_ORDER = 0; + /** * A human-readable description of the implementation, used in HealthIndicator. + * * @return The description. */ String description(); /** * Gets all ServiceInstances associated with a particular serviceId. + * * @param serviceId The serviceId to query. * @return A List of ServiceInstance. */ @@ -47,8 +52,13 @@ public interface DiscoveryClient extends Ordered { */ List getServices(); + /** + * Default implementation for getting order of discovery clients. + * + * @return order + */ @Override default int getOrder() { - return Ordered.LOWEST_PRECEDENCE; + return DEFAULT_ORDER; } } diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientOrderTest.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientOrderTest.java index 24148fa8..a068e36a 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientOrderTest.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientOrderTest.java @@ -1,9 +1,10 @@ package org.springframework.cloud.client.discovery.composite; import static org.assertj.core.api.Assertions.assertThat; -import static org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientTestsConfig.A_CUSTOM_DISCOVERY_CLIENT; +import static org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientTestsConfig.CUSTOM_DISCOVERY_CLIENT; import static org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientTestsConfig.CUSTOM_SERVICE_ID; -import static org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientTestsConfig.THIRD_DISCOVERY_CLIENT; +import static org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientTestsConfig.DEFAULT_ORDER_DISCOVERY_CLIENT; +import static org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientTestsConfig.FOURTH_DISCOVERY_CLIENT; import java.util.List; @@ -37,11 +38,13 @@ public class CompositeDiscoveryClientOrderTest { // then: assertThat(discoveryClients.get(0).description()) - .isEqualTo(A_CUSTOM_DISCOVERY_CLIENT); + .isEqualTo(CUSTOM_DISCOVERY_CLIENT); assertThat(discoveryClients.get(1).description()) - .isEqualTo("Simple Discovery Client"); + .isEqualTo(DEFAULT_ORDER_DISCOVERY_CLIENT); assertThat(discoveryClients.get(2).description()) - .isEqualTo(THIRD_DISCOVERY_CLIENT); + .isEqualTo("Simple Discovery Client"); + assertThat(discoveryClients.get(3).description()) + .isEqualTo(FOURTH_DISCOVERY_CLIENT); } @Test diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTestsConfig.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTestsConfig.java index b332602e..31426334 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTestsConfig.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTestsConfig.java @@ -21,21 +21,27 @@ import org.springframework.context.annotation.Configuration; @EnableAutoConfiguration public class CompositeDiscoveryClientTestsConfig { - static final String A_CUSTOM_DISCOVERY_CLIENT = "A custom discovery client"; - static final String THIRD_DISCOVERY_CLIENT = "Third discovery client"; + static final String DEFAULT_ORDER_DISCOVERY_CLIENT = "Default order discovery client"; + static final String CUSTOM_DISCOVERY_CLIENT = "A custom discovery client"; + static final String FOURTH_DISCOVERY_CLIENT = "Fourth discovery client"; static final String CUSTOM_SERVICE_ID = "custom"; @Bean public DiscoveryClient customDiscoveryClient() { - return aDiscoveryClient(1, A_CUSTOM_DISCOVERY_CLIENT); + return aDiscoveryClient(-1, CUSTOM_DISCOVERY_CLIENT); } @Bean public DiscoveryClient thirdOrderCustomDiscoveryClient() { - return aDiscoveryClient(3, THIRD_DISCOVERY_CLIENT); + return aDiscoveryClient(3, FOURTH_DISCOVERY_CLIENT); } - private DiscoveryClient aDiscoveryClient(int order, String description) { + @Bean + public DiscoveryClient defaultOrderDiscoveryClient() { + return aDiscoveryClient(null, DEFAULT_ORDER_DISCOVERY_CLIENT); + } + + private DiscoveryClient aDiscoveryClient(Integer order, String description) { return new DiscoveryClient() { @Override public String description() { @@ -59,7 +65,7 @@ public class CompositeDiscoveryClientTestsConfig { @Override public int getOrder() { - return order; + return order != null ? order : DiscoveryClient.super.getOrder(); } }; } From 66a959b413933d63be40418971387120320c65c3 Mon Sep 17 00:00:00 2001 From: Olga Maciaszek-Sharma Date: Fri, 7 Sep 2018 11:28:40 +0200 Subject: [PATCH 5/7] Change default order for SimpleDiscoveryClient. --- .../client/discovery/simple/SimpleDiscoveryProperties.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryProperties.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryProperties.java index 9d25c487..f242ceea 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryProperties.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryProperties.java @@ -10,7 +10,7 @@ import javax.annotation.PostConstruct; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cloud.client.ServiceInstance; -import org.springframework.core.Ordered; +import org.springframework.cloud.client.discovery.DiscoveryClient; /** * Properties to hold the details of a @@ -35,7 +35,7 @@ public class SimpleDiscoveryProperties { */ private SimpleServiceInstance local = new SimpleServiceInstance(); - private int order = Ordered.LOWEST_PRECEDENCE; + private int order = DiscoveryClient.DEFAULT_ORDER; public Map> getInstances() { return this.instances; From ae2bba4d27fe7c9e7a1d4a97518021c42260b6a2 Mon Sep 17 00:00:00 2001 From: Olga Maciaszek-Sharma Date: Fri, 7 Sep 2018 14:32:11 +0200 Subject: [PATCH 6/7] Adjust doc to order property changes. --- docs/src/main/asciidoc/spring-cloud-commons.adoc | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/docs/src/main/asciidoc/spring-cloud-commons.adoc b/docs/src/main/asciidoc/spring-cloud-commons.adoc index 40ef3984..2c669209 100644 --- a/docs/src/main/asciidoc/spring-cloud-commons.adoc +++ b/docs/src/main/asciidoc/spring-cloud-commons.adoc @@ -262,21 +262,7 @@ the `getOrder()` method so that it returns the value that is suitable for your s properties to set the order of the `DiscoveryClient` implementations provided by Spring Cloud, among others `ConsulDiscoveryClient`, `EurekaDiscoveryClient` and `ZookeeperDiscoveryClient`. In order to do it, you just need to set the -`spring.cloud.discovery.client.{clientIdentifier}.order` property to the desired value. - -The following example shows how to set order value for Spring Cloud `DiscoveryClient` implementations: - -[source,yaml,indent=0] ----- -spring: - cloud: - discovery: - client: - eureka: 1 ----- - -The following client identifiers are currently supported with this syntax: `eureka`, `consul`, `zookeeper`, -`cloudfoundry`, `simple`. +`spring.cloud.{clientIdentifier}.discovery.order` (or `eureka.client.order` for Eureka) property to the desired value. === ServiceRegistry From e115562cf89aa69a0e91c62510fedd58e819243d Mon Sep 17 00:00:00 2001 From: Olga Maciaszek-Sharma Date: Mon, 10 Sep 2018 09:21:20 +0200 Subject: [PATCH 7/7] Fix after code review - add license notice, reformat imports, edit docs. --- .../main/asciidoc/spring-cloud-commons.adoc | 3 +- .../client/discovery/DiscoveryClient.java | 2 +- .../composite/CompositeDiscoveryClient.java | 16 +++++++++ .../simple/SimpleDiscoveryClient.java | 16 +++++++++ .../simple/SimpleDiscoveryProperties.java | 16 +++++++++ .../CompositeDiscoveryClientOrderTest.java | 30 ++++++++++++---- .../CompositeDiscoveryClientTests.java | 34 ++++++++++++++----- .../CompositeDiscoveryClientTestsConfig.java | 20 +++++++++-- 8 files changed, 117 insertions(+), 20 deletions(-) diff --git a/docs/src/main/asciidoc/spring-cloud-commons.adoc b/docs/src/main/asciidoc/spring-cloud-commons.adoc index 2c669209..b07587c4 100644 --- a/docs/src/main/asciidoc/spring-cloud-commons.adoc +++ b/docs/src/main/asciidoc/spring-cloud-commons.adoc @@ -255,7 +255,8 @@ To disable the description field of the `DiscoveryClientHealthIndicator`, set `s Otherwise, it can bubble up as the `description` of the rolled up `HealthIndicator`. ==== Ordering `DiscoveryClient` instances -`DiscoveryClient` interface extends `Ordered` to let you define the order of the returned discovery clients, similar to +`DiscoveryClient` interface extends `Ordered`. This is useful when using multiple discovery + clients, as it allows you to define the order of the returned discovery clients, similar to how you can order the beans loaded by a Spring application. By default, the order of any `DiscoveryClient` is set to `0`. If you want to set a different order for your custom `DiscoveryClient` implementations, you just need to override the `getOrder()` method so that it returns the value that is suitable for your setup. Apart from this, you can use diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/DiscoveryClient.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/DiscoveryClient.java index 0b7cb1ca..6c0f00ab 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/DiscoveryClient.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/DiscoveryClient.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2015 the original author or authors. + * Copyright 2013-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClient.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClient.java index e7f6deb5..2df075d8 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClient.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClient.java @@ -1,3 +1,19 @@ +/* + * Copyright 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.springframework.cloud.client.discovery.composite; import java.util.ArrayList; diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClient.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClient.java index 4dbd9b3d..a6bae3dc 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClient.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClient.java @@ -1,3 +1,19 @@ +/* + * Copyright 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.springframework.cloud.client.discovery.simple; import java.util.ArrayList; diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryProperties.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryProperties.java index f242ceea..84590862 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryProperties.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryProperties.java @@ -1,3 +1,19 @@ +/* + * Copyright 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.springframework.cloud.client.discovery.simple; import java.net.URI; diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientOrderTest.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientOrderTest.java index a068e36a..f9f925e3 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientOrderTest.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientOrderTest.java @@ -1,10 +1,20 @@ -package org.springframework.cloud.client.discovery.composite; +/* + * Copyright 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ -import static org.assertj.core.api.Assertions.assertThat; -import static org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientTestsConfig.CUSTOM_DISCOVERY_CLIENT; -import static org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientTestsConfig.CUSTOM_SERVICE_ID; -import static org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientTestsConfig.DEFAULT_ORDER_DISCOVERY_CLIENT; -import static org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientTestsConfig.FOURTH_DISCOVERY_CLIENT; +package org.springframework.cloud.client.discovery.composite; import java.util.List; @@ -17,6 +27,12 @@ import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.test.context.junit4.SpringRunner; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientTestsConfig.CUSTOM_DISCOVERY_CLIENT; +import static org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientTestsConfig.CUSTOM_SERVICE_ID; +import static org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientTestsConfig.DEFAULT_ORDER_DISCOVERY_CLIENT; +import static org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientTestsConfig.FOURTH_DISCOVERY_CLIENT; + /** * Tests for the support of ordered {@link DiscoveryClient} instances in {@link CompositeDiscoveryClient} * @@ -24,7 +40,7 @@ import org.springframework.test.context.junit4.SpringRunner; */ @RunWith(SpringRunner.class) @SpringBootTest(properties = "spring.cloud.discovery.client.simple.order:2", classes = { - CompositeDiscoveryClientTestsConfig.class }) + CompositeDiscoveryClientTestsConfig.class}) public class CompositeDiscoveryClientOrderTest { @Autowired diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTests.java index 0d6330a1..f0eca3bf 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTests.java @@ -1,7 +1,20 @@ -package org.springframework.cloud.client.discovery.composite; +/* + * Copyright 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ -import static org.assertj.core.api.Assertions.assertThat; -import static org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientTestsConfig.CUSTOM_SERVICE_ID; +package org.springframework.cloud.client.discovery.composite; import java.net.URI; @@ -14,9 +27,12 @@ import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.test.context.junit4.SpringRunner; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientTestsConfig.CUSTOM_SERVICE_ID; + /** * Tests for behavior of Composite Discovery Client - * + * * @author Biju Kunjummen */ @@ -26,8 +42,8 @@ import org.springframework.test.context.junit4.SpringRunner; "spring.cloud.discovery.client.simple.instances.service1[0].uri=http://s1-1:8080", "spring.cloud.discovery.client.simple.instances.service1[1].uri=https://s1-2:8443", "spring.cloud.discovery.client.simple.instances.service2[0].uri=https://s2-1:8080", - "spring.cloud.discovery.client.simple.instances.service2[1].uri=https://s2-2:443", }, classes = { - CompositeDiscoveryClientTestsConfig.class }) + "spring.cloud.discovery.client.simple.instances.service2[1].uri=https://s2-2:443",}, classes = { + CompositeDiscoveryClientTestsConfig.class}) public class CompositeDiscoveryClientTests { @Autowired @@ -45,17 +61,17 @@ public class CompositeDiscoveryClientTests { assertThat(s1.getUri()).isEqualTo(URI.create("http://s1-1:8080")); assertThat(s1.isSecure()).isEqualTo(false); } - + @Test public void getServicesShouldAggregateAllServiceNames() { assertThat(this.discoveryClient.getServices()).containsOnlyOnce("service1", "service2", "custom"); } - + @Test public void getDescriptionShouldBeComposite() { assertThat(this.discoveryClient.description()).isEqualTo("Composite Discovery Client"); } - + @Test public void getInstancesShouldRespectOrder() { assertThat(this.discoveryClient.getInstances(CUSTOM_SERVICE_ID)).hasSize(1); diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTestsConfig.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTestsConfig.java index 31426334..2a846eaf 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTestsConfig.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientTestsConfig.java @@ -1,6 +1,20 @@ -package org.springframework.cloud.client.discovery.composite; +/* + * Copyright 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ -import static java.util.Collections.singletonList; +package org.springframework.cloud.client.discovery.composite; import java.util.Collections; import java.util.List; @@ -12,6 +26,8 @@ import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import static java.util.Collections.singletonList; + /** * Test configuration for {@link CompositeDiscoveryClient} tests. *