spring boot Actuator 监控组件
Actuator对应用程序进行监视和管理,通过restful api请求来监管、审计、收集应用的运行情况,针对微服务而言它是必不可少的一个环节…
在pom.xml文件中 添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
在配置文件中添加配置:
info:
head: head
body: body
management:
endpoints:
web:
exposure:
#加载所有的端点,默认只加载了info、health
include: '*'
endpoint:
health:
show-details: always
#可以关闭指定的端点
shutdown:
enabled: false
启动后访问路径: http://localhost:8080/actuator
/autoconfig | 查看自动配置 |
---|---|
/configprops | 查看配置属性 |
/beans | 查看bean及其关系列表 |
/dump | 打印线程栈 |
/env | 查看所有环境变量 |
/mappings | 查看所有url映射 |
/health | 查看应用健康指标 |
/info | 查看应用信息 |
… | … |
自定义健康端点
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.stereotype.Component;
@Component("my2")
public class MyAbstractHealthIndicator extends AbstractHealthIndicator {
private static final String VERSION = "v1.0.0";
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
int code = check();
if (code != 0) {
builder.down().withDetail("code", code).withDetail("version", VERSION).build();
}
builder.withDetail("code", code)
.withDetail("version", VERSION).up().build();
}
private int check() {
return 0;
}
}