Don't override values with defaults.

Defaults were set too late and were overriding values set in the environment. Specifically, spring.application.name took precedence over eureka.instance.appname. fixes gh-1516
parent b3f1a300
...@@ -19,7 +19,6 @@ package org.springframework.cloud.netflix.eureka; ...@@ -19,7 +19,6 @@ package org.springframework.cloud.netflix.eureka;
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.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;
...@@ -44,7 +43,7 @@ import lombok.Setter; ...@@ -44,7 +43,7 @@ import lombok.Setter;
*/ */
@Data @Data
@ConfigurationProperties("eureka.instance") @ConfigurationProperties("eureka.instance")
public class EurekaInstanceConfigBean implements CloudEurekaInstanceConfig, EnvironmentAware, InitializingBean { public class EurekaInstanceConfigBean implements CloudEurekaInstanceConfig, EnvironmentAware {
private static final String UNKNOWN = "unknown"; private static final String UNKNOWN = "unknown";
...@@ -331,10 +330,7 @@ public class EurekaInstanceConfigBean implements CloudEurekaInstanceConfig, Envi ...@@ -331,10 +330,7 @@ public class EurekaInstanceConfigBean implements CloudEurekaInstanceConfig, Envi
@Override @Override
public void setEnvironment(Environment environment) { public void setEnvironment(Environment environment) {
this.environment = environment; this.environment = environment;
} // set some defaults from the environment, but allow the defaults to use relaxed binding
@Override
public void afterPropertiesSet() throws Exception {
RelaxedPropertyResolver springPropertyResolver = new RelaxedPropertyResolver(this.environment, "spring.application."); RelaxedPropertyResolver springPropertyResolver = new RelaxedPropertyResolver(this.environment, "spring.application.");
String springAppName = springPropertyResolver.getProperty("name"); String springAppName = springPropertyResolver.getProperty("name");
if(StringUtils.hasText(springAppName)) { if(StringUtils.hasText(springAppName)) {
......
...@@ -215,6 +215,14 @@ public class EurekaClientAutoConfigurationTests { ...@@ -215,6 +215,14 @@ public class EurekaClientAutoConfigurationTests {
assertEquals("mytestupper", getInstanceConfig().getSecureVirtualHostName()); assertEquals("mytestupper", getInstanceConfig().getSecureVirtualHostName());
} }
@Test
public void testInstanceNamePreferred() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "SPRING_APPLICATION_NAME=mytestspringappname",
"eureka.instance.appname=mytesteurekaappname");
setupContext();
assertEquals("mytesteurekaappname", getInstanceConfig().getAppname());
}
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