使用Spring Boot执行器安全关闭休息服务器? [英] Using the Spring Boot actuator to shutdown a rest server safely?

查看:141
本文介绍了使用Spring Boot执行器安全关闭休息服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是如何进行的后续问题以正确的方式关闭Spring Boot应用程序?

假设我们有一台服务器,当前连接了3个客户端,并且每个客户端都运行很长时间。然后我们使用以下命令关闭服务器:

Suppose we have a server that has 3 clients currently connected and each client is running a long job. We then shutdown the server with the command:

curl -X POST localhost:port/shutdown

这样做:

A)让服务器完成3的作业客户端在关闭之前是否正在运行?

A) Let the server finish the jobs that the 3 clients are running before shutting down?

B)禁止发生任何其他连接,以便服务器最终关闭?

B) Disallow any other connections from happening such that the server will eventually shut down?

推荐答案

Spring Boot关闭端点调用此类: org.springframework.boot.actuate.endpoint.ShutdownEndpoint 调用<你的 ApplicationContext 上的code> close()。这反过来...

The Spring Boot shutdown endpoint invokes this class: org.springframework.boot.actuate.endpoint.ShutdownEndpoint which invokes close() on your ApplicationContext. This in turn ...


  • 销毁豆子

  • 关闭bean工厂

  • 停止嵌入式servlet容器

如果您的bean已经订购并且已经仔细编写了关闭方法,那么这应该是精细。但如果没有,那么在该关闭周期的某个时刻3个客户端正在运行的作业可能会被中断。此外,可能会在调用shutdown和关闭周期之间的小时间窗口中建立新连接。

If your beans are ordered and have carefully written shutdown methods, then this should be fine. But if not, then at some point in that shutdown cycle "the jobs that the 3 clients are running" are likely to be interrupted. In addition, it is possible that new connections may be made in the small time window between you invoking shutdown and the shutdown cycle kicking in.

Spring提供应用程序事件和侦听器挂钩这允许您参与关机周期。 ContextClosedEvent 在销毁 bean之前发布,嵌入式容器已关闭等,因此您可以使用它来实现自己的关闭行为。默认行为启动。例如:

Spring provides application events and listener hooks which allow you to participate in the shutdown cycle. The ContextClosedEvent is published before beans are destroyed, the embedded container is shutdown, etc, so you can use this to implement your own shutdown behaviour before the default behaviour kicks in. For example:

public class ShutdownListener implements ApplicationListener<ContextClosedEvent> {
    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        // 
    }
}

您可以实现此侦听器,使其

You could implement this listener such that it


  • 使用HTTP拒绝连接请求503(或等同于你没有处理HTTP请求的东西)

  • 暂停以允许任何机上工作在继续关机周期之前完成

您以与在Spring Boot中注册任何ApplicationListener相同的方式注册此侦听器,例如

You register this listener in the same way as you register any ApplicationListener in Spring Boot e.g.

SpringApplication app = new SpringApplication(MyApplication.class);
app.addListeners(new ShutdownListener());

这篇关于使用Spring Boot执行器安全关闭休息服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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