Java Spring Web 应用程序健康检查

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21439101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:43:50  来源:igfitidea点击:

Spring web application healthchecks

javaspringamazon-web-services

提问by Gabriel C

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.

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

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.

这个想法是他们的平台将在部署后针对该 URL 发出请求,以查看应用程序是否成功启动。因此,如果请求的结果是 HTTP 200,则应用程序可能没问题。但是,如果它导致 HTTP 500 或其他问题,则平台知道应用程序存在问题。

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.

因此,我希望我可以开发某种 servlet 来检查 Spring 应用程序上下文是否已成功初始化,以向平台提供适当的 HTTP 响应代码。

Has anybody attempted something like this? For similar purposes?

有没有人尝试过这样的事情?出于类似的目的?

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

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

回答by dimchez

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

我建议使用Metrics 的健康检查功能。您可以设置许多扩展HealthCheck类和实现check()方法的类。这些健康检查实现将是 Spring 管理的 bean 本身,并且可以自动装配 Spring bean 并验证它们。然后配置HealthCheckServlet以监视应用程序状态。还要检查metrics-spring项目。它将使 Spring 和 Metrics 集成更简单。

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

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

@Configuration
@EnableMetrics
public class MetricsConfig extends MetricsConfigurerAdapter { }

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

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

You also need and implementation of ServletContextListenerto wire up HealthCheckServletand Spring. This HealthCheckContextListenershould be added to your web.xml

您还需要和 Spring 连接ServletContextListener起来HealthCheckServlet并实现。这HealthCheckContextListener应该添加到您的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);
    }
}

回答by Alex

You should consider what constitutes a healthy app for you (e.g., servlet tier? JMS queues? FTP servers? etc.) and have your health check verify those services' availability. Obviously the health check is going to run frequently, so you don't want to initiate expensive operations over and over again.

您应该考虑什么对您来说是健康的应用程序(例如,servlet 层?JMS 队列?FTP 服务器?等)并让您的健康检查验证这些服务的可用性。显然,健康检查将频繁运行,因此您不想一遍又一遍地启动昂贵的操作。

Spring Bootis a new project that aims to simplify Spring development by favoring convention instead of configuration. They have implemented a "health check" feature that you can add to a project via an Actuator add-in module.

Spring Boot是一个新项目,旨在通过支持约定而不是配置来简化 Spring 开发。他们实现了“健康检查”功能,您可以通过 Actuator插件模块将其添加到项目

Here's a reference to their Health Check implementation-- it uses a controller class to return "ok" and, if there is a data source, attempts to run a query to confirm that the database is accessible (something like "SELECT .. from dual" in Oracle syntax).

这是对他们的 Health Check实现的引用——它使用控制器类返回“ok”,如果有数据源,则尝试运行查询以确认数据库可访问(类似于“SELECT .. from dual " 在 Oracle 语法中)。

回答by Jukka

The simplest thing you can do is this:

您可以做的最简单的事情是:

@Controller
class HealthCheckController {

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

Extendable to also test particular beans, DataSources etc.

还可以扩展以测试特定的 bean、数据源等。