Springboot Actuator 可以帮助你监控和管理springboot应用,比如健康检查,统计,审计和HTTP追踪等,所有的这些特性都可以通过HTTP endpoints来获得。
Actuator同时还可以与外部应用监控系统整合,比如 Prometheus, Graphite, DataDog, Influx, Wavefront, New Relic等。这些系统提供了非常好的仪表盘、图标、分析和告警等功能,使得你可以通过统一的接口轻松的监控和管理你的应用。
Actuator使用Micrometer来整合上面提到的外部应用监控系统。这使得只要通过非常小的配置就可以集成任何应用监控系统。
我将通过自己敲得一部分代码来体现actuaor的魅力
首先在pom.xml中添加actuator的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
再来配置actuator的配置信息
management:
server:
point: 8888 #不设置的话是默认端口springboot的端口,现在是 8888
endpoints:
# 暴露 EndPoint 以供访问,有jmx和web两种方式,
jmx:
exposure:
exclude: '*' //!!!注意!!!如果是yml配置文件一定要是"*"或者'*'
//!!!注意!!!如果是.properties一定是*不能加引号或者双引号
include: '*'
web: #大部分应用是web应用
exposure:
# exclude: '*' exclude 的优先级高于 include,
include: ["health","info","beans","mappings","logfile","metrics","shutdown","env"] #或者 '*'
base-path: /actuator # 配置 Endpoint 的基础路径不配置是/actuator
path-mapping:
health: healthcheck #配置health 映射为/actuator/healthcheck 默认是/actuator/health
cors: # 配置跨域资源共享
allowed-origins: http://example.com
allowed-methods: GET,POST
enabled-by-default: true # 修改全局 endpoint 默认设置,如果开启false,
#表示全部端点禁用,需要在endpoint中配置需要开启的端点
endpoint:
auditevents: # 显示当前引用程序的审计事件信息,默认开启
enabled: true
cache:
time-to-live: 10s # 配置端点缓存响应的时间
beans: #显示一个应用中所有 Spring Beans 的完整列表,默认开启
enabled: true
conditions: # 显示配置类和自动配置类的状态及它们被应用和未被应用的原因,默认开启
enabled: true
configprops: # 显示一个所有@ConfigurationProperties的集合列表,默认开启
enabled: true
env: #显示来自Spring的 ConfigurableEnvironment的属性,默认开启
enabled: true
flyway: # 显示数据库迁移路径,如果有的话,默认开启
enabled: true
health: # 显示健康信息,默认开启
enabled: true
show-details: always #这个设置会显示所有health指标中的详细信息,
#不设置只显示UP或者DOWN状态
info: # 显示任意的应用信息,默认开启
enabled: true
liquibase: # 展示任何Liquibase数据库迁移路径,如果有的话,默认开启
enabled: true
metrics: # 展示当前应用的metrics信息,默认开启
enabled: true
mappings: # 显示一个所有@RequestMapping路径的集合列表,默认开启
enabled: true
scheduledtasks: #显示应用程序中的计划任务,默认开启
enabled: true
sessions: #允许从Spring会话支持的会话存储中检索和删除用户会话。
#使用Spring Session对反应性Web应用程序的支持时不可用。默认开启。
enabled: true
shutdown: # 允许应用以优雅的方式关闭,默认关闭
enabled: true
threaddump: # 执行一个线程dump
enabled: true
# web 应用时可以使用以下端点
heapdump: # 返回一个GZip压缩的hprof堆dump文件,默认开启
enabled: true
jolokia: # 通过HTTP暴露JMX beans(当Jolokia在类路径上时,WebFlux不可用),默认开启
enabled: true
logfile: # 返回日志文件内容(如果设置了logging.file或logging.path属性的话),
# 支持使用HTTP Range头接收日志文件内容的部分信息,默认开启
enabled: true
prometheus: # 以可以被Prometheus服务器抓取的格式显示metrics信息,默认开启
enabled: true
Endpoint映射默认前缀是/actuator,但是可以通过base-path 自定义。
每个endpoint光开启是不够的,还需要先通过jmx或者web去暴露他们,通过exclude(排除)和include(包含)属性配置,大多数的应用程序选择web(http)公开,大白话就是先要对endpoints进行一个统一的设置,先设置暴露(exposure)只有暴露了才可以在页面中访问,暴露了以后再去endpoint中进行单个端点进行设置,不暴露的话在端点中设置了也访问不了。
Endpoint ID | Description |
---|---|
auditevents | 显示应用暴露的审计事件 (比如认证进入、 订单失败) |
info | 显示应用基本信息 |
health | 显示应用健康状态 |
metrics | 显示应用多样的度量信息 |
loggers | 显示和修改配置的loggers |
logfile | 返回log file中的内容(如果logging.file或者logging.path被设置) |
httptrace | 显示HTTP足迹,最近100个HTTP request/repsponse |
env | 显示当前的环境特性 |
flyway | 显示数据库迁移路径的详细信息 |
liquidbase | 显示Liquibase 数据库迁移的纤细信息 |
shutdown | 让你逐步关闭应用 |
mappings | 显示所有的@RequestMapping路径 |
scheduledtasks | 显示应用中的调度任务 |
threaddump | 执行一个线程dump |
heapdump | 返回一个GZip压缩的JVM堆dump |
总结
Springboot Actuator:是用来为应用做 健康检查、审计、统计和监控