Merge branch 'master' into 2.0.x

This commit is contained in:
Spencer Gibb
2017-10-18 14:51:44 -04:00
4 changed files with 41 additions and 32 deletions

View File

@@ -35,22 +35,28 @@ import org.springframework.util.StringUtils;
*/
public class DiscoveryClientHostLocator implements HostLocator {
private final DiscoveryClient client;
private final ServiceInstance localServiceInstance;
private final ZipkinProperties zipkinProperties;
@Deprecated
public DiscoveryClientHostLocator(DiscoveryClient client, ZipkinProperties zipkinProperties) {
this.client = client;
Assert.notNull(this.client, "client");
Assert.notNull(client, "client");
this.localServiceInstance = client.getLocalServiceInstance();
this.zipkinProperties = zipkinProperties;
}
public DiscoveryClientHostLocator(ServiceInstance localServiceInstance, ZipkinProperties zipkinProperties) {
Assert.notNull(localServiceInstance, "localServiceInstance");
this.localServiceInstance = localServiceInstance;
this.zipkinProperties = zipkinProperties;
}
@Override
public Host locate(Span span) {
ServiceInstance instance = this.client.getLocalServiceInstance();
String serviceId = StringUtils.hasText(this.zipkinProperties.getService().getName()) ?
this.zipkinProperties.getService().getName() : instance.getServiceId();
return new Host(serviceId, getIpAddress(instance),
instance.getPort());
this.zipkinProperties.getService().getName() : this.localServiceInstance.getServiceId();
return new Host(serviceId, getIpAddress(this.localServiceInstance),
this.localServiceInstance.getPort());
}
private String getIpAddress(ServiceInstance instance) {

View File

@@ -29,6 +29,7 @@ import org.springframework.boot.autoconfigure.integration.IntegrationAutoConfigu
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.cloud.sleuth.Sampler;
import org.springframework.cloud.sleuth.SpanAdjuster;
@@ -143,12 +144,12 @@ public class SleuthStreamAutoConfiguration {
private Environment environment;
@Autowired(required = false)
private DiscoveryClient client;
private Registration registration;
@Bean
public HostLocator zipkinEndpointLocator() {
if (this.client != null) {
return new DiscoveryClientHostLocator(this.client, this.zipkinProperties);
if (this.registration != null) {
return new DiscoveryClientHostLocator(this.registration, this.zipkinProperties);
}
return new ServerPropertiesHostLocator(this.serverProperties, this.environment, this.zipkinProperties,
this.inetUtils);

View File

@@ -4,7 +4,7 @@ import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -28,7 +28,7 @@ public class DiscoveryClientEndpointLocatorConfigurationTest {
@Test
public void endpointLocatorShouldDefaultToServerPropertiesEndpointLocatorEvenWhenDiscoveryClientPresent() {
try (ConfigurableApplicationContext ctxt = new SpringApplication(
ConfigurationWithDiscoveryClient.class).run("--spring.jmx.enabled=false",
ConfigurationWithRegistration.class).run("--spring.jmx.enabled=false",
"--spring.main.web_environment=false")) {
assertThat(ctxt.getBean(HostLocator.class))
.isInstanceOf(ServerPropertiesHostLocator.class);
@@ -48,7 +48,7 @@ public class DiscoveryClientEndpointLocatorConfigurationTest {
@Test
public void endpointLocatorShouldBeFallbackHavingEndpointLocatorWhenAskedTo() {
try (ConfigurableApplicationContext ctxt = new SpringApplication(
ConfigurationWithDiscoveryClient.class).run("--spring.jmx.enabled=false",
ConfigurationWithRegistration.class).run("--spring.jmx.enabled=false",
"--spring.zipkin.locator.discovery.enabled=true",
"--spring.main.web_environment=false")) {
assertThat(ctxt.getBean(HostLocator.class))
@@ -59,7 +59,7 @@ public class DiscoveryClientEndpointLocatorConfigurationTest {
@Test
public void endpointLocatorShouldRespectExistingEndpointLocatorEvenWhenAskedToBeDiscovery() {
try (ConfigurableApplicationContext ctxt = new SpringApplication(
ConfigurationWithDiscoveryClient.class,
ConfigurationWithRegistration.class,
ConfigurationWithCustomLocator.class).run("--spring.jmx.enabled=false",
"--spring.zipkin.locator.discovery.enabled=true",
"--spring.main.web_environment=false")) {
@@ -75,9 +75,9 @@ public class DiscoveryClientEndpointLocatorConfigurationTest {
@Configuration
@EnableAutoConfiguration
public static class ConfigurationWithDiscoveryClient {
@Bean public DiscoveryClient getDiscoveryClient() {
return Mockito.mock(DiscoveryClient.class);
public static class ConfigurationWithRegistration {
@Bean public Registration registration() {
return Mockito.mock(Registration.class);
}
}

View File

@@ -20,32 +20,35 @@ import java.net.URI;
import java.util.Map;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.commons.util.InetUtils;
import static org.assertj.core.api.BDDAssertions.then;
import static org.mockito.BDDMockito.given;
/**
* @author Marcin Grzejszczak
*/
public class DiscoveryClientHostLocatorTest {
DiscoveryClient discoveryClient = Mockito.mock(DiscoveryClient.class);
DiscoveryClientHostLocator discoveryClientHostLocator =
new DiscoveryClientHostLocator(this.discoveryClient, new ZipkinProperties());
@Test(expected = IllegalArgumentException.class)
public void should_throw_exception_when_no_discovery_client_is_present() throws Exception {
new DiscoveryClientHostLocator(null, new ZipkinProperties());
public void should_throw_exception_when_no_registration_is_present() throws Exception {
new DiscoveryClientHostLocator((Registration)null, new ZipkinProperties());
}
private DiscoveryClientHostLocator hostLocator(ServiceInstance serviceInstance) {
return hostLocator(serviceInstance, new ZipkinProperties());
}
private DiscoveryClientHostLocator hostLocator(ServiceInstance serviceInstance, ZipkinProperties zipkinProperties) {
return new DiscoveryClientHostLocator(serviceInstance, zipkinProperties);
}
@Test
public void should_create_Host_with_0_ip_when_exception_occurs_on_resolving_host() throws Exception {
given(this.discoveryClient.getLocalServiceInstance()).willReturn(serviceInstanceWithInvalidHost());
DiscoveryClientHostLocator hostLocator = hostLocator(serviceInstanceWithInvalidHost());
Host host = this.discoveryClientHostLocator.locate(null);
Host host = hostLocator.locate(null);
then(host.getServiceName()).isEqualTo("serviceId");
then(host.getPort()).isEqualTo((short)8_000);
@@ -54,9 +57,9 @@ public class DiscoveryClientHostLocatorTest {
@Test
public void should_create_valid_Host_when_proper_host_is_passed() throws Exception {
given(this.discoveryClient.getLocalServiceInstance()).willReturn(serviceInstanceWithValidHost());
DiscoveryClientHostLocator hostLocator = hostLocator(serviceInstanceWithValidHost());
Host host = this.discoveryClientHostLocator.locate(null);
Host host = hostLocator.locate(null);
then(host.getServiceName()).isEqualTo("serviceId");
then(host.getPort()).isEqualTo((short)8_000);
@@ -65,12 +68,11 @@ public class DiscoveryClientHostLocatorTest {
@Test
public void should_override_the_service_name_from_properties() throws Exception {
given(this.discoveryClient.getLocalServiceInstance()).willReturn(serviceInstanceWithValidHost());
ZipkinProperties zipkinProperties = new ZipkinProperties();
zipkinProperties.getService().setName("foo");
this.discoveryClientHostLocator = new DiscoveryClientHostLocator(this.discoveryClient, zipkinProperties);
DiscoveryClientHostLocator hostLocator = new DiscoveryClientHostLocator(serviceInstanceWithValidHost(), zipkinProperties);
Host host = this.discoveryClientHostLocator.locate(null);
Host host = hostLocator.locate(null);
then(host.getServiceName()).isEqualTo("foo");
then(host.getPort()).isEqualTo((short)8_000);