Spring Web应用程序运行状况检查 [英] Spring web application healthchecks

查看:127
本文介绍了Spring Web应用程序运行状况检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在亚马逊的Beanstalk平台上部署基于Spring的Web应用程序,它们为我提供了为我的应用程序设置健康检查URL路径的选项。

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.

我们的想法是,在部署之后,他们的平台将针对该URL执行请求,以查看应用程序是否已成功启动。因此,如果请求导致HTTP 200,则应用程序可能正常。但如果它导致HTTP 500或其他东西,平台就会知道应用程序存在问题。

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.

所以,我希望我可以开发某种类型的servlet检查Spring Application Context是否已成功初始化,以便为平台提供适当的HTTP响应代码。

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?

我想知道Spring是否已为此提供了一些优雅的解决方案。

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

推荐答案

我建议使用 Metrics中的运行状况检查功能。您可以设置一些扩展的类 HealthCheck 类并实现 check()方法。这些运行状况检查实现将是Spring托管bean本身,并且可以自动装配Spring bean并验证它们。然后将 HealthCheckServlet 配置为监控应用状态。另请查看 metrics-spring 项目。它将使Spring和Metrics集成更简单。

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.

如果您使用的是Java Spring配置,那么您可能有一个像这样扩展的Metrics配置 MetricsConfigurerAdapter 来自metrics-spring

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 { }

然后 @Import(value = {MetricsConfig.class}) 到你的Spring配置。

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

你还需要并实现 ServletContextListener 来连接 HealthCheckServlet 和春天。应将 HealthCheckContextListener 添加到 web.xml

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);
    }
}

这篇关于Spring Web应用程序运行状况检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆