[英]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.