Spring Boot的Actuator 提供了运行状态监控的功能,Actuator的监控数据可以通过REST、远程shell和JMX方式获得。我们首先来通过REST方式查看Actuator的节点的方法,这种是最常见且简单的方法。
在工程的pom文件中引入Actuator的起步依赖spring-boot-starter-actuator,代码清单如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
在配置文件application.yml中配置management.port和management.security.enabled,这两个配置分别配置了Actuator对外暴露REST API接口的端口号和Actuator采取非安全验证方式,其代码清单如下:
management:
port: 9001
security:
enabled: false
在上述的配置代码中指定了Actuator对外暴露REST API接口的端口号为9001,如果不指定,端口为应用程序的启动端口,这样做的目的是将程序端口和程序的监控端口分开。Spring Boot 1.5X版本默认开启了Actuator的安全认证,为了能够在浏览器上展示效果,不做安全验证,将management.security.enables=false。启动工程,在控制台可以看到如下信息:
Mapped "{[/heapdump || /heapdump.json],methods=[GET],produces=[application/octet-stream]}" onto public void org.springframework.boot.actuate.endpoint.mvc.HeapdumpMvcEndpoint.invoke(boolean,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.io.IOException,javax.servlet.ServletException
Mapped "{[/auditevents || /auditevents.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public org.springframework.http.ResponseEntity<?> org.springframework.boot.actuate.endpoint.mvc.AuditEventsMvcEndpoint.findByPrincipalAndAfterAndType(java.lang.String,java.util.Date,java.lang.String)
Mapped "{[/beans || /beans.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
Mapped "{[/loggers/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.get(java.lang.String)
...
Spring Boot Actuator的关键特性是在应用程序里提供众多的Web节点,通过这些节点可以实时地了解应用程序的运行状况。有了Actuator,你可以知道Bean在Spring应用程序上下文里是如何组装在一起的,并且可以获取环境属性的信息和运行时度量信息等。
Actuator提供了13个API接口,用于监控运行状态的Spring Boots的状况,具体如表
类型 | API端口 | 描述 |
---|---|---|
GET | /autoconfig | 提供了一份自动配置报告,记录哪些自动配置条件通过了,哪些没有通过 |
GET | /configprops | 描述配置属性如何注入Bean |
GET | /beans/描述应用程序上下文里全部的Bean,以及它们的关系 | |
GET | /dump | 获取线程活动的快照 |
GET | /env | 获取全部环境属性 |
GET | /env/{name} | 根据名称获取特定的环境属性值 |
GET | /health | 应用程序的健康指标 |
GET | /info | 获取应用程序的信息 |
GET | /mappings | 描述全部的URI路径,以及它们和控制器(包含Actuator端点)的映射关系 |
GET | /metrics | 获取应用程序度量信息,例如内存用量和HTTP请求计数 |
GET | /metrics/{name} | 获取指定名称的度量信息 |
POST | /shutdown | 关闭应用程序,需要将endpoints.shutdown.enabled设置为true |
GET | /trace | 提供基本的HTTP请求跟踪信息(时间戳、HTTP头等) |
4.4.1 查看运行程序的健康状态
打开浏览器访问“http://localhost:9001/health",可以查看应用程序的运行状态和磁盘状态等信息。浏览器显示的信息如下:
{
status: "UP",
diskSpace: {
status: "UP",
total: 177972215808,
free: 168924540928,
threshold: 10485760
}
}
“/health” API接口提供的信息是由一个或多个健康指示器提供的健康信息的组合结果。下表给出了Spring Boot自带的健康指示器。
指示器 | 键 | 内容 |
---|---|---|
ApplicationHealthIndicator | none | 永远为UP |
DataSourceHealthIndicator | db | 如果数据库能连上,则为UP;否则为DOWN |
DiskSpaceHealthIndicator | diskSpace | 如果可用空间大于阈值,则为UP和可用磁盘空间;如果空间不足,则为DOWNload |
JmsHealthIndicator | jms | 如果能连上消息代理,则为UP;否则为DOWN |
MailHealthIndicator | 如果能连上邮件服务器,则为UP和邮件服务器主机和端口;否则为DOWN | |
MongoHealthIndicator | mongo | 如果能连上RabbitMQ服务器,则为UP和版本号;否则为DOWN |
RabbitHealthIndicator | rabbit | 如果能连上RabbitMQ服务器,则为UP和版本号;否则为DOWN |
RedisHealthIndicator | redis | 如果能连上服务器,则为UP和Redis服务器版本;否则为DOWN |
SolrHealthIndicator | solr | 如果能连上Solr服务器,则为UP;否则为DOWN |
4.4.2 查看运行程序的Bean
如果需要了解Spring Boot上下文注入了哪些Bean,这些Bean的类型、状态、生命周期等信息时,只需要发起一个GET类型的请求,请求API为"/beans",在浏览器上访问"http://localhost:9001/beans",浏览器会显示如下信息
{
context: "application:dev:8082:management",
parent: "application:dev:8082",
beans: [
{
bean: "embeddedServletContainerFactory",
aliases: [ ],
scope: "singleton",
type: "org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory",
resource: "null",
dependencies: [ ]
},
{
bean: "endpointWebMvcChildContextConfiguration",
aliases: [ ],
scope: "singleton",
type: "org.springframework.boot.actuate.autoconfigure.EndpointWebMvcChildContextConfiguration$$EnhancerBySpringCGLIB$$abd9a6a6",
resource: "null",
dependencies: [ ]
},
{
bean: "propertyPlaceholderAutoConfiguration",
aliases: [ ],
scope: "singleton",
type: "org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration$$EnhancerBySpringCGLIB$$38887ffd",
resource: "null",
dependencies: [ ]
},
{
bean: "embeddedServletContainerAutoConfiguration",
aliases: [ ],
scope: "singleton",
type: "org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration$$EnhancerBySpringCGLIB$$a43ede04",
resource: "null",
dependencies: [ ]
},
...
在返回的信息中包含了Bean的以下4类信息。
- bean:Spring应用程序上下文中的Bean名称或Id。
- resource:class文件的物理位置,通常是一上Url,指向构建出的Jar文件的路径。
- scope:Bean的作用域(通常是单例singleton,也可以是ptototype、request和session)。
- type:Bean的类型。
4.4.3 使用Actuator关闭应用程序
当需要关闭某个应用程序时,只需要通过Actuator发送一个POST请求"/shutdown"。很显示关闭程序是一件非常危险的事,所以默认的情况下关闭应用程序的API接口没有开启的。打开POSTMAN通过POST请求"http://localhost:9001/shutdown",得到的信息如下:
{
timestamp: 1559030075501,
status: 404,
error: "Not Found",
message: "No message available",
path: "/shutdown"
}
上述信息显示找不到该请求路径,这是因为在默认的情况下这个节点是没有开启的,需要将endpoints.shutdown.enabled改为true。在程序的配置文件application.yml中添加如下代码:
endpoints:
shutdown:
enabled: true
加上配置之后,重启Spring Boot程序,再发送一次POST请求,得到的响应信息如下:
从得到的响应信息可以知道程序已经关闭。另外,Actuator的其他10个API接口为Spring Boot程序的运行状态给开发人员或者运维人员提供了许多有用的信息,这些信息帮助我们更好地了解程序所处的状态,例如稳定性如何、故障点在哪里。在这里就不一一介绍了,有兴趣的可以对每个API逐一了解。
4.4.4 使用shell连接Actuator
通过REST API这种方式,开发人员通过Actuator可以很方便地了解运行中的程序的监控信息。Actuator也可以通过shell的方式连接。通过shell连接Actuator,需要在工程的pom文件加上shell的起步依赖spring-boot-starter-remote-shell,代码如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-remote-shell</artifactId>
</dependency>
在程序的pom文件加上spring-boot-starter-remote-shell起步依赖后,启动Spring Boot应用程序,在程序的控制台会输出连接shell的密码,密码是随机的,每次都不一样,大概格式如下:
Using default password for shell access: 50181663-9367-48fe-b644-efd194601173
与密码相对应的用户名是user,可能通过远程SSH连接shell,它的端口是2000,这是固定的。这里我们使用SecureCRT
连接成功如图
连接上shell后,这时可以通过终端查看Actuator的各个端点。Spring Boot提供了4个特有的shell命令,如表
命令 | 说明 |
---|---|
beans | 列出了SpringBoot程序应用上下文的Bean |
endpoint | 调用Actuator端点 |
metrics | Spring Boot的度量段义和 |
autoconfig | 生成自动配置说明报告 |
现在以第一个命令beans为例来做演示,连接上shell后,在终端输入beans,终端显示了应用上下文的注册信息,如图
由上图可知,beans命令和通过请求REST API接口“/beans"的结果相同,都是以JSON的数据格式输出了应用上下文的所有bean信息。其他命令就不一一展示了。
Actuator是Spring Boot的一个非常重要的功能,Actuator为开发人员提供了Spring Boot的运行状态信息,通过Actuator可以查看程序的运行状态的信息。