I'm exploring options to activate readiness and liveness probes for SpringBoot 1.X. It's essential to have both endpoints enabled for seamless Kubernetes deployments. Any insights on achieving this?
/actuator/health/liveness
/actuator/health/readiness
Is it feasible to obtain those features in the upcoming Spring version, or should I proceed with implementing them myself?
Thanks in advance.
if someone needs the same, that's the final code I came up:
For the Kubernetes Health Indicator:
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.CompositeHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthAggregator;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import java.util.Locale;
import java.util.Map;
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class KubernetesHealthIndicator implements HealthIndicator {
private final HealthAggregator healthAggregator;
private final Map<String, HealthIndicator> healthIndicators;
@Override
public Health health() {
Assert.notNull(healthAggregator, "HealthAggregator must not be null");
Assert.notNull(healthIndicators, "HealthIndicators must not be null");
CompositeHealthIndicator healthIndicator = new CompositeHealthIndicator(healthAggregator);
for (Map.Entry<String, HealthIndicator> entry : healthIndicators.entrySet()) {
healthIndicator.addHealthIndicator(getKey(entry.getKey()), entry.getValue());
}
return healthIndicator.health();
}
private String getKey(String name) {
int index = name.toLowerCase(Locale.ENGLISH).indexOf("healthindicator");
if (index > 0) {
return name.substring(0, index);
}
return name;
}
}
For the KubernetesHeathCheckController:
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping(value = "/actuator/health", produces = {MediaType.APPLICATION_JSON_VALUE})
public class KubernetesHealthCheckController {
private final KubernetesHealthIndicator kubernetesHealthIndicator;
@GetMapping("/liveness")
public Health liveness() {
return kubernetesHealthIndicator.health();
}
@GetMapping("/readiness")
public Health readiness() {
return kubernetesHealthIndicator.health();
}
}