Spring Boot Actuator 是 Spring Boot 提供的一个强大的子项目,它为应用程序提供了丰富的生产级功能,帮助开发者监控和管理应用。
Spring Boot Actuator 是一个用于监控和管理 Spring Boot 应用的模块,它通过 HTTP 端点或 JMX 暴露了一系列内置的端点(endpoints),可以让你了解应用程序的运行状况、性能指标、配置信息等。
端点ID | 默认HTTP路径 | 描述 | 默认启用 |
---|---|---|---|
health |
/actuator/health |
应用健康状态 | 是 |
info |
/actuator/info |
应用自定义信息 | 是 |
metrics |
/actuator/metrics |
应用指标数据 | 是 |
env |
/actuator/env |
环境变量和配置属性 | 否 |
mappings |
/actuator/mappings |
所有@RequestMapping路径 | 否 |
loggers |
/actuator/loggers |
查看和修改日志级别 | 否 |
threaddump |
/actuator/threaddump |
线程转储 | 否 |
// 自定义健康指示器示例
@Component
public class CustomHealthIndicator implements HealthIndicator {
@Override
public Health health() {
// 自定义健康检查逻辑
boolean isHealthy = checkSystem();
return isHealthy
? Health.up().withDetail("message", "服务正常").build()
: Health.down().withDetail("error", "服务异常").build();
}
}
# 安全配置示例
management.endpoints.web.exposure.include=health,info,metrics
management.endpoint.health.show-details=when_authorized
spring.security.user.name=admin
spring.security.user.password=secret
特点:
配置示例:
management.endpoint.health.show-details=always
management.health.db.enabled=true
management.health.redis.enabled=true
支持的指标类型:
自定义指标示例:
@Service
public class OrderService {
private final Counter orderCounter;
public OrderService(MeterRegistry registry) {
this.orderCounter = registry.counter("orders.count");
}
public void createOrder() {
// 业务逻辑
orderCounter.increment();
}
}
可查看信息:
功能:
# 修改日志级别示例
POST /actuator/loggers/com.example
{
"configuredLevel": "DEBUG"
}
// 自定义端点示例
@Endpoint(id = "features")
@Component
public class FeaturesEndpoint {
@ReadOperation
public Map<String, Object> features() {
return Map.of(
"feature1", true,
"feature2", false
);
}
@WriteOperation
public void configureFeature(@Selector String name, boolean enabled) {
// 实现特性开关逻辑
}
}
支持将指标数据导出到:
Prometheus配置示例:
<dependency>
<groupId>io.micrometergroupId>
<artifactId>micrometer-registry-prometheusartifactId>
dependency>
management.endpoints.web.exposure.include=prometheus,metrics,health
// 记录审计事件
@Autowired
private AuditEventRepository auditEventRepository;
public void login(String username) {
auditEventRepository.add(new AuditEvent(
username,
"AUTHENTICATION_SUCCESS",
Map.of("ip", getClientIp())
));
}
安全防护:
端点定制:
# 自定义端点路径和端口
management.server.port=9001
management.endpoints.web.base-path=/manage
management.endpoints.web.path-mapping.health=status
// 集成第三方系统健康检查
@Component
public class RedisHealthIndicator implements HealthIndicator {
@Autowired
private RedisTemplate redisTemplate;
@Override
public Health health() {
try {
redisTemplate.getConnectionFactory().getConnection().ping();
return Health.up().build();
} catch (Exception e) {
return Health.down().withException(e).build();
}
}
}
特性 | Spring Actuator | 传统监控方案 |
---|---|---|
集成难度 | 与Spring Boot无缝集成 | 需要额外配置 |
功能全面性 | 应用层面监控 | 系统层面监控为主 |
自定义能力 | 高度可定制 | 通常固定 |
实时性 | 实时数据 | 可能有延迟 |
学习曲线 | 对Spring开发者友好 | 需要学习专用查询语言 |
# K8s部署配置示例
livenessProbe:
httpGet:
path: /actuator/health
port: 8080
initialDelaySeconds: 60
periodSeconds: 10
readinessProbe:
httpGet:
path: /actuator/health
port: 8080
initialDelaySeconds: 30
periodSeconds: 5
# 部署后健康检查
curl -s http://app:8080/actuator/health | grep -q '"status":"UP"'
// 基于自定义指标扩缩容
@Endpoint(id = "traffic")
@Component
public class TrafficEndpoint {
private AtomicInteger requests = new AtomicInteger();
@ReadOperation
public int currentTraffic() {
return requests.get();
}
public void increment() {
requests.incrementAndGet();
}
}
Spring Boot Actuator的核心价值:
通过合理配置和使用Actuator,可以显著提高Spring Boot应用在生产环境中的可维护性和可靠性,是构建健壮企业级应用的重要工具。