微服务学习笔记(三)— Spring Boot 运行状态监控 Actuator
Spring Boot有四大神器,分别是auto-configuration、starters、cli、actuator,本文主要讲actuator。
actuator是spring boot提供的对应用系统的自省和监控的集成功能,可以对应用系统进行配置查看、相关功能统计等。 Actuator 的监控数据可以通过阻REST 、远程shell 和JMX方式获得。本文只介绍REST方式。
本文基于spring boot 2.0 以上版本,相对较低版本actuator变化较大,请读者注意。
1、开启Actuator
在pom文件中引入Actuator的起步依赖spring-boot-starter-actuator.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <scope>test</scope> </dependency>
2、修改配置application.yml文件
这两个配置分别配置了Actuator 对外暴露REST API 接口的端口号和Actuator 采取非安全验证方式
Spring Boot Actuator 的关键特性是在应用程序里提供众多的Web 节点,通过这些节点可以实时地了解应用程序的运行状况。有了Actuator ,你可以知道Bean 在Spring 应用程序上下文里是如何组装在一起的,并且可以获取环境属性的信息和运行时度量信息等。
spring-boot 2.0 版本当中,作为安全性考虑,将actuator 控件中的端口,只默认开放/health 和/info 两个端口,其他端口默认关闭。当你需要使用气短监控端口时,需要手动在application.properties 文件中添加配置。配置形式为 management.endpoints.web.exposure.include=xxx 开启xxx端口的访问 , 如果去掉所有的端口访问限制,则配置形式为 management.endpoints.web.exposure.include=*
spring-boot 2.0 版本当中还有一个变动,所有的端口 默认访问形式都是 actuator/XXX 例如actuator/health
3、启动应用程序
4、访问测试: localhost:9002/autuator
5、监控端点说明
ID | 描述 | 默认启用 |
---|---|---|
auditevents | 显示当前应用程序的审计事件信息 | Yes |
beans | 显示一个应用中所有Spring Beans 的完整列表 |
Yes |
conditions | 显示配置类和自动配置类 (configuration and auto-configuration classes)的状态及它们被应用或未被应用的原因 |
Yes |
configprops | 显示一个所有@ConfigurationProperties 的集合列表 |
Yes |
env | 显示来自Spring的 ConfigurableEnvironment 的属性 |
Yes |
flyway | 显示数据库迁移路径,如果有的话 | Yes |
health | 显示应用的健康信息 (当使用一个未认证连接访问时显示一个简单的’status’,使用认证连接访问则显示全部信息详情) |
Yes |
info | 显示任意的应用信息
|
Yes |
liquibase | 展示任何Liquibase数据库迁移路径,如果有的话 | Yes |
metrics | 展示当前应用的metrics 信息 |
Yes |
mappings | 显示一个所有@RequestMapping 路径的集合列表 |
Yes |
scheduledtasks | 显示应用程序中的计划任务
|
Yes |
sessions | 允许从Spring会话支持的会话存储中检索和删除(retrieval and deletion)用户会话。使用Spring Session对反应性Web应用程序的支持时不可用。 | Yes |
shutdown | 允许应用以优雅的方式关闭(默认情况下不启用) | No |
threaddump | 执行一个线程dump | Yes |
推荐中文翻译详细说明
https://blog.****.net/alinyua/article/details/80009435
https://www.cnblogs.com/ityouknow/p/8423590.html
(本章结束)