日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Shutdown SpringBoot App

發(fā)布時(shí)間:2024/2/28 javascript 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Shutdown SpringBoot App 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

    • Shutdown Endpoint
    • close Application Context
    • 退出SpringApplication
    • 從外部程序kill App

Shutdown SpringBoot App

Spring Boot使用ApplicationContext來創(chuàng)建,初始化和銷毀所用的bean。本文將會(huì)講解如何shut down一個(gè)spring boot應(yīng)用程序。

Shutdown Endpoint

Spring Boot actuator自帶了shutdown的endpoint。首先我們添加pom依賴:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId> </dependency>

接下來我們需要開啟shutdown的配置:

management.endpoints.web.exposure.include=* management.endpoint.shutdown.enabled=true

上面的配置對(duì)外暴露了 /shutdown 接口。我們可以直接這樣調(diào)用:

curl -X POST localhost:8080/actuator/shutdown

close Application Context

我們也可以直接調(diào)用Application Context的close() 方法來關(guān)閉Application Context。

@SpringBootApplication public class ConfigurableApp {public static void main(String[] args) {ConfigurableApplicationContext ctx = newSpringApplicationBuilder(ConfigurableApp.class).web(WebApplicationType.NONE).run();System.out.println("Spring Boot application started");ctx.getBean(TerminateBean.class);ctx.close();} }

為了驗(yàn)證App是否被關(guān)閉,我們可以在TerminateBean中添加@PreDestroy來監(jiān)測(cè)App是否被關(guān)閉:

@Component public class TerminateBean {@PreDestroypublic void onDestroy() throws Exception {System.out.println("Spring Container is destroyed!");} }

這是程序的輸出:

2020-02-03 23:12:08.583 INFO 30527 --- [ main] com.flydean.ConfigurableApp : Started ConfigurableApp in 2.922 seconds (JVM running for 3.559) Spring Boot application started Spring Container is destroyed!

還有一種辦法就是暴露close接口如下所示:

@RestController public class ShutdownController implements ApplicationContextAware {private ApplicationContext context;@PostMapping("/shutdownContext")public void shutdownContext() {((ConfigurableApplicationContext) context).close();}@Overridepublic void setApplicationContext(ApplicationContext ctx) throws BeansException {this.context = ctx;} }

這樣我們就可以通過/shutdownContext接口來關(guān)閉ApplicationContext。

退出SpringApplication

上篇文章我們講過可以通過實(shí)現(xiàn)ExitCodeGenerator 接口來返回特定的exit code:

@SpringBootApplication public class ExitCodeApp implements ExitCodeGenerator {public static void main(String[] args) {System.exit(SpringApplication.exit(SpringApplication.run(ExitCodeApp.class, args)));}@Overridepublic int getExitCode() {return 11;} }

從外部程序kill App

熟悉shell的同學(xué)都知道如果想在外部kill一個(gè)程序,需要知道該App的pid,Spring Boot也可以很方便的生成pid:

@SpringBootApplication public class KillApp {public static void main(String[] args) {SpringApplicationBuilder app = new SpringApplicationBuilder(KillApp.class).web(WebApplicationType.NONE);app.build().addListeners(new ApplicationPidFileWriter("./bin/shutdown.pid"));app.run();} }

上面的程序?qū)?huì)在./bin/shutdown.pid生成應(yīng)用程序的pid,供shell使用。

我們可以這樣使用:

kill $(cat ./bin/shutdown.pid)

本文的例子可以參考 https://github.com/ddean2009/learn-springboot2/tree/master/springboot-shutdown

更多精彩內(nèi)容且看:

  • 區(qū)塊鏈從入門到放棄系列教程-涵蓋密碼學(xué),超級(jí)賬本,以太坊,Libra,比特幣等持續(xù)更新
  • Spring Boot 2.X系列教程:七天從無到有掌握Spring Boot-持續(xù)更新
  • Spring 5.X系列教程:滿足你對(duì)Spring5的一切想象-持續(xù)更新
  • java程序員從小工到專家成神之路(2020版)-持續(xù)更新中,附詳細(xì)文章教程

更多教程請(qǐng)參考 flydean的博客

總結(jié)

以上是生活随笔為你收集整理的Shutdown SpringBoot App的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。