SpringBoot-Actuator應用監控以及優雅停機

Actuator是SpringBoot提供的對應用系統的自省和監控的集成功能,可以對應用系統進行配置查看、相關功能統計等。
添加依賴:

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

application.yml

management:
    context-path: /management
    health:
        mail:
            enabled: false

一、執行器端點(endpoints) 可用於監控應用及與應用進行交互,Spring Boot包含很
多內置的端點,你也可以添加自己的。例如, health 端點提供了應用的基本健康
信息。 端點暴露的方式取決於你採用的技術類型,大部分應用選擇HTTP監控,端
點的ID映射到一個URL。例如, health 端點默認映射到 /management/health 。
下面的端點都是可用的:
HTTP方法 路徑 描述 鑑權
GET /autoconfig 查看自動配置的使用情況 true
GET /configprops 查看配置屬性,包括默認配置 true
GET /beans 查看bean及其關係列表 true
GET /dump 打印線程棧 true
GET /env 查看所有環境變量 true
GET /env/{name} 查看具體變量值 true
GET /health 查看應用健康指標 false
GET /info 查看應用信息 false
GET /mappings 查看所有url映射 true
GET /metrics 查看應用基本指標 true
GET /metrics/{name} 查看具體指標 true
POST/shutdown 關閉應用 true
GET /trace 查看基本追蹤信息 true
如圖
健康指標:

二、配置文件配置啓用shutdown的HTTP訪問
application.properties

#啓用shutdown endpoint的HTTP訪問
endpoints.shutdown.enabled=true
#不需要驗證 
endpoints.shutdown.sensitive=false

配置完成後可通過post請求,停止應用
返回結果:

{
  "message": "Shutting down, bye..."
}

控制檯:

734896 [Thread-12] INFO  com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Close initiated... 
734899 [Thread-12] INFO  com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Closed. 

增加安全驗證
Maven依賴

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

application.properties

endpoints.shutdown.sensitive=true
security.user.name=admin
security.user.password=123456

此時再次發送請求時,則需要進行登錄操作後才能執行。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章