[英]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 应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.