跟我學Spring Cloud(Finchley版)-03-監控:強大的Spring Boot Actuator
第2節( 跟我學Spring Cloud(Finchley版)-02-構建分散式應用 )說過:
應用沒有監控,沒有畫板,一切指標都沒有。在這個Growth Hack逐漸成為主流的時代,不弄個Dashboard把系統壓力、QPS、CPU、記憶體、日活啥的視覺化,你好意思出來混嗎……
本節我們來解決該問題。
Spring Boot Actuator是Spring Boot官方提供的監控元件。只需為專案新增以下依賴,即可就整合Spring Boot Actuator。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
監控端點
Actuator為我們提供了很多監控端點,如下表所示。
端點(Spring Boot 2.x) | 描述 | HTTP方法 | 是否敏感 | 端點(Spring Boot 1.x) |
---|---|---|---|---|
conditions | 顯示自動配置的資訊 | GET | 是 | autoconfig |
beans | 顯示應用程式上下文所有的Spring bean | GET | 是 | beans |
configprops | 顯示所有@ConfigurationProperties的配置屬性列表 | GET | 是 | configprops |
dump | 顯示執行緒活動的快照 | GET | 是 | dump |
env | 顯示環境變數,包括系統環境變數以及應用環境變數 | GET | 是 | env |
health | 顯示應用程式的健康指標,值由HealthIndicator的實現類提供;結果有UP、 DOWN、OUT_OF_SERVICE、UNKNOWN;如需檢視詳情,需配置:management.endpoint.health.show-details
|
GET | 否 | health |
info | 顯示應用的資訊,可使用info.* 屬性自定義info端點公開的資料 |
GET | 否 | info |
mappings | 顯示所有的URL路徑 | GET | 是 | mappings |
metrics | 顯示應用的度量標準資訊 | GET | 是 | metrics |
表-Spring Boot Actuator常用端點及描述
只需訪問http://{ip}:{port}/actuator/{endpoint}
端點,即可監控應用的執行狀況。
測試1:/health端點
為前文編寫的microservice-simple-provider-user
服務整合Actuator後,我們來做一些測試:
訪問http://localhost:8000/actuator/health ,即可獲得如下結果:
{"status":"UP"}
測試2:/health端點展示詳情
為/health
端點配置顯示詳情:
management:
endpoint:
health:
# 是否展示健康檢查詳情
show-details: always
再次訪問http://localhost:8000/actuator/health ,即可獲得如下結果:
{
"status": "UP",
"details": {
"db": {
"status": "UP",
"details": {
"database": "H2",
"hello": 1
}
},
"diskSpace": {
"status": "UP",
"details": {
"total": 250790436864,
"free": 43443773440,
"threshold": 10485760
}
}
}
}
從中可以看到,/health
端點展示了DB的健康情況以及磁碟的健康情況。
測試3:暴露敏感路徑
預設情況下,敏感路徑並不暴露。如需暴露(以metrics為例),需新增配置:
management:
endpoints:
web:
exposure:
# 暴露metrics端點,如需暴露多個,用,分隔;如需暴露所有端點,用'*'
include: metrics
訪問:http://localhost:8000/actuator/metrics ,可獲得類似如下的結果:
{
"names": ["jvm.memory.max", "http.server.requests", "jdbc.connections.active", "process.files.max", "jvm.gc.memory.promoted", "tomcat.cache.hit", "system.load.average.1m", "tomcat.cache.access", "jvm.memory.used", "jvm.gc.max.data.size", "jdbc.connections.max", "jdbc.connections.min", "jvm.gc.pause", "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", "hikaricp.connections.idle", "hikaricp.connections.pending", "tomcat.global.request", "tomcat.sessions.expired", "hikaricp.connections", "jvm.threads.live", "jvm.threads.peak", "tomcat.global.received", "hikaricp.connections.active", "hikaricp.connections.creation", "process.uptime", "tomcat.sessions.rejected", "process.cpu.usage", "tomcat.threads.config.max", "jvm.classes.loaded", "hikaricp.connections.max", "hikaricp.connections.min", "jvm.classes.unloaded", "tomcat.global.error", "tomcat.sessions.active.current", "tomcat.sessions.alive.max", "jvm.gc.live.data.size", "tomcat.servlet.request.max", "hikaricp.connections.usage", "tomcat.threads.current", "tomcat.servlet.request", "hikaricp.connections.timeout", "process.files.open", "jvm.buffer.count", "jvm.buffer.total.capacity", "tomcat.sessions.active.max", "hikaricp.connections.acquire", "tomcat.threads.busy", "process.start.time", "tomcat.servlet.error"]
}
訪問http://localhost:8000/actuator/metrics/{name} ,{name}
列表如上,即可檢視當前應用的度量指標。例如訪問:http://localhost:8000/actuator/metrics/jvm.memory.max 即可檢視JVM可管理的最大記憶體,結果類似如下:
{
"name": "jvm.memory.max",
"description": "The maximum amount of memory in bytes that can be used for memory management",
"baseUnit": "bytes",
"measurements": [{
"statistic": "VALUE",
"value": 5.597298687E9
}],
"availableTags": [{
"tag": "area",
"values": ["heap", "nonheap"]
}, {
"tag": "id",
"values": ["Compressed Class Space", "PS Survivor Space", "PS Old Gen", "Metaspace", "PS Eden Space", "Code Cache"]
}]
}
TIPS
-
如需暴露所有監控端點可配置:
management: endpoints: web: exposure: include: '*'
有關Spring Boot 1.x與2.x端點的差異,詳見:https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide#endpoints
擴充閱讀
如果能對Actuator端點的文字資料進行圖形化的展示,我們就可以實現比較低層次的“Growth Hack”啦!開源界已經有這樣的工具——Spring Boot Admin
,介面如下。有興趣的可前往https://github.com/codecentric/spring-boot-admin瞭解。
說明
- 由於Actuator本身是Spring Boot中的元件,並不是本套教程的重點(其實筆者本不想寫這一節,但後面又會持續用這些端點,並且Spring Cloud在這些端點的基礎上還做了一些增加,所以還是有必要介紹一下),因此本節只是對Actuator進行了比較簡單的介紹,讀者可自行挖掘Actuator的其他能力。也可持續關注本公眾號,本系列完成後,筆者將會扒開Actuator的底褲,深度介紹Spring Boot監控的那些事兒。
配套程式碼
GitHub:
- https://github.com/eacdy/spring-cloud-study/tree/master/2018-Finchley/microservice-simple-provider-user
- https://github.com/eacdy/spring-cloud-study/tree/master/2018-Finchley/microservice-simple-consumer-movie
Gitee:
- https://gitee.com/itmuch/spring-cloud-study/tree/master/2018-Finchley/microservice-simple-provider-user
- https://gitee.com/itmuch/spring-cloud-study/tree/master/2018-Finchley/microservice-simple-consumer-movie
本文首發
http://www.itmuch.com/spring-cloud/finchley-3/
乾貨分享
相關文章
- 跟我學Spring Cloud(Finchley版)-08-Ribbon深入SpringCloud
- 跟我學Spring Cloud(Finchley版)-06-服務註冊與SpringCloud
- spring-boot-actuatorSpringboot
- Spring Cloud實戰系列(八) - 微服務監控Spring Boot AdminCloud微服務Spring Boot
- Prometheus+Grafana實現服務效能監控:windows主機監控、Spring Boot監控、Spring Cloud Alibaba Seata監控PrometheusGrafanaWindowsSpring BootCloud
- Spring Boot Actuator 整合 PrometheusSpring BootPrometheus
- Spring Cloud Config 分散式配置中心【Finchley 版】SpringCloud分散式
- springcloud學習筆記(五)Spring Cloud ActuatorSpringGCCloud筆記
- Spring Boot Actuator詳解與深入應用(三):Prometheus+Grafana應用監控Spring BootPrometheusGrafana
- springMVC中增加spring-boot-actuatorSpringMVCboot
- spring-boot-route(十八)spring-boot-adtuator監控應用Springboot
- spring-boot-route(十九)spring-boot-admin監控服務Springboot
- spring-boot-admin對spring-boot專案進行監控Springboot
- Spring Boot應用監控實戰Spring Boot
- Spring Boot中使用Prometheus監控教程Spring BootPrometheus
- Spring Cloud構建微服務架構-spring cloud服務監控中心SpringCloud微服務架構
- Spring Cloud服務框架版本升級--JDK10+Gradle4.9+Spring Boot 2.0+Finchley.SR1Cloud框架JDKGradleSpring Boot
- Spring Boot Admin2.1應用監控Spring Boot
- Spring Boot整合Spring Cloud Netflix元件Spring BootCloud元件
- springboot 07 Spring Boot Actuator: Production-ready featuresSpring Boot
- Spring Cloud Finchley版中Consul多例項註冊的問題處理SpringCloud
- 跟我學SpringCloud | 第十二篇:Spring Cloud Gateway初探SpringGCCloudGateway
- Spring Boot Actuator詳解與深入應用(二):Actuator 2.xSpring Boot
- Spring Boot Actuator詳解與深入應用(一):Actuator 1.xSpring Boot
- 升級Spring Cloud到Finchley後的一點坑SpringCloud
- spring-boot-plus1.1.0.釋出-整合Spring Boot Admin管理和監控應用Spring Boot
- spring cloud Alibaba 之 spring boot 基礎學習筆記CloudSpring Boot筆記
- Spring Cloud Sleuth鏈路監控應用(十四)SpringCloud
- 使用Prometheus和Grafana監控Spring Boot應用PrometheusGrafanaSpring Boot
- Spring Boot中如何使用Ostara監控應用?Spring Boot
- 備忘錄二:Spring Boot Actuator+Prometheus+GrafanaSpring BootPrometheusGrafana
- 系統健康檢查利器-Spring Boot-ActuatorSpring Boot
- Trampoline視覺化監控你的Spring Boot微服務視覺化Spring Boot微服務
- Spring Cloud與Spring Boot版本匹配關係CloudSpring Boot
- Spring Boot系列(四) Spring Cloud 之 Config ClientSpring BootCloudclient
- 跟我學SpringCloud | 第六篇:Spring Cloud Config Github配置中心SpringGCCloudGithub
- Spring Cloud Alibaba與Spring Boot、Spring Cloud之間不得不說的版本關係CloudSpring Boot
- Spring Boot透過Actuator顯示git和build的資訊Spring BootGitUI