您好,登錄后才能下訂單哦!
第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}
端點,即可監控應用的運行狀況。
為前文編寫的microservice-simple-provider-user
服務整合Actuator后,我們來做一些測試:
訪問<http://localhost:8000/actuator/health> ,即可獲得如下結果:
{"status":"UP"}
為/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的健康情況以及磁盤的健康情況。
默認情況下,敏感路徑并不暴露。如需暴露(以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"]
}]
}
如需暴露所有監控端點可配置:
management:
endpoints:
web:
exposure:
include: '*'
如果能對Actuator端點的文字數據進行圖形化的展示,我們就可以實現比較低層次的“Growth Hack”啦!開源界已經有這樣的工具——Spring Boot Admin
,界面如下。有興趣的可前往<https://github.com/codecentric/spring-boot-admin>了解。
GitHub:
Gitee:
原文:http://www.itmuch.com/spring-cloud/finchley-3/ ,轉載請說明出處。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。