Only attempt to get status from discovery client if it is initialized.
Attempt to fix https://github.com/spring-cloud/spring-cloud-netflix/issues/278
This commit is contained in:
@@ -17,19 +17,25 @@
|
||||
package org.springframework.cloud.client.discovery.health;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import lombok.extern.apachecommons.CommonsLog;
|
||||
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.boot.actuate.health.Status;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@CommonsLog
|
||||
public class DiscoveryClientHealthIndicator implements DiscoveryHealthIndicator, Ordered {
|
||||
public class DiscoveryClientHealthIndicator implements DiscoveryHealthIndicator, Ordered,
|
||||
ApplicationListener<InstanceRegisteredEvent> {
|
||||
|
||||
private AtomicBoolean discoveryInitialized = new AtomicBoolean(false);
|
||||
|
||||
private int order = Ordered.HIGHEST_PRECEDENCE;
|
||||
|
||||
@@ -39,17 +45,31 @@ public class DiscoveryClientHealthIndicator implements DiscoveryHealthIndicator,
|
||||
this.discoveryClient = discoveryClient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(InstanceRegisteredEvent event) {
|
||||
if (this.discoveryInitialized.compareAndSet(false, true)) {
|
||||
log.debug("Discovery Client has been initialized");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Health health() {
|
||||
Health.Builder builder = new Health.Builder();
|
||||
try {
|
||||
List<String> services = this.discoveryClient.getServices();
|
||||
builder.status(new Status("UP", this.discoveryClient.description()))
|
||||
.withDetail("services", services);
|
||||
|
||||
if (this.discoveryInitialized.get()) {
|
||||
try {
|
||||
List<String> services = this.discoveryClient.getServices();
|
||||
builder.status(new Status("UP", this.discoveryClient.description()))
|
||||
.withDetail("services", services);
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.error("Error", e);
|
||||
builder.down(e);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.error("Error", e);
|
||||
builder.down(e);
|
||||
else {
|
||||
builder.status(new Status(Status.UNKNOWN.getCode(),
|
||||
"Discovery Client not initialized"));
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ import org.springframework.boot.actuate.health.OrderedHealthAggregator;
|
||||
import org.springframework.boot.actuate.health.Status;
|
||||
import org.springframework.cloud.client.CommonsClientAutoConfiguration;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
@@ -48,6 +49,9 @@ public class DiscoveryCompositeHealthIndicatorTests {
|
||||
@Autowired
|
||||
DiscoveryCompositeHealthIndicator healthIndicator;
|
||||
|
||||
@Autowired
|
||||
DiscoveryClientHealthIndicator clientHealthIndicator;
|
||||
|
||||
@Configuration
|
||||
public static class Config {
|
||||
@Bean
|
||||
@@ -83,12 +87,22 @@ public class DiscoveryCompositeHealthIndicatorTests {
|
||||
public void testHealthIndicator() {
|
||||
assertNotNull("healthIndicator was null", this.healthIndicator);
|
||||
Health health = this.healthIndicator.health();
|
||||
assertNotNull("health was null", health);
|
||||
Status status = health.getStatus();
|
||||
assertNotNull("status was null", status);
|
||||
assertEquals("status code was wrong", "UP", status.getCode());
|
||||
assertHealth(health, Status.UNKNOWN);
|
||||
|
||||
clientHealthIndicator.onApplicationEvent(new InstanceRegisteredEvent(this, null));
|
||||
|
||||
health = this.healthIndicator.health();
|
||||
Status status = assertHealth(health, Status.UP);
|
||||
assertEquals("status desciption was wrong", "TestDiscoveryClient",
|
||||
status.getDescription());
|
||||
}
|
||||
|
||||
protected Status assertHealth(Health health, Status expected) {
|
||||
assertNotNull("health was null", health);
|
||||
Status status = health.getStatus();
|
||||
assertNotNull("status was null", status);
|
||||
assertEquals("status code was wrong", expected.getCode(), status.getCode());
|
||||
return status;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user