Actuator 是 Spring Boot 提供的對應用系統的自省和監控功能。經過 Actuator,可使用數據化的指標去度量應用的運行狀況,好比查看服務器的磁盤、內存、CPU等信息,系統的線程、gc、運行狀態等等。html
Actuator 一般經過使用 HTTP 和 JMX 來管理和監控應用,大多數狀況使用 HTTP 的方式。git
端點 | 描述 |
---|---|
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 |
<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 兩個端點。github
添加management.endpoints.web.exposure.include=*
配置後啓動應用,訪問 http://127.0.0.1:8080/actuator 咱們能夠看到全部的 Actuator 端點列表。web
若是將management.endpoints.enabled-by-default
設置爲false,則禁用全部端點,如需啓用則以下:spring
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"}
若是須要看到詳細信息,則須要作添加配置:tomcat
management.endpoint.health.show-details=always
訪問返回信息以下:springboot
{"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/do...
獲取系統線程的轉儲信息,主要展現了線程名、線程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" ] } ] }
https://docs.spring.io/spring...
非特殊說明,本文版權歸 朝霧輕寒 全部,轉載請註明出處.
原文標題:Spring Boot 2.X(十六):應用監控之 Spring Boot Actuator 使用及配置
原文地址: https://www.zwqh.top/article/info/25
若是文章對您有幫助,請掃碼關注下個人公衆號,文章持續更新中...