前言

  1. 一般JavaWeb应用都需要被管理以及监控,比如:查看应用所占的内存,还有查看应用的运行状态等等,这样做的目的就是对应用进行实时监控,避免应用出现问题了都无从查起。
  2. 我们都知道SpringBoot最牛的地方就是快速整合第三方技术,让我们能够快速开发,避免一些繁琐的配置。所有在SpringBoot中,也集成了监控功能的插件,就是SpringBoot Actuator

SpringBoot Actuator简介

  1. Actuator是SpringBoot项目中一个非常强大一个功能,有助于对应用程序进行监视和管理,通过restful api请求来监管、审计、收集应用的运行情况。
  2. Actuator的核心是端点Endpoint,它用来监视应用程序及交互,spring-boot-actuator中已经内置了非常多的Endpoint(health、info、beans、metrics、httptrace、shutdown等等),同时也允许我们自己扩展自己的Endpoints。
  3. 每个Endpoint都可以启用和禁用。要远程访问Endpoint,还必须通过JMX或HTTP进行暴露,大部分应用选择HTTP,Endpoint 的ID默认映射到一个带/actuator前缀的URL。例如,health端点默认映射到/actuator/health。

Endpoint端点介绍

Actuator的核心就是Endpoint,每一个Endpoint都代表着监控某一项数据。

端点

描述

auditevents

获取当前应用暴露的审计事件信息

beans

获取应用中所有的 Spring Beans 的完整关系列表

caches

获取公开可以用的缓存

conditions

获取自动配置条件信息,记录哪些自动配置条件通过和没通过的原因

configprops

获取所有配置属性,包括默认配置,显示一个所有 @ConfigurationProperties 的整理列版本

env

获取所有环境变量

flyway

获取已应用的所有Flyway数据库迁移信息,需要一个或多个 Flyway Bean

liquibase

获取已应用的所有Liquibase数据库迁移。需要一个或多个 Liquibase Bean

health

获取应用程序健康指标(运行状况信息)

httptrace

获取HTTP跟踪信息(默认情况下,最近100个HTTP请求-响应交换)。需要 HttpTraceRepository Bean

info

获取应用程序信息

integrationgraph

显示 Spring Integration 图。需要依赖 spring-integration-core

loggers

显示和修改应用程序中日志的配置

logfile

返回日志文件的内容(如果已设置或logging.file.path属性)

metrics

获取系统度量指标信息

mappings

显示所有@RequestMapping路径的整理列表

scheduledtasks

显示应用程序中的计划任务

sessions

允许从Spring Session支持的会话存储中检索和删除用户会话。需要使用Spring Session的基于Servlet的Web应用程序

shutdown

关闭应用,要求endpoints.shutdown.enabled设置为true,默认为 false

threaddump

获取系统线程转储信息

heapdump

返回hprof堆转储文件

jolokia

通过HTTP公开JMX bean(当Jolokia在类路径上时,不适用于WebFlux)。需要依赖 jolokia-core

prometheus

以Prometheus服务器可以抓取的格式公开指标。需要依赖 micrometer-registry-prometheus

SpringBoot Actuator使用及配置

  1. 上面介绍了Actuator以及Endpoint,接下来就开始使用它,只需要引入下面启动器依赖就可以使用Actuator了。
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- actuator -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  1. 在SpringBoot2.X中,Actuator 默认只开放 health 和 info 两个端点。我们先不做任何配置,直接启动SpringBoot项目,然后在浏览器输入:http://localhost:8080/actuator查看默认开放的端点,如下图所示:
  2. spring boot actuator不起作用 spring boot actuator配置_Endpoint

  3. 我们可以看到端点的访问地址都是以**/actuator**为前缀,这是SpringBoot默认设置的地址,我们可以在配置文件中修改,如下配置:
management:
  endpoints:
    web:
      base-path: /myActuator

spring boot actuator不起作用 spring boot actuator配置_Endpoint_02

  1. 恢复默认的访问路径,然后在浏览器上访问http://localhost:8080/actuator/health,如果出现UP表示应用程序正常运行。
  2. 接下来暴露所有的端点Endpoint,如下配置:
management:
	  endpoints:
	    web:
	      base-path: /actuator #配置端点访问前缀
	      exposure:
	        include: '*' #暴露所有端点

spring boot actuator不起作用 spring boot actuator配置_spring_03

  1. 当然也可以禁用所有端点,如下配置:
management:
  endpoints:
    web:
      base-path: /actuator #配置端点访问前缀
      exposure:
        include: '*' #暴露所有端点
    enabled-by-default: false #禁用所有端点

spring boot actuator不起作用 spring boot actuator配置_应用程序_04

  1. 关闭所有端点之后,可以指定暴露某个端点,如下配置:
management:
  endpoints:
    web:
      base-path: /actuator #配置端点访问前缀
      exposure:
        include: '*' #暴露所有端点
    enabled-by-default: false #禁用所有端点
  endpoint:
    info:
      enabled: true #上面关闭所有端点之后,又暴露指定的端点,这里暴露info端点
  1. 还可以指定不暴露某个端点,下面首先暴露所有端点,然后又指定不暴露:infobeansenv这三个端点。
management:
  endpoints:
    web:
      base-path: /actuator #配置端点访问前缀
      exposure:
        include: '*'  #暴露所有端点
        exclude: info,beans,env #在暴露所有端点的前提下,可以排除某个端点(不暴露)

详细介绍Endpoint

上面介绍了Actuator的使用,以及端点Endpoint的配置(暴露与不暴露),接下来再详细介绍常用的Endpoint。

health

  1. health主要用来检测应用的运行状况,是使用最多的一个监控点。
  2. 监控软件通常使用该接口实时监测应用运行状况,在系统出现故障时把报警信息推送给相关人员,如磁盘空间使用情况、数据库和缓存等的一些健康指标。
  3. 默认情况下health端点是开放的,访问http://127.0.0.1:8080/actuator/health即可看到应用运行状态。
  4. 如果应用程序正常时,默认访问会显示:{“status”:“UP”}。如果需要更详细的信息,需要配置如下内容:
management:
  endpoint:
    health:
      show-details: always

info

查看应用信息是否在配置文件中配置。如我们在项目中配置是:

info:
  app:
    name: SpringBoot Actuator
    version: v1.0.0
    description: this is a SpringBoot Actuator Project

spring boot actuator不起作用 spring boot actuator配置_应用程序_05

env

  1. 通过 env 可以获取到所有关于当前 SpringBoot 应用程序的运行环境信息,如:操作系统信息(systemProperties)、环境变量信息、JDK 版本及 ClassPath 信息、当前启用的配置文件(activeProfiles)、propertySources、应用程序配置信息(applicationConfig)等。
  2. 可以通过http://127.0.0.1:8080/actuator/env/{name},name表示想要查看的信息,可以独立显示。

beans

  1. beans显示应用程序启动之后,SpringIOC容器注册了哪些Bean。
  2. 主要展示了 bean 的别名、类型、是否单例、类的地址、依赖等信息。

总结

  1. 上面几种是比较常用的Endpoint,其他的Endpoint可以点击链接进行查看。附上官方说明文档:
    https://docs.spring.io/spring-boot/docs/2.2.1.RELEASE/reference/html/production-ready-features.html
  2. 此篇博客也为下面一篇博客打基础。
    SpringBoot2.X监控和管理神器:SpringBoot Admin
  3. 本篇博客源代码