|
|
@ -33,21 +33,21 @@ import org.springframework.core.Ordered; |
|
|
|
import static java.util.Collections.emptyList; |
|
|
|
import static java.util.Collections.emptyList; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* A health indicator which indicates whether or not the discovery client has been |
|
|
|
* A health indicator which indicates whether the discovery client has been initialized. |
|
|
|
* initialized. |
|
|
|
|
|
|
|
* |
|
|
|
* |
|
|
|
* @author Tim Ysewyn |
|
|
|
* @author Tim Ysewyn |
|
|
|
* @author Chris Bono |
|
|
|
* @author Chris Bono |
|
|
|
|
|
|
|
* @author Olga Maciaszek-Sharma |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class ReactiveDiscoveryClientHealthIndicator |
|
|
|
public class ReactiveDiscoveryClientHealthIndicator |
|
|
|
implements ReactiveDiscoveryHealthIndicator, Ordered, ApplicationListener<InstanceRegisteredEvent<?>> { |
|
|
|
implements ReactiveDiscoveryHealthIndicator, Ordered, ApplicationListener<InstanceRegisteredEvent<?>> { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final Log LOG = LogFactory.getLog(ReactiveDiscoveryClientHealthIndicator.class); |
|
|
|
|
|
|
|
|
|
|
|
private final ReactiveDiscoveryClient discoveryClient; |
|
|
|
private final ReactiveDiscoveryClient discoveryClient; |
|
|
|
|
|
|
|
|
|
|
|
private final DiscoveryClientHealthIndicatorProperties properties; |
|
|
|
private final DiscoveryClientHealthIndicatorProperties properties; |
|
|
|
|
|
|
|
|
|
|
|
private final Log log = LogFactory.getLog(ReactiveDiscoveryClientHealthIndicator.class); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private AtomicBoolean discoveryInitialized = new AtomicBoolean(false); |
|
|
|
private AtomicBoolean discoveryInitialized = new AtomicBoolean(false); |
|
|
|
|
|
|
|
|
|
|
|
private int order = Ordered.HIGHEST_PRECEDENCE; |
|
|
|
private int order = Ordered.HIGHEST_PRECEDENCE; |
|
|
@ -60,14 +60,14 @@ public class ReactiveDiscoveryClientHealthIndicator |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void onApplicationEvent(InstanceRegisteredEvent<?> event) { |
|
|
|
public void onApplicationEvent(InstanceRegisteredEvent<?> event) { |
|
|
|
if (this.discoveryInitialized.compareAndSet(false, true)) { |
|
|
|
if (discoveryInitialized.compareAndSet(false, true)) { |
|
|
|
this.log.debug("Discovery Client has been initialized"); |
|
|
|
LOG.debug("Discovery Client has been initialized"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public Mono<Health> health() { |
|
|
|
public Mono<Health> health() { |
|
|
|
if (this.discoveryInitialized.get()) { |
|
|
|
if (discoveryInitialized.get()) { |
|
|
|
return doHealthCheck(); |
|
|
|
return doHealthCheck(); |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
else { |
|
|
@ -78,38 +78,39 @@ public class ReactiveDiscoveryClientHealthIndicator |
|
|
|
|
|
|
|
|
|
|
|
private Mono<Health> doHealthCheck() { |
|
|
|
private Mono<Health> doHealthCheck() { |
|
|
|
// @formatter:off
|
|
|
|
// @formatter:off
|
|
|
|
return Mono.just(this.properties.isUseServicesQuery()) |
|
|
|
return Mono.just(properties.isUseServicesQuery()) |
|
|
|
.flatMap(useServices -> useServices ? doHealthCheckWithServices() : doHealthCheckWithProbe()) |
|
|
|
.flatMap(useServices -> useServices ? doHealthCheckWithServices() : doHealthCheckWithProbe()) |
|
|
|
.onErrorResume(exception -> { |
|
|
|
.onErrorResume(exception -> { |
|
|
|
this.log.error("Error", exception); |
|
|
|
if (LOG.isErrorEnabled()) { |
|
|
|
|
|
|
|
LOG.error("Error", exception); |
|
|
|
|
|
|
|
} |
|
|
|
return Mono.just(Health.down().withException(exception).build()); |
|
|
|
return Mono.just(Health.down().withException(exception).build()); |
|
|
|
}); |
|
|
|
}); |
|
|
|
// @formatter:on
|
|
|
|
// @formatter:on
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private Mono<Health> doHealthCheckWithProbe() { |
|
|
|
private Mono<Health> doHealthCheckWithProbe() { |
|
|
|
// @formatter:off
|
|
|
|
return discoveryClient.reactiveProbe().doOnError(exception -> { |
|
|
|
return Mono.justOrEmpty(this.discoveryClient) |
|
|
|
if (LOG.isErrorEnabled()) { |
|
|
|
.flatMap(client -> { |
|
|
|
LOG.error("Probe has failed.", exception); |
|
|
|
client.probe(); |
|
|
|
} |
|
|
|
return Mono.just(client); |
|
|
|
}).then(buildHealthUp(discoveryClient)); |
|
|
|
}) |
|
|
|
} |
|
|
|
.map(client -> { |
|
|
|
|
|
|
|
String description = (this.properties.isIncludeDescription()) ? client.description() : ""; |
|
|
|
private Mono<Health> buildHealthUp(ReactiveDiscoveryClient discoveryClient) { |
|
|
|
return Health.status(new Status("UP", description)).build(); |
|
|
|
String description = (properties.isIncludeDescription()) ? discoveryClient.description() : ""; |
|
|
|
}); |
|
|
|
return Mono.just(Health.status(new Status("UP", description)).build()); |
|
|
|
// @formatter:on
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private Mono<Health> doHealthCheckWithServices() { |
|
|
|
private Mono<Health> doHealthCheckWithServices() { |
|
|
|
// @formatter:off
|
|
|
|
// @formatter:off
|
|
|
|
return Mono.justOrEmpty(this.discoveryClient) |
|
|
|
return Mono.justOrEmpty(discoveryClient) |
|
|
|
.flatMapMany(ReactiveDiscoveryClient::getServices) |
|
|
|
.flatMapMany(ReactiveDiscoveryClient::getServices) |
|
|
|
.collectList() |
|
|
|
.collectList() |
|
|
|
.defaultIfEmpty(emptyList()) |
|
|
|
.defaultIfEmpty(emptyList()) |
|
|
|
.map(services -> { |
|
|
|
.map(services -> { |
|
|
|
String description = (this.properties.isIncludeDescription()) ? |
|
|
|
String description = (properties.isIncludeDescription()) ? |
|
|
|
this.discoveryClient.description() : ""; |
|
|
|
discoveryClient.description() : ""; |
|
|
|
return Health.status(new Status("UP", description)) |
|
|
|
return Health.status(new Status("UP", description)) |
|
|
|
.withDetail("services", services).build(); |
|
|
|
.withDetail("services", services).build(); |
|
|
|
}); |
|
|
|
}); |
|
|
@ -123,7 +124,7 @@ public class ReactiveDiscoveryClientHealthIndicator |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public int getOrder() { |
|
|
|
public int getOrder() { |
|
|
|
return this.order; |
|
|
|
return order; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void setOrder(int order) { |
|
|
|
public void setOrder(int order) { |
|
|
|