Changed to RelaxedPropertyResolver from Environment

This commit is contained in:
Marcin Grzejszczak
2017-10-18 18:50:17 +02:00
parent d02c62bb44
commit c3f8c2d8e5
3 changed files with 23 additions and 20 deletions

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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 {