Actuator 簡(jiǎn)介
Actuator 是 Spring Boot 提供的對(duì)應(yīng)用系統(tǒng)的自省和監(jiān)控功能傅蹂。通過(guò) Actuator,可以使用數(shù)據(jù)化的指標(biāo)去度量應(yīng)用的運(yùn)行情況氓轰,比如查看服務(wù)器的磁盤、內(nèi)存靴庆、CPU等信息炉抒,系統(tǒng)的線程稚叹、gc塞茅、運(yùn)行狀態(tài)等等野瘦。
Actuator 通常通過(guò)使用 HTTP 和 JMX 來(lái)管理和監(jiān)控應(yīng)用,大多數(shù)情況使用 HTTP 的方式飒泻。
Actuator 端點(diǎn)說(shuō)明
端點(diǎn) | 描述 |
---|---|
auditevents | 獲取當(dāng)前應(yīng)用暴露的審計(jì)事件信息 |
beans | 獲取應(yīng)用中所有的 Spring Beans 的完整關(guān)系列表 |
caches | 獲取公開(kāi)可以用的緩存 |
conditions | 獲取自動(dòng)配置條件信息鞭光,記錄哪些自動(dòng)配置條件通過(guò)和沒(méi)通過(guò)的原因 |
configprops | 獲取所有配置屬性,包括默認(rèn)配置蠢络,顯示一個(gè)所有 @ConfigurationProperties 的整理列版本 |
env | 獲取所有環(huán)境變量 |
flyway | 獲取已應(yīng)用的所有Flyway數(shù)據(jù)庫(kù)遷移信息衰猛,需要一個(gè)或多個(gè) Flyway Bean |
liquibase | 獲取已應(yīng)用的所有Liquibase數(shù)據(jù)庫(kù)遷移。需要一個(gè)或多個(gè) Liquibase Bean |
health | 獲取應(yīng)用程序健康指標(biāo)(運(yùn)行狀況信息) |
httptrace | 獲取HTTP跟蹤信息(默認(rèn)情況下刹孔,最近100個(gè)HTTP請(qǐng)求-響應(yīng)交換)啡省。需要 HttpTraceRepository Bean |
info | 獲取應(yīng)用程序信息 |
integrationgraph | 顯示 Spring Integration 圖。需要依賴 spring-integration-core |
loggers | 顯示和修改應(yīng)用程序中日志的配置 |
logfile | 返回日志文件的內(nèi)容(如果已設(shè)置logging.file.name或logging.file.path屬性) |
metrics | 獲取系統(tǒng)度量指標(biāo)信息 |
mappings | 顯示所有@RequestMapping路徑的整理列表 |
scheduledtasks | 顯示應(yīng)用程序中的計(jì)劃任務(wù) |
sessions | 允許從Spring Session支持的會(huì)話存儲(chǔ)中檢索和刪除用戶會(huì)話方库。需要使用Spring Session的基于Servlet的Web應(yīng)用程序 |
shutdown | 關(guān)閉應(yīng)用,要求endpoints.shutdown.enabled設(shè)置為true遂庄,默認(rèn)為 false |
threaddump | 獲取系統(tǒng)線程轉(zhuǎn)儲(chǔ)信息 |
heapdump | 返回hprof堆轉(zhuǎn)儲(chǔ)文件 |
jolokia | 通過(guò)HTTP公開(kāi)JMX bean(當(dāng)Jolokia在類路徑上時(shí)霹肝,不適用于WebFlux)苗沧。需要依賴 jolokia-core |
prometheus | 以Prometheus服務(wù)器可以抓取的格式公開(kāi)指標(biāo)嗤无。需要依賴 micrometer-registry-prometheus |
Actuator 使用及配置
快速使用
項(xiàng)目依賴
<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
#啟動(dòng)所有端點(diǎn)
management.endpoints.web.exposure.include=*
#自定義管理端點(diǎn)路徑
#management.endpoints.web.base-path=/manage
Spring Boot 2.X 中,Actuator 默認(rèn)只開(kāi)放 health 和 info 兩個(gè)端點(diǎn)。
添加management.endpoints.web.exposure.include=*
配置后啟動(dòng)應(yīng)用倍谜,訪問(wèn) http://127.0.0.1:8080/actuator 我們可以看到所有的 Actuator 端點(diǎn)列表。
如果將management.endpoints.enabled-by-default
設(shè)置為false,則禁用所有端點(diǎn),如需啟用則如下:
management.endpoints.enabled-by-default=false
management.endpoint.info.enabled=true
禁用的端點(diǎn)將從應(yīng)用程序上下文中完全刪除挣棕。如果只想更改公開(kāi)端點(diǎn)词身,使用include和exclude屬性拗馒。使用如下:
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans
management.endpoints.web.base-path=/manage
配置表示將 /actuator 路徑重定義為 /manage。
常用端點(diǎn)詳解
health
主要用來(lái)檢測(cè)應(yīng)用的運(yùn)行狀況涡相,是使用最多的一個(gè)監(jiān)控點(diǎn)丙号。監(jiān)控軟件通常使用該接口實(shí)時(shí)監(jiān)測(cè)應(yīng)用運(yùn)行狀況刺彩,在系統(tǒng)出現(xiàn)故障時(shí)把報(bào)警信息推送給相關(guān)人員,如磁盤空間使用情況摆出、數(shù)據(jù)庫(kù)和緩存等的一些健康指標(biāo)象踊。
默認(rèn)情況下 health 端點(diǎn)是開(kāi)放的泌射,訪問(wèn) http://127.0.0.1:8080/actuator/health 即可看到應(yīng)用運(yùn)行狀態(tài)。
{"status":"UP"}
如果需要看到詳細(xì)信息钾菊,則需要做添加配置:
management.endpoint.health.show-details=always
訪問(wèn)返回信息如下:
{"status":"UP","details":{"diskSpace":{"status":"UP","details":{"total":180002725888,"free":8687988736,"threshold":10485760}}}}
info
查看應(yīng)用信息是否在 application.properties 中配置朱监。如我們?cè)陧?xiàng)目中配置是:
info.app.name=Spring Boot Actuator Demo
info.app.version=v1.0.0
info.app.description=Spring Boot Actuator Demo
啟動(dòng)項(xiàng)目,訪問(wèn) http://127.0.0.1:8080/actuator/info 返回信息如下:
{"app":{"name":"Spring Boot Actuator Demo","version":"v1.0.0","description":"Spring Boot Actuator Demo"}}
env
通過(guò) env 可以獲取到所有關(guān)于當(dāng)前 Spring Boot 應(yīng)用程序的運(yùn)行環(huán)境信息脐区,如:操作系統(tǒng)信息(systemProperties)衰琐、環(huán)境變量信息、JDK 版本及 ClassPath 信息、當(dāng)前啟用的配置文件(activeProfiles)、propertySources淫奔、應(yīng)用程序配置信息(applicationConfig)等。
可以通過(guò) http://127.0.0.1:8080/actuator/env/{name} ,name表示想要查看的信息,可以獨(dú)立顯示前弯。
beans
訪問(wèn) 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
通過(guò) conditions 可以在應(yīng)用運(yùn)行時(shí)查看代碼了某個(gè)配置在什么條件下生效橱野,或者某個(gè)自動(dòng)配置為什么沒(méi)有生效。
訪問(wèn) 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"
}
],
...
}
}
}
}
loggers
獲取系統(tǒng)的日志信息毕谴。
訪問(wèn) 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"
},
...
}
}
mappings
查看所有 URL 映射成畦,即所有 @RequestMapping 路徑的整理列表。
訪問(wèn) 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
},
...
]
}
}
}
}
}
heapdump
訪問(wèn):http://127.0.0.1:8080/actuator/heapdump會(huì)自動(dòng)生成一個(gè) GZip 壓縮的 Jvm 的堆文件 heapdump,我們可以使用 JDK 自帶的 Jvm 監(jiān)控工具 VisualVM 打開(kāi)此文件查看涝开。如圖:
VisualVM下載:https://visualvm.github.io/download.html
threaddump
獲取系統(tǒng)線程的轉(zhuǎn)儲(chǔ)信息循帐,主要展示了線程名、線程ID舀武、線程的狀態(tài)拄养、是否等待鎖資源等信息。在工作中银舱,我們可以通過(guò)查看線程的情況來(lái)排查相關(guān)問(wèn)題瘪匿。
訪問(wèn) 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
},
...
]
}
shutdown
開(kāi)啟可以接口關(guān)閉 Spring Boot 應(yīng)用,要使用這個(gè)功能需要做如下配置:
management.endpoint.shutdown.enabled=true
可以通過(guò) post(僅支持 post) 請(qǐng)求訪問(wèn) http://127.0.0.1:8080/actuator/shutdown 關(guān)閉應(yīng)用寻馏。
metrics
訪問(wèn) http://127.0.0.1:8080/actuator/metrics 可以獲取系統(tǒng)度量指標(biāo)信息項(xiàng)如下:
{
"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"
]
}
對(duì)應(yīng)訪問(wèn) names 中的指標(biāo)棋弥,可以查看具體的指標(biāo)信息。如訪問(wèn) 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"
]
}
]
}
示例代碼
參考文檔
https://docs.spring.io/spring-boot/docs/2.2.1.RELEASE/reference/html/production-ready-features.html
非特殊說(shuō)明诚欠,本文版權(quán)歸 朝霧輕寒 所有顽染,轉(zhuǎn)載請(qǐng)注明出處.
原文標(biāo)題:Spring Boot 2.X(十六):應(yīng)用監(jiān)控之 Spring Boot Actuator 使用及配置