Spring healthchecks web application

I am deploying w760-based web applications on the Amazon Beanstalk platform and they give me the option to set the healthcheck URL for my application.

The idea is that after deployment, their platform will query this URL to see if the application is running successfully. Thus, if the request results in HTTP 200, the application is probably fine. But if this leads to HTTP 500 or something else, the platform knows that the problem is with the application.

So, I would like to develop some kind of servlet that would check whether Spring the Application Context was successfully initialized or not, in order to provide the corresponding HTTP response code to the platform.

Has anyone tried something like this? For similar purposes?

I am wondering if Spring already offers some elegant solutions for this.

+7
java spring amazon-web-services
source share
3 answers

I would suggest using the Health Checks from Metrics . You can create several classes that extend the HealthCheck class and implement the check() method. These health check implementations would be Spring managed beans themselves and could autwire Spring beans and test them. Then configure the HealthCheckServlet to monitor the health of the application. Also check out the metrics-spring design. This will simplify the integration of Spring and Metrics.

If you use the Java Spring configuration, you may have a Metrics configuration that extends the MetricsConfigurerAdapter from the metrics - spring

 @Configuration @EnableMetrics public class MetricsConfig extends MetricsConfigurerAdapter { } 

And then @Import(value = {MetricsConfig.class}) to the Spring configuration.

You will also need to implement a ServletContextListener to connect 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); } } 
+8
source share

You should think about what constitutes an application that is useful to you (for example, JMS servlet queues, FTP servers, etc.), and check the functionality of these services; check the health check. Obviously, a health check will be performed frequently, so you do not want to run expensive operations over and over again.

Spring Boot is a new project whose goal is to simplify Spring development, rather than setting up a convention. They have implemented a health check function, which you can add to the project through the add module.

Here is a link to their health check implementation - it uses the controller class to return "ok" and, if there is a data source, it tries to execute a request to confirm the availability of the database (something like "SELECT .. from dual" in Oracle syntax).

+2
source share

The simplest thing you can do is the following:

 @Controller class HealthCheckController { @ResponseStatus(OK) @RequestMapping(value = "/ping", method = HEAD) { public void ping() { } } 

Extensible for testing specific beans, DataSources, etc.

+1
source share

All Articles