Java 检查 spring boot 应用程序是否正在运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52289267/
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
checking whether spring boot application is running or not
提问by Rohan RM
I've created a spring boot project and deployed it on a vm. I've added a command in local.rc that starts the spring boot application on reboot. I want to check whether the command got executed and the application is running. How do I do that?
我创建了一个 Spring Boot 项目并将其部署在虚拟机上。我在 local.rc 中添加了一个命令,该命令在重新启动时启动 Spring Boot 应用程序。我想检查命令是否已执行以及应用程序是否正在运行。我怎么做?
采纳答案by Michal
There are two ways
有两种方式
On system level - you can run your project as a service, which is documented in the Official documentation - Deployments. Then you can query the application status
service myapp status
.On application level - include Spring Boot Actuator in your app and use the Actuator endpoints such as
/actuator/health
as per Official documentation - Production Ready Endpoints. These endpoints can be exposed via HTTP or JMX.
在系统级别 - 您可以将您的项目作为服务运行,这在官方文档 - 部署中进行了记录。然后就可以查询申请状态了
service myapp status
。在应用程序级别 - 在您的应用程序中包含 Spring Boot Actuator 并使用 Actuator 端点,例如
/actuator/health
根据官方文档 - 生产就绪端点。这些端点可以通过HTTP 或 JMX公开。
Note: prior to spring boot 2.0 the actuator endpoint is /health
注意:在 spring boot 2.0 之前,执行器端点是 /health
回答by Mark Bramnik
If it's a web project, it makes sense to include spring-boot-actuator (just add a dependency in maven and start the microservice).
如果是web项目,包含spring-boot-actuator是有意义的(只需在maven中添加一个依赖并启动微服务)。
In this case, it will automatically expose the following endpoint (for example, its actually can be flexibly set up):
在这种情况下,它会自动暴露以下端点(例如,它实际上可以灵活设置):
http://<HOST>:<PORT>/health
Just issue an HTTP GET request, and if you get 200 - it's up and running.
只需发出一个 HTTP GET 请求,如果您得到 200 - 它已启动并正在运行。
If using an actuator is not an option (although it should be really addressed as a first bet), then you can merely telnet to http://<HOST>:<PORT>
如果使用执行器不是一种选择(尽管它应该真正作为第一个赌注解决),那么您只需 telnet 即可 http://<HOST>:<PORT>
The ratio behind this is that that PORT is exposed and ready to "listen" to external connections only after the application context is really started.
这背后的比率是,只有在应用程序上下文真正启动后,该 PORT 才会公开并准备好“侦听”外部连接。