關閉Spring Boot應用程序,我們可以通過OS命令kill -9 進程ID 實現將進程殺死。但是,有沒有一種更好的方式,比如通過REST請求實現?Spring Boot Actoator提供了實現。通過提供的shutdown服務可以實現安全的關閉Spring Boot應用。簡單實用步驟如下:
step1:pom引入spring boot Actoator依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
step2:開啟shutdown endpoint,默認是關閉的,需要在
application.properties
中開啟shutdown endpoint
:
通過上面的設置即可實現關閉spring boot應用,但是你會發現,這樣會十分不安全,只要通過服務調用即可關閉應用,所以,具體應用中常常需要進行安全認證,比如借助Spring boot security,步驟如下:
step1:pom.xml添加security依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
step2:開啟安全驗證,application.properties
中變更配置:
security.user.password=password #密碼
management.security.role=XX_ROLE#角色
step3:指定路徑、IP、端口
endpoints.shutdown.path=/custompath #指定shutdown的路徑,如果需要統一指定應用的路徑,則可以用management.context-path=/manage
management.port=XXX #指定管理端口
management.address=X.X.X.X #指定客戶端ID