Spring Boot - 等待 Web 服务器启动 [英] Spring Boot - Wait for web server to start

查看:29
本文介绍了Spring Boot - 等待 Web 服务器启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Spring Boot 应用程序中,我需要等到(默认 Tomcat)Web 服务器完全初始化并准备好接收流量,然后再向其他应用程序发送消息,告诉它们向我发送 HTTP 请求(特别是一个命中的监控系统)我的/health).

In my Spring Boot application I need to wait until the (default Tomcat) web server is fully initialized and ready to take traffic before I send messages to other applications telling them to send HTTP requests to me (specifically a monitoring system that hits my /health).

我已经尝试将向其他应用程序发送消息的逻辑放在 ApplicationListener 中,但现在还为时过早.其他应用程序尝试向我发出请求并失败.现在我在 onApplicationEvent 中设置了一个延迟,这有效,但它很笨拙.

I've tried putting the logic that sends messages to other applications in a ApplicationListener<ContextRefreshedEvent> but it's still too early. The other applications try to make requests to me and fail. Right now I've put a delay in the onApplicationEvent and that works but it's hacky and racy.

我也尝试添加一个 ServletContextInitializer 但它甚至更早被触发.

I've also tried adding a ServletContextInitializer but that fired even earlier.

我假设我需要使用 Tomcat API,但我想看看 Boot API 中是否有相关内容.

I'm assuming that I'll need to use a Tomcat API but I wanted to see if there was something in the Boot API for this.

推荐答案

最简单的做法是在 SpringApplication.run() 返回后发送消息.在 Tomcat(或任何其他受支持的嵌入式容器)完全启动并侦听配置的端口之前,此方法不会返回.然而,虽然这很简单,但它并不是一种非常巧妙的方法,因为它混合了您的主配置类和应用程序的一些运行时逻辑的关注点.

The simplest thing to do is to send the message once SpringApplication.run() has returned. This method won't return until Tomcat (or any other supported embedded container) is fully started and listening on the configured port(s). However, while this is simple, it's not a very neat approach as it mixes the concerns of your main configuration class and some of your application's runtime logic.

相反,您可以使用 SpringApplicationRunListener.finished() 在 Tomcat 完全启动并监听配置的端口之前不会被调用.

Instead, you can use a SpringApplicationRunListener. finished() will not be called until Tomcat is fully started and listening on the configured port.

创建一个名为 src/main/resources/META-INF/spring.factories 的文件,列出您的运行侦听器.例如:

Create a file named src/main/resources/META-INF/spring.factories listing your run listener. For example:

org.springframework.boot.SpringApplicationRunListener=com.example.MyRunListener

使用所需的构造函数创建您的运行侦听器并实现 SpringApplicationRunListener.例如:

Create your run listener with the required constructor and implement SpringApplicationRunListener. For example:

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

public class MyRunListener implements SpringApplicationRunListener {

    public MyRunListener(SpringApplication application, String[] args) { }

    @Override
    public void starting() { }

    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) { }

    @Override
    public void contextPrepared(ConfigurableApplicationContext context) { }

    @Override
    public void contextLoaded(ConfigurableApplicationContext context) { }

    @Override
    public void started(ConfigurableApplicationContext context) {
        // Send message; Tomcat is running and listening on the configured port(s)
    }

    @Override
    public void running(ConfigurableApplicationContext context) { }

    @Override
    public void failed(ConfigurableApplicationContext context, Throwable exception) { }

这篇关于Spring Boot - 等待 Web 服务器启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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