重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
第2节( 跟我学Spring Cloud(Finchley版)-02-构建分布式应用 )说过:
创新互联建站主要从事成都做网站、网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务汪清,10年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108
应用没有监控,没有画板,一切指标都没有。在这个Growth Hack逐渐成为主流的时代,不弄个Dashboard把系统压力、QPS、CPU、内存、日活啥的可视化,你好意思出来混吗……
本节我们来解决该问题。
Spring Boot Actuator是Spring Boot官方提供的监控组件。只需为项目添加以下依赖,即可就整合Spring Boot Actuator。
org.springframework.boot
spring-boot-starter-actuator
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后,我们来做一些测试:
访问
{"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的健康情况以及磁盘的健康情况。
默认情况下,敏感路径并不暴露。如需暴露(以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}
列表如上,即可查看当前应用的度量指标。例如访问:
{
"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
,界面如下。有兴趣的可前往
GitHub:
Gitee:
原文:http://www.itmuch.com/spring-cloud/finchley-3/ ,转载请说明出处。