我想从运行在单个tomcat服务器上的多个应用程序中以编程方式关闭spring boot应用程序,而不是停止tomcat。
我在谷歌上搜索了几个解决方案,比如
System.exit(0)
和
SpringbootApplication.exit()
导致关闭tomcat。我不想关掉tomcat。只是特定的应用程序。
我怎么能做到这一点..通过编程,有没有办法做到这一点。
请帮帮我!
发布于 2019-11-04 10:21:29
一种方法是使用执行器。
在您的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 security,那么您可能需要做一些调整以允许这个端点通过。您可以使用curl调用post调用,如下所示
curl -X POST http://host:port/actuator/shutdown
发布于 2019-11-04 10:20:28
您可以通过注册一个端点(尽管安全性很高)来实现这一点,您的应用程序可以向该端点发送关机请求,并且您可以像下面这样编写端点代码:
ConfigurableApplicationContext ctx = SpringApplication.run(YourApplicationClassName.class, args);
int exitCode = SpringApplication.exit(ctx, new ExitCodeGenerator() {
@Override
public int getExitCode() {
// no errors
return 0;
}
});
安全-我建议,如果你想通过其他应用程序向应用程序发送终止信号,你可以使用应用程序令牌来唯一地识别应用程序的特权,以关闭你的应用程序。
发布于 2019-11-04 10:31:14
一种方法是终止应用程序进程。
首先,应用程序必须将其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命令终止引导应用程序。
ps:从here窃取。
https://stackoverflow.com/questions/58691312
复制相似问题