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
This commit is contained in:
Olga Maciaszek-Sharma
2018-09-07 10:08:30 +02:00
parent 3105663946
commit 40f8603e4c
4 changed files with 34 additions and 15 deletions

View File

@@ -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.

View File

@@ -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<String> getServices();
/**
* Default implementation for getting order of discovery clients.
*
* @return order
*/
@Override
default int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
return DEFAULT_ORDER;
}
}

View File

@@ -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

View File

@@ -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();
}
};
}