Commit 08028db9 by Ryan Baxter Committed by GitHub

Merge pull request #1425 from ryanjbaxter/eureka-app-name-default

Resolve spring.application.name via property resolver
parents 81959b5e 85b2fe37
......@@ -64,6 +64,7 @@ import static org.springframework.cloud.commons.util.IdUtils.getDefaultInstanceI
* @author Spencer Gibb
* @author Jon Schneider
* @author Matt Jenkins
* @author Ryan Baxter
*/
@Configuration
@EnableConfigurationProperties
......@@ -107,9 +108,16 @@ public class EurekaClientAutoConfiguration {
@ConditionalOnMissingBean(value = EurekaInstanceConfig.class, search = SearchStrategy.CURRENT)
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);
instance.setNonSecurePort(this.nonSecurePort);
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 (StringUtils.hasText(this.hostname)) {
instance.setHostname(this.hostname);
......
......@@ -16,31 +16,31 @@
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.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.cloud.commons.util.InetUtils.HostInfo;
import com.netflix.appinfo.DataCenterInfo;
import com.netflix.appinfo.InstanceInfo.InstanceStatus;
import com.netflix.appinfo.MyDataCenterInfo;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
/**
* @author Dave Syer
* @author Spencer Gibb
* @author Ryan Baxter
*/
@Data
@ConfigurationProperties("eureka.instance")
public class EurekaInstanceConfigBean implements CloudEurekaInstanceConfig {
private static final String UNKNOWN = "unknown";
@Getter(AccessLevel.PRIVATE)
@Setter(AccessLevel.PRIVATE)
private HostInfo hostInfo;
......@@ -52,8 +52,7 @@ public class EurekaInstanceConfigBean implements CloudEurekaInstanceConfig {
/**
* Get the name of the application to be registered with eureka.
*/
@Value("${spring.application.name:unknown}")
private String appname = "unknown";
private String appname = UNKNOWN;
/**
* Get the name of the application group to be registered with eureka.
......@@ -119,8 +118,7 @@ public class EurekaInstanceConfigBean implements CloudEurekaInstanceConfig {
* virtual host name.Think of this as similar to the fully qualified domain name, that
* the users of your services will need to find this instance.
*/
@Value("${spring.application.name:unknown}")
private String virtualHostName;
private String virtualHostName = UNKNOWN;
/**
* Get the unique Id (within the scope of the appName) of this instance to be
......@@ -135,8 +133,7 @@ public class EurekaInstanceConfigBean implements CloudEurekaInstanceConfig {
* secure virtual host name.Think of this as similar to the fully qualified domain
* name, that the users of your services will need to find this instance.
*/
@Value("${spring.application.name:unknown}")
private String secureVirtualHostName;
private String secureVirtualHostName = UNKNOWN;
/**
* Gets the AWS autoscaling group name associated with this instance. This information
......
......@@ -189,6 +189,23 @@ public class EurekaClientAutoConfigurationTests {
// Mockito.verify(http).addFilter(Matchers.any(HTTPBasicAuthFilter.class));
}
@Test
public void testDefaultAppName() throws Exception {
setupContext();
assertEquals("unknown", getInstanceConfig().getAppname());
assertEquals("unknown", getInstanceConfig().getVirtualHostName());
assertEquals("unknown", getInstanceConfig().getSecureVirtualHostName());
}
@Test
public void testAppName() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "spring.application.name=mytest");
setupContext();
assertEquals("mytest", getInstanceConfig().getAppname());
assertEquals("mytest", getInstanceConfig().getVirtualHostName());
assertEquals("mytest", getInstanceConfig().getSecureVirtualHostName());
}
private void testNonSecurePort(String propName) {
addEnvironment(this.context, propName + ":8888");
setupContext();
......
......@@ -20,15 +20,18 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.cloud.commons.util.InetUtilsProperties;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.StringUtils;
import com.netflix.appinfo.InstanceInfo.InstanceStatus;
import static org.junit.Assert.assertEquals;
......@@ -38,6 +41,7 @@ import static org.springframework.boot.test.util.EnvironmentTestUtils.addEnviron
/**
* @author Dave Syer
* @author Spencer Gibb
* @author Ryan Baxter
*/
public class EurekaInstanceConfigBeanTests {
......@@ -185,6 +189,14 @@ public class EurekaInstanceConfigBeanTests {
}
@Test
public void testDefaultAppName() throws Exception {
setupContext();
assertEquals("default app name is wrong", "unknown", getInstanceConfig().getAppname());
assertEquals("default virtual hostname is wrong", "unknown", getInstanceConfig().getVirtualHostName());
assertEquals("default secure virtual hostname is wrong", "unknown", getInstanceConfig().getSecureVirtualHostName());
}
private void setupContext() {
this.context.register(PropertyPlaceholderAutoConfiguration.class,
TestConfiguration.class);
......@@ -198,9 +210,19 @@ public class EurekaInstanceConfigBeanTests {
@Configuration
@EnableConfigurationProperties
protected static class TestConfiguration {
@Autowired
ConfigurableEnvironment env;
@Bean
public EurekaInstanceConfigBean eurekaInstanceConfigBean() {
return new EurekaInstanceConfigBean(new InetUtils(new InetUtilsProperties()));
EurekaInstanceConfigBean configBean = new EurekaInstanceConfigBean(new InetUtils(new InetUtilsProperties()));
RelaxedPropertyResolver springPropertyResolver = new RelaxedPropertyResolver(env, "spring.application.");
String springAppName = springPropertyResolver.getProperty("name");
if(StringUtils.hasText(springAppName)) {
configBean.setSecureVirtualHostName(springAppName);
configBean.setVirtualHostName(springAppName);
configBean.setAppname(springAppName);
}
return configBean;
}
}
......
......@@ -16,10 +16,13 @@
package org.springframework.cloud.netflix.sidecar;
import static org.springframework.cloud.commons.util.IdUtils.getDefaultInstanceId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.actuator.HasFeatures;
import org.springframework.cloud.commons.util.InetUtils;
......@@ -32,10 +35,9 @@ import org.springframework.util.StringUtils;
import com.netflix.appinfo.HealthCheckHandler;
import com.netflix.discovery.EurekaClientConfig;
import static org.springframework.cloud.commons.util.IdUtils.getDefaultInstanceId;
/**
* @author Spencer Gibb
* @author Ryan Baxter
*/
@Configuration
@EnableConfigurationProperties
......@@ -73,9 +75,16 @@ public class SidecarConfiguration {
@Bean
public EurekaInstanceConfigBean eurekaInstanceConfigBean() {
EurekaInstanceConfigBean config = new EurekaInstanceConfigBean(inetUtils);
RelaxedPropertyResolver springPropertyResolver = new RelaxedPropertyResolver(env, "spring.application.");
String springAppName = springPropertyResolver.getProperty("name");
int port = this.sidecarProperties.getPort();
config.setNonSecurePort(port);
config.setInstanceId(getDefaultInstanceId(this.env));
if(StringUtils.hasText(springAppName)) {
config.setAppname(springAppName);
config.setVirtualHostName(springAppName);
config.setSecureVirtualHostName(springAppName);
}
if (StringUtils.hasText(this.hostname)) {
config.setHostname(this.hostname);
}
......
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