Spring Boot 项目健康检查工具Actuator
前言
公司经过野蛮生长阶段,系统稳定性,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
<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>
复制代码
检查点
- 应用启动了我们可以检查暴露的端口,通过http://localhost:8080/actuator来展示所有通过HTTP暴露的endpoints。
- 打开http://localhost:8080/actuator/health {"status":"UP"} 表示系统健康。
ID | Description |
---|---|
auditevents | Exposes audit events information for the current application. Requires an AuditEventRepository bean. |
beans | Displays a complete list of all the Spring beans in your application. |
caches | Exposes available caches. |
conditions | Shows the conditions that were evaluated on configuration and auto-configuration classes and the reasons why they did or did not match. |
configprops | Displays a collated list of all @ConfigurationProperties . |
env | Exposes properties from Spring’s ConfigurableEnvironment . |
flyway | Shows any Flyway database migrations that have been applied. Requires one or more Flyway beans. |
health | Shows application health information. |
httptrace | Displays HTTP trace information (by default, the last 100 HTTP request-response exchanges). Requires an HttpTraceRepository bean. |
info | Displays arbitrary application info. |
integrationgraph | Shows the Spring Integration graph. Requires a dependency on spring-integration-core . |
loggers | Shows and modifies the configuration of loggers in the application. |
liquibase | Shows any Liquibase database migrations that have been applied. Requires one or more Liquibase beans. |
metrics | Shows ‘metrics’ information for the current application. |
mappings | Displays a collated list of all @RequestMapping paths. |
quartz | Shows information about Quartz Scheduler jobs. |
scheduledtasks | Displays the scheduled tasks in your application. |
sessions | Allows retrieval and deletion of user sessions from a Spring Session-backed session store. Requires a Servlet-based web application using Spring Session. |
shutdown | Lets the application be gracefully shutdown. Disabled by default. |
startup | Shows the startup steps data collected by the ApplicationStartup . Requires the SpringApplication to be configured with a BufferingApplicationStartup . |
threaddump | Performs 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.");
}
}
复制代码
其他更高级的玩法可以参考一下下面的官方文档。
参考文档
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
链接:https://juejin.cn/post/7029671345172512775