介绍
在你发布系统到生产环境时,SpringBoot 提供了一些额外的特征帮助你监控、管理你的应用。
开始
首先添加依赖到自己的工程
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
actuator使用http来暴露地址时,只有info、health是默认打开的,一个地址(端点)在enabled且exposed情况下才被认为是可用的。
下面是可以配置的地址(端点),配置health可用访问时要使用:/actuator/health
ID | 描述 |
---|---|
info | 显示任意的程序信息 |
health | 显示应用的健康信息 |
beans | 显示应用里Spring beans的所有集合 |
mappings | 显示所有@RequestMapping路径的列表。 |
使用
使用yml配置可用的地址
server.port: 8080
management:
server:
port: 8080
address: 127.0.0.1
endpoint:
info:
enabled: true
health:
enabled: true
# 将错误的详细信息显示出来,包含自定义的详细信息
show-details: always
# 启用mappings功能
mappings:
enabled: true
endpoints:
web:
# 把需要的地址暴露出来,实际访问是http://localhost:8080/actuator/mappings
exposure:
include: health,info,mappings
health:
# 不用检查磁盘空间的健康
diskspace:
enabled: false
使用java自定义health的信息
@Component
public class MyHealthIndicator implements HealthIndicator {
@Override
public Health health() {
int errorCode = check(); // 执行健康检查
if (errorCode != 0) {
//错误情况下返回
return Health.down().withDetail("Error Code", errorCode).withDetail("Error Message","出错了").build();
}
return Health.up().build();
}
private int check() {
return 500;
}
}