Rename DiscoveryClient*Locator to ServiceInstance*Locator.
Removes references to removed DiscoveryClient.getLocalServiceInstance()
This commit is contained in:
@@ -19,33 +19,25 @@ package org.springframework.cloud.sleuth.stream;
|
||||
import java.net.InetAddress;
|
||||
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.cloud.sleuth.Span;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* An {@link HostLocator} that tries to find local service information from a
|
||||
* {@link DiscoveryClient}.
|
||||
* {@link org.springframework.cloud.client.serviceregistry.Registration}.
|
||||
*
|
||||
* You can override the value of service id by {@link ZipkinProperties#setName(String)}
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class DiscoveryClientHostLocator implements HostLocator {
|
||||
public class ServiceInstanceHostLocator implements HostLocator {
|
||||
|
||||
private final ServiceInstance localServiceInstance;
|
||||
private final ZipkinProperties zipkinProperties;
|
||||
|
||||
@Deprecated
|
||||
public DiscoveryClientHostLocator(DiscoveryClient client, ZipkinProperties zipkinProperties) {
|
||||
Assert.notNull(client, "client");
|
||||
this.localServiceInstance = client.getLocalServiceInstance();
|
||||
this.zipkinProperties = zipkinProperties;
|
||||
}
|
||||
|
||||
public DiscoveryClientHostLocator(ServiceInstance localServiceInstance, ZipkinProperties zipkinProperties) {
|
||||
public ServiceInstanceHostLocator(ServiceInstance localServiceInstance, ZipkinProperties zipkinProperties) {
|
||||
Assert.notNull(localServiceInstance, "localServiceInstance");
|
||||
this.localServiceInstance = localServiceInstance;
|
||||
this.zipkinProperties = zipkinProperties;
|
||||
@@ -149,7 +149,7 @@ public class SleuthStreamAutoConfiguration {
|
||||
@Bean
|
||||
public HostLocator zipkinEndpointLocator() {
|
||||
if (this.registration != null) {
|
||||
return new DiscoveryClientHostLocator(this.registration, this.zipkinProperties);
|
||||
return new ServiceInstanceHostLocator(this.registration, this.zipkinProperties);
|
||||
}
|
||||
return new ServerPropertiesHostLocator(this.serverProperties, this.environment, this.zipkinProperties,
|
||||
this.inetUtils);
|
||||
|
||||
@@ -14,7 +14,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
/**
|
||||
* @author Matcin Wielgus
|
||||
*/
|
||||
public class DiscoveryClientEndpointLocatorConfigurationTest {
|
||||
public class ServiceInstanceHostLocatorConfigurationTest {
|
||||
@Test
|
||||
public void endpointLocatorShouldDefaultToServerPropertiesEndpointLocator() {
|
||||
try (ConfigurableApplicationContext ctxt = new SpringApplication(
|
||||
@@ -52,7 +52,7 @@ public class DiscoveryClientEndpointLocatorConfigurationTest {
|
||||
"--spring.zipkin.locator.discovery.enabled=true",
|
||||
"--spring.main.web_environment=false")) {
|
||||
assertThat(ctxt.getBean(HostLocator.class))
|
||||
.isInstanceOf(DiscoveryClientHostLocator.class);
|
||||
.isInstanceOf(ServiceInstanceHostLocator.class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,24 +29,24 @@ import static org.assertj.core.api.BDDAssertions.then;
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
public class DiscoveryClientHostLocatorTest {
|
||||
public class ServiceInstanceHostLocatorTest {
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void should_throw_exception_when_no_registration_is_present() throws Exception {
|
||||
new DiscoveryClientHostLocator((Registration)null, new ZipkinProperties());
|
||||
new ServiceInstanceHostLocator((Registration)null, new ZipkinProperties());
|
||||
}
|
||||
|
||||
private DiscoveryClientHostLocator hostLocator(ServiceInstance serviceInstance) {
|
||||
private ServiceInstanceHostLocator hostLocator(ServiceInstance serviceInstance) {
|
||||
return hostLocator(serviceInstance, new ZipkinProperties());
|
||||
}
|
||||
|
||||
private DiscoveryClientHostLocator hostLocator(ServiceInstance serviceInstance, ZipkinProperties zipkinProperties) {
|
||||
return new DiscoveryClientHostLocator(serviceInstance, zipkinProperties);
|
||||
private ServiceInstanceHostLocator hostLocator(ServiceInstance serviceInstance, ZipkinProperties zipkinProperties) {
|
||||
return new ServiceInstanceHostLocator(serviceInstance, zipkinProperties);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_create_Host_with_0_ip_when_exception_occurs_on_resolving_host() throws Exception {
|
||||
DiscoveryClientHostLocator hostLocator = hostLocator(serviceInstanceWithInvalidHost());
|
||||
ServiceInstanceHostLocator hostLocator = hostLocator(serviceInstanceWithInvalidHost());
|
||||
|
||||
Host host = hostLocator.locate(null);
|
||||
|
||||
@@ -57,7 +57,7 @@ public class DiscoveryClientHostLocatorTest {
|
||||
|
||||
@Test
|
||||
public void should_create_valid_Host_when_proper_host_is_passed() throws Exception {
|
||||
DiscoveryClientHostLocator hostLocator = hostLocator(serviceInstanceWithValidHost());
|
||||
ServiceInstanceHostLocator hostLocator = hostLocator(serviceInstanceWithValidHost());
|
||||
|
||||
Host host = hostLocator.locate(null);
|
||||
|
||||
@@ -70,7 +70,7 @@ public class DiscoveryClientHostLocatorTest {
|
||||
public void should_override_the_service_name_from_properties() throws Exception {
|
||||
ZipkinProperties zipkinProperties = new ZipkinProperties();
|
||||
zipkinProperties.getService().setName("foo");
|
||||
DiscoveryClientHostLocator hostLocator = new DiscoveryClientHostLocator(serviceInstanceWithValidHost(), zipkinProperties);
|
||||
ServiceInstanceHostLocator hostLocator = new ServiceInstanceHostLocator(serviceInstanceWithValidHost(), zipkinProperties);
|
||||
|
||||
Host host = hostLocator.locate(null);
|
||||
|
||||
@@ -14,12 +14,12 @@ public class FallbackHavingEndpointLocator implements EndpointLocator {
|
||||
|
||||
private static final Log log = LogFactory.getLog(FallbackHavingEndpointLocator.class);
|
||||
|
||||
private final DiscoveryClientEndpointLocator discoveryClientEndpointLocator;
|
||||
private final ServiceInstanceEndpointLocator serviceInstanceEndpointLocator;
|
||||
private final ServerPropertiesEndpointLocator serverPropertiesEndpointLocator;
|
||||
|
||||
public FallbackHavingEndpointLocator(DiscoveryClientEndpointLocator discoveryClientEndpointLocator,
|
||||
public FallbackHavingEndpointLocator(ServiceInstanceEndpointLocator serviceInstanceEndpointLocator,
|
||||
ServerPropertiesEndpointLocator serverPropertiesEndpointLocator) {
|
||||
this.discoveryClientEndpointLocator = discoveryClientEndpointLocator;
|
||||
this.serviceInstanceEndpointLocator = serviceInstanceEndpointLocator;
|
||||
this.serverPropertiesEndpointLocator = serverPropertiesEndpointLocator;
|
||||
}
|
||||
|
||||
@@ -29,11 +29,11 @@ public class FallbackHavingEndpointLocator implements EndpointLocator {
|
||||
}
|
||||
|
||||
private Endpoint endpoint() {
|
||||
if (this.discoveryClientEndpointLocator == null) {
|
||||
if (this.serviceInstanceEndpointLocator == null) {
|
||||
return this.serverPropertiesEndpointLocator.local();
|
||||
}
|
||||
try {
|
||||
return this.discoveryClientEndpointLocator.local();
|
||||
return this.serviceInstanceEndpointLocator.local();
|
||||
} catch (Exception e) {
|
||||
log.warn("Exception occurred while trying to fetch the Zipkin process endpoint. Falling back to server properties endpoint locator.", e);
|
||||
return this.serverPropertiesEndpointLocator.local();
|
||||
|
||||
@@ -21,7 +21,6 @@ import java.lang.invoke.MethodHandles;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.cloud.commons.util.InetUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -29,47 +28,40 @@ import zipkin.Endpoint;
|
||||
|
||||
/**
|
||||
* An {@link EndpointLocator} that tries to find local service information from a
|
||||
* {@link DiscoveryClient}.
|
||||
* {@link org.springframework.cloud.client.serviceregistry.Registration}.
|
||||
*
|
||||
* You can override the name using {@link ZipkinProperties.Service#setName(String)}
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class DiscoveryClientEndpointLocator implements EndpointLocator {
|
||||
public class ServiceInstanceEndpointLocator implements EndpointLocator {
|
||||
|
||||
private static final Log log = LogFactory.getLog(MethodHandles.lookup().lookupClass());
|
||||
|
||||
private final ServiceInstance serviceInstance;
|
||||
private final ServiceInstance localServiceInstance;
|
||||
private final ZipkinProperties zipkinProperties;
|
||||
|
||||
@Deprecated
|
||||
public DiscoveryClientEndpointLocator(DiscoveryClient client,
|
||||
ZipkinProperties zipkinProperties) {
|
||||
this.serviceInstance = client.getLocalServiceInstance();
|
||||
this.zipkinProperties = zipkinProperties;
|
||||
}
|
||||
|
||||
public DiscoveryClientEndpointLocator(ServiceInstance serviceInstance,
|
||||
public ServiceInstanceEndpointLocator(ServiceInstance localServiceInstance,
|
||||
ZipkinProperties zipkinProperties) {
|
||||
this.serviceInstance = serviceInstance;
|
||||
this.localServiceInstance = localServiceInstance;
|
||||
this.zipkinProperties = zipkinProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Endpoint local() {
|
||||
if (this.serviceInstance == null) {
|
||||
if (this.localServiceInstance == null) {
|
||||
throw new NoServiceInstanceAvailableException();
|
||||
}
|
||||
String serviceName = StringUtils.hasText(this.zipkinProperties.getService().getName()) ?
|
||||
this.zipkinProperties.getService().getName() : this.serviceInstance.getServiceId();
|
||||
this.zipkinProperties.getService().getName() : this.localServiceInstance.getServiceId();
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Span will contain serviceName [" + serviceName + "]");
|
||||
}
|
||||
return Endpoint.builder()
|
||||
.serviceName(serviceName)
|
||||
.ipv4(getIpAddress(this.serviceInstance))
|
||||
.port(this.serviceInstance.getPort()).build();
|
||||
.ipv4(getIpAddress(this.localServiceInstance))
|
||||
.port(this.localServiceInstance.getPort()).build();
|
||||
}
|
||||
|
||||
private int getIpAddress(ServiceInstance instance) {
|
||||
@@ -194,9 +194,9 @@ public class ZipkinAutoConfiguration {
|
||||
this.zipkinProperties));
|
||||
}
|
||||
|
||||
private DiscoveryClientEndpointLocator discoveryClientEndpointLocator() {
|
||||
private ServiceInstanceEndpointLocator discoveryClientEndpointLocator() {
|
||||
if (this.registration != null) {
|
||||
return new DiscoveryClientEndpointLocator(this.registration, this.zipkinProperties);
|
||||
return new ServiceInstanceEndpointLocator(this.registration, this.zipkinProperties);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,8 @@ import static org.mockito.BDDMockito.given;
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class FallbackHavingEndpointLocatorTests {
|
||||
|
||||
@Mock DiscoveryClientEndpointLocator discoveryClientEndpointLocator;
|
||||
@Mock
|
||||
ServiceInstanceEndpointLocator serviceInstanceEndpointLocator;
|
||||
@Mock ServerPropertiesEndpointLocator serverPropertiesEndpointLocator;
|
||||
Endpoint expectedEndpoint = Endpoint.builder()
|
||||
.serviceName("my-tomcat").ipv4(127 << 24 | 1).port(8080).build();
|
||||
@@ -31,9 +32,9 @@ public class FallbackHavingEndpointLocatorTests {
|
||||
|
||||
@Test
|
||||
public void should_use_system_property_locator_if_discovery_client_locator_throws_an_exception() {
|
||||
given(this.discoveryClientEndpointLocator.local()).willThrow(new RuntimeException());
|
||||
given(this.serviceInstanceEndpointLocator.local()).willThrow(new RuntimeException());
|
||||
given(this.serverPropertiesEndpointLocator.local()).willReturn(this.expectedEndpoint);
|
||||
FallbackHavingEndpointLocator sut = new FallbackHavingEndpointLocator(this.discoveryClientEndpointLocator,
|
||||
FallbackHavingEndpointLocator sut = new FallbackHavingEndpointLocator(this.serviceInstanceEndpointLocator,
|
||||
this.serverPropertiesEndpointLocator);
|
||||
|
||||
Endpoint endpoint = sut.local();
|
||||
@@ -43,8 +44,8 @@ public class FallbackHavingEndpointLocatorTests {
|
||||
|
||||
@Test
|
||||
public void should_use_discovery_client_locator_by_default() {
|
||||
given(this.discoveryClientEndpointLocator.local()).willReturn(this.expectedEndpoint);
|
||||
FallbackHavingEndpointLocator sut = new FallbackHavingEndpointLocator(this.discoveryClientEndpointLocator,
|
||||
given(this.serviceInstanceEndpointLocator.local()).willReturn(this.expectedEndpoint);
|
||||
FallbackHavingEndpointLocator sut = new FallbackHavingEndpointLocator(this.serviceInstanceEndpointLocator,
|
||||
this.serverPropertiesEndpointLocator);
|
||||
|
||||
Endpoint endpoint = sut.local();
|
||||
|
||||
@@ -14,7 +14,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
/**
|
||||
* @author Marcin Wielgus
|
||||
*/
|
||||
public class DiscoveryClientEndpointLocatorConfigurationTest {
|
||||
public class ServiceInstanceEndpointLocatorConfigurationTest {
|
||||
|
||||
@Test
|
||||
public void endpointLocatorShouldDefaultToServerPropertiesEndpointLocator() {
|
||||
@@ -24,7 +24,7 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.commons.util.InetUtils;
|
||||
import org.springframework.cloud.sleuth.zipkin.DiscoveryClientEndpointLocator.NoServiceInstanceAvailableException;
|
||||
import org.springframework.cloud.sleuth.zipkin.ServiceInstanceEndpointLocator.NoServiceInstanceAvailableException;
|
||||
|
||||
import static org.assertj.core.api.BDDAssertions.then;
|
||||
|
||||
@@ -34,25 +34,25 @@ import zipkin.Endpoint;
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class DiscoveryClientEndpointLocatorTest {
|
||||
public class ServiceInstanceEndpointLocatorTest {
|
||||
|
||||
@Test(expected = NoServiceInstanceAvailableException.class)
|
||||
public void should_throw_exception_when_no_instances_are_available() throws Exception {
|
||||
DiscoveryClientEndpointLocator endpointLocator = endpointLocator(null);
|
||||
ServiceInstanceEndpointLocator endpointLocator = endpointLocator(null);
|
||||
endpointLocator.local();
|
||||
}
|
||||
|
||||
private DiscoveryClientEndpointLocator endpointLocator(ServiceInstance serviceInstance) {
|
||||
private ServiceInstanceEndpointLocator endpointLocator(ServiceInstance serviceInstance) {
|
||||
return endpointLocator(serviceInstance, new ZipkinProperties());
|
||||
}
|
||||
|
||||
private DiscoveryClientEndpointLocator endpointLocator(ServiceInstance serviceInstance, ZipkinProperties zipkinProperties) {
|
||||
return new DiscoveryClientEndpointLocator(serviceInstance, zipkinProperties);
|
||||
private ServiceInstanceEndpointLocator endpointLocator(ServiceInstance serviceInstance, ZipkinProperties zipkinProperties) {
|
||||
return new ServiceInstanceEndpointLocator(serviceInstance, zipkinProperties);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_create_endpoint_with_0_ip_when_exception_occurs_on_resolving_host() throws Exception {
|
||||
DiscoveryClientEndpointLocator endpointLocator = endpointLocator(serviceInstanceWithInvalidHost());
|
||||
ServiceInstanceEndpointLocator endpointLocator = endpointLocator(serviceInstanceWithInvalidHost());
|
||||
|
||||
Endpoint local = endpointLocator.local();
|
||||
|
||||
@@ -63,7 +63,7 @@ public class DiscoveryClientEndpointLocatorTest {
|
||||
|
||||
@Test
|
||||
public void should_create_valid_endpoint_when_proper_host_is_passed() throws Exception {
|
||||
DiscoveryClientEndpointLocator endpointLocator = endpointLocator(serviceInstanceWithValidHost());
|
||||
ServiceInstanceEndpointLocator endpointLocator = endpointLocator(serviceInstanceWithValidHost());
|
||||
|
||||
Endpoint local = endpointLocator.local();
|
||||
|
||||
@@ -76,7 +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 = endpointLocator(serviceInstanceWithValidHost(), zipkinProperties);
|
||||
ServiceInstanceEndpointLocator locator = endpointLocator(serviceInstanceWithValidHost(), zipkinProperties);
|
||||
|
||||
Endpoint local = locator.local();
|
||||
|
||||
@@ -69,11 +69,6 @@ class ZipkinDiscoveryClient implements DiscoveryClient {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServiceInstance getLocalServiceInstance() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ServiceInstance> getInstances(String s) {
|
||||
if ("zipkin".equals(s)) {
|
||||
|
||||
@@ -66,11 +66,6 @@ class ZipkinDiscoveryClient implements DiscoveryClient {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServiceInstance getLocalServiceInstance() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ServiceInstance> getInstances(String s) {
|
||||
if ("zipkin".equals(s)) {
|
||||
|
||||
Reference in New Issue
Block a user