Browse Source
# Conflicts: # docs/pom.xml # pom.xml # spring-cloud-commons-dependencies/pom.xml # spring-cloud-commons/pom.xml # spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClientAutoConfiguration.java # spring-cloud-context/pom.xml # spring-cloud-starter/pom.xmlpull/227/head
Spencer Gibb
7 years ago
9 changed files with 312 additions and 90 deletions
@ -0,0 +1,73 @@
@@ -0,0 +1,73 @@
|
||||
package org.springframework.cloud.client.discovery.composite; |
||||
|
||||
import org.springframework.cloud.client.ServiceInstance; |
||||
import org.springframework.cloud.client.discovery.DiscoveryClient; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.LinkedHashSet; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* A {@link DiscoveryClient} composed of other Discovery Client's and will delegate the |
||||
* calls to each of them in order |
||||
* |
||||
* @author Biju Kunjummen |
||||
*/ |
||||
public class CompositeDiscoveryClient implements DiscoveryClient { |
||||
|
||||
private final List<DiscoveryClient> discoveryClients; |
||||
|
||||
public CompositeDiscoveryClient(List<DiscoveryClient> discoveryClients) { |
||||
this.discoveryClients = discoveryClients; |
||||
} |
||||
|
||||
@Override |
||||
public String description() { |
||||
return "Composite Discovery Client"; |
||||
} |
||||
|
||||
@Override |
||||
public ServiceInstance getLocalServiceInstance() { |
||||
if (this.discoveryClients != null) { |
||||
for (DiscoveryClient discoveryClient : discoveryClients) { |
||||
ServiceInstance serviceInstance = discoveryClient.getLocalServiceInstance(); |
||||
if (serviceInstance != null) { |
||||
return serviceInstance; |
||||
} |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
public List<ServiceInstance> getInstances(String serviceId) { |
||||
if (this.discoveryClients != null) { |
||||
for (DiscoveryClient discoveryClient : discoveryClients) { |
||||
List<ServiceInstance> instances = discoveryClient.getInstances(serviceId); |
||||
if (instances != null && instances.size() > 0) { |
||||
return instances; |
||||
} |
||||
} |
||||
} |
||||
return Collections.emptyList(); |
||||
} |
||||
|
||||
@Override |
||||
public List<String> getServices() { |
||||
LinkedHashSet<String> services = new LinkedHashSet<>(); |
||||
if (this.discoveryClients != null) { |
||||
for (DiscoveryClient discoveryClient : discoveryClients) { |
||||
List<String> serviceForClient = discoveryClient.getServices(); |
||||
if (serviceForClient != null) { |
||||
services.addAll(serviceForClient); |
||||
} |
||||
} |
||||
} |
||||
return new ArrayList<>(services); |
||||
} |
||||
|
||||
List<DiscoveryClient> getDiscoveryClients() { |
||||
return discoveryClients; |
||||
} |
||||
} |
@ -0,0 +1,28 @@
@@ -0,0 +1,28 @@
|
||||
package org.springframework.cloud.client.discovery.composite; |
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore; |
||||
import org.springframework.cloud.client.discovery.DiscoveryClient; |
||||
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.context.annotation.Primary; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* Auto-configuration for Composite Discovery Client. |
||||
* |
||||
* @author Biju Kunjummen |
||||
*/ |
||||
|
||||
@Configuration |
||||
@AutoConfigureBefore(SimpleDiscoveryClientAutoConfiguration.class) |
||||
public class CompositeDiscoveryClientAutoConfiguration { |
||||
|
||||
@Bean |
||||
@Primary |
||||
public CompositeDiscoveryClient compositeDiscoveryClient(List<DiscoveryClient> discoveryClients) { |
||||
return new CompositeDiscoveryClient(discoveryClients); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,79 @@
@@ -0,0 +1,79 @@
|
||||
package org.springframework.cloud.client.discovery.composite; |
||||
|
||||
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.ServiceInstance; |
||||
import org.springframework.cloud.client.discovery.DiscoveryClient; |
||||
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClient; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.test.context.junit4.SpringRunner; |
||||
|
||||
import java.util.List; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Composite Discovery Client should be the one found by default. |
||||
* |
||||
* @author Biju Kunjummen |
||||
*/ |
||||
|
||||
@RunWith(SpringRunner.class) |
||||
@SpringBootTest |
||||
public class CompositeDiscoveryClientAutoConfigurationTests { |
||||
|
||||
@Autowired |
||||
private DiscoveryClient discoveryClient; |
||||
|
||||
@Test |
||||
public void compositeDiscoveryClientShouldBeTheDefault() { |
||||
assertThat(discoveryClient).isInstanceOf(CompositeDiscoveryClient.class); |
||||
CompositeDiscoveryClient compositeDiscoveryClient = (CompositeDiscoveryClient) discoveryClient; |
||||
assertThat(compositeDiscoveryClient.getDiscoveryClients()).hasSize(2); |
||||
assertThat(compositeDiscoveryClient.getDiscoveryClients().get(0).description()) |
||||
.isEqualTo("A custom discovery client"); |
||||
} |
||||
|
||||
@Test |
||||
public void simpleDiscoveryClientShouldBeHaveTheLowestPrecedence() { |
||||
CompositeDiscoveryClient compositeDiscoveryClient = (CompositeDiscoveryClient) discoveryClient; |
||||
assertThat(compositeDiscoveryClient.getDiscoveryClients().get(0).description()) |
||||
.isEqualTo("A custom discovery client"); |
||||
assertThat(compositeDiscoveryClient.getDiscoveryClients().get(1)) |
||||
.isInstanceOf(SimpleDiscoveryClient.class); |
||||
} |
||||
|
||||
@EnableAutoConfiguration |
||||
@Configuration |
||||
public static class Config { |
||||
|
||||
@Bean |
||||
public DiscoveryClient customDiscoveryClient1() { |
||||
return new DiscoveryClient() { |
||||
@Override |
||||
public String description() { |
||||
return "A custom discovery client"; |
||||
} |
||||
|
||||
@Override |
||||
public ServiceInstance getLocalServiceInstance() { |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
public List<ServiceInstance> getInstances(String serviceId) { |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
public List<String> getServices() { |
||||
return null; |
||||
} |
||||
}; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,116 @@
@@ -0,0 +1,116 @@
|
||||
package org.springframework.cloud.client.discovery.composite; |
||||
|
||||
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 java.net.URI; |
||||
import java.util.Arrays; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Tests for behavior of Composite Discovery Client |
||||
* |
||||
* @author Biju Kunjummen |
||||
*/ |
||||
|
||||
@RunWith(SpringRunner.class) |
||||
@SpringBootTest(properties = { |
||||
"spring.application.name=service0", |
||||
"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" }) |
||||
public class CompositeDiscoveryClientTests { |
||||
|
||||
@Autowired |
||||
private DiscoveryClient discoveryClient; |
||||
|
||||
@Test |
||||
public void getInstancesByServiceIdShouldDelegateCall() { |
||||
assertThat(this.discoveryClient).isInstanceOf(CompositeDiscoveryClient.class); |
||||
|
||||
assertThat(this.discoveryClient.getInstances("service1")).hasSize(2); |
||||
|
||||
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")); |
||||
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")).hasSize(1); |
||||
assertThat(this.discoveryClient.getInstances("custom")).hasSize(1); |
||||
} |
||||
|
||||
@Test |
||||
public void getInstancesByUnknownServiceIdShouldReturnAnEmptyList() { |
||||
assertThat(this.discoveryClient.getInstances("unknown")).hasSize(0); |
||||
} |
||||
|
||||
|
||||
@Test |
||||
public void localServiceInstanceShouldReturnTheFirstMatch() { |
||||
assertThat(this.discoveryClient.getLocalServiceInstance().getServiceId()).isEqualTo("service0"); |
||||
} |
||||
|
||||
@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 ServiceInstance getLocalServiceInstance() { |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
public List<ServiceInstance> 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<String> getServices() { |
||||
return Arrays.asList("custom"); |
||||
} |
||||
}; |
||||
} |
||||
} |
||||
} |
@ -1,70 +0,0 @@
@@ -1,70 +0,0 @@
|
||||
package org.springframework.cloud.client.discovery.simple; |
||||
|
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.SpringApplication; |
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
||||
import org.springframework.boot.test.context.SpringBootTest; |
||||
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.test.context.junit4.SpringRunner; |
||||
|
||||
import java.util.List; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* @author Biju Kunjummen |
||||
*/ |
||||
@RunWith(SpringRunner.class) |
||||
@SpringBootTest(classes = UserDefinedDiscoveryClientOverridesDefaultsTests.App.class) |
||||
public class UserDefinedDiscoveryClientOverridesDefaultsTests { |
||||
|
||||
@Autowired |
||||
DiscoveryClient discoveryClient; |
||||
|
||||
@Test |
||||
public void testDiscoveryClientIsNotNoop() { |
||||
assertThat(discoveryClient).isNotInstanceOf(SimpleDiscoveryClient.class); |
||||
|
||||
assertThat(discoveryClient.description()) |
||||
.isEqualTo("user defined discovery client"); |
||||
} |
||||
|
||||
@EnableAutoConfiguration |
||||
@Configuration |
||||
public static class App { |
||||
|
||||
@Bean |
||||
public DiscoveryClient discoveryClient() { |
||||
return new DiscoveryClient() { |
||||
@Override |
||||
public String description() { |
||||
return "user defined discovery client"; |
||||
} |
||||
|
||||
@Override |
||||
public ServiceInstance getLocalServiceInstance() { |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
public List<ServiceInstance> getInstances(String serviceId) { |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
public List<String> getServices() { |
||||
return null; |
||||
} |
||||
}; |
||||
} |
||||
|
||||
public static void main(String[] args) { |
||||
SpringApplication.run(App.class, args); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue