Merge branch 'master' into 2.0.x
# Conflicts: # spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ZipkinAutoConfiguration.java # spring-cloud-sleuth-zipkin/src/test/java/org/springframework/cloud/sleuth/zipkin/DiscoveryClientEndpointLocatorConfigurationTest.java # spring-cloud-sleuth-zipkin/src/test/java/org/springframework/cloud/sleuth/zipkin/DiscoveryClientEndpointLocatorTest.java
This commit is contained in:
@@ -40,30 +40,36 @@ public class DiscoveryClientEndpointLocator implements EndpointLocator {
|
||||
|
||||
private static final Log log = LogFactory.getLog(MethodHandles.lookup().lookupClass());
|
||||
|
||||
private final DiscoveryClient client;
|
||||
private final ServiceInstance serviceInstance;
|
||||
private final ZipkinProperties zipkinProperties;
|
||||
|
||||
@Deprecated
|
||||
public DiscoveryClientEndpointLocator(DiscoveryClient client,
|
||||
ZipkinProperties zipkinProperties) {
|
||||
this.client = client;
|
||||
this.serviceInstance = client.getLocalServiceInstance();
|
||||
this.zipkinProperties = zipkinProperties;
|
||||
}
|
||||
|
||||
public DiscoveryClientEndpointLocator(ServiceInstance serviceInstance,
|
||||
ZipkinProperties zipkinProperties) {
|
||||
this.serviceInstance = serviceInstance;
|
||||
this.zipkinProperties = zipkinProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Endpoint local() {
|
||||
ServiceInstance instance = this.client.getLocalServiceInstance();
|
||||
if (instance == null) {
|
||||
if (this.serviceInstance == null) {
|
||||
throw new NoServiceInstanceAvailableException();
|
||||
}
|
||||
String serviceName = StringUtils.hasText(this.zipkinProperties.getService().getName()) ?
|
||||
this.zipkinProperties.getService().getName() : instance.getServiceId();
|
||||
this.zipkinProperties.getService().getName() : this.serviceInstance.getServiceId();
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Span will contain serviceName [" + serviceName + "]");
|
||||
}
|
||||
return Endpoint.builder()
|
||||
.serviceName(serviceName)
|
||||
.ipv4(getIpAddress(instance))
|
||||
.port(instance.getPort()).build();
|
||||
.ipv4(getIpAddress(this.serviceInstance))
|
||||
.port(this.serviceInstance.getPort()).build();
|
||||
}
|
||||
|
||||
private int getIpAddress(ServiceInstance instance) {
|
||||
|
||||
@@ -33,6 +33,7 @@ import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.cloud.client.serviceregistry.Registration;
|
||||
import org.springframework.cloud.sleuth.Sampler;
|
||||
import org.springframework.cloud.sleuth.SpanAdjuster;
|
||||
import org.springframework.cloud.sleuth.SpanReporter;
|
||||
@@ -184,7 +185,7 @@ public class ZipkinAutoConfiguration {
|
||||
private Environment environment;
|
||||
|
||||
@Autowired(required=false)
|
||||
private DiscoveryClient client;
|
||||
private Registration registration;
|
||||
|
||||
@Bean
|
||||
public EndpointLocator zipkinEndpointLocator() {
|
||||
@@ -194,8 +195,8 @@ public class ZipkinAutoConfiguration {
|
||||
}
|
||||
|
||||
private DiscoveryClientEndpointLocator discoveryClientEndpointLocator() {
|
||||
if (this.client!=null) {
|
||||
return new DiscoveryClientEndpointLocator(this.client, this.zipkinProperties);
|
||||
if (this.registration != null) {
|
||||
return new DiscoveryClientEndpointLocator(this.registration, this.zipkinProperties);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
@@ -29,7 +29,7 @@ public class DiscoveryClientEndpointLocatorConfigurationTest {
|
||||
@Test
|
||||
public void endpointLocatorShouldDefaultToServerPropertiesEndpointLocatorEvenWhenDiscoveryClientPresent() {
|
||||
ConfigurableApplicationContext ctxt = new SpringApplication(
|
||||
ConfigurationWithDiscoveryClient.class).run("--spring.jmx.enabled=false",
|
||||
ConfigurationWithRegistration.class).run("--spring.jmx.enabled=false",
|
||||
"--spring.cloud.discovery.client.composite-indicator.enabled=false");
|
||||
assertThat(ctxt.getBean(EndpointLocator.class))
|
||||
.isInstanceOf(ServerPropertiesEndpointLocator.class);
|
||||
@@ -49,7 +49,7 @@ public class DiscoveryClientEndpointLocatorConfigurationTest {
|
||||
@Test
|
||||
public void endpointLocatorShouldBeFallbackHavingEndpointLocatorWhenAskedTo() {
|
||||
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.cloud.discovery.client.composite-indicator.enabled=false");
|
||||
assertThat(ctxt.getBean(EndpointLocator.class))
|
||||
@@ -60,7 +60,7 @@ public class DiscoveryClientEndpointLocatorConfigurationTest {
|
||||
@Test
|
||||
public void endpointLocatorShouldRespectExistingEndpointLocatorEvenWhenAskedToBeDiscovery() {
|
||||
ConfigurableApplicationContext ctxt = new SpringApplication(
|
||||
ConfigurationWithDiscoveryClient.class,
|
||||
ConfigurationWithRegistration.class,
|
||||
ConfigurationWithCustomLocator.class).run("--spring.jmx.enabled=false",
|
||||
"--spring.zipkin.locator.discovery.enabled=true",
|
||||
"--spring.cloud.discovery.client.composite-indicator.enabled=false");
|
||||
@@ -76,9 +76,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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,23 +16,19 @@
|
||||
|
||||
package org.springframework.cloud.sleuth.zipkin;
|
||||
|
||||
import zipkin.Endpoint;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.cloud.commons.util.InetUtils;
|
||||
import org.springframework.cloud.sleuth.zipkin.DiscoveryClientEndpointLocator.NoServiceInstanceAvailableException;
|
||||
|
||||
import static org.assertj.core.api.BDDAssertions.then;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
import zipkin.Endpoint;
|
||||
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
@@ -40,24 +36,25 @@ import static org.mockito.BDDMockito.given;
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class DiscoveryClientEndpointLocatorTest {
|
||||
|
||||
@Mock DiscoveryClient discoveryClient;
|
||||
DiscoveryClientEndpointLocator discoveryClientEndpointLocator;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.discoveryClientEndpointLocator = new DiscoveryClientEndpointLocator(this.discoveryClient, new ZipkinProperties());
|
||||
}
|
||||
|
||||
@Test(expected = NoServiceInstanceAvailableException.class)
|
||||
public void should_throw_exception_when_no_instances_are_available() throws Exception {
|
||||
this.discoveryClientEndpointLocator.local();
|
||||
DiscoveryClientEndpointLocator endpointLocator = endpointLocator(null);
|
||||
endpointLocator.local();
|
||||
}
|
||||
|
||||
private DiscoveryClientEndpointLocator endpointLocator(ServiceInstance serviceInstance) {
|
||||
return endpointLocator(serviceInstance, new ZipkinProperties());
|
||||
}
|
||||
|
||||
private DiscoveryClientEndpointLocator endpointLocator(ServiceInstance serviceInstance, ZipkinProperties zipkinProperties) {
|
||||
return new DiscoveryClientEndpointLocator(serviceInstance, zipkinProperties);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_create_endpoint_with_0_ip_when_exception_occurs_on_resolving_host() throws Exception {
|
||||
given(this.discoveryClient.getLocalServiceInstance()).willReturn(serviceInstanceWithInvalidHost());
|
||||
DiscoveryClientEndpointLocator endpointLocator = endpointLocator(serviceInstanceWithInvalidHost());
|
||||
|
||||
Endpoint local = this.discoveryClientEndpointLocator.local();
|
||||
Endpoint local = endpointLocator.local();
|
||||
|
||||
then(local.serviceName).isEqualTo("serviceid");
|
||||
then(local.port).isEqualTo((short)8_000);
|
||||
@@ -66,9 +63,9 @@ public class DiscoveryClientEndpointLocatorTest {
|
||||
|
||||
@Test
|
||||
public void should_create_valid_endpoint_when_proper_host_is_passed() throws Exception {
|
||||
given(this.discoveryClient.getLocalServiceInstance()).willReturn(serviceInstanceWithValidHost());
|
||||
DiscoveryClientEndpointLocator endpointLocator = endpointLocator(serviceInstanceWithValidHost());
|
||||
|
||||
Endpoint local = this.discoveryClientEndpointLocator.local();
|
||||
Endpoint local = endpointLocator.local();
|
||||
|
||||
then(local.serviceName).isEqualTo("serviceid");
|
||||
then(local.port).isEqualTo((short)8_000);
|
||||
@@ -79,8 +76,7 @@ public class DiscoveryClientEndpointLocatorTest {
|
||||
public void should_create_endpoint_with_overridden_name() throws Exception {
|
||||
ZipkinProperties zipkinProperties = new ZipkinProperties();
|
||||
zipkinProperties.getService().setName("foo");
|
||||
DiscoveryClientEndpointLocator locator = new DiscoveryClientEndpointLocator(this.discoveryClient, zipkinProperties);
|
||||
given(this.discoveryClient.getLocalServiceInstance()).willReturn(serviceInstanceWithValidHost());
|
||||
DiscoveryClientEndpointLocator locator = endpointLocator(serviceInstanceWithValidHost(), zipkinProperties);
|
||||
|
||||
Endpoint local = locator.local();
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package org.springframework.cloud.sleuth.zipkin2;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.URI;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
@@ -92,7 +94,37 @@ public class DefaultEndpointLocatorConfigurationTest {
|
||||
@EnableAutoConfiguration
|
||||
public static class ConfigurationWithRegistration {
|
||||
@Bean public Registration getRegistration() {
|
||||
return () -> "from-registration";
|
||||
return new Registration() {
|
||||
@Override
|
||||
public String getServiceId() {
|
||||
return "from-registration";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHost() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPort() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSecure() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getUri() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getMetadata() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user