繁体   English   中英

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

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

我想从在单个 tomcat 服务器上运行的多个应用程序以编程方式关闭 spring 引导应用程序,而无需停止 tomcat。

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

System.exit(0) 

SpringbootApplication.exit()

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

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

请帮忙!

一种方法是使用致动器。

在您的 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=*

有了这个,您可以点击这个 rest 端点来关闭应用程序

http://host:port/actuator/shutdown

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

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

您可以通过注册一个端点(虽然高度安全)来实现这一点,您的应用程序可以向该端点发送关闭请求,并且您可以对端点进行编码,如下所示:

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

安全性 -我建议如果您想通过其他应用程序向应用程序发送终止信号,您可以使用唯一标识有权关闭您的应用程序的应用程序的应用程序令牌。

一种方法是杀死应用程序进程

首先,应用程序必须将其 PID 写入文件(shutdown.pid):

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

然后你可以创建一个文件(shutdown.bat)并添加这一行:

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

shutdown.bat 的执行从shutdown.pid 文件中提取进程ID 并使用kill 命令终止Boot 应用程序。

ps:从这里偷来的。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM