对于一个产品级的应用来说,必要的应用监控和管理功能是必不可少的。如果要自己实现这个功能非常繁琐且通用性不强。SpringBoot通过 actuator 模块来为基于 SpringBoot 开发的应用提供这些产品级的能力。actuator 已经内置了大量的监控和管理功能,这些功能不需要程序员进行开发,只需要进行一些必要的配置就可以达到开箱即用的效果。同时 actuator 模块也提供自定义监控和管理应用程序的能力,让程序员能够开发符合自己需求的应用监控和管理功能。这里我们先不讨论需要程序员自定义开发的 actuator 功能,只讨论如何通过配置来开启和使用 actuator 内置的功能。
SpringBoot actuator 通过两种模式 JMX 和 HTTP 两种方式来对外提供 actuator 的功能。每一个功能都可以单独设置是否开启,已经是否通过 JMX 方式开启和是否通过 HTTP 方式开启。主要的内置功能和默认配置如下表所示

要使用 actuator 功能,只需要在 SpringBoot 应用 maven 配置文件 pom.xml 中引入对应的依赖即可
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
以前一篇文章《SpringBoot 中使用 JDBC Templet》中的工程为基础,引入上面的依赖后,启动工程即可使用 actuator 的功能。这里我们采用 http 的方式来使用这些功能。
http 方式访问这些功能,只用访问对应功能的 url 地址即可。地址默认是 http://ip:port/{context-path}/actuator/{endpoint} ,根据上表,我们可以判断,默认智能使用 health 和 info 两个功能。如果要使用其他功能,需要修改应用的配置文件。原来配置文件是 .properties 格式的,这里我们修改成 yaml 格式。配置文件信息如下
server:
port: 3030
servlet:
context-path: /security
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://10.110.2.56:3306/springboot_jdbc?charset=utf8mb4&useSSL=false
username: springboot
password: springboot
hikari:
maximum-pool-size: 10
minimum-idle: 0
max-lifetime: 27000000
jackson:
date-format: yyyy-MM-dd HH:mm:ss
serialization:
indent-output: true
time-zone: GMT+8
logging:
level:
com.yanggaochao: debug
management:
endpoint:
shutdown:
enabled: true
endpoints:
web:
exposure:
include: [
"health",
"info",
"beans",
"configprops",
"env",
"sessions",
"metrics",
"httptrace",
"heapdump",
"auditevents",
"threaddump",
"conditions",
"loggers",
"shutdown",
"mappings"
]
其中
management:
endpoint:
shutdown:
enabled: true
配置是开启原来默认不开启的关闭应用功能。而下面的配置则让对应的功能能够通过 http 的方式使用。
management:
endpoints:
web:
exposure:
include: [
"health",
"info",
"beans",
"configprops",
"env",
"sessions",
"metrics",
"httptrace",
"heapdump",
"auditevents",
"threaddump",
"conditions",
"loggers",
"shutdown",
"mappings"
]
重新启动应用后,如果我们在浏览器器的地址栏输入如下地址
http://localhost:3030/security/actuator/mappings
我们能够得到下面的结果,显示在这个应用中所有可以访问的 http 服务的详细信息。

以上是 SpringBoot acutator 功能的基本使用方法,后面有机会再将 自定义 actuator 的方法。
网友评论