Spring Boot - Actuator简介:

Spring Boot Actuator模块提供了一个监控和管理生产环境的模块,可以使用http、jmx、ssh、telnet等拉管理和监控应用。审计(Auditing)、 健康(health)、数据采集(metrics gathering)会自动加入到应用里面。

Spring Boot - Actuator的特点:

Spring Boot Actuator的关键特性是在应用程序里提供了众多的Web站点,通过他们可以来了解应用程序运行时的内部状况。Actuator为Spring Boot项目带来了许多有用的特性,包括:

  • 管理端点
  • 合理的异常处理以及默认的”/error”映射端点
  • 获取应用信息的”/info”端点
  • 当启用Spring Security时,会有一个审计事件框架

SpringBoot - Actuator 的端点:

Spring Boot Actuator提供了13个 端点,有了它,你可以知道 Bean 在 Spring应用上下文里是如何组装在一起的,掌握应用程序可以获取的环境属性信息,获取运行时度量信息的快照……

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

SpringBoot - Actuator的使用:

需要启动 Actuator 的端点,只需要在项目中引入Actuator的起步依赖即可。

在 Maven 中使用:

示例代码:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

在 Gradle 中使用:

示例代码:

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