Spring Boot Actualtor 運行狀態監控
參考官方文檔:詳細可以查看官方文檔
Spring Boot Actuator 可以幫助你監控和管理Spring Boot應用,監控數據可以使用 REST、 JMX或者shell來獲得。 下面配置REST方式使用Actuator。
項目引入 Actuator 起步依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
配置文件 application.yml 中添加 Actuator 相關配置信息。
配置 port 是爲了區分項目的端口與監控的端口,若不配置默認訪問的爲項目端口。
# 在Spring Boot 2.0 之前版本只需要 management.port 不需要中間的 server
management:
server:
port: 9001 # Spring Boot 2.0 之後直接訪問 localhost:9001/health 無效,需要加上/actuator localhost:9001/actuator/health
# Spring Boot 1.5 - 2.0 之間的版本需要設置下面的 security.enabled 爲false 否則需要安全認證
# security:
# enabled: false
endpoints:
web:
base-path: /manager # 默認情況下,路徑爲 /actuator, 所有Web端點均可用/actuator/{id},配置後更改爲/manager/{id}
exposure:
include: health,dump, env # 設置開啓端點,端點之間使用英文逗號隔開,這樣只能訪問當前設置的端點,若要開啓所有可以使用 "*",如include: "*", yaml文件中引號是必須的,properties文件則不需要
# exclude: env # 排除不需要的端點
endpoint:
health:
show-details: always # 展示詳細 health 信息 Spring Boot 2.0 之後不直接展示詳細信息需要設置此配置
以下是部分端口信息,詳細可以查看官方文檔 這裏的版本爲2.2.1,與1.x的會有差別,如autoconfig 更改爲conditions,原 trace 改爲 httptrace
類型 | 端口 | 描述 |
---|---|---|
get | auditevents | 顯示應用暴露的審計事件 (比如認證進入、訂單失敗) |
get | beans | 描述應用程序上下文裏所有的bean以及它們的關係 |
get | caches | 檢索應用程序的緩存 |
get | conditions | conditions 端點提供有關的配置和自動配置類條件的評估信息 |
get | configprops | 描述配置屬性如何注入bean |
get | env | 獲取所有環境屬性 |
get | /env/{property.name} | 根據名稱獲取特定的環境屬性值 |
get | health | 應用程序的健康指標 |
get | httptrace | 提供關於HTTP請求-響應交換信息(時間戳,HTTP頭等) |
get | info | 獲取應用程序信息 |
get | logfile | 獲取日誌信息 |
get | mappings | 描述全部的URI路徑,以及他們和控制器(包括Actuator端點)的映射關係 |
get | metrics | 訪問應用程序指標,例如內存量和HTTP請求計數 |
get | shutdown | 關閉應用程序,需要設置 endpoints.shutdown.enable=true |
查看開啓的端點
訪問 http://localhost:9001/actuator
{
"_links": {
"self": {
"href": "http://localhost:9001/actuator",
"templated": false
},
"env": {
"href": "http://localhost:9001/actuator/env",
"templated": false
},
"env-toMatch": {
"href": "http://localhost:9001/actuator/env/{toMatch}",
"templated": true
}
}
}
查看運行程序健康狀態
訪問: http://localhost:9001/manager/health
{
"status": "UP",
"components": {
"diskSpace": {
"status": "UP",
"details": {
"total": 215288442880,
"free": 152013299712,
"threshold": 10485760
}
},
"ping": {
"status": "UP"
}
}
}