Commit b9d1d5e6 by Spencer Gibb

Merge pull request #478 from jkschneider/early-eureka-client

* early-eureka-client: make EurekaClient available before smart lifecycle phase 0
parents c39efb96 191a1ccc
...@@ -21,6 +21,9 @@ import java.util.concurrent.ConcurrentMap; ...@@ -21,6 +21,9 @@ import java.util.concurrent.ConcurrentMap;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
...@@ -28,6 +31,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean ...@@ -28,6 +31,7 @@ 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.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.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.event.HeartbeatEvent; import org.springframework.cloud.client.discovery.event.HeartbeatEvent;
import org.springframework.cloud.client.discovery.event.ParentHeartbeatEvent; import org.springframework.cloud.client.discovery.event.ParentHeartbeatEvent;
import org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration; import org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration;
...@@ -37,7 +41,10 @@ import org.springframework.context.ConfigurableApplicationContext; ...@@ -37,7 +41,10 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.EurekaInstanceConfig; import com.netflix.appinfo.EurekaInstanceConfig;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.EurekaClientConfig; import com.netflix.discovery.EurekaClientConfig;
import com.netflix.discovery.converters.XmlXStream; import com.netflix.discovery.converters.XmlXStream;
...@@ -53,6 +60,9 @@ public class EurekaClientAutoConfiguration implements ApplicationListener<Parent ...@@ -53,6 +60,9 @@ public class EurekaClientAutoConfiguration implements ApplicationListener<Parent
private static final ConcurrentMap<String, String> listenerAdded = new ConcurrentHashMap<>(); private static final ConcurrentMap<String, String> listenerAdded = new ConcurrentHashMap<>();
@Autowired
private ApplicationContext context;
@Value("${server.port:${SERVER_PORT:${PORT:8080}}}") @Value("${server.port:${SERVER_PORT:${PORT:8080}}}")
int nonSecurePort; int nonSecurePort;
...@@ -77,6 +87,29 @@ public class EurekaClientAutoConfiguration implements ApplicationListener<Parent ...@@ -77,6 +87,29 @@ public class EurekaClientAutoConfiguration implements ApplicationListener<Parent
return instance; return instance;
} }
@Bean
@ConditionalOnMissingBean(EurekaClient.class)
@SneakyThrows
public EurekaClient eurekaClient() {
return new CloudEurekaClient(applicationInfoManager(), eurekaClientConfigBean(), context);
}
@Bean
@ConditionalOnMissingBean(ApplicationInfoManager.class)
public ApplicationInfoManager applicationInfoManager() {
return new ApplicationInfoManager(eurekaInstanceConfigBean(), instanceInfo());
}
@Bean
@ConditionalOnMissingBean(InstanceInfo.class)
public InstanceInfo instanceInfo() {
return new InstanceInfoFactory().create(eurekaInstanceConfigBean());
}
@Bean
public DiscoveryClient discoveryClient() {
return new EurekaDiscoveryClient();
}
/** /**
* propagate HeartbeatEvent from parent to child. Do it via a * propagate HeartbeatEvent from parent to child. Do it via a
......
...@@ -21,9 +21,6 @@ import java.util.List; ...@@ -21,9 +21,6 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.PreDestroy;
import lombok.SneakyThrows;
import lombok.extern.apachecommons.CommonsLog; import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -36,7 +33,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean ...@@ -36,7 +33,6 @@ 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.context.embedded.EmbeddedServletContainerInitializedEvent; import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent; import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
...@@ -48,7 +44,6 @@ import org.springframework.core.Ordered; ...@@ -48,7 +44,6 @@ import org.springframework.core.Ordered;
import com.netflix.appinfo.ApplicationInfoManager; import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.EurekaInstanceConfig; import com.netflix.appinfo.EurekaInstanceConfig;
import com.netflix.appinfo.HealthCheckHandler; import com.netflix.appinfo.HealthCheckHandler;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.appinfo.InstanceInfo.InstanceStatus; import com.netflix.appinfo.InstanceInfo.InstanceStatus;
import com.netflix.discovery.EurekaClient; import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.EurekaClientConfig; import com.netflix.discovery.EurekaClientConfig;
...@@ -70,9 +65,6 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order ...@@ -70,9 +65,6 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order
private AtomicInteger port = new AtomicInteger(0); private AtomicInteger port = new AtomicInteger(0);
@Autowired @Autowired
private EurekaClientConfig clientConfig;
@Autowired
private EurekaInstanceConfigBean instanceConfig; private EurekaInstanceConfigBean instanceConfig;
@Autowired(required = false) @Autowired(required = false)
...@@ -81,12 +73,19 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order ...@@ -81,12 +73,19 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order
@Autowired @Autowired
private ApplicationContext context; private ApplicationContext context;
@Autowired
private ApplicationInfoManager applicationInfoManager;
@Autowired
private EurekaClient eurekaClient;
@Override @Override
public void start() { public void start() {
// only set the port if the nonSecurePort is 0 and this.port != 0 // only set the port if the nonSecurePort is 0 and this.port != 0
if (this.port.get() != 0 && this.instanceConfig.getNonSecurePort() == 0) { if (this.port.get() != 0 && this.instanceConfig.getNonSecurePort() == 0) {
this.instanceConfig.setNonSecurePort(this.port.get()); this.instanceConfig.setNonSecurePort(this.port.get());
} }
// only initialize if nonSecurePort is greater than 0 and it isn't already running // only initialize if nonSecurePort is greater than 0 and it isn't already running
// because of containerPortInitializer below // because of containerPortInitializer below
if (!this.running.get() && this.instanceConfig.getNonSecurePort() > 0) { if (!this.running.get() && this.instanceConfig.getNonSecurePort() > 0) {
...@@ -95,11 +94,11 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order ...@@ -95,11 +94,11 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order
+ " with eureka with status " + " with eureka with status "
+ this.instanceConfig.getInitialStatus()); + this.instanceConfig.getInitialStatus());
applicationInfoManager().setInstanceStatus( applicationInfoManager.setInstanceStatus(
this.instanceConfig.getInitialStatus()); this.instanceConfig.getInitialStatus());
if (this.healthCheckHandler != null) { if (this.healthCheckHandler != null) {
eurekaClient().registerHealthCheck(this.healthCheckHandler); eurekaClient.registerHealthCheck(this.healthCheckHandler);
} }
this.context.publishEvent(new InstanceRegisteredEvent<>(this, this.context.publishEvent(new InstanceRegisteredEvent<>(this,
this.instanceConfig)); this.instanceConfig));
...@@ -111,8 +110,8 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order ...@@ -111,8 +110,8 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order
public void stop() { public void stop() {
log.info("Unregistering application " + this.instanceConfig.getAppname() log.info("Unregistering application " + this.instanceConfig.getAppname()
+ " with eureka with status DOWN"); + " with eureka with status DOWN");
if (applicationInfoManager().getInfo() != null) { if (applicationInfoManager.getInfo() != null) {
applicationInfoManager().setInstanceStatus( applicationInfoManager.setInstanceStatus(
InstanceStatus.DOWN); InstanceStatus.DOWN);
} }
this.running.set(false); this.running.set(false);
...@@ -145,30 +144,6 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order ...@@ -145,30 +144,6 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order
} }
@Bean @Bean
@ConditionalOnMissingBean(EurekaClient.class)
@SneakyThrows
public EurekaClient eurekaClient() {
return new CloudEurekaClient(applicationInfoManager(), clientConfig, context);
}
@Bean
@ConditionalOnMissingBean(ApplicationInfoManager.class)
public ApplicationInfoManager applicationInfoManager() {
return new ApplicationInfoManager(instanceConfig, instanceInfo());
}
@Bean
@ConditionalOnMissingBean(InstanceInfo.class)
public InstanceInfo instanceInfo() {
return new InstanceInfoFactory().create(instanceConfig);
}
@Bean
public DiscoveryClient discoveryClient() {
return new EurekaDiscoveryClient();
}
@Bean
protected ApplicationListener<EmbeddedServletContainerInitializedEvent> containerPortInitializer() { protected ApplicationListener<EmbeddedServletContainerInitializedEvent> containerPortInitializer() {
return new ApplicationListener<EmbeddedServletContainerInitializedEvent>() { return new ApplicationListener<EmbeddedServletContainerInitializedEvent>() {
...@@ -192,8 +167,7 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order ...@@ -192,8 +167,7 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public EurekaHealthIndicator eurekaHealthIndicator( public EurekaHealthIndicator eurekaHealthIndicator(EurekaClient eurekaClient,
EurekaClient eurekaClient,
EurekaInstanceConfig config) { EurekaInstanceConfig config) {
CompositeMetricReader metrics = new CompositeMetricReader(this.metricReaders.toArray(new MetricReader[0])); CompositeMetricReader metrics = new CompositeMetricReader(this.metricReaders.toArray(new MetricReader[0]));
return new EurekaHealthIndicator(eurekaClient, metrics, config); return new EurekaHealthIndicator(eurekaClient, metrics, config);
......
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