第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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。