spring boot actuator监控和管理

本文介绍如何使用 Spring Boot Actuator 模块监控应用程序的状态,包括健康检查、磁盘空间监控、自定义健康指标等功能,并展示了如何通过 HTTP 访问这些监控信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring-boot-actuator模块提供了一个监控和管理生产环境的模块,可以使用http、jmx、ssh、telnet等拉管理和监控应用。审计(Auditing)、

健康(health)、数据采集(metrics gathering)会自动加入到应用里面。

首先,写一个最基本的spring boot项目。

基于Maven的项目添加‘starter’依赖:

1
2
3
4
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

以下是所有监控描述:



health

比如:http://localhost:8080/health  

你可以得到结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
status: "UP",
 
diskSpace: {
 
status: "UP",
 
total: 107374174208,
 
free: 14877962240,
 
threshold: 10485760
 
}
}


可以检查的其他一些情况的健康信息。下面的HealthIndicators会被Spring Boot自动配置:

1
2
3
4
5
6
7
8
9
10
11
DiskSpaceHealthIndicator     低磁盘空间检测
 
DataSourceHealthIndicator  检查是否能从DataSource获取连接
 
MongoHealthIndicator   检查一个Mongo数据库是否可用(up)
 
RabbitHealthIndicator   检查一个Rabbit服务器是否可用(up)
 
RedisHealthIndicator      检查一个Redis服务器是否可用(up)
 
SolrHealthIndicator  检查一个Solr服务器是否可用(up)

自定义当然也可以,你可以注册实现了HealthIndicator接口的Spring beans,Health响应需要包含一个status和可选的用于展示的详情。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
  
@Component
public class MyHealth implements HealthIndicator {
  
    @Override
    public Health health() {
        int errorCode = check(); // perform some specific health check
        if (errorCode != 0) {
        return Health.down().withDetail("Error Code", errorCode).build();
        }
        return Health.up().build();
    }
}


trace

访问http://localhost:8080/trace 可以看到结果,默认为最新的一些HTTP请求


info

当执行 http://localhost:8080/info  的时候,结果什么没有 

但是,在application.properties加入一些配置

1
2
3
4
5
info.app.name=ecs
info.app.version=1.0.0
  
info.build.artifactId=@project.artifactId@
info.build.version=@project.version@

执行/info就可以看到有些信息了。

/info 是用来在构建的时候,自动扩展属性的。对于Maven项目,可以通过 @..@ 占位符引用Maven的’project properties’

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值