Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring web application healthchecks

I'm deploying Spring based web applications on Amazon's Beanstalk platform, and they give me the option of setting a "healthcheck" URL path for my application.

The idea is that their platform will do a request against that URL, after the deployment, to see if the application is successfully started. So, if the request results in an HTTP 200, the application is probably fine. But if it results in an HTTP 500 or something else, the platform knows there's a problem with the application.

So, I wish I could develop some kind of servlet that would check if the Spring Application Context was successfully initialised, or not, to give an appropriate HTTP response code to the platform.

Has anybody attempted something like this? For similar purposes?

I'm wondering if Spring already provides some elegant solution for this.

like image 973
Gabriel C Avatar asked Jan 29 '14 18:01

Gabriel C


People also ask

What is the default health check URL in spring boot?

The application will start on port 8080 by default. Once the application has started, you can list all the actuator endpoints exposed over HTTP from the URL http://localhost:8080/actuator . The status will be UP as long as the application is healthy.

How does spring boot actuator health check work?

Spring Boot automatically provides a health indicator for standard components (like a DataSource ). The health check provided by a DataSource creates a connection to a database and performs a simple query, such as select 1 from dual to check that it is working.

How do I monitor my spring boot application?

The first part of monitoring Spring Boot applications is choosing a metrics database. By default, Spring Boot will configure a Micrometer metrics registry in every application. This default implementation collects a pre-defined set of application metrics such as memory and CPU usage, HTTP requests, and a few others.

How do I enable health check endpoint in spring boot?

Enabling & Disabling Health Check Endpoints You can enable or disable an actuator endpoint by setting the property management. endpoint. <id>. enabled to true or false (where id is the identifier for the endpoint).


1 Answers

I'd suggest using health checks functionality from Metrics. You could set up a number of classes that extend HealthCheck class and implement check() method. These health check implementations would be Spring managed beans themselves and could autowire Spring beans and validate them. Then configure HealthCheckServlet to monitor application state. Also check metrics-spring project. It will make Spring and Metrics integration simpler.

If you are using Java Spring configuration you might have a Metrics config like this that extends MetricsConfigurerAdapter from metrics-spring

@Configuration
@EnableMetrics
public class MetricsConfig extends MetricsConfigurerAdapter { }

And then @Import(value = {MetricsConfig.class}) to your Spring config.

You also need and implementation of ServletContextListener to wire up HealthCheckServlet and Spring. This HealthCheckContextListener should be added to your web.xml

public class HealthCheckContextListener extends
    HealthCheckServlet.ContextListener implements ServletContextListener {

    private WebApplicationContext context;

    public HealthCheckContextListener(WebApplicationContext context) {
        this.context = context;
    }

    public HealthCheckContextListener() {}

    @Override
    public void contextInitialized(ServletContextEvent event) {
        this.context = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext()); 
        event.getServletContext().setAttribute(HealthCheckServlet.HEALTH_CHECK_REGISTRY,
            context.getBean(HealthCheckRegistry.class));
    }

    @Override
    protected HealthCheckRegistry getHealthCheckRegistry() {
        return (HealthCheckRegistry) context.getBean(HealthCheckRegistry.class);
    }
}
like image 152
dimchez Avatar answered Sep 25 '22 09:09

dimchez