如何以正确的方式关闭 Spring Boot 应用程序? [英] How to shutdown a Spring Boot Application in a correct way?

查看:38
本文介绍了如何以正确的方式关闭 Spring Boot 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Spring Boot 文档中,他们说每个 SpringApplication 都会向 JVM 注册一个关闭钩子,以确保 ApplicationContext 在退出时正常关闭."

In the Spring Boot Document, they said that 'Each SpringApplication will register a shutdown hook with the JVM to ensure that the ApplicationContext is closed gracefully on exit.'

当我在shell命令上点击ctrl+c时,应用程序可以正常关闭.如果我在生产机器上运行应用程序,我必须使用命令java -jar ProApplicton.jar.但是我不能关闭shell终端,否则会关闭进程.

When I click ctrl+c on the shell command, the application can be shutdown gracefully. If I run the application in a production machine, I have to use the command java -jar ProApplicaton.jar. But I can't close the shell terminal, otherwise it will close the process.

如果我运行像 nohup java -jar ProApplicaton.jar & 这样的命令,我就不能使用 ctrl+c 正常关闭它.

If I run command like nohup java -jar ProApplicaton.jar &, I can't use ctrl+c to shutdown it gracefully.

在生产环境中启动和停止 Spring Boot 应用程序的正确方法是什么?

What is the correct way to start and stop a Spring Boot Application in the production environment?

推荐答案

如果您正在使用执行器模块,您可以通过 JMXHTTP 关闭应用程序,如果端点已启用.

If you are using the actuator module, you can shutdown the application via JMX or HTTP if the endpoint is enabled.

添加到application.properties:

endpoints.shutdown.enabled=true

endpoints.shutdown.enabled=true

以下网址将可用:

/actuator/shutdown - 允许应用正常关闭(默认不启用).

/actuator/shutdown - Allows the application to be gracefully shutdown (not enabled by default).

根据端点的公开方式,敏感参数可用作安全提示.

Depending on how an endpoint is exposed, the sensitive parameter may be used as a security hint.

例如,敏感端点在通过 HTTP 访问时将需要用户名/密码(如果未启用网络安全,则直接禁用).

For example, sensitive endpoints will require a username/password when they are accessed over HTTP (or simply disabled if web security is not enabled).

来自 Spring 引导文档

这篇关于如何以正确的方式关闭 Spring Boot 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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