Commit f904b52a by Dave Syer

Tweak eureka lifecycle so it re-registers after a refresh

This version of Eureka is a bit of a singleton pig, so it's hard to control the lifecycle, but this seems to work for all the tests and also the eureka-first and ribbon-eureka samples. Fixes gh-551 for 1.0.x
parent ac86e779
......@@ -21,8 +21,9 @@ import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor;
import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEvent;
import org.springframework.cloud.netflix.eureka.EurekaDiscoveryClientConfiguration;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import com.netflix.appinfo.ApplicationInfoManager;
......@@ -31,15 +32,15 @@ import com.netflix.discovery.DiscoveryManager;
import com.netflix.discovery.EurekaClientConfig;
/**
* Bootstrap configuration for a config client that wants to lookup the config server via
* discovery.
* Bootstrap configuration for a config client that wants to lookup the config
* server via discovery.
*
* @author Dave Syer
*/
@ConditionalOnBean({ EurekaDiscoveryClientConfiguration.class })
@ConditionalOnProperty(value = "spring.cloud.config.discovery.enabled", matchIfMissing = false)
@Configuration
public class DiscoveryClientConfigServiceAutoConfiguration {
public class DiscoveryClientConfigServiceAutoConfiguration implements ApplicationListener<RefreshScopeRefreshedEvent> {
@Autowired
private EurekaClientConfig clientConfig;
......@@ -48,27 +49,22 @@ public class DiscoveryClientConfigServiceAutoConfiguration {
private EurekaInstanceConfig instanceConfig;
@Autowired
private ConfigurationPropertiesBindingPostProcessor binder;
@Autowired
private EurekaDiscoveryClientConfiguration lifecycle;
@PostConstruct
public void init() {
this.lifecycle.stop();
rebind(this.clientConfig, "eurekaClientConfig");
rebind(this.instanceConfig, "eurekaInstanceConfig");
if (DiscoveryManager.getInstance().getDiscoveryClient() != null) {
DiscoveryManager.getInstance().getDiscoveryClient().shutdown();
}
ApplicationInfoManager.getInstance().initComponent(this.instanceConfig);
DiscoveryManager.getInstance().initComponent(this.instanceConfig,
this.clientConfig);
DiscoveryManager.getInstance().initComponent(this.instanceConfig, this.clientConfig);
this.lifecycle.start();
}
private void rebind(Object bean, String name) {
this.binder.postProcessBeforeInitialization(bean, name);
@Override
public void onApplicationEvent(RefreshScopeRefreshedEvent arg0) {
init();
}
}
......@@ -16,8 +16,6 @@
package org.springframework.cloud.netflix.config;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
......@@ -27,6 +25,7 @@ import org.springframework.cloud.client.discovery.event.HeartbeatMonitor;
import org.springframework.cloud.config.client.ConfigClientProperties;
import org.springframework.cloud.config.client.ConfigServicePropertySourceLocator;
import org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
......@@ -37,6 +36,8 @@ import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.DiscoveryClient;
import com.netflix.discovery.DiscoveryManager;
import lombok.extern.apachecommons.CommonsLog;
/**
* Bootstrap configuration for a config client that wants to lookup the config server via
* discovery.
......@@ -57,9 +58,12 @@ public class DiscoveryClientConfigServiceBootstrapConfiguration implements
@Autowired
private ConfigClientProperties config;
@Autowired
private ApplicationContext context;
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextRefreshedEvent) {
if (event instanceof ContextRefreshedEvent && ((ContextRefreshedEvent) event).getApplicationContext()==context) {
refresh();
}
else if (event instanceof HeartbeatEvent) {
......
......@@ -27,6 +27,7 @@ import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.boot.builder.ParentContextApplicationContextInitializer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.CommonsClientAutoConfiguration;
......@@ -71,13 +72,13 @@ public class EurekaClientAutoConfiguration implements ApplicationListener<Parent
}
@Bean
@ConditionalOnMissingBean(EurekaClientConfig.class)
@ConditionalOnMissingBean(value=EurekaClientConfig.class, search = SearchStrategy.CURRENT)
public EurekaClientConfigBean eurekaClientConfigBean() {
return new EurekaClientConfigBean();
}
@Bean
@ConditionalOnMissingBean(EurekaInstanceConfig.class)
@ConditionalOnMissingBean(value=EurekaInstanceConfig.class, search = SearchStrategy.CURRENT)
public EurekaInstanceConfigBean eurekaInstanceConfigBean() {
EurekaInstanceConfigBean instance = new EurekaInstanceConfigBean();
instance.setNonSecurePort(this.nonSecurePort);
......
......@@ -91,6 +91,7 @@ public class DiscoveryClientConfigServiceAutoConfigurationTests {
this.context.setParent(parent);
this.context.register(DiscoveryClientConfigServiceAutoConfiguration.class);
this.context.refresh();
DiscoveryManager.getInstance().setDiscoveryClient(this.client);
}
@Configuration
......
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