亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從單個 tomcat 上運行的多個應用程序關閉 spring boot 應用程序

從單個 tomcat 上運行的多個應用程序關閉 spring boot 應用程序

慕尼黑的夜晚無繁華 2024-01-28 17:23:28
我想以編程方式從單個 tomcat 服務器上運行的多個應用程序關閉 spring boot 應用程序,而不停止 tomcat。我在谷歌上搜索發現了幾個解決方案,例如System.exit(0)和SpringbootApplication.exit()導致關閉tomcat。我不想關閉tomcat。只是特定的應用程序。我該怎么做......以編程方式有什么辦法可以做到這一點。請幫忙!
查看完整描述

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


查看完整回答
反對 回復 2024-01-28
?
嚕嚕噠

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;

            }

        });

安全性 - 我建議,如果您想通過其他應用程序向應用程序發送終止信號,您可以使用應用程序令牌來唯一標識有權關閉您的應用程序的應用程序。


查看完整回答
反對 回復 2024-01-28
?
蕪湖不蕪

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:從這里偷來的。



查看完整回答
反對 回復 2024-01-28
  • 3 回答
  • 0 關注
  • 202 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號