本章介紹SpringBoot監控管理
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 使用及配置
1、新建一個SpringBoot Web項,引入spring-boot-starter-actuator依賴
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-actuator</artifactId> 4 <optional>true</optional> 5 </dependency>
完整pom如下:

1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.test</groupId> 8 <artifactId>test-springboot-actuator</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 12 <parent> 13 <groupId>org.springframework.boot</groupId> 14 <artifactId>spring-boot-starter-parent</artifactId> 15 <version>2.1.8.RELEASE</version> 16 </parent> 17 18 <properties> 19 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 20 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 21 <java.version>1.8</java.version> 22 </properties> 23 24 <dependencies> 25 <dependency> 26 <groupId>org.springframework.boot</groupId> 27 <artifactId>spring-boot-starter-web</artifactId> 28 </dependency> 29 30 <dependency> 31 <groupId>org.springframework.boot</groupId> 32 <artifactId>spring-boot-starter-actuator</artifactId> 33 <optional>true</optional> 34 </dependency> 35 36 37 <dependency> 38 <groupId>org.springframework.boot</groupId> 39 <artifactId>spring-boot-starter-test</artifactId> 40 <scope>test</scope> 41 </dependency> 42 43 </dependencies> 44 45 46 <!-- SpringBoot打包插件,可以將代碼打包成一個可執行的jar包 --> 47 <build> 48 <plugins> 49 <plugin> 50 <groupId>org.springframework.boot</groupId> 51 <artifactId>spring-boot-maven-plugin</artifactId> 52 </plugin> 53 </plugins> 54 </build> 55 </project>
2、啟動項目,訪問地址 http://localhost:8080/actuator,獲取內容如下:
{ "_links": { "self": { "href": "http://localhost:8080/actuator", "templated": false }, "health": { "href": "http://localhost:8080/actuator/health", "templated": false }, "health-component": { "href": "http://localhost:8080/actuator/health/{component}", "templated": true }, "health-component-instance": { "href": "http://localhost:8080/actuator/health/{component}/{instance}", "templated": true }, "info": { "href": "http://localhost:8080/actuator/info", "templated": false } } }
可以看到,只有health和info是因為actuator默認只暴露了health和info端點
2、編輯application.properties配置文件,打開其他端點,以及配置管理端點的訪問路徑
1 # 默認端點可用 2 management.endpoints.enabled-by-default=true 3 # 通過HTTP公開所有的端點 4 management.endpoints.web.exposure.include=* 5 # 自定義管理端點路徑 默認是:/actuator 6 management.endpoints.web.base-path=/actuator
常用端點詳解
1、health
默認情況下 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":250790436864,"free":150934597632,"threshold":10485760}}}}
2、info
查看應用信息是否在 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"}}
3、env
通過 env 可以獲取到所有關於當前 Spring Boot 應用程序的運行環境信息,如:操作系統信息(systemProperties)、環境變量信息、JDK 版本及 ClassPath 信息、當前啟用的配置文件(activeProfiles)、propertySources、應用程序配置信息(applicationConfig)等。
可以通過 http://127.0.0.1:8080/actuator/env/{name} ,name表示想要查看的信息,可以獨立顯示。
4、beans
訪問 http://127.0.0.1:8080/actuator/beans 返回部分信息如下:
{ "contexts": { "application": { "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": [] }, ... }, "parentId": null } } }
5、conditions
通過 conditions 可以在應用運行時查看代碼了某個配置在什么條件下生效,或者某個自動配置為什么沒有生效。
{ "contexts": { "application": { "positiveMatches": {...}, "negativeMatches": {...}, "unconditionalClasses": [...] } } }
6、loggers
獲取系統的日志信息。
訪問 http://127.0.0.1:8080/actuator/loggers 返回部分信息如下:
{ "levels": [ "OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE" ], "loggers": { "ROOT": { "configuredLevel": "INFO", "effectiveLevel": "INFO" }, "com": { "configuredLevel": null, "effectiveLevel": "INFO" }, ... } }
7、mappings
查看所有 URL 映射,即所有 @RequestMapping 路徑的整理列表。
訪問 http://127.0.0.1:8080/actuator/mappings 返回部分信息如下:
8、heapdump
訪問:http://127.0.0.1:8080/actuator/heapdump會自動生成一個 GZip 壓縮的 Jvm 的堆文件 heapdump,我們可以使用 JDK 自帶的 Jvm 監控工具 VisualVM 打開此文件查看。
9、threaddump
獲取系統線程的轉儲信息,主要展示了線程名、線程ID、線程的狀態、是否等待鎖資源等信息。在工作中,我們可以通過查看線程的情況來排查相關問題。
訪問 http://127.0.0.1:8080/actuator/threaddump
10、metrics
訪問 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" ] }
11、shutdown
開啟可以接口關閉 Spring Boot 應用,要使用這個功能需要做如下配置:
1 # 開啟應用關閉端口 2 management.endpoint.shutdown.enabled=true 3 # 暴露shutdown的web端口 4 management.endpoints.web.exposure.include=shutdown
可以通過 post(僅支持 post) 請求訪問 http://127.0.0.1:8080/actuator/shutdown 關閉應用。
自定義HealthIndicator
1、自定義組件,實現HealthIndicator接口,並注入容器中,代碼如下:
1 // 注入Spring容器中 2 @Component 3 public class MyAppHealthIndicator implements HealthIndicator { 4 @Override 5 public Health health() { 6 // 自定義的檢查方法 7 Health build = Health.up().build(); 8 return build; 9 } 10 }
2、啟動項目,訪問 http://localhost:8080/actuator/health,效果如下:
3、修改組件代碼,如下:
1 // 注入Spring容器中 2 @Component 3 public class MyAppHealthIndicator implements HealthIndicator { 4 @Override 5 public Health health() { 6 // 自定義的檢查方法 7 // Health build = Health.up().build(); 8 return Health.down().withDetail("msg", "服務異常").build(); 9 } 10 }
4、重啟項目,效果如下: