Commit 88f79507 by Dave Syer

Fix race condition in server

parent 98d0da97
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
*/ */
package org.springframework.platform.netflix.eureka; package org.springframework.platform.netflix.eureka;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; import javax.annotation.PreDestroy;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -25,8 +24,10 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; ...@@ -25,8 +24,10 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.context.SmartLifecycle;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.Ordered;
import com.netflix.appinfo.ApplicationInfoManager; import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.InstanceInfo.InstanceStatus; import com.netflix.appinfo.InstanceInfo.InstanceStatus;
...@@ -43,10 +44,14 @@ import com.netflix.discovery.EurekaClientConfig; ...@@ -43,10 +44,14 @@ import com.netflix.discovery.EurekaClientConfig;
@ConditionalOnClass(EurekaClientConfig.class) @ConditionalOnClass(EurekaClientConfig.class)
@ConditionalOnExpression("${eureka.client.enabled:true}") @ConditionalOnExpression("${eureka.client.enabled:true}")
public class EurekaClientAutoConfiguration implements public class EurekaClientAutoConfiguration implements
ApplicationListener<ContextRefreshedEvent> { ApplicationListener<ContextRefreshedEvent>, SmartLifecycle, Ordered {
private static final Logger logger = LoggerFactory.getLogger(EurekaClientAutoConfiguration.class); private static final Logger logger = LoggerFactory.getLogger(EurekaClientAutoConfiguration.class);
private boolean running;
private int order = 0;
@Autowired @Autowired
private EurekaClientConfigBean clientConfig; private EurekaClientConfigBean clientConfig;
...@@ -59,15 +64,45 @@ public class EurekaClientAutoConfiguration implements ...@@ -59,15 +64,45 @@ public class EurekaClientAutoConfiguration implements
ApplicationInfoManager.getInstance().setInstanceStatus(InstanceStatus.UP); ApplicationInfoManager.getInstance().setInstanceStatus(InstanceStatus.UP);
} }
@PostConstruct
public void init() {
DiscoveryManager.getInstance().initComponent(instanceConfig, clientConfig);
}
@PreDestroy @PreDestroy
public void close() { public void close() {
logger.info("Removing application {} from eureka", instanceConfig.getAppname()); logger.info("Removing application {} from eureka", instanceConfig.getAppname());
DiscoveryManager.getInstance().shutdownComponent(); 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 ...@@ -25,6 +25,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.SmartLifecycle; import org.springframework.context.SmartLifecycle;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.context.ServletContextAware; import org.springframework.web.context.ServletContextAware;
import com.netflix.blitz4j.LoggingConfiguration; import com.netflix.blitz4j.LoggingConfiguration;
...@@ -42,7 +43,7 @@ import com.netflix.eureka.PeerAwareInstanceRegistry; ...@@ -42,7 +43,7 @@ import com.netflix.eureka.PeerAwareInstanceRegistry;
@ConditionalOnClass(EurekaServerConfig.class) @ConditionalOnClass(EurekaServerConfig.class)
@ConditionalOnExpression("${eureka.server.enabled:true}") @ConditionalOnExpression("${eureka.server.enabled:true}")
public class EurekaServerAutoConfiguration implements ServletContextAware, public class EurekaServerAutoConfiguration implements ServletContextAware,
SmartLifecycle { SmartLifecycle, Ordered {
@Autowired @Autowired
private EurekaServerConfig eurekaServerConfig; private EurekaServerConfig eurekaServerConfig;
...@@ -54,6 +55,8 @@ public class EurekaServerAutoConfiguration implements ServletContextAware, ...@@ -54,6 +55,8 @@ public class EurekaServerAutoConfiguration implements ServletContextAware,
private boolean running; private boolean running;
private int order = 0;
@Override @Override
public void setServletContext(ServletContext servletContext) { public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext; this.servletContext = servletContext;
...@@ -105,4 +108,9 @@ public class EurekaServerAutoConfiguration implements ServletContextAware, ...@@ -105,4 +108,9 @@ public class EurekaServerAutoConfiguration implements ServletContextAware,
callback.run(); callback.run();
} }
@Override
public int getOrder() {
return order;
}
} }
...@@ -17,6 +17,8 @@ package org.springframework.platform.netflix.eureka; ...@@ -17,6 +17,8 @@ package org.springframework.platform.netflix.eureka;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import java.util.Collections;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
...@@ -24,6 +26,8 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties ...@@ -24,6 +26,8 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.MapPropertySource;
/** /**
* @author Dave Syer * @author Dave Syer
...@@ -67,6 +71,24 @@ public class EurekaClientConfigBeanTests { ...@@ -67,6 +71,24 @@ public class EurekaClientConfigBeanTests {
} }
@Test @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() { public void serviceUrlWithDefault() {
EnvironmentTestUtils.addEnvironment(context, EnvironmentTestUtils.addEnvironment(context,
"eureka.client.serviceUrl.defaultZone:", "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