SpringBoot 整合 actuator 实现应用监控
1、Actuator介绍
Actuator 是 Spring Boot 提供的对应用系统的自省和监控的集成功能,可以查看应用配置的详细信息,例如自动化配置信息、创建的 Spring beans 以及一些环境属性等。
Actuator 提供了 13 个接口,具体如下表所示。
HTTP 方法 | 路径 | 描述 |
---|---|---|
GET | /auditevents | 显示应用暴露的审计事件 (比如认证进入、订单失败) |
GET | /beans | 描述应用程序上下文里全部的 Bean,以及它们的关系 |
GET | /conditions | 就是 1.0 的 /autoconfig ,提供一份自动配置生效的条件情况,记录哪些自动配置条件通过了,哪些没通过 |
GET | /configprops | 描述配置属性(包含默认值)如何注入Bean |
GET | /env | 获取全部环境属性 |
GET | /env/{name} | 根据名称获取特定的环境属性值 |
GET | /flyway | 提供一份 Flyway 数据库迁移信息 |
GET | /liquidbase | 显示Liquibase 数据库迁移的纤细信息 |
GET | /health | 报告应用程序的健康指标,这些值由 HealthIndicator 的实现类提供 |
GET | /heapdump | dump 一份应用的 JVM 堆信息 |
GET | /httptrace | 显示HTTP足迹,最近100个HTTP request/repsponse |
GET | /info | 获取应用程序的定制信息,这些信息由info打头的属性提供 |
GET | /logfile | 返回log file中的内容(如果 logging.file 或者 logging.path 被设置) |
GET | /loggers | 显示和修改配置的loggers |
GET | /metrics | 报告各种应用程序度量信息,比如内存用量和HTTP请求计数 |
GET | /metrics/{name} | 报告指定名称的应用程序度量值 |
GET | /scheduledtasks | 展示应用中的定时任务信息 |
GET | /sessions | 如果我们使用了 Spring Session 展示应用中的 HTTP sessions 信息 |
POST | /shutdown | 关闭应用程序,要求endpoints.shutdown.enabled设置为true |
GET | /mappings | 描述全部的 URI路径,以及它们和控制器(包含Actuator端点)的映射关系 |
GET | /threaddump | 获取线程活动的快照 |
2、Actuator使用
1.引入依赖
<!-- actuator监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.添加配置
可以根据需要添加配置
### 应用监控配置
# 指定访问这些监控方法的端口
management.server.port=8080
### 开启所有端点
management.endpoints.web.exposure.include=*
# 将beans端点缓存的生存时间设置为10秒
management.endpoint.beans.cache.time-to-live=10s
#查看详细的应用健康信息
management.endpoint.health.show-details=always
3.测试
我下载了JSONView插件,所以展示的速度json数据
因为我配置了所有端点,所以它会访问所有端点。
3、自定义端点
actuator提供了许多原生端点,如果这些端点无法满足要求,还可以自定义端点,自定义端点时,只需要继承抽象类AbstractEndpoint即可。
1.创建自定义AyUserEndPoint类
在项目目录下新建一个actuator包,在actuator包下新建自定义端点类AyUserEndPoint,用于监控数据库用户信息情况。
代码如下:
import com.example.demojpa.service.AyUserService;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/*
* 自定义端点
* ye
* 2020.11.8
* */
@Component
@Endpoint(id = "userEndPoints")
public class AyUserEndPoint {
@Resource
private AyUserService ayUserService;
@ReadOperation
public Map<String,Object> invoke(){
Map<String,Object> map = new HashMap<String, Object>();
//当地时间
map.put("current_time",new Date());
//用户总数量
map.put("user_num",ayUserService.findAll().size());
return map;
}
}
@Endpoint(id = “userEndPoints”): @Endpoint注解简化了创建用户自定义端点的过程,相当于@WebEndpoint和@JmxEndpoint的整合,两种方式都支持。
2.测试
访问/actuator/userEndPoints,出现相关信息
4、自定义HealthIndicator
有时候,我们需要自定义符合自己业务的需求的检查健康,需要自定义HealthIndicator来获取更多健康信息。
1.创建TheHealthIndicator类
在actuator包下新建TheHealthIndicator类
代码如下:
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.stereotype.Component;
/*
* 自定义健康类
* ye
* 2020.11.08
* */
@Component
public class TheHealthIndicator extends AbstractHealthIndicator {
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
Long totalSpace = cheakTocalSpace();
Long free = checkFree();
String status = checkStatus();
builder
.up()
.withDetail("status",status)
.withDetail("total",totalSpace)
.withDetail("free",free)
.build();
}
private String checkStatus(){
//可以根据实际项目,获取相关参数
return "UP";
}
private Long cheakTocalSpace(){
//可以根据实际项目,获取相关参数
return 10L;
}
private Long checkFree(){
//可以根据实际项目,获取相关参数
return 100L;
}
}
2.测试
TheHealthIndicator类的健康信息在the,即TheHealthIndicator去掉HealthIndicator