Spring Boot 项目健康检查工具Actuator

1,141 阅读3分钟

这是我参与11月更文挑战的第12天,活动详情查看:2021最后一次更文挑战

前言

公司经过野蛮生长阶段,系统稳定性,CI,CD持续集成交付,基于Spring Cloud 微服务系统使用Spring Boot健康检查工具,提高发布的效率和全方位监控。在没有监控之前,比如说一个Feign微服务项目,我们可能在SpringBoot项目启动之后通过一个Rest接口比如说 127.0.0.1:8888/ok 这个种接口的形式,集成在自动化脚本里面去校验发布时候正常,或者人工去做一些校验。

其实这些都可以通过 Actuator 工具包去做。

官方文档: docs.spring.io/spring-boot…

Actuator 使用

在Spring boot应用中,要实现可监控的功能,依赖的是 spring-boot-starter-actuator 这个组件。它提供了很多监控和管理你的spring boot应用的HTTP或者JMX端点,并且你可以有选择地开启和关闭部分功能。当你的spring boot应用中引入下面的依赖之后,将自动的拥有审计、健康检查、Metrics监控功能。

添加依赖

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

创建一个spring web工程

初始化工程的时候勾选一些常用的工具类,比如lombok和actuator

image.png

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

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

检查点

  1. 应用启动了我们可以检查暴露的端口,通过http://localhost:8080/actuator来展示所有通过HTTP暴露的endpoints。
  2. 打开http://localhost:8080/actuator/health {"status":"UP"} 表示系统健康。
IDDescription
auditeventsExposes audit events information for the current application. Requires an AuditEventRepository bean.
beansDisplays a complete list of all the Spring beans in your application.
cachesExposes available caches.
conditionsShows the conditions that were evaluated on configuration and auto-configuration classes and the reasons why they did or did not match.
configpropsDisplays a collated list of all @ConfigurationProperties.
envExposes properties from Spring’s ConfigurableEnvironment.
flywayShows any Flyway database migrations that have been applied. Requires one or more Flyway beans.
healthShows application health information.
httptraceDisplays HTTP trace information (by default, the last 100 HTTP request-response exchanges). Requires an HttpTraceRepository bean.
infoDisplays arbitrary application info.
integrationgraphShows the Spring Integration graph. Requires a dependency on spring-integration-core.
loggersShows and modifies the configuration of loggers in the application.
liquibaseShows any Liquibase database migrations that have been applied. Requires one or more Liquibase beans.
metricsShows ‘metrics’ information for the current application.
mappingsDisplays a collated list of all @RequestMapping paths.
quartzShows information about Quartz Scheduler jobs.
scheduledtasksDisplays the scheduled tasks in your application.
sessionsAllows retrieval and deletion of user sessions from a Spring Session-backed session store. Requires a Servlet-based web application using Spring Session.
shutdownLets the application be gracefully shutdown. Disabled by default.
startupShows the startup steps data collected by the ApplicationStartup. Requires the SpringApplication to be configured with a BufferingApplicationStartup.
threaddumpPerforms a thread dump.

自定义健康指标

可以通过实现HealthIndicator接口来自定义一个健康指标,或者继承AbstractHealthIndicator类。 application.properties 添加参数

management.endpoint.health.show-details=always
@Component
public class MyHealthIndicator extends AbstractHealthIndicator {
    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        builder.up()
                .withDetail("app", "Alive and Kicking")
                .withDetail("error", "Nothing! I'm ok.");
    }
}

image.png

其他更高级的玩法可以参考一下下面的官方文档。

参考文档

Spring Boot Actuator: Production-ready features
Micrometer: Spring Boot 2’s new application metrics collector
Spring Boot Actuator: Health check, Auditing, Metrics gathering and Monitoring