Actuator 簡介
Actuator 是 Spring Boot 提供的對應用系統的自省和監控功能。通過 Actuator,可以使用資料化的名額去度量應用的運作情況,比如檢視伺服器的磁盤、記憶體、CPU等資訊,系統的線程、gc、運作狀态等等。
Actuator 通常通過使用 HTTP 和 JMX 來管理和監控應用,大多數情況使用 HTTP 的方式。
Actuator 端點說明
端點 | 描述 |
---|---|
auditevents | 擷取目前應用暴露的審計事件資訊 |
beans | 擷取應用中所有的 Spring Beans 的完整關系清單 |
caches | 擷取公開可以用的緩存 |
conditions | 擷取自動配置條件資訊,記錄哪些自動配置條件通過和沒通過的原因 |
configprops | 擷取所有配置屬性,包括預設配置,顯示一個所有 @ConfigurationProperties 的整理列版本 |
env | 擷取所有環境變量 |
flyway | 擷取已應用的所有Flyway資料庫遷移資訊,需要一個或多個 Flyway Bean |
liquibase | 擷取已應用的所有Liquibase資料庫遷移。需要一個或多個 Liquibase Bean |
health | 擷取應用程式健康名額(運作狀況資訊) |
httptrace | 擷取HTTP跟蹤資訊(預設情況下,最近100個HTTP請求-響應交換)。需要 HttpTraceRepository Bean |
info | 擷取應用程式資訊 |
integrationgraph | 顯示 Spring Integration 圖。需要依賴 spring-integration-core |
loggers | 顯示和修改應用程式中日志的配置 |
logfile | 傳回日志檔案的内容(如果已設定logging.file.name或logging.file.path屬性) |
metrics | 擷取系統度量名額資訊 |
mappings | 顯示所有@RequestMapping路徑的整理清單 |
scheduledtasks | 顯示應用程式中的計劃任務 |
sessions | 允許從Spring Session支援的會話存儲中檢索和删除使用者會話。需要使用Spring Session的基于Servlet的Web應用程式 |
shutdown | 關閉應用,要求endpoints.shutdown.enabled設定為true,預設為 false |
threaddump | 擷取系統線程轉儲資訊 |
heapdump | 傳回hprof堆轉儲檔案 |
jolokia | 通過HTTP公開JMX bean(當Jolokia在類路徑上時,不适用于WebFlux)。需要依賴 jolokia-core |
prometheus | 以Prometheus伺服器可以抓取的格式公開名額。需要依賴 micrometer-registry-prometheus |
Actuator 使用及配置
快速使用
項目依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
配置檔案
management.endpoints.enabled-by-default=true
#啟動所有端點
management.endpoints.web.exposure.include=*
#自定義管理端點路徑
#management.endpoints.web.base-path=/manage
Spring Boot 2.X 中,Actuator 預設隻開放 health 和 info 兩個端點。
添加
management.endpoints.web.exposure.include=*
配置後啟動應用,通路
http://127.0.0.1:8080/actuator我們可以看到所有的 Actuator 端點清單。
如果将
management.endpoints.enabled-by-default
設定為false,則禁用所有端點,如需啟用則如下:
management.endpoints.enabled-by-default=false
management.endpoint.info.enabled=true
禁用的端點将從應用程式上下文中完全删除。如果隻想更改公開端點,使用include和exclude屬性。使用如下:
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans
management.endpoints.web.base-path=/manage
配置表示将 /actuator 路徑重定義為 /manage。
常用端點詳解
主要用來檢測應用的運作狀況,是使用最多的一個監控點。監控軟體通常使用該接口實時監測應用運作狀況,在系統出現故障時把報警資訊推送給相關人員,如磁盤空間使用情況、資料庫和緩存等的一些健康名額。
預設情況下 health 端點是開放的,通路
http://127.0.0.1:8080/actuator/health即可看到應用運作狀态。
{"status":"UP"}
如果需要看到詳細資訊,則需要做添加配置:
management.endpoint.health.show-details=always
通路傳回資訊如下:
{"status":"UP","details":{"diskSpace":{"status":"UP","details":{"total":180002725888,"free":8687988736,"threshold":10485760}}}}
檢視應用資訊是否在 application.properties 中配置。如我們在項目中配置是:
info.app.name=Spring Boot Actuator Demo
info.app.version=v1.0.0
info.app.description=Spring Boot Actuator Demo
啟動項目,通路
http://127.0.0.1:8080/actuator/info傳回資訊如下:
{"app":{"name":"Spring Boot Actuator Demo","version":"v1.0.0","description":"Spring Boot Actuator Demo"}}
通過 env 可以擷取到所有關于目前 Spring Boot 應用程式的運作環境資訊,如:作業系統資訊(systemProperties)、環境變量資訊、JDK 版本及 ClassPath 資訊、目前啟用的配置檔案(activeProfiles)、propertySources、應用程式配置資訊(applicationConfig)等。
可以通過
http://127.0.0.1:8080/actuator/env/{name} ,name表示想要檢視的資訊,可以獨立顯示。
通路
http://127.0.0.1:8080/actuator/beans傳回部分資訊如下:
{
"contexts": {
"Spring Boot Actuator Demo": {
"beans": {
"endpointCachingOperationInvokerAdvisor": {
"aliases": [
],
"scope": "singleton",
"type": "org.springframework.boot.actuate.endpoint.invoker.cache.CachingOperationInvokerAdvisor",
"resource": "class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/EndpointAutoConfiguration.class]",
"dependencies": [
"environment"
]
},
"defaultServletHandlerMapping": {
"aliases": [
],
"scope": "singleton",
"type": "org.springframework.web.servlet.HandlerMapping",
"resource": "class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]",
"dependencies": [
]
},
...
}
}
}
}
從傳回的資訊中我們可以看出主要展示了 bean 的别名、類型、是否單例、類的位址、依賴等資訊。
通過 conditions 可以在應用運作時檢視代碼了某個配置在什麼條件下生效,或者某個自動配置為什麼沒有生效。
http://127.0.0.1:8080/actuator/conditions{
"contexts": {
"Spring Boot Actuator Demo": {
"positiveMatches": {
"SpringBootAdminClientAutoConfiguration": [
{
"condition": "OnWebApplicationCondition",
"message": "@ConditionalOnWebApplication (required) found 'session' scope"
},
{
"condition": "SpringBootAdminClientEnabledCondition",
"message": "matched"
}
],
"SpringBootAdminClientAutoConfiguration#metadataContributor": [
{
"condition": "OnBeanCondition",
"message": "@ConditionalOnMissingBean (types: de.codecentric.boot.admin.client.registration.metadata.CompositeMetadataContributor; SearchStrategy: all) did not find any beans"
}
],
...
}
}
}
}
擷取系統的日志資訊。
http://127.0.0.1:8080/actuator/loggers{
"levels": [
"OFF",
"ERROR",
"WARN",
"INFO",
"DEBUG",
"TRACE"
],
"loggers": {
"ROOT": {
"configuredLevel": "INFO",
"effectiveLevel": "INFO"
},
"cn": {
"configuredLevel": null,
"effectiveLevel": "INFO"
},
"cn.zwqh": {
"configuredLevel": null,
"effectiveLevel": "INFO"
},
"cn.zwqh.springboot": {
"configuredLevel": null,
"effectiveLevel": "INFO"
},
...
}
}
檢視所有 URL 映射,即所有 @RequestMapping 路徑的整理清單。
http://127.0.0.1:8080/actuator/mappings{
"contexts": {
"Spring Boot Actuator Demo": {
"mappings": {
"dispatcherServlets": {
"dispatcherServlet": [
{
"handler": "ResourceHttpRequestHandler [class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], ServletContext resource [/], class path resource []]",
"predicate": "/**/favicon.ico",
"details": null
},
...
]
}
}
}
}
}
通路:
http://127.0.0.1:8080/actuator/heapdump會自動生成一個 GZip 壓縮的 Jvm 的堆檔案 heapdump,我們可以使用 JDK 自帶的 Jvm 監控工具 VisualVM 打開此檔案檢視。如圖:

VisualVM下載下傳:
https://visualvm.github.io/download.html擷取系統線程的轉儲資訊,主要展示了線程名、線程ID、線程的狀态、是否等待鎖資源等資訊。在工作中,我們可以通過檢視線程的情況來排查相關問題。
http://127.0.0.1:8080/actuator/threaddump{
"threads": [
{
"threadName": "DestroyJavaVM",
"threadId": 40,
"blockedTime": -1,
"blockedCount": 0,
"waitedTime": -1,
"waitedCount": 0,
"lockName": null,
"lockOwnerId": -1,
"lockOwnerName": null,
"inNative": false,
"suspended": false,
"threadState": "RUNNABLE",
"stackTrace": [
],
"lockedMonitors": [
],
"lockedSynchronizers": [
],
"lockInfo": null
},
...
]
}
開啟可以接口關閉 Spring Boot 應用,要使用這個功能需要做如下配置:
management.endpoint.shutdown.enabled=true
可以通過 post(僅支援 post) 請求通路
http://127.0.0.1:8080/actuator/shutdown關閉應用。
http://127.0.0.1:8080/actuator/metrics
可以擷取系統度量名額資訊項如下:
{
"names": [
"jvm.memory.max",
"jvm.threads.states",
"jvm.gc.pause",
"http.server.requests",
"process.files.max",
"jvm.gc.memory.promoted",
"system.load.average.1m",
"jvm.memory.used",
"jvm.gc.max.data.size",
"jvm.memory.committed",
"system.cpu.count",
"logback.events",
"tomcat.global.sent",
"jvm.buffer.memory.used",
"tomcat.sessions.created",
"jvm.threads.daemon",
"system.cpu.usage",
"jvm.gc.memory.allocated",
"tomcat.global.request.max",
"tomcat.global.request",
"tomcat.sessions.expired",
"jvm.threads.live",
"jvm.threads.peak",
"tomcat.global.received",
"process.uptime",
"tomcat.sessions.rejected",
"process.cpu.usage",
"tomcat.threads.config.max",
"jvm.classes.loaded",
"jvm.classes.unloaded",
"tomcat.global.error",
"tomcat.sessions.active.current",
"tomcat.sessions.alive.max",
"jvm.gc.live.data.size",
"tomcat.threads.current",
"process.files.open",
"jvm.buffer.count",
"jvm.buffer.total.capacity",
"tomcat.sessions.active.max",
"tomcat.threads.busy",
"process.start.time"
]
}
對應通路 names 中的名額,可以檢視具體的名額資訊。如通路
http://127.0.0.1:8080/actuator/metrics/jvm.memory.used{
"name": "jvm.memory.used",
"description": "The amount of used memory",
"baseUnit": "bytes",
"measurements": [
{
"statistic": "VALUE",
"value": 1.16828136E8
}
],
"availableTags": [
{
"tag": "area",
"values": [
"heap",
"nonheap"
]
},
{
"tag": "id",
"values": [
"Compressed Class Space",
"PS Survivor Space",
"PS Old Gen",
"Metaspace",
"PS Eden Space",
"Code Cache"
]
}
]
}
示例代碼
github 碼雲參考文檔
https://docs.spring.io/spring-boot/docs/2.2.1.RELEASE/reference/html/production-ready-features.html非特殊說明,本文版權歸
朝霧輕寒所有,轉載請注明出處.
原文标題:Spring Boot 2.X(十六):應用監控之 Spring Boot Actuator 使用及配置
原文位址:
https://www.zwqh.top/article/info/25如果文章對您有幫助,請掃碼關注下我的公衆号,文章持續更新中...