Spring boot : checks at startup

For some reasons, your can mess up with the connectivity : lab hostnames in prod, unavailable dependencies or wrong network routing rules.

You can use some health indicators in spring boot to check the health of your spring boot application.
The actuator framework exposes an endpoint which replies with UP or DOWN

Hitting the endpoint will execute the health indicators to give you a status of the service anytime.

That can happen during your service lifetime,
But what you would like to do is to get an immediate failure at startup time, so you get instant feedback of the misconfiguration of the application.

I found a way to do that by triggering a second execution of the health checks and making the service crash if one of the health indicators is red :
Replace your main method by this one :

public class Application {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);

        // bad health check should bring service down
                .filter(status -> status == Status.DOWN)
                .ifPresent(status -> SpringApplication.exit(context, () -> 1));

Now, a bad health will bring the service down, and with an efficient toolchain / image service (docker for example), the buggy instance will never be exposed to live traffic.

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.