diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/eureka/EurekaClientConfiguration.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/eureka/EurekaClientConfiguration.java index 2f0f850a..48bcc3de 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/eureka/EurekaClientConfiguration.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/eureka/EurekaClientConfiguration.java @@ -59,9 +59,6 @@ public class EurekaClientConfiguration implements SmartLifecycle, Ordered { private int port = 0; - @Autowired - private EurekaClientConfigBean clientConfig; - @Autowired private EurekaInstanceConfigBean instanceConfig; @@ -83,7 +80,7 @@ public class EurekaClientConfiguration implements SmartLifecycle, Ordered { discoveryManagerIntitializer().init(); logger.info("Registering application {} with eureka with status UP", instanceConfig.getAppname()); - ApplicationInfoManager.getInstance().setInstanceStatus(InstanceStatus.UP); + ApplicationInfoManager.getInstance().setInstanceStatus(instanceConfig.getInitialStatus()); running = true; } } diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBean.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBean.java index 040b4f58..113232c7 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBean.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBean.java @@ -32,6 +32,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; import com.netflix.appinfo.DataCenterInfo; import com.netflix.appinfo.EurekaInstanceConfig; +import com.netflix.appinfo.InstanceInfo.InstanceStatus; /** * @author Dave Syer @@ -101,6 +102,8 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig { private String hostname = hostInfo[1]; private boolean preferIpAddress = false; + + private InstanceStatus initialStatus = InstanceStatus.UP; public String getHostname() { return preferIpAddress ? ipAddress : hostname; diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBeanTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBeanTests.java index eba5afef..48369568 100644 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBeanTests.java +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBeanTests.java @@ -17,13 +17,16 @@ package org.springframework.cloud.netflix.eureka; import org.junit.After; import org.junit.Test; +import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Configuration; +import com.netflix.appinfo.InstanceInfo.InstanceStatus; + import static org.junit.Assert.assertEquals; +import static org.springframework.boot.test.EnvironmentTestUtils.*; /** * @author Dave Syer @@ -42,13 +45,9 @@ public class EurekaInstanceConfigBeanTests { @Test public void basicBinding() { - EnvironmentTestUtils.addEnvironment(context, - "eureka.instance.appGroupName=mygroup"); - context.register(PropertyPlaceholderAutoConfiguration.class, - TestConfiguration.class); - context.refresh(); - assertEquals("mygroup", context.getBean(EurekaInstanceConfigBean.class) - .getAppGroupName()); + addEnvironment(context, "eureka.instance.appGroupName=mygroup"); + setupContext(); + assertEquals("mygroup", getInstanceConfig().getAppGroupName()); } @Test @@ -72,12 +71,40 @@ public class EurekaInstanceConfigBeanTests { } private void testNonSecurePort(String propName) { - EnvironmentTestUtils.addEnvironment(context, propName +":8888"); + addEnvironment(context, propName + ":8888"); + setupContext(); + assertEquals(8888, getInstanceConfig().getNonSecurePort()); + } + + @Test + public void testDefaultInitialStatus() { + setupContext(); + assertEquals("initialStatus wrong", InstanceStatus.UP, + getInstanceConfig().getInitialStatus()); + } + + @Test(expected = BeanCreationException.class) + public void testBadInitialStatus() { + addEnvironment(context, "eureka.instance.initial-status:FOO"); + setupContext(); + } + + @Test + public void testCustomInitialStatus() { + addEnvironment(context, "eureka.instance.initial-status:STARTING"); + setupContext(); + assertEquals("initialStatus wrong", InstanceStatus.STARTING, + getInstanceConfig().getInitialStatus()); + } + + private void setupContext() { context.register(PropertyPlaceholderAutoConfiguration.class, TestConfiguration.class); context.refresh(); - assertEquals(8888, - context.getBean(EurekaInstanceConfigBean.class).getNonSecurePort()); + } + + protected EurekaInstanceConfigBean getInstanceConfig() { + return context.getBean(EurekaInstanceConfigBean.class); } /*