3 回答

TA貢獻1856條經驗 獲得超11個贊
一種方法是使用執行器。
在您的 pom 中添加此依賴項
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
將這些屬性添加到您的 yml / 屬性文件中
management.endpoint.shutdown.enabled=true
endpoints.shutdown.enabled=true
management.endpoints.web.exposure.include=*
完成此操作后,您可以點擊此休息端點來關閉應用程序
http://host:port/actuator/shutdown
這是一個POST調用。如果您在應用程序中使用 Spring Security,那么您可能需要進行一些調整以允許此端點通過。您可以使用curl來調用post調用,例如
curl -X POST http://host:port/actuator/shutdown

TA貢獻1784條經驗 獲得超7個贊
您可以通過注冊一個端點(盡管高度安全)來做到這一點,您的應用程序可以向該端點發送關閉請求,并且您可以對端點進行編碼,如下所示:
ConfigurableApplicationContext ctx = SpringApplication.run(YourApplicationClassName.class, args);
int exitCode = SpringApplication.exit(ctx, new ExitCodeGenerator() {
@Override
public int getExitCode() {
// no errors
return 0;
}
});
安全性 - 我建議,如果您想通過其他應用程序向應用程序發送終止信號,您可以使用應用程序令牌來唯一標識有權關閉您的應用程序的應用程序。

TA貢獻1796條經驗 獲得超7個贊
一種方法是終止應用程序進程。
首先,應用程序必須將其 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:從這里偷來的。
添加回答
舉報