第11章 Spring Boot使用Actuator
在生产环境中,需要实时或定期监控服务的可用性,spring-Boot的Actuator 功能提供了很多监控所需的接口。
Actuator是Spring Boot提供的对应用系统的自省和监控的集成功能,可以对应用系统进行配置查看、健康检查、相关功能统计等,一般运维人员使用多些。
11.1 使用该功能步骤
我们这里监控03-springboot-web程序
11.1.1 在项目的pom.xml中添加如下依赖
<!--Spring Boot Actuator依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
11.1.2 在application.properties或application.yml配置文件中指定监控的HTTP端口及路径
我这里没有进行配置
- #服务运行的端口
server.port=8080
server.servlet.context-path=/03-springboot-web
- #actuator监控的端口(端口可配可不配,如果不配置,则使用和server.port相同的端口)
management.server.port=8100
- #actuator监控的访问上下文根路径(路径可配可不配,如果不配置,则使用和server.context-path相同的路径)
management.server.servlet.context-path=/03-springboot-web
11.1.3 在application.properties或application.yml配置文件中设置开启所有的端口
#默认只开启了health和info,设置为*,则包含所有的web入口端点
management.endpoints.web.exposure.include=*
11.1.4 启动MySQL

11.1.5 启动Redis

11.1.7 启动03-springboot-web
(1) 浏览器访问http://localhost:8080/actuator/health
 
(2)浏览器访问http://localhost:8080/actuator/info
默认没有内容

需要自己在application.properties配置文件中添加信息,需要以info开头,后面的内容可以自己设定,一般配置项目的版权等信息,例如
#配置项目版权相关信息
info.contact.email=bjpowernode@163.com
info.contact.phone=010-84846003
配置完毕后,重启项目再进行访问

11.2 Actuator 提供的主要功能
| HTTP方法 | 路径 | 描述 | 
| GET | /configprops | 查看配置属性,包括默认配置 | 
| GET | /beans | 查看Spring容器目前初始化的bean及其关系列表 | 
| GET | /env | 查看所有环境变量 | 
| GET | /mappings | 查看所有url映射 | 
| GET | /health | 查看应用健康指标 | 
| GET | /info | 查看应用信息 | 
| GET | /metrics | 查看应用基本指标 | 
| GET | /metrics/{name} | 查看具体指标 | 
| JMX | /shutdown | 关闭应用 | 
shutdown的使用
注意:/shutdown不能直接在浏览器中访问
- 先在application.properties文件中配置
# 启用关闭springboot的服务
management.endpoint.shutdown.enabled=true
- 双击打开jconsole

- 选择Spring Boot进程,进行连接管理
 

- 点击shutdown,可关闭Spring Boot服务
![]()  
 
                    
                 
 
                
            
         
 浙公网安备 33010602011771号
浙公网安备 33010602011771号