Reusing environment to get address; fixes #701

This commit is contained in:
Marcin Grzejszczak
2017-09-19 10:59:51 +01:00
parent c9945ad173
commit 867a49bac8
2 changed files with 29 additions and 4 deletions

View File

@@ -25,7 +25,9 @@ import org.springframework.boot.context.embedded.EmbeddedServletContainerInitial
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.cloud.commons.util.InetUtilsProperties;
import org.springframework.cloud.sleuth.Span;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.event.EventListener;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -43,15 +45,17 @@ import org.springframework.util.StringUtils;
* @author Dave Syer
* @since 1.0.0
*/
public class ServerPropertiesHostLocator implements HostLocator {
public class ServerPropertiesHostLocator implements HostLocator, EnvironmentAware {
private static final Log log = LogFactory.getLog(MethodHandles.lookup().lookupClass());
private static final String IP_ADDRESS_PROP_NAME = "spring.cloud.client.ipAddress";
private final ServerProperties serverProperties; // Nullable
private final String appName;
private final InetUtils inetUtils;
private final ZipkinProperties zipkinProperties;
private Integer port; // Lazy assigned
private Environment environment;
@Deprecated
public ServerPropertiesHostLocator(ServerProperties serverProperties, String appName) {
@@ -103,6 +107,9 @@ public class ServerPropertiesHostLocator implements HostLocator {
if (this.serverProperties != null && this.serverProperties.getAddress() != null) {
address = this.serverProperties.getAddress().getHostAddress();
}
else if (this.environment != null) {
address = this.environment.getProperty(IP_ADDRESS_PROP_NAME, String.class);
}
else {
address = this.inetUtils.findFirstNonLoopbackAddress().getHostAddress();
}
@@ -125,4 +132,8 @@ public class ServerPropertiesHostLocator implements HostLocator {
return serviceName;
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
}

View File

@@ -16,8 +16,6 @@
package org.springframework.cloud.sleuth.zipkin;
import zipkin.Endpoint;
import java.lang.invoke.MethodHandles;
import java.nio.ByteBuffer;
@@ -27,8 +25,11 @@ import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.cloud.commons.util.InetUtilsProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.event.EventListener;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import zipkin.Endpoint;
/**
* {@link EndpointLocator} implementation that:
@@ -43,15 +44,18 @@ import org.springframework.util.StringUtils;
* @author Dave Syer
* @since 1.0.0
*/
public class ServerPropertiesEndpointLocator implements EndpointLocator {
public class ServerPropertiesEndpointLocator implements EndpointLocator,
EnvironmentAware {
private static final Log log = LogFactory.getLog(MethodHandles.lookup().lookupClass());
private static final String IP_ADDRESS_PROP_NAME = "spring.cloud.client.ipAddress";
private final ServerProperties serverProperties;
private final String appName;
private final InetUtils inetUtils;
private final ZipkinProperties zipkinProperties;
private Integer port;
private Environment environment;
@Deprecated
public ServerPropertiesEndpointLocator(ServerProperties serverProperties,String appName) {
@@ -108,8 +112,18 @@ public class ServerPropertiesEndpointLocator implements EndpointLocator {
return ByteBuffer.wrap(this.serverProperties.getAddress().getAddress())
.getInt();
}
else if (this.environment != null) {
String ipAddress = this.environment
.getProperty(IP_ADDRESS_PROP_NAME, String.class);
return InetUtils.getIpAddressAsInt(ipAddress);
}
else {
return ByteBuffer.wrap(this.inetUtils.findFirstNonLoopbackAddress().getAddress()).getInt();
}
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
}