Custom Readiness Probes in Spring Boot

Writing a custom readiness probe in Spring Boot, for example for usage in Kubernetes, is basically very simple:

@Component
public class CustomReadinessStateHealthIndicator extends ReadinessStateHealthIndicator
{
   public CustomReadinessStateHealthIndicator(final ApplicationAvailability availability)
   {
      super(availability); // Does the mapping from ReadinessState to Status
   }

   @Override
   protected AvailabilityState getState(final ApplicationAvailability applicationAvailability)
   {
      // Your logic goes here
      return ...; // Should return ReadinessState.REFUSING_TRAFFIC or ACCEPTING_TRAFFIC
   }
}

So far, so simple. The only stumbling block you might come across is hidden (admittedly not much) in a note in the Spring Boot documentation:

The identifier for a given HealthIndicator is the name of the bean without the HealthIndicator suffix, if it exists.

So when adding your custom HealthIndicator to the configuration, remember to strip away the suffix, if needed (or just name your class differently):

management.endpoint.health.probes.enabled=true
management.health.readinessState.enabled=true
management.endpoint.health.group.readiness.include=readinessState,customReadinessState

Took me a few minutes to figure out, but otherwise, it works quite well.