diff --git a/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin2/DefaultEndpointLocator.java b/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin2/DefaultEndpointLocator.java index 402165ab8..815237abe 100644 --- a/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin2/DefaultEndpointLocator.java +++ b/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin2/DefaultEndpointLocator.java @@ -17,6 +17,7 @@ package org.springframework.cloud.sleuth.zipkin2; import java.lang.invoke.MethodHandles; +import java.net.InetAddress; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -52,24 +53,27 @@ public class DefaultEndpointLocator implements EndpointLocator { private final Registration registration; private final ServerProperties serverProperties; - private final InetUtils inetUtils; private final ZipkinProperties zipkinProperties; private final RelaxedPropertyResolver resolver; private Integer port; + private InetAddress firstNonLoopbackAddress; public DefaultEndpointLocator(Registration registration, ServerProperties serverProperties, Environment environment, ZipkinProperties zipkinProperties, InetUtils inetUtils) { this.registration = registration; this.serverProperties = serverProperties; this.zipkinProperties = zipkinProperties; - if (inetUtils == null) { - this.inetUtils = new InetUtils(new InetUtilsProperties()); - } else { - this.inetUtils = inetUtils; - } + this.firstNonLoopbackAddress = findFirstNonLoopbackAddress(inetUtils); this.resolver = new RelaxedPropertyResolver(environment); } + private InetAddress findFirstNonLoopbackAddress(InetUtils inetUtils) { + if (inetUtils == null) { + inetUtils = new InetUtils(new InetUtilsProperties()); + } + return inetUtils.findFirstNonLoopbackAddress(); + } + @Override public Endpoint local() { String serviceName = getLocalServiceName(); @@ -124,7 +128,7 @@ public class DefaultEndpointLocator implements EndpointLocator { return builder; } else { - return builder.ip(this.inetUtils.findFirstNonLoopbackAddress()); + return builder.ip(this.firstNonLoopbackAddress); } } }