Move initalizing appname, etc.. to afterPropertiesSet

parent 111a95d2
...@@ -111,21 +111,15 @@ public class EurekaClientAutoConfiguration { ...@@ -111,21 +111,15 @@ public class EurekaClientAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean(value = EurekaInstanceConfig.class, search = SearchStrategy.CURRENT) @ConditionalOnMissingBean(value = EurekaInstanceConfig.class, search = SearchStrategy.CURRENT)
public EurekaInstanceConfigBean eurekaInstanceConfigBean(InetUtils inetUtils) { public EurekaInstanceConfigBean eurekaInstanceConfigBean(InetUtils inetUtils) {
RelaxedPropertyResolver relaxedPropertyResolver = new RelaxedPropertyResolver(env, "eureka.instance.");
RelaxedPropertyResolver springPropertyResolver = new RelaxedPropertyResolver(env, "spring.application.");
String springAppName = springPropertyResolver.getProperty("name");
EurekaInstanceConfigBean instance = new EurekaInstanceConfigBean(inetUtils); EurekaInstanceConfigBean instance = new EurekaInstanceConfigBean(inetUtils);
instance.setNonSecurePort(this.nonSecurePort); instance.setNonSecurePort(this.nonSecurePort);
instance.setInstanceId(getDefaultInstanceId(this.env)); instance.setInstanceId(getDefaultInstanceId(this.env));
if(StringUtils.hasText(springAppName)) {
instance.setAppname(springAppName);
instance.setVirtualHostName(springAppName);
instance.setSecureVirtualHostName(springAppName);
}
if (this.managementPort != this.nonSecurePort && this.managementPort != 0) { if (this.managementPort != this.nonSecurePort && this.managementPort != 0) {
if (StringUtils.hasText(this.hostname)) { if (StringUtils.hasText(this.hostname)) {
instance.setHostname(this.hostname); instance.setHostname(this.hostname);
} }
RelaxedPropertyResolver relaxedPropertyResolver = new RelaxedPropertyResolver(env, "eureka.instance.");
String statusPageUrlPath = relaxedPropertyResolver.getProperty("statusPageUrlPath"); String statusPageUrlPath = relaxedPropertyResolver.getProperty("statusPageUrlPath");
String healthCheckUrlPath = relaxedPropertyResolver.getProperty("healthCheckUrlPath"); String healthCheckUrlPath = relaxedPropertyResolver.getProperty("healthCheckUrlPath");
if (StringUtils.hasText(statusPageUrlPath)) { if (StringUtils.hasText(statusPageUrlPath)) {
......
...@@ -16,20 +16,27 @@ ...@@ -16,20 +16,27 @@
package org.springframework.cloud.netflix.eureka; package org.springframework.cloud.netflix.eureka;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.commons.util.InetUtils; import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.cloud.commons.util.InetUtils.HostInfo; import org.springframework.cloud.commons.util.InetUtils.HostInfo;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import com.netflix.appinfo.DataCenterInfo; import com.netflix.appinfo.DataCenterInfo;
import com.netflix.appinfo.InstanceInfo.InstanceStatus; import com.netflix.appinfo.InstanceInfo.InstanceStatus;
import com.netflix.appinfo.MyDataCenterInfo; import com.netflix.appinfo.MyDataCenterInfo;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
/** /**
* @author Dave Syer * @author Dave Syer
* @author Spencer Gibb * @author Spencer Gibb
...@@ -37,7 +44,7 @@ import com.netflix.appinfo.MyDataCenterInfo; ...@@ -37,7 +44,7 @@ import com.netflix.appinfo.MyDataCenterInfo;
*/ */
@Data @Data
@ConfigurationProperties("eureka.instance") @ConfigurationProperties("eureka.instance")
public class EurekaInstanceConfigBean implements CloudEurekaInstanceConfig { public class EurekaInstanceConfigBean implements CloudEurekaInstanceConfig, EnvironmentAware, InitializingBean {
private static final String UNKNOWN = "unknown"; private static final String UNKNOWN = "unknown";
...@@ -272,6 +279,7 @@ public class EurekaInstanceConfigBean implements CloudEurekaInstanceConfig { ...@@ -272,6 +279,7 @@ public class EurekaInstanceConfigBean implements CloudEurekaInstanceConfig {
private InstanceStatus initialStatus = InstanceStatus.UP; private InstanceStatus initialStatus = InstanceStatus.UP;
private String[] defaultAddressResolutionOrder = new String[0]; private String[] defaultAddressResolutionOrder = new String[0];
private Environment environment;
public String getHostname() { public String getHostname() {
return getHostName(false); return getHostName(false);
...@@ -319,4 +327,20 @@ public class EurekaInstanceConfigBean implements CloudEurekaInstanceConfig { ...@@ -319,4 +327,20 @@ public class EurekaInstanceConfigBean implements CloudEurekaInstanceConfig {
} }
return this.preferIpAddress ? this.ipAddress : this.hostname; return this.preferIpAddress ? this.ipAddress : this.hostname;
} }
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
@Override
public void afterPropertiesSet() throws Exception {
RelaxedPropertyResolver springPropertyResolver = new RelaxedPropertyResolver(this.environment, "spring.application.");
String springAppName = springPropertyResolver.getProperty("name");
if(StringUtils.hasText(springAppName)) {
setAppname(springAppName);
setVirtualHostName(springAppName);
setSecureVirtualHostName(springAppName);
}
}
} }
...@@ -206,6 +206,15 @@ public class EurekaClientAutoConfigurationTests { ...@@ -206,6 +206,15 @@ public class EurekaClientAutoConfigurationTests {
assertEquals("mytest", getInstanceConfig().getSecureVirtualHostName()); assertEquals("mytest", getInstanceConfig().getSecureVirtualHostName());
} }
@Test
public void testAppNameUpper() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "SPRING_APPLICATION_NAME=mytestupper");
setupContext();
assertEquals("mytestupper", getInstanceConfig().getAppname());
assertEquals("mytestupper", getInstanceConfig().getVirtualHostName());
assertEquals("mytestupper", getInstanceConfig().getSecureVirtualHostName());
}
private void testNonSecurePort(String propName) { private void testNonSecurePort(String propName) {
addEnvironment(this.context, propName + ":8888"); addEnvironment(this.context, propName + ":8888");
setupContext(); setupContext();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment