Commit 88f79507 by Dave Syer

Fix race condition in server

parent 98d0da97
......@@ -15,7 +15,6 @@
*/
package org.springframework.platform.netflix.eureka;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.slf4j.Logger;
......@@ -25,8 +24,10 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationListener;
import org.springframework.context.SmartLifecycle;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.Ordered;
import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.InstanceInfo.InstanceStatus;
......@@ -43,10 +44,14 @@ import com.netflix.discovery.EurekaClientConfig;
@ConditionalOnClass(EurekaClientConfig.class)
@ConditionalOnExpression("${eureka.client.enabled:true}")
public class EurekaClientAutoConfiguration implements
ApplicationListener<ContextRefreshedEvent> {
ApplicationListener<ContextRefreshedEvent>, SmartLifecycle, Ordered {
private static final Logger logger = LoggerFactory.getLogger(EurekaClientAutoConfiguration.class);
private boolean running;
private int order = 0;
@Autowired
private EurekaClientConfigBean clientConfig;
......@@ -55,19 +60,49 @@ public class EurekaClientAutoConfiguration implements
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
logger.info("Registering application {} with eureka with status UP", instanceConfig.getAppname());
logger.info("Registering application {} with eureka with status UP", instanceConfig.getAppname());
ApplicationInfoManager.getInstance().setInstanceStatus(InstanceStatus.UP);
}
@PostConstruct
public void init() {
DiscoveryManager.getInstance().initComponent(instanceConfig, clientConfig);
}
@PreDestroy
public void close() {
logger.info("Removing application {} from eureka", instanceConfig.getAppname());
DiscoveryManager.getInstance().shutdownComponent();
}
@Override
public void start() {
DiscoveryManager.getInstance().initComponent(instanceConfig, clientConfig);
}
@Override
public void stop() {
running = false;
}
@Override
public boolean isRunning() {
return running;
}
@Override
public int getPhase() {
return 0;
}
@Override
public boolean isAutoStartup() {
return true;
}
@Override
public void stop(Runnable callback) {
callback.run();
}
@Override
public int getOrder() {
return order;
}
}
......@@ -25,6 +25,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.ApplicationContext;
import org.springframework.context.SmartLifecycle;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.context.ServletContextAware;
import com.netflix.blitz4j.LoggingConfiguration;
......@@ -42,7 +43,7 @@ import com.netflix.eureka.PeerAwareInstanceRegistry;
@ConditionalOnClass(EurekaServerConfig.class)
@ConditionalOnExpression("${eureka.server.enabled:true}")
public class EurekaServerAutoConfiguration implements ServletContextAware,
SmartLifecycle {
SmartLifecycle, Ordered {
@Autowired
private EurekaServerConfig eurekaServerConfig;
......@@ -54,6 +55,8 @@ public class EurekaServerAutoConfiguration implements ServletContextAware,
private boolean running;
private int order = 0;
@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
......@@ -105,4 +108,9 @@ public class EurekaServerAutoConfiguration implements ServletContextAware,
callback.run();
}
@Override
public int getOrder() {
return order;
}
}
......@@ -17,6 +17,8 @@ package org.springframework.platform.netflix.eureka;
import static org.junit.Assert.assertEquals;
import java.util.Collections;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
......@@ -24,6 +26,8 @@ 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 org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.MapPropertySource;
/**
* @author Dave Syer
......@@ -67,6 +71,24 @@ public class EurekaClientConfigBeanTests {
}
@Test
public void serviceUrlWithCompositePropertySource() {
CompositePropertySource source = new CompositePropertySource("composite");
context.getEnvironment().getPropertySources().addFirst(source);
source.addPropertySource(new MapPropertySource("config", Collections
.<String, Object> singletonMap("eureka.client.serviceUrl.defaultZone",
"http://example.com")));
context.register(PropertyPlaceholderAutoConfiguration.class,
TestConfiguration.class);
context.refresh();
assertEquals("{defaultZone=http://example.com}",
context.getBean(EurekaClientConfigBean.class).getServiceUrl().toString());
assertEquals(
"[http://example.com]",
context.getBean(EurekaClientConfigBean.class)
.getEurekaServerServiceUrls("defaultZone").toString());
}
@Test
public void serviceUrlWithDefault() {
EnvironmentTestUtils.addEnvironment(context,
"eureka.client.serviceUrl.defaultZone:",
......
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