第2節(jié)( 跟我學(xué)Spring Cloud(Finchley版)-02-構(gòu)建分布式應(yīng)用 )說過:
創(chuàng)新互聯(lián)建站主要從事成都做網(wǎng)站、網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)汪清,10年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18982081108
應(yīng)用沒有監(jiān)控,沒有畫板,一切指標(biāo)都沒有。在這個Growth Hack逐漸成為主流的時代,不弄個Dashboard把系統(tǒng)壓力、QPS、CPU、內(nèi)存、日活啥的可視化,你好意思出來混嗎……
本節(jié)我們來解決該問題。
Spring Boot Actuator是Spring Boot官方提供的監(jiān)控組件。只需為項目添加以下依賴,即可就整合Spring Boot Actuator。
org.springframework.boot
spring-boot-starter-actuator
Actuator為我們提供了很多監(jiān)控端點,如下表所示。
端點(Spring Boot 2.x) | 描述 | HTTP方法 | 是否敏感 | 端點(Spring Boot 1.x) |
---|---|---|---|---|
conditions | 顯示自動配置的信息 | GET | 是 | autoconfig |
beans | 顯示應(yīng)用程序上下文所有的Spring bean | GET | 是 | beans |
configprops | 顯示所有@ConfigurationProperties的配置屬性列表 | GET | 是 | configprops |
dump | 顯示線程活動的快照 | GET | 是 | dump |
env | 顯示環(huán)境變量,包括系統(tǒng)環(huán)境變量以及應(yīng)用環(huán)境變量 | GET | 是 | env |
health | 顯示應(yīng)用程序的健康指標(biāo),值由HealthIndicator的實現(xiàn)類提供;結(jié)果有UP、 DOWN、OUT_OF_SERVICE、UNKNOWN;如需查看詳情,需配置:management.endpoint.health.show-details |
GET | 否 | health |
info | 顯示應(yīng)用的信息,可使用info.* 屬性自定義info端點公開的數(shù)據(jù) |
GET | 否 | info |
mappings | 顯示所有的URL路徑 | GET | 是 | mappings |
metrics | 顯示應(yīng)用的度量標(biāo)準(zhǔn)信息 | GET | 是 | metrics |
表-Spring Boot Actuator常用端點及描述
只需訪問http://{ip}:{port}/actuator/{endpoint}
端點,即可監(jiān)控應(yīng)用的運行狀況。
為前文編寫的microservice-simple-provider-user
服務(wù)整合Actuator后,我們來做一些測試:
訪問
{"status":"UP"}
為/health
端點配置顯示詳情:
management:
endpoint:
health:
# 是否展示健康檢查詳情
show-details: always
再次訪問
{
"status": "UP",
"details": {
"db": {
"status": "UP",
"details": {
"database": "H2",
"hello": 1
}
},
"diskSpace": {
"status": "UP",
"details": {
"total": 250790436864,
"free": 43443773440,
"threshold": 10485760
}
}
}
}
從中可以看到,/health
端點展示了DB的健康情況以及磁盤的健康情況。
默認(rèn)情況下,敏感路徑并不暴露。如需暴露(以metrics為例),需添加配置:
management:
endpoints:
web:
exposure:
# 暴露metrics端點,如需暴露多個,用,分隔;如需暴露所有端點,用'*'
include: 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}
列表如上,即可查看當(dāng)前應(yīng)用的度量指標(biāo)。例如訪問:
{
"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"]
}]
}
如需暴露所有監(jiān)控端點可配置:
management:
endpoints:
web:
exposure:
include: '*'
如果能對Actuator端點的文字?jǐn)?shù)據(jù)進(jìn)行圖形化的展示,我們就可以實現(xiàn)比較低層次的“Growth Hack”啦!開源界已經(jīng)有這樣的工具——Spring Boot Admin
,界面如下。有興趣的可前往
GitHub:
Gitee:
原文:http://www.itmuch.com/spring-cloud/finchley-3/ ,轉(zhuǎn)載請說明出處。