使用Actuator可以在SpringBoot程序运行时通过http请求的方式动态的监控和管理程序,官方对其功能的介绍如下:
Spring Boot includes a number of additional features to help you monitor and manage your application when you push it to production. You can choose to manage and monitor your application by using HTTP endpoints or with JMX. Auditing, health, and metrics gathering can also be automatically applied to your application.
一、基础配置
想要在项目中使用Actuator,则需要导入Starter依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
Actuator中提供了端点的概念,即endpoint
,想要监控或操作Actuator的某一功能,一定要将其对相应的端点开放。
server:
port: 8080
management:
endpoints:
web:
exposure:
include: "*" # 暴露所有端点
配置完成后,启动项目,访问:http://localhost:8080/actuator
,出现了一系列的可操作路径。
访问以上路径就可以对SpringBoot程序进行动态监控和操作了,大致包含以下的功能:
No. | endpoint | 描述 |
---|---|---|
1 | beans | 注册到Sprng容器中的Bean对象集合 |
2 | caches | 缓存信息 |
3 | health | 应用的健康状态 |
4 | info | 应用的基本信息,需要手工配置 |
5 | conditions | 自动配置生效的条件 |
6 | configprops | 获取所有的配置属性 |
7 | auditevents | 显示应用暴露的审计事件 (比如认证进入、订单失败) |
8 | metrics | 应用多样的度量信息 |
9 | loggers | 日志配置 |
10 | httptrace | HTTP足迹,显示最近100个HTTP request/repsponse |
11 | env | 当前的环境特性 |
12 | flyway | 显示数据库迁移路径的详细信息 |
13 | shutdown | 关闭应用 |
14 | mappings | 所有的@RequestMapping路径 |
15 | scheduledtask | 应用中的调度任务 |
16 | threaddump | 线程信息 |
17 | heapdump | JVM堆dump |
二、heapdump信息
SpringBoot程序运行在JVM上,是JVM的一个执行线程,Actuator提供了heapdump端点可以获取到Springboot所运行JVM的实时情况,访问:http://localhost:8080/actuator/heapdump,可以进行dump文件的下载。
使用visualvm可以打开dump文件,在jdk6到jdk8中,visualvm是默认集成的,从jdk9开始,jdk默认不再集成visualvm,需要到github下载安装(下载地址)。
打开visualvm,点击左上角file->load导入dump文件即可查看jvm运行信息(导出dump文件时刻的jvm信息)。
三、服务信息
访问info端点可以查看当前程序的描述信息,这需要开发者手工配置信息(实际上info是一个Map集合,再application,yml中编写信息即可)。
server:
port: 8080
management:
endpoints:
web:
exposure:
include: "*" # 暴露所有端点
info:
app:
name: springboot项目
group: com.it
version: 1.0.0
description: 测试info
author: NicholasGUB
访问:http://localhost:8080/actuator/info:
以上的形式虽然能完成信息的自定义,但是将信息写在配置文件中导致程序启动后无法修改,想要灵活的修改程序的描述信息,那么描述信息应该在数据库中获取。
新建Info构建器需要实现InfoContributor接口:
@Component
public class ServerInfoContributor implements InfoContributor { // info构建器
@Override
public void contribute(Info.Builder builder) {
// 此处可以读取数据库
builder.withDetail("date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
}
重新访问info端点,发现定义在构建器中的内容正常显示,并且和application.yml中的内容共存。
四、服务健康状态
访问health端点可以查看当前服务(程序)的健康状态,此功能在SpringCloud中较为常用,随着业务的增多和微服务的流行,难免要将一个服务拆分为多个。
当服务拆分为多个之后,开发者(运维人员)需要监控每一个服务的健康状态,当服务出现故障之后,需要将服务信息返回给管理者方便修复。通常情况下,服务的健康状态由服务内引入组件的健康状态决定,当所有组件全部正常运行时,此服务才是健康的。
actuator提供了健康断言接口HealthIndicator,实现此接口可以编写服务健康的判断逻辑。
@Component
public class DefaultHealthIndicator implements HealthIndicator {
@Override
public Health health() {
boolean isHealth = true;
// 此处应判断各个组件是否连接正常,其中一个连接异常则设置isHealth = false
isHealth = false; // 假如redis连接异常
String errorMsg = "redis连接异常";
if(!isHealth) {
return Health.down().withDetail("message",errorMsg).build();
}
return Health.up().build();
}
}
修改application.yml文件,显示不健康时的异常信息。
management:
endpoint:
health:
show-details: always # 总是显示异常信息
访问:http://localhost:8080/actuator/health,此时status的值为down,服务是不健康的并且在details中显示了异常原因。
五、远程结束服务
actuator提供有shutdown端点可以在SpringBoot程序运行时,调用/actuator/shutdown
接口远程结束程序,此功能默认是关闭的,需要配置application.yml文件开启。
server:
port: 8080
management:
server:
port: 8081 # actuator监听8081端口
endpoint:
shutdown:
enabled: true # 开启shutdown功能
shutdown端点只能通过POST
请求的方式调用,可以使用curl工具模拟(curl下载地址)。
下载解压后,将curl根目录添加到环境变量,在cmd窗口输入:curl --version,显示如下内容则安装成功。
curl 7.55.1 (Windows) libcurl/7.55.1 WinSSL
Release-Date: 2017-11-14, security patched: 2019-11-05
Protocols: dict file ftp ftps http https imap imaps pop3 pop3s smtp smtps telnet tftp
Features: AsynchDNS IPv6 Largefile SSPI Kerberos SPNEGO NTLM SSL
在命令行输入curl -X POST "http://localhost:8081/actuator/shutdown"
显示如下结果,程序关闭成功。
{“message”:“Shutting down, bye…”}
六、自定义Endpoint
Actuator提供有@Endpoint
注解帮助开发者定义自己的Endpoint,自定义端点的请求方式也分为以下三种:
No | 注解 | 请求方式 | 描述 |
---|---|---|---|
1 | @ReadOperation | GET | 读取数据 |
2 | @WriteOperation | GET | 写入数据 |
3 | @DeleteOperation | GET | 删除数据 |
新建SelfEndpoint类,添加@Endpoint注解声明endpoint并命名。
@Configuration
@Endpoint(id = "self")
public class SelfEndpoint {
@ReadOperation
public Map<String, Object> endpoint(@Selector String selector) { // 接收参数
Map<String, Object> endpointMap = new HashMap<>();
endpointMap.put("author", "NicholasGUB");
endpointMap.put("selector", selector);
return endpointMap;
}
}
直接在浏览器输入:http://localhost:8081/actuator/self/selector