SpringBoot自带监控功能Actuator,可以帮助实现对程序内部运行情况监控,比如监控状况、Bean加载情况、环境变量、日志信息、线程信息等
1.加入maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
加入依赖后就可以用 http://localhost:8080/actuator 查看信息
2.修改配置文件
management:
endpoints:
enabled-by-default: true #默认开启所有监控端点 true
web:
exposure:
include: '*' # 以web方式暴露所有端点
endpoint:
health:
show-details: always #显示health详细信息
enabled: true #开启heakth监控端点
info: #这部分是自己添加的内容
appName: boot-actuator
appVersion: 1.0.0
mavenProjectName: @project.artifactId@ #从pom.xml中获取值
mavenProjectVersion: @project.version@
3.在配置文件中使用@时出错,需要在pom.xml的build中加入以下内容
<!--为了能使用@@-->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
4.自己添加的信息也可以在代码中实习
@Component
public class AppInfoContributor implements InfoContributor {
@Override
public void contribute(Info.Builder builder) {
builder.withDetail("msg","hello")
.withDetails(Collections.singletonMap("world","11111"));
}
}
5.可以修改原有的内容
@Component
public class MyComHealthIndicator extends AbstractHealthIndicator {
/**
* 真实的检查方法
* @param builder
* @throws Exception
*/
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
Map<String, Object> map = new HashMap<>();
if (false){
builder.status(Status.UP);
map.put("count",1);
map.put("ms",100);
}else {
builder.status(Status.DOWN);
map.put("err","连接超时");
map.put("ms",3000);
}
builder.withDetail("code",100)
.withDetails(map);
}
}
6.测试结果
http://localhost:8080/actuator
http://localhost:8080/actuator/health
http://localhost:8080/actuator/info