跟我学SpringCloud(Finchley版)-03-监控:强大的BootActuator
第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}
端点,即可监控应用的运行状况。
测试1:/health端点
为前文编写的microservice-simple-provider-user
服务整合Actuator后,我们来做一些测试:
访问
{"status":"UP"}
测试2:/health端点展示详情
为/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的健康情况以及磁盘的健康情况。
测试3:暴露敏感路径
默认情况下,敏感路径并不暴露。如需暴露(以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"]
}]
}
TIPS
如需暴露所有监控端点可配置:
management: endpoints: web: exposure: include: '*'
- 有关Spring Boot 1.x与2.x端点的差异,详见:
拓展阅读
如果能对Actuator端点的文字数据进行图形化的展示,我们就可以实现比较低层次的“Growth Hack”啦!开源界已经有这样的工具——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/ ,转载请说明出处。
干货分享
文章名称:跟我学SpringCloud(Finchley版)-03-监控:强大的BootActuator
文章URL:http://azwzsj.com/article/iegheh.html