HealthIndicator thêm custom health check vào /actuator/health — quan trọng cho Kubernetes liveness/readiness probe.
java
@Component("externalApi")
class ExternalApiHealthIndicator implements HealthIndicator {
private final RestClient restClient;
@Override public Health health() {
try {
var resp = restClient.get().uri("https://api.external.com/health")
.retrieve().toEntity(String.class);
return resp.getStatusCode().is2xxSuccessful()
? Health.up().withDetail("status", "reachable").build()
: Health.down().withDetail("statusCode", resp.getStatusCode()).build();
} catch (Exception e) {
return Health.down(e).withDetail("message", "External API unreachable").build();
}
}
}Kết quả: /actuator/health → {"status":"DOWN","components":{"externalApi":{"status":"DOWN"}}}.
Liveness vs Readiness (K8s):
yaml
management.endpoint.health.group:
liveness.include: livenessState
readiness.include: readinessState, db, externalApi/actuator/health/readiness = DOWN → K8s dừng route traffic đến pod.