From 0bdb39c9cd3664e9b7068e8060043d7ce78ef811 Mon Sep 17 00:00:00 2001 From: kamnowisz Date: Mon, 22 Jan 2018 19:31:48 +0100 Subject: [PATCH] Optimize default endpoint ip address resolution (#831) --- .../sleuth/zipkin2/DefaultEndpointLocator.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) 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); } } }