简体   繁体   English

关闭 spring 从单个 tomcat 上运行的多个应用程序启动应用程序

[英]Shutdown spring boot application from multiple applications which are running on single tomcat

I want to shutdown spring boot application programmatically from multiple applications running on single tomcat server without stopping tomcat.我想从在单个 tomcat 服务器上运行的多个应用程序以编程方式关闭 spring 引导应用程序,而无需停止 tomcat。

I have searched over Google found couple of solutions like我在谷歌上搜索了几个解决方案,比如

System.exit(0) 

and

SpringbootApplication.exit()

causing shutting down tomcat.导致关闭 tomcat。 I don't want to shutdown tomcat.我不想关闭 tomcat。 Just the particular application.只是特定的应用程序。

How can I do that.. programmatically is there any way to do this.我该怎么做..以编程方式有什么方法可以做到这一点。

Pls help!请帮忙!

One way is to use the actuator.一种方法是使用致动器。

Add this dependency in your pom在您的 pom 中添加此依赖项

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

Add these properties in your yml / properties file在 yml / properties 文件中添加这些属性

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

With this in place, you can hit this rest endpoint to shutdown the application有了这个,您可以点击这个 rest 端点来关闭应用程序

http://host:port/actuator/shutdown

This is a POST call.这是一个POST调用。 If you are using spring security in your application, then you might want to make a few adjustments to allow this endpoint to go through.如果您在应用程序中使用 spring 安全性,那么您可能需要进行一些调整以允许此端点通过 go。 you can invoke post call using curl like您可以使用 curl 调用 post call

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

You can do that by registering an endpoint(highly secure though) to which your applications can send request for shutdown and you can code endpoint as below:您可以通过注册一个端点(虽然高度安全)来实现这一点,您的应用程序可以向该端点发送关闭请求,并且您可以对端点进行编码,如下所示:

         ConfigurableApplicationContext ctx = SpringApplication.run(YourApplicationClassName.class, args);
        int exitCode = SpringApplication.exit(ctx, new ExitCodeGenerator() {
            @Override
            public int getExitCode() {
                // no errors
                return 0;
            }
        });

Security - I would recommend that if you want to send termination signal to an app through other apps, you can use app token that uniquely identify the apps priviledged to shutdown your application.安全性 -我建议如果您想通过其他应用程序向应用程序发送终止信号,您可以使用唯一标识有权关闭您的应用程序的应用程序的应用程序令牌。

One way to do it is to kill the app process .一种方法是杀死应用程序进程

First, the application has to write its PID to a file (shutdown.pid):首先,应用程序必须将其 PID 写入文件(shutdown.pid):

SpringApplicationBuilder app = new SpringApplicationBuilder(Application.class)
  .web(WebApplicationType.NONE);
app.build().addListeners(new ApplicationPidFileWriter("./bin/shutdown.pid"));
app.run();

Then you can create a file (shutdown.bat) and add this line:然后你可以创建一个文件(shutdown.bat)并添加这一行:

kill $(cat ./bin/shutdown.pid)

The execution of shutdown.bat extracts the Process ID from the shutdown.pid file and uses the kill command to terminate the Boot application. shutdown.bat 的执行从shutdown.pid 文件中提取进程ID 并使用kill 命令终止Boot 应用程序。

ps: stolen from here . ps:从这里偷来的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot应用程序Tomcat服务器未运行 - Spring Boot Application Tomcat Server not running 在春季启动中运行Tomcat的多个实例? - running multiple instances of Tomcat in spring boot? 有没有办法在 IntelliJ IDEA 中使用单个运行配置运行多个 Spring Boot 应用程序? - Is there a way to run multiple Spring Boot applications with a single Running Configuration in IntelliJ IDEA? 如何在同一个 Tomcat 上部署多个带有外部配置的 Spring Boot 应用程序? - How to deploy multiple Spring boot applications with external configurations on the same Tomcat? 多个Spring Boot应用程序 - Multiple Spring Boot Applications 从Shell脚本正常关闭Spring Boot应用程序 - Gracefully Shutdown Spring Boot Application from Shell Script 在tomcat中与多个应用程序一起运行时,Struts2应用程序中出现NullpointerException - NullpointerException in Struts2 application when running with multiple applications in tomcat 在单个服务器中部署多个Spring Boot Web应用程序 - Deploying Multiple Spring Boot Web Applications in Single Server 在嵌入式Tomcat上从Spring-Boot Web应用程序运行Python脚本 - Running a Python script from Spring-Boot Web Application on embedded Tomcat 如何在 docker 中持久化在 tomcat 上运行的 Spring Boot 应用程序的会话 - How to persist sessions of spring boot application running on tomcat inside docker
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM