spring boot 开启Actuator之旅

1,089 阅读2分钟

本文主要介绍如果想通过http的形式访问spring boot (1.x2.x\color{red}{1.x 和 2.x})的Actuator该进行哪些配置

spring boot 如何开启Actuator Web端点

引入Actuator依赖

如果springboot项目继承了spring-boot-starter-parent, 使用起步依赖, Actuator依赖可以不用加依赖版本号

compile "org.springframework.boot:spring-boot-starter-actuator"

否则需要加依赖版本号(和spring boot版本号保持一致)

compile "org.springframework.boot:spring-boot-starter-actuator:2.0.7.RELEASE"

开启web端点

spring boot 1.x

spring boot 1.x 的Actuator Web所有端点是默认开启的, 可以直接使用

spring boot 2.x

spring boot 2.x 的Actuator Web端点只有/health和/info默认开启, 如果还想访问其它端点, 需要在配置文件application.properties中配置开启

选择若干个要开启的端点⬇️

management.endpoints.web.exposure.include=["auditevents","health","info"]

开启全部端点⬇️

management.endpoints.web.exposure.include=*
  • 在yaml 文件属于关键字,所以需要加引号⬇️
management:
  endpoints:
    web:
      exposure:
        include: "*"   

如果想要enable /shutdown端点⬇️

management.endpoint.shutdown.enabled=true

如果想要暴露所有enable的web端点除了env⬇️

management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env

开始使用

spring boot 1.x

spring boot 1.x 通过postman的get方式访问http://ip:port/端点路径\color{red}{http://ip:port/端点路径} 的形式就可以直接访问一个运行中的spring boot项目

路径举例⬇️

http://127.0.0.1:8121/health

spring boot 2.x

spring boot 2.x 相对于之前, 在端点路径前新增了/actuator\color{red}{/actuator}路径

路径举例⬇️

http://127.0.0.1:8121/actuator/health

spring boot 1.x 常用端点路径如下⬇️

HTTP方法路径描述
GET/autoconfig提供了一份自动配置报告,记录哪些自动配置条件通过了,哪些没通过
GET/configprops描述配置属性(包含默认值)如何注入Bean
GET/beans描述应用程序上下文里全部的Bean,以及它们的关系
GET/dump获取线程活动的快照
GET/env获取全部环境属性
GET/env/{name}根据名称获取特定的环境属性值
GET/health报告应用程序的健康指标,这些值由HealthIndicator的实现类提供
GET/info获取应用程序的定制信息,这些信息由info打头的属性提供
GET/mappings描述全部的URI路径,以及它们和控制器(包含Actuator端点)的映射关系
GET/metrics报告各种应用程序度量信息,比如内存用量和HTTP请求计数
GET/metrics/{name}报告指定名称的应用程序度量值
POST/shutdown关闭应用程序,要求endpoints.shutdown.enabled设置为true
GET/trace提供基本的HTTP请求跟踪信息(时间戳、HTTP头等)

spring boot 2.x 端点路径变化如下⬇️

1.x 端点2.x 端点(改变)
/autoconfig重命名为 /conditions
/health现在有一个 management.endpoint.health.show-details 选项 never, always, when-authenticated,而不是依靠 sensitive 标志来确定 health 端点是否必须显示全部细节。 默认情况下,/actuator/health公开并且不显示细节。
/trace重命名为 /httptrace