首页 > 解决方案 > 从单个tomcat上运行的多个应用程序关闭spring boot应用程序

问题描述

我想在不停止 tomcat 的情况下从单个 tomcat 服务器上运行的多个应用程序以编程方式关闭 spring boot 应用程序。

我在谷歌上搜索了几个解决方案,比如

System.exit(0) 

SpringbootApplication.exit()

导致关闭tomcat。我不想关闭tomcat。只是特定的应用程序。

我该怎么做..以编程方式有什么方法可以做到这一点。

请帮忙!

标签: javaspringspring-boottomcat

解决方案


一种方法是使用致动器。

在您的 pom 中添加此依赖项

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

在 yml / properties 文件中添加这些属性

management.endpoint.shutdown.enabled=true
endpoints.shutdown.enabled=true
management.endpoints.web.exposure.include=*

有了这个,你可以点击这个休息端点来关闭应用程序

http://host:port/actuator/shutdown

这是一个POST调用。如果您在应用程序中使用 Spring Security,那么您可能需要进行一些调整以允许此端点通过。您可以使用 curl 调用 post call

curl -X POST http://host:port/actuator/shutdown

推荐阅读