引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
在配置文件中加入
management.endpoints.web.exposure.include=*
management.endpoint.health.enabled=true
management.endpoint.health.show-details=always
可以通过访问 服务名:端口号/actuator
{
_links: {
self: {
href: "http://localhost:8080/actuator",
templated: false
},
beans: {
href: "http://localhost:8080/actuator/beans",
templated: false
},
caches: {
href: "http://localhost:8080/actuator/caches",
templated: false
},
caches-cache: {
href: "http://localhost:8080/actuator/caches/{cache}",
templated: true
},
health: {
href: "http://localhost:8080/actuator/health",
templated: false
},
health-path: {
href: "http://localhost:8080/actuator/health/{*path}",
templated: true
},
info: {
href: "http://localhost:8080/actuator/info",
templated: false
},
conditions: {
href: "http://localhost:8080/actuator/conditions",
templated: false
},
configprops: {
href: "http://localhost:8080/actuator/configprops",
templated: false
},
configprops-prefix: {
href: "http://localhost:8080/actuator/configprops/{prefix}",
templated: true
},
env-toMatch: {
href: "http://localhost:8080/actuator/env/{toMatch}",
templated: true
},
env: {
href: "http://localhost:8080/actuator/env",
templated: false
},
loggers-name: {
href: "http://localhost:8080/actuator/loggers/{name}",
templated: true
},
loggers: {
href: "http://localhost:8080/actuator/loggers",
templated: false
},
heapdump: {
href: "http://localhost:8080/actuator/heapdump",
templated: false
},
threaddump: {
href: "http://localhost:8080/actuator/threaddump",
templated: false
},
prometheus: {
href: "http://localhost:8080/actuator/prometheus",
templated: false
},
metrics-requiredMetricName: {
href: "http://localhost:8080/actuator/metrics/{requiredMetricName}",
templated: true
},
metrics: {
href: "http://localhost:8080/actuator/metrics",
templated: false
},
scheduledtasks: {
href: "http://localhost:8080/actuator/scheduledtasks",
templated: false
},
mappings: {
href: "http://localhost:8080/actuator/mappings",
templated: false
}
}
}
需要实现自定义健康检查断点,需要引入依赖
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
定义一个名为MyHealth的actuator
/**
* 1. 通过实现HealthIndicator接口来定制健康状态对象(Health)返回
* 2. 继承AbstractHealthIndicator
*/
@Component
public class MyHealthIndicator extends AbstractHealthIndicator {
@Autowired
HealthDemo healthDemo;
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
// 自定义检查方法
int check = healthDemo.check();
if (check == 1) {
// 存活
builder.up().
withDetail("code",200)
.build();
} else {
// 下线
builder.down()
.withDetail("code", 404)
.build();
}
}
}
通过http://localhost:8080/actuator/health
{
status: "UP",
components: {
db: {
status: "UP",
details: {
database: "MySQL",
validationQuery: "isValid()"
}
},
diskSpace: {
status: "UP",
details: {
total: 1000240963584,
free: 385116925952,
threshold: 10485760,
exists: true
}
},
my: {
status: "UP",
details: {
code: 200
}
},
ping: {
status: "UP"
}
}
}
使用MeterRegistry实现计数
@Component
public class HealthDemo {
Counter counter = null;
public int check() {
// 业务代码判断组件是否是存活状态
return 1;
}
public HealthDemo(MeterRegistry meterRegistry) {
// 得到一个名为myHealth.health的计数器
counter = meterRegistry.counter("myHealth.health");
}
public void haha() {
System.out.println("haha");
counter.increment();
}
}
@PostMapping("/haha")
public void haha(@RequestBody @Valid GetDeviceListByDeptReq req) {
healthDemo.haha();
}
调用后http://localhost:8080/actuator/metrics/myHealth.health查看
{
name: "myHealth.health",
description: null,
baseUnit: null,
measurements: [{
statistic: "COUNT",
value: 11
}],
availableTags: []
}