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