在SpringBoot项目中,我们不仅需要关注业务功能的开发,更要时刻掌握应用的运行状态。想知道CPU使用率多少?内存是否告急?数据库连接是否正常?用户访问量有多大?这些都是运维过程中的重要指标。SpringBoot提供的Actuator功能就像是一个"健康助手",帮我们实时监控这些关键数据,让运维工作变得轻松自如。
快速入门Actuator
首先在项目中添加Actuator依赖:
ounter(lineounter(lineounter(lineounter(line
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
为了保护监控接口的安全,建议同时添加Security依赖:
ounter(lineounter(lineounter(lineounter(line
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
在application.yml中开启监控端点:
ounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(line
management:
endpoints:
web:
exposure:
include: "*" # 暴露所有端点
endpoint:
health:
show-details: always # 显示详细健康信息
监控端点详解
Actuator提供了丰富的监控端点,最常用的有:
/actuator/health
:应用健康状况/actuator/info
:应用信息/actuator/metrics
:性能指标/actuator/env
:环境配置/actuator/loggers
:日志配置
温馨提示:默认情况下,只有health和info端点是开放的,其他端点需要手动配置开启。
自定义健康检查
来看一个自定义健康检查的例子:
ounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(line
@Component
public class CustomHealthIndicator implements HealthIndicator {
@Override
public Health health() {
// 模拟检查某个关键服务
boolean serviceStatus = checkService();
if (serviceStatus) {
return Health.up()
.withDetail("message", "服务正常")
.withDetail("timestamp", System.currentTimeMillis())
.build();
} else {
return Health.down()
.withDetail("message", "服务异常")
.withDetail("timestamp", System.currentTimeMillis())
.build();
}
}
private boolean checkService() {
// 实际的服务检查逻辑
return true;
}
}
添加自定义指标
使用Micrometer API添加自定义计数器:
ounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(line
@Service
public class UserService {
private final Counter userLoginCounter;
public UserService(MeterRegistry registry) {
this.userLoginCounter = Counter.builder("app.user.logins")
.description("用户登录次数")
.register(registry);
}
public void userLogin(String username) {
// 处理登录逻辑
userLoginCounter.increment();
}
}
监控数据可视化
温馨提示:虽然Actuator提供的是JSON格式的数据,但我们可以使用Spring Boot Admin来实现可视化监控。
添加Spring Boot Admin依赖:
ounter(lineounter(lineounter(lineounter(lineounter(line
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.7.0</version>
</dependency>
在启动类上添加开启Admin服务器的注解:
ounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(line
@EnableAdminServer
@SpringBootApplication
public class MonitorApplication {
public static void main(String[] args) {
SpringApplication.run(MonitorApplication.class, args);
}
}
总结要点
Actuator是SpringBoot的"体检医生",提供了丰富的监控端点 使用前需要添加依赖并配置开启需要的端点 可以自定义健康检查逻辑和监控指标 建议配合Spring Boot Admin使用,实现可视化监控 注意保护监控接口的安全性,生产环境建议启用安全认证
通过Actuator,我们可以实时掌握应用的运行状态,及时发现和解决问题,让应用运行更加稳定可靠。希望这篇文章能帮助你更好地理解和使用SpringBoot的监控功能!