本帖最后由 liupeng_hm 于 2019-8-26 16:44 编辑
应用没有监控,没有画板,一切指标都没有。在这个Growth Hack逐渐成为主流的时代,不弄个Dashboard把系统压力、QPS、CPU、内存、日活啥的可视化,你好意思出来混吗……
本节我们来解决该问题。 Spring Boot Actuator是Spring Boot官方提供的监控组件。只需为项目添加以下依赖,即可就整合Spring Boot Actuator。 监控端点Actuator为我们提供了很多监控端点,如下表所示。 [td]端点(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、OUTOFSERVICE、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后,我们来做一些测试: 访问http://localhost:8000/actuator/health ,即可获得如下结果: 测试2:/health端点展示详情为 /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的健康情况以及磁盘的健康情况。 测试3:暴露敏感路径默认情况下,敏感路径并不暴露。如需暴露(以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"] }
TIPS1 如需暴露所有监控端点可配置: management: endpoints: web: exposure: include: '*'
拓展阅读说明配套代码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
|