@@ -56,6 +56,12 @@
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>2.1.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.springframework.cloud.sleuth.zipkin;
|
||||
|
||||
import com.twitter.zipkin.gen.Endpoint;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Endpoint locator that will try to call an endpoint via Discovery Client
|
||||
* and will fallback to Server Properties if an exception is thrown
|
||||
*/
|
||||
@Slf4j
|
||||
public class FallbackHavingEndpointLocator implements EndpointLocator {
|
||||
|
||||
private final DiscoveryClientEndpointLocator discoveryClientEndpointLocator;
|
||||
private final ServerPropertiesEndpointLocator serverPropertiesEndpointLocator;
|
||||
|
||||
public FallbackHavingEndpointLocator(DiscoveryClientEndpointLocator discoveryClientEndpointLocator,
|
||||
ServerPropertiesEndpointLocator serverPropertiesEndpointLocator) {
|
||||
this.discoveryClientEndpointLocator = discoveryClientEndpointLocator;
|
||||
this.serverPropertiesEndpointLocator = serverPropertiesEndpointLocator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Endpoint local() {
|
||||
if (discoveryClientEndpointLocator == null) {
|
||||
return serverPropertiesEndpointLocator.local();
|
||||
}
|
||||
try {
|
||||
return discoveryClientEndpointLocator.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 serverPropertiesEndpointLocator.local();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -84,10 +84,15 @@ public class ZipkinAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public EndpointLocator zipkinEndpointLocator() {
|
||||
return new FallbackHavingEndpointLocator(discoveryClientEndpointLocator(),
|
||||
new ServerPropertiesEndpointLocator(this.serverProperties));
|
||||
}
|
||||
|
||||
private DiscoveryClientEndpointLocator discoveryClientEndpointLocator() {
|
||||
if (this.client!=null) {
|
||||
return new DiscoveryClientEndpointLocator(this.client);
|
||||
}
|
||||
return new ServerPropertiesEndpointLocator(this.serverProperties);
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package org.springframework.cloud.sleuth.zipkin;
|
||||
|
||||
import com.twitter.zipkin.gen.Endpoint;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.BDDMockito;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import static org.assertj.core.api.BDDAssertions.then;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class FallbackHavingEndpointLocatorTest {
|
||||
|
||||
@Mock DiscoveryClientEndpointLocator discoveryClientEndpointLocator;
|
||||
@Mock ServerPropertiesEndpointLocator serverPropertiesEndpointLocator;
|
||||
Endpoint expectedEndpoint = new Endpoint();
|
||||
|
||||
@Test
|
||||
public void should_use_system_property_locator_if_discovery_client_locator_is_not_present() {
|
||||
BDDMockito.given(serverPropertiesEndpointLocator.local()).willReturn(expectedEndpoint);
|
||||
FallbackHavingEndpointLocator sut = new FallbackHavingEndpointLocator(null,
|
||||
serverPropertiesEndpointLocator);
|
||||
|
||||
Endpoint endpoint = sut.local();
|
||||
|
||||
then(endpoint).isSameAs(expectedEndpoint);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_use_system_property_locator_if_discovery_client_locator_throws_an_exception() {
|
||||
BDDMockito.given(discoveryClientEndpointLocator.local()).willThrow(new RuntimeException());
|
||||
BDDMockito.given(serverPropertiesEndpointLocator.local()).willReturn(expectedEndpoint);
|
||||
FallbackHavingEndpointLocator sut = new FallbackHavingEndpointLocator(discoveryClientEndpointLocator,
|
||||
serverPropertiesEndpointLocator);
|
||||
|
||||
Endpoint endpoint = sut.local();
|
||||
|
||||
then(endpoint).isSameAs(expectedEndpoint);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_use_discovery_client_locator_by_default() {
|
||||
BDDMockito.given(discoveryClientEndpointLocator.local()).willReturn(expectedEndpoint);
|
||||
BDDMockito.given(serverPropertiesEndpointLocator.local()).willThrow(new RuntimeException());
|
||||
FallbackHavingEndpointLocator sut = new FallbackHavingEndpointLocator(discoveryClientEndpointLocator,
|
||||
serverPropertiesEndpointLocator);
|
||||
|
||||
Endpoint endpoint = sut.local();
|
||||
|
||||
then(endpoint).isSameAs(expectedEndpoint);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user