org.zodiac.actuate.eureka.EurekaClientHealthIndicator Maven / Gradle / Ivy
package org.zodiac.actuate.eureka;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.springframework.aop.support.AopUtils;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status;
import org.springframework.boot.actuate.health.Health.Builder;
import org.zodiac.actuate.bootstrap.discovery.health.AppDiscoveryHealthIndicator;
import org.zodiac.commons.util.spring.Springs;
import com.netflix.appinfo.EurekaInstanceConfig;
import com.netflix.discovery.DiscoveryClient;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.EurekaClientConfig;
import com.netflix.discovery.shared.Application;
import com.netflix.discovery.shared.Applications;
public class EurekaClientHealthIndicator implements AppDiscoveryHealthIndicator {
private final EurekaClient eurekaClient;
private final EurekaInstanceConfig instanceConfig;
private final EurekaClientConfig clientConfig;
public EurekaClientHealthIndicator(EurekaClient eurekaClient, EurekaInstanceConfig instanceConfig,
EurekaClientConfig clientConfig) {
super();
this.eurekaClient = eurekaClient;
this.instanceConfig = instanceConfig;
this.clientConfig = clientConfig;
}
@Override
public String getName() {
return "EurekaClient";
}
@Override
public Health health() {
Builder builder = Health.unknown();
Status status = getStatus(builder);
return builder.status(status).withDetail("applications", getApplications()).build();
}
private Status getStatus(Builder builder) {
Status status =
new Status(this.eurekaClient.getInstanceRemoteStatus().toString(), "Remote status from Eureka server");
DiscoveryClient discoveryClient = getDiscoveryClient();
if (discoveryClient != null && clientConfig.shouldFetchRegistry()) {
long lastFetch = discoveryClient.getLastSuccessfulRegistryFetchTimePeriod();
if (lastFetch < 0) {
status = new Status("UP",
"Eureka application discovery client has not yet successfully connected to a Eureka server.");
} else if (lastFetch > clientConfig.getRegistryFetchIntervalSeconds() * 2000) {
status = new Status("UP",
"Eureka application discovery client is reporting failures to connect to a Eureka server.");
builder.withDetail("renewalPeriod", instanceConfig.getLeaseRenewalIntervalInSeconds());
builder.withDetail("failCount", lastFetch / clientConfig.getRegistryFetchIntervalSeconds());
}
}
return status;
}
private DiscoveryClient getDiscoveryClient() {
DiscoveryClient discoveryClient = null;
if (AopUtils.isAopProxy(eurekaClient)) {
discoveryClient = Springs.getTargetObject(eurekaClient);
} else if (DiscoveryClient.class.isInstance(eurekaClient)) {
discoveryClient = (DiscoveryClient)eurekaClient;
}
return discoveryClient;
}
private Map getApplications() {
Applications applications = this.eurekaClient.getApplications();
if (applications == null) {
return Collections.emptyMap();
}
Map result = new HashMap<>();
for (Application application : applications.getRegisteredApplications()) {
if (!application.getInstances().isEmpty()) {
result.put(application.getName(), application.getInstances().size());
}
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy