spring boot项目 中止运行 最常用的几种方法
生活随笔
收集整理的這篇文章主要介紹了
spring boot项目 中止运行 最常用的几种方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
spring boot項目 中止運行 最常用的幾種方法:
1. 調用接口,停止應用上下文
@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;}
}
POST 調用 localhost:port/shutdownContext
2. 通過Actuator Shutdown 端點服務
Spring Boot Actuator是一個主要用于應用指標監控和健康檢查的服務。可以通過Http訪問對應的Endpoint來獲取應用的健康及指標信息。另外,它還提供了一個遠程通過http實現應用shutdown的端點。
首先,我們要在Spring Boot中引入Actuator。
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
生產環境通常出于安全考慮,不能將應用關閉的訪問完全公開,我們還要引入spring-boot-starter-security。具體的安全配置,請參考學習Spring security,在此不多做敘述!
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId>
</dependency>
默認情況下,出于安全考慮shutdown端點服務是處于關閉狀態的,我們需要通過配置開啟:
management.endpoints.web.exposure.include=*
management.endpoint.shutdown.enabled=true
endpoints.shutdown.enabled=true
至此,我們就可以通過發送一個Post請求,來停掉Spring Boot應用服務。
POST 調用 localhost:port/actuator/shutdown
這種方法的缺陷在于:當你引入Actuator的shutdown服務的時候,其他的監控服務也自動被引入了。
3. kill殺掉進程
查詢到進程的pid,然后使用kill命令。
總結
以上是生活随笔為你收集整理的spring boot项目 中止运行 最常用的几种方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 设置普通用户执行docker命令,执行d
- 下一篇: 力扣每日一题——独一无二出现的次数