SpringBoot监控

231 阅读7分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

微服务的特点决定了功能模块的部署是分布式的,大部分功能模块都是运行在不同的机器上,彼此通过服务调用进行交互,前后台的业务流会经过很多个微服务的处理和传递,出现了异常如何快速定位是哪个环节出现了问题?在这种情况下,微服务的监控显得尤为重要。springboot作为微服务框架,除了它强大的快速开发功能外,还有就是它提供了actuator模块,引入该模块能够自动为springboot应用提供一系列用于监控的端点

Acturator

Actuator是spring boot的一个附加功能,可帮助你在应用程序生产环境时监视和管理应用程序。可以使用HTTP的各种请求来监管,审计,收集应用的运行情况。Spring Boot Actuator提供了对单个Spring Boot的监控,信息包含:应用状态、内存、线程、堆栈等等,比较全面的监控了SpringBoot应用的整个生命周期。特别对于微服务管理十分有意义。

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

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

  1. 应用配置类:可以查看应用在运行期的静态信息:例如自动配置信息、加载的 springbean信息、yml 文件配置信息、环境信息、请求映射信息;
  2. 度量指标类:主要是运行期的动态信息,例如堆栈、请求链、一些健康指标、metrics 信息等;
  3. 操作控制类:主要是指 shutdown,用户可以发送一个请求将应用的监控功能关闭。

Actuator 提供了 13 个接口,具体如下表所示。

HTTP方法路径描述
GET/auditevents显示应用暴露的审计事件
GET/beans描述应用程序上下文里全部的 Bean,以及它们的关系
GET/conditions就是 1.0 的 /autoconfig ,提供一份自动配置生效的条件情况,记录哪些自动配置条件通过了,哪些没通过
GET/configprops描述配置属性(包含默认值)如何注入Bean
GET/env获取全部环境属性
GET/env/{name}根据名称获取特定的环境属性值
GET/flyway提供一份 Flyway 数据库迁移信息
GET/liquidbase显示Liquibase 数据库迁移的纤细信息
GET/health报告应用程序的健康指标,这些值由 HealthIndicator 的实现类提供
GET/heapdumpdump 一份应用的 JVM 堆信息
GET/httptrace显示HTTP足迹,最近100个HTTP request/repsponse
GET/info获取应用程序的定制信息,这些信息由info打头的属性提供
GET/logfile返回log file中的内容(如果 logging.file 或者 logging.path 被设置)
GET/loggers显示和修改配置的loggers
GET/metrics报告各种应用程序度量信息,比如内存用量和HTTP请求计数
GET/metrics/{name}报告指定名称的应用程序度量值
GET/scheduledtasks展示应用中的定时任务信息
GET/sessions如果我们使用了 Spring Session 展示应用中的 HTTP sessions 信息
Post/shutdown关闭应用程序,要求endpoints.shutdown.enabled设置为true
GET/mappings描述全部的 URI路径,以及它们和控制器(包含Actuator端点)的映射关系
GET/threaddump获取线程活动的快照

使用Actuator

使用Actuator功能与springBoot使用其他功能一样简单,只需要在pom.xml中添加如下依赖:

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

为了保证 actuator 暴露的监控接口的安全性,需要添加安全控制的依赖spring-boot-startsecurity依赖,访问应用监控端点时,都需要输入验证信息。Security 依赖,可以选择不加,不进行安全管理。

配置文件

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

management.endpoints.web.exposure.include=*

#展示细节,除了always之外还有when-authorized、never,默认值是never
management.endpoint.health.show-details=always
#management.endpoints.web.base-path=/monitor

management.endpoint.shutdown.enabled=true

配置完成之后,启动项目就可以继续验证各个监控功能了。

属性详解

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

可以打开所有的监控点

management.endpoints.web.exposure.include=*

也可以选择打开部分

management.endpoints.web.exposure.include=beans,trace

Actuator 默认所有的监控点路径都在/actuator/* ,当然如果有需要这个路径也支持定制。设置完重启后,再次访问地址就会变成/manage/*

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

health

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

health 通过合并几个健康指数检查应用的健康情况。Spring Boot Actuator 有几个预定义的健康指标比如DataSourceHealthIndicator,DiskSpaceHealthIndicator,RedisHealthIndicator 等,它使用这些健康指标作为健康检查的一部分。

举个例子,如果你的应用使用 Redis, RedisHealthindicator 将被当作检查的一部分;如果使 用 MongoDB,那么MongoHealthIndicator 将被当作检查的一部分。

info

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

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

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

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

beans

根据示例就可以看出,展示了 bean 的别名、类型、是否单例、类的地址、依赖等信息。

[
    {
        "context": "application:8080:management",
        "parent": "application:8080",
        "beans": [
        {
            "bean": "embeddedServletContainerFactory",
            "aliases": [
            ],
            "scope": "singleton",
            "type":
            "org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletConta        inerFactory",
            "resource": "null",
            "dependencies": [
            ]
        },
        {
            "bean": "endpointWebMvcChildContextConfiguration",
            "aliases": [
            ],
            "scope": "singleton",
            "type":
            "org.springframework.boot.actuate.autoconfigure.EndpointWebMvcChildContextCo        nfiguration$$EnhancerBySpringCGLIB$$a4a10f9d",
            "resource": "null",
            "dependencies": [
            ]
        }
    }
]

conditions

Spring Boot 的自动配置功能非常便利,但有时候也意味着出问题比较难找出具体的原因。使用conditions 可以在应用运行时查看代码了某个配置在什么条件下生效,或者某个自动配置为什么没有生效。

heapdump

返回一个 GZip 压缩的 JVM 堆 dump

mappings

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

threaddump

/threaddump 接口会生成当前线程活动的快照。这个功能非常好,方便我们在日常定位问题的时候查看线程的情况。 主要展示了线程名、线程ID、线程的状态、是否等待锁资源等信息。

shutdown

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

management.endpoint.shutdown.enabled=true

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

Spring Boot Admin

Spring Boot Admin 是一个针对spring-boot的actuator接口进行UI美化封装的监控工具。他可以 返回在列表中浏览所有被监控spring-boot项目的基本信息比如:Spring容器管理的所有的bean、 详细的Health信息、内存信息、JVM信息、垃圾回收信息、各种配置信息(比如数据源、缓存列表 和命中率)等,Threads 线程管理,Environment 管理等

搭建Server端

pom.xml

<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>
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>

@EnableAdminServer

@EnableAdminServer
@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

搭建client端

pom.xml

<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>
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>2.1.0</version>
</dependency>

application.yml(代码复制格式不可用)

server:
    port: 8080
#自定义配置信息用于"/actuator/info"读取
info:
    name: 老王
    age: 100
    phone: 110
#通过下面的配置启用所有的监控端点,默认情况下,这些端点是禁用的;
management:
    endpoints:
    web:
        exposure:
            include: "*"
    endpoint:
        health:
            show-details: always
## 将Client作为服务注册到Server,通过Server来监听项目的运行情况
spring:
    boot:
        admin:
            client:
                url: http://localhost:8081
##application实例名
    application:
        name : spring-boot-admin-client

启动 client……几秒后刷新,可以看到 client 端已注册到 server。