From c3f8c2d8e591ae7e9114dd6d74b05bd894b1f918 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Wed, 18 Oct 2017 18:50:17 +0200 Subject: [PATCH] Changed to RelaxedPropertyResolver from Environment --- .../stream/ServerPropertiesHostLocator.java | 17 +++++++++-------- .../zipkin/ServerPropertiesEndpointLocator.java | 15 ++++++++------- .../sleuth/zipkin2/DefaultEndpointLocator.java | 11 ++++++----- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/spring-cloud-sleuth-stream/src/main/java/org/springframework/cloud/sleuth/stream/ServerPropertiesHostLocator.java b/spring-cloud-sleuth-stream/src/main/java/org/springframework/cloud/sleuth/stream/ServerPropertiesHostLocator.java index 19b5b85a7..692506910 100644 --- a/spring-cloud-sleuth-stream/src/main/java/org/springframework/cloud/sleuth/stream/ServerPropertiesHostLocator.java +++ b/spring-cloud-sleuth-stream/src/main/java/org/springframework/cloud/sleuth/stream/ServerPropertiesHostLocator.java @@ -21,6 +21,7 @@ import java.lang.invoke.MethodHandles; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.boot.autoconfigure.web.ServerProperties; +import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent; import org.springframework.cloud.commons.util.InetUtils; import org.springframework.cloud.commons.util.InetUtilsProperties; @@ -55,7 +56,7 @@ public class ServerPropertiesHostLocator implements HostLocator, EnvironmentAwar private final InetUtils inetUtils; private final ZipkinProperties zipkinProperties; private Integer port; // Lazy assigned - private Environment environment; + private RelaxedPropertyResolver resolver; @Deprecated public ServerPropertiesHostLocator(ServerProperties serverProperties, String appName, @@ -74,7 +75,7 @@ public class ServerPropertiesHostLocator implements HostLocator, EnvironmentAwar public ServerPropertiesHostLocator(ServerProperties serverProperties, Environment environment, ZipkinProperties zipkinProperties, InetUtils inetUtils) { this(serverProperties, "", zipkinProperties, inetUtils); - this.environment = environment; + this.resolver = new RelaxedPropertyResolver(environment); } @Override @@ -109,8 +110,8 @@ public class ServerPropertiesHostLocator implements HostLocator, EnvironmentAwar 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 if (this.resolver != null) { + address = this.resolver.getProperty(IP_ADDRESS_PROP_NAME, String.class); } else { address = this.inetUtils.findFirstNonLoopbackAddress().getHostAddress(); @@ -119,14 +120,14 @@ public class ServerPropertiesHostLocator implements HostLocator, EnvironmentAwar } private String getServiceName(Span span) { - String serviceName; + String serviceName = "unknown"; if (StringUtils.hasText(this.zipkinProperties.getService().getName())) { serviceName = this.zipkinProperties.getService().getName(); } else if (span.getProcessId() != null) { serviceName = span.getProcessId(); } - else { - serviceName = this.environment.getProperty("spring.application.name", "unknown"); + else if (this.resolver != null) { + serviceName = this.resolver.getProperty("spring.application.name", "unknown"); } if (log.isDebugEnabled()) { log.debug("Span will contain serviceName [" + serviceName + "]"); @@ -136,6 +137,6 @@ public class ServerPropertiesHostLocator implements HostLocator, EnvironmentAwar @Override public void setEnvironment(Environment environment) { - this.environment = environment; + this.resolver = new RelaxedPropertyResolver(environment); } } diff --git a/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ServerPropertiesEndpointLocator.java b/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ServerPropertiesEndpointLocator.java index c4afda9e4..e89a64099 100644 --- a/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ServerPropertiesEndpointLocator.java +++ b/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ServerPropertiesEndpointLocator.java @@ -22,6 +22,7 @@ import java.nio.ByteBuffer; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.boot.autoconfigure.web.ServerProperties; +import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent; import org.springframework.cloud.commons.util.InetUtils; import org.springframework.cloud.commons.util.InetUtilsProperties; @@ -56,7 +57,7 @@ public class ServerPropertiesEndpointLocator implements EndpointLocator, private final InetUtils inetUtils; private final ZipkinProperties zipkinProperties; private Integer port; - private Environment environment; + private RelaxedPropertyResolver resolver; @Deprecated public ServerPropertiesEndpointLocator(ServerProperties serverProperties, @@ -74,7 +75,7 @@ public class ServerPropertiesEndpointLocator implements EndpointLocator, public ServerPropertiesEndpointLocator(ServerProperties serverProperties, Environment environment, ZipkinProperties zipkinProperties, InetUtils inetUtils) { this(serverProperties, "", zipkinProperties, inetUtils); - this.environment = environment; + this.resolver = new RelaxedPropertyResolver(environment); } @Override @@ -94,8 +95,8 @@ public class ServerPropertiesEndpointLocator implements EndpointLocator, if (StringUtils.hasText(this.zipkinProperties.getService().getName())) { return this.zipkinProperties.getService().getName(); } - if (this.environment != null) { - return this.environment.getProperty("spring.application.name", "unknown"); + if (this.resolver != null) { + return this.resolver.getProperty("spring.application.name", "unknown"); } return "unknown"; } @@ -124,8 +125,8 @@ public class ServerPropertiesEndpointLocator implements EndpointLocator, return ByteBuffer.wrap(this.serverProperties.getAddress().getAddress()) .getInt(); } - else if (this.environment != null) { - String ipAddress = this.environment + else if (this.resolver != null) { + String ipAddress = this.resolver .getProperty(IP_ADDRESS_PROP_NAME, String.class); return InetUtils.getIpAddressAsInt(ipAddress); } @@ -136,6 +137,6 @@ public class ServerPropertiesEndpointLocator implements EndpointLocator, @Override public void setEnvironment(Environment environment) { - this.environment = environment; + this.resolver = new RelaxedPropertyResolver(environment); } } diff --git a/spring-cloud-sleuth-zipkin2/src/main/java/org/springframework/cloud/sleuth/zipkin2/DefaultEndpointLocator.java b/spring-cloud-sleuth-zipkin2/src/main/java/org/springframework/cloud/sleuth/zipkin2/DefaultEndpointLocator.java index c047b60a5..402165ab8 100644 --- a/spring-cloud-sleuth-zipkin2/src/main/java/org/springframework/cloud/sleuth/zipkin2/DefaultEndpointLocator.java +++ b/spring-cloud-sleuth-zipkin2/src/main/java/org/springframework/cloud/sleuth/zipkin2/DefaultEndpointLocator.java @@ -21,6 +21,7 @@ import java.lang.invoke.MethodHandles; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.boot.autoconfigure.web.ServerProperties; +import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent; import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.commons.util.InetUtils; @@ -51,22 +52,22 @@ public class DefaultEndpointLocator implements EndpointLocator { private final Registration registration; private final ServerProperties serverProperties; - private final Environment environment; private final InetUtils inetUtils; private final ZipkinProperties zipkinProperties; + private final RelaxedPropertyResolver resolver; private Integer port; public DefaultEndpointLocator(Registration registration, ServerProperties serverProperties, Environment environment, ZipkinProperties zipkinProperties, InetUtils inetUtils) { this.registration = registration; this.serverProperties = serverProperties; - this.environment = environment; this.zipkinProperties = zipkinProperties; if (inetUtils == null) { this.inetUtils = new InetUtils(new InetUtilsProperties()); } else { this.inetUtils = inetUtils; } + this.resolver = new RelaxedPropertyResolver(environment); } @Override @@ -91,7 +92,7 @@ public class DefaultEndpointLocator implements EndpointLocator { log.warn("error getting service name from registration", e); } } - return this.environment.getProperty("spring.application.name", "unknown"); + return this.resolver.getProperty("spring.application.name", "unknown"); } @EventListener(EmbeddedServletContainerInitializedEvent.class) @@ -118,8 +119,8 @@ public class DefaultEndpointLocator implements EndpointLocator { && builder.parseIp(this.serverProperties.getAddress())) { return builder; } - else if (this.environment != null && this.environment.containsProperty(IP_ADDRESS_PROP_NAME) - && builder.parseIp(this.environment.getProperty(IP_ADDRESS_PROP_NAME, String.class))) { + else if (this.resolver.containsProperty(IP_ADDRESS_PROP_NAME) + && builder.parseIp(this.resolver.getProperty(IP_ADDRESS_PROP_NAME, String.class))) { return builder; } else {