# 使用 Spring Boot Actuator 监控应用

Spring Boot 是⼀个自带监控的开源框架,组件 Spring Boot Actuator 负责监控应用的各项静态和动态的变量。在项目中结合 Spring Boot Actuator 的使用,便可轻松对 Spring Boot 应用监控治理。

# Actuator 监控

只需要在项目中添加 spring-boot-starter-actuator,就自动启用了监控功能。

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

Spring Boot Actuator 是 Spring Boot 提供的对应用系统的检查和监控的集成功能,可以查看应用配置的详细信息,例如自动化配置信息、创建的 Spring beans 以及⼀些环境属性等。

Actuator 监控分成两类:原生端点和用户自定义端点。自定义端点主要是指扩展性,用户可以根据自己的实际应用,定义⼀些比较关心的指标,在运行期进行监控。

# Actuator 的 REST 接口

原生端点是在应用程序里提供众多 Web 接口,通过它们了解应用程序运行时的内部状况,原生端点又可以分成三类:

  • 应用配置类:可以查看应用在运行期的静态信息,例如自动配置信息、加载的 springbean 信息、yml 文件配置信息、环境信息、请求映射信息;

  • 度量指标类:主要是运行期的动态信息,如堆栈、请求连、⼀些健康指标、metrics 信息等;

  • 操作控制类:主要是指 shutdown,用户可以发送⼀个请求将应用的监控功能关闭。

Actuator 提供了十多个接口。

# 常见命令详解

在 Spring Boot 2.x 中为了安全期间,Actuator 只开放了两个端点 /actuator/health/actuator/info,可以在配置文件中设置打开其它。

可以打开所有的监控点:

management.endpoints.web.exposure.include=*

Actuator 默认所有的监控点路径都在 /actuator/*,当然如果你对 actuator 这个名字不满意,你也可以自定义:

management.endpoints.web.base-path=/manage

设置完重启后,再次访问地址就会变成 /manage/* ,不再是以前的 /actuator/*当然一般情况下不会动这个配置。

Actuator 几乎监控了应用涉及的方方面面,我们重点讲述⼀些经常在项目中常用的命令。

# health

health 主要用来检查应用的运行状态,这是我们使用最高频的⼀个监控点,通常使用此接口提醒我们应用实例的运行状态,以及应用不“健康”的原因,如数据库连接、磁盘空间不够等。

默认情况下 /actuator/health 的状态是开放的,添加依赖后启动项目,访问:http://127.0.0.1:8080/actuator/health (opens new window) 即可看到应用的状态。

{
    "status" : "UP"
}

默认情况下只是展示简单的 UPDOWN 状态,为了查询更详细的监控指标信息,可以在配置文件中添加以下信息:

management.endpoint.health.show-details=always

重启后再次访问网址 http://localhost:8080/actuator/health (opens new window),返回信息如下:

{
  "status": "UP",
  "diskSpace": {
    "status": "UP",
    "total": 209715195904,
    "free": 183253909504,
    "threshold": 10485760
  }
}

可以看到 HealthEndPoint 给我们提供默认的监控结果,包含磁盘空间描述总磁盘空间,剩余的磁盘空间和最小阈值。

# info

info 是我们自己在配置文件中以 info 开头的配置信息,比如在示例项目中的配置是:

info.app.name=spring-boot-actuator
info.app.version=1.0.0
info.app.test=test

启动示例项目,访问 http://localhost:8080/actuator/info (opens new window) 返回部分信息如下:

info
{
    "app": {
        "name": "spring-boot-actuator",
        "version": "1.0.0",
        "test":"test"
    }
}

# env

展示了系统环境变量的配置信息,包括使用的环境变量、JVM 属性、命令行参数、项目使用的 jar 包等信息。

启动示例项目,访问网址 http://localhost:8080/actuator/env (opens new window) 返回部分信息如下:

{
  "profiles": [

  ],
  "server.ports": {
    "local.management.port": 8088,
    "local.server.port": 8080
  },
  "servletContextInitParams": {

  },
  "systemProperties": {
    ...
  }

为了避免敏感信息暴露到 /actuator/env 里,所有名为 password、secret、key(或者名字中最后⼀段是这些)的属性在 /actuator/env 里都会加上 *

举个例⼦,如果有⼀个属性名字是 database.password,那么它在 /actuator/env 中的显示效果是这样的:

"database.password": "****"

# /env/{name} 用法

就是 env 的扩展可以获取指定配置信息,比如 http://localhost:8080/actuator/env/java.vm.version,返回 {"java.vm.version":"25.101-b13"}

# shutdown

开启接口优雅关闭 Spring Boot 应用,要使用这个功能首先需要在配置文件中开启:

management.endpoint.shutdown.enabled=true

配置完成之后,启动示例项目,使用 curl 模拟 post 请求访问 shutdown 接口。

shutdown 接口默认只支持 post 请求

curl -X POST "http://localhost:8080/actuator/shutdown"
{
 "message": "Shutting down, bye..."
}

此时会发现应用已经被关闭。

# mappings

描述全部的 URI 路径,以及它们和控制器的映射关系。

启动示例项目,访问网址 http://localhost:8080/actuator/mappings 返回部分信息如下:

{
  "/**/favicon.ico": {
    "bean": "faviconHandlerMapping"
  },
  "{[/hello]}": {
    "bean": "requestMappingHandlerMapping",
    "method": "public java.lang.String com.neo.controller.HelloController.index()"
  },
  "{[/error]}": {
    "bean": "requestMappingHandlerMapping",
    "method": "public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)"
  }
}