Commit eb8a9020 by Dave Syer

Ensure eureka client re-registers after config client activity

Reverts part of a change made for gh-349 and rationalizes a bit of the Eureka configuration. The Eureka client cannot be shutdown without attempting to restart it (but there isn't an obvious public API for that), so it's better not to shut it down at all. See gh-421
parent 61294659
......@@ -18,7 +18,6 @@ package org.springframework.cloud.netflix.config;
import javax.annotation.PostConstruct;
import com.netflix.discovery.EurekaClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
......@@ -52,18 +51,16 @@ public class DiscoveryClientConfigServiceAutoConfiguration {
@Autowired
private EurekaDiscoveryClientConfiguration lifecycle;
@Autowired
private EurekaClient eurekaClient;
@PostConstruct
public void init() {
this.lifecycle.stop();
rebind(this.clientConfig, "eurekaClientConfig");
rebind(this.instanceConfig, "eurekaInstanceConfig");
eurekaClient.shutdown();;
// Danger, here be dragons (once it shuts down it's hard to resurrect it)
// eurekaClient.shutdown();
// FIXME: reinit EurekaClient and ApplicationInfoManager
//applicationInfoManager.initComponent(this.instanceConfig);
//discoveryManager.initComponent(this.instanceConfig, this.clientConfig);
// applicationInfoManager.initComponent(this.instanceConfig);
// discoveryManager.initComponent(this.instanceConfig, this.clientConfig);
this.lifecycle.start();
}
......
......@@ -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;
......@@ -36,6 +34,8 @@ import org.springframework.context.event.SmartApplicationListener;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import lombok.extern.apachecommons.CommonsLog;
/**
* Bootstrap configuration for a config client that wants to lookup the config server via
* discovery.
......@@ -89,8 +89,8 @@ public class DiscoveryClientConfigServiceBootstrapConfiguration implements
private void refresh() {
try {
log.info("Locating configserver via discovery");
InstanceInfo server = eurekaClient
log.debug("Locating configserver via discovery");
InstanceInfo server = this.eurekaClient
.getNextServerFromEureka(this.config.getDiscovery().getServiceId(),
false);
String url = server.getHomePageUrl();
......
......@@ -21,8 +21,6 @@ import java.util.concurrent.ConcurrentMap;
import javax.annotation.PostConstruct;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
......@@ -50,6 +48,8 @@ import com.netflix.discovery.EurekaClientConfig;
import com.netflix.discovery.converters.JsonXStream;
import com.netflix.discovery.converters.XmlXStream;
import lombok.SneakyThrows;
/**
* @author Dave Syer
*/
......@@ -94,25 +94,25 @@ public class EurekaClientAutoConfiguration implements ApplicationListener<Parent
@Bean
@ConditionalOnMissingBean(EurekaClient.class)
@SneakyThrows
public EurekaClient eurekaClient() {
return new CloudEurekaClient(applicationInfoManager(), eurekaClientConfigBean(), this.context);
public EurekaClient eurekaClient(ApplicationInfoManager applicationInfoManager, EurekaClientConfig config) {
return new CloudEurekaClient(applicationInfoManager, config, this.context);
}
@Bean
@ConditionalOnMissingBean(ApplicationInfoManager.class)
public ApplicationInfoManager applicationInfoManager() {
return new ApplicationInfoManager(eurekaInstanceConfigBean(), instanceInfo());
public ApplicationInfoManager applicationInfoManager(EurekaInstanceConfig config, InstanceInfo instanceInfo) {
return new ApplicationInfoManager(config, instanceInfo);
}
@Bean
@ConditionalOnMissingBean(InstanceInfo.class)
public InstanceInfo instanceInfo() {
return new InstanceInfoFactory().create(eurekaInstanceConfigBean());
public InstanceInfo instanceInfo(EurekaInstanceConfig config) {
return new InstanceInfoFactory().create(config);
}
@Bean
public DiscoveryClient discoveryClient() {
return new EurekaDiscoveryClient();
public DiscoveryClient discoveryClient(EurekaInstanceConfig config, EurekaClient client) {
return new EurekaDiscoveryClient(config, client);
}
/**
......
......@@ -24,28 +24,29 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import com.netflix.appinfo.EurekaInstanceConfig;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.shared.Application;
import com.netflix.discovery.shared.Applications;
import lombok.RequiredArgsConstructor;
/**
* @author Spencer Gibb
*/
@RequiredArgsConstructor
public class EurekaDiscoveryClient implements DiscoveryClient {
public static final String DESCRIPTION = "Spring Cloud Eureka Discovery Client";
@Autowired
private EurekaInstanceConfigBean config;
private final EurekaInstanceConfig config;
@Autowired
private EurekaClient eurekaClient;
private final EurekaClient eurekaClient;
@Override
public String description() {
......@@ -62,7 +63,7 @@ public class EurekaDiscoveryClient implements DiscoveryClient {
@Override
public String getHost() {
return EurekaDiscoveryClient.this.config.getHostname();
return EurekaDiscoveryClient.this.config.getHostName(false);
}
@Override
......
......@@ -69,7 +69,7 @@ public class DiscoveryClientConfigServiceAutoConfigurationTests {
this.context
.getBeanNamesForType(DiscoveryClientConfigServiceAutoConfiguration.class).length);
Mockito.verify(this.client, times(2)).getNextServerFromEureka("CONFIGSERVER", false);
Mockito.verify(this.client).shutdown();
Mockito.verify(this.client, times(0)).shutdown();
ConfigClientProperties locator = this.context
.getBean(ConfigClientProperties.class);
assertEquals("http://foo:7001/", locator.getRawUri());
......
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