springboot与监控管理
目录
概述
通过引入spring-boot-starter-actuator,可以使用Spring Boot为我们提供的准 生产环境下的应用监控和管理功能。我们可以通过HTTP,JMX,SSH协议来进 行操作,自动得到审计、健康及指标信息等
使用步骤
-
引入spring-boot-starter-actuator
-
增加监控的项
1
management.endpoints.web.exposure.include=*
-
访问端点
http://localhost:8080/actuator/端点名
监控和管理端点
端点名 | 描述 |
---|---|
mappings | 可以查看到所有的接口映射信息(请求url->处理方法),包括actuator自己 |
self | 注意,/actuator, 后面不用加selft, 查看系统所有的actuator端点url |
heapdump | 下载内存快照 |
threaddump | 查看内存快照 |
info | 见表格下演示 |
metrics | 返回当前系统的一些监控指标,例如内存、处理器等 |
env | 应用当前环境信息,包括服务器设置、系统环境、application设置等等 |
loggers | 日志控制器对应级别 |
configprops | 所有配置的属性情况 |
health | 查看应用健康状况,查看表格下面演示 |
scheduledtasks | 查看应用所有定时任务 |
info
显示InfoProperties属性,包括继承InfoProperties的子类属性
例如我们在配置文件中增加
|
|
访问:http://localhost:8080/actuator/info
|
|
health
例如我们添加redis依赖并配置redis主机信息
|
|
本地并不存在redis服务,所以访问http://localhost:8080/actuator/health
会报告{"status":"DOWN"}
,并且服务器上会打印错误日志
|
|
启动redis服务,再次访问health端点,此时会报告{"status":"UP"}
如何自定义健康指示器
步骤:
- 编写一个指示器 实现 HealthIndicator 接口
- 指示器的名字 xxxxHealthIndicator
- 加入容器中
示例:
|
|
修改端点信息
-
关闭远程端点功能
1 2
management.endpoints.web.exposure.include=* management.endpoint.health.enabled=false
-
仅开启需要的端点
去除
management.endpoints.web.exposure.include=*
1
management.endpoint.health.enabled=true
-
修改manament访问端口号
1 2
# 通过http://localhost:9999/actuator 访问 management.server.port=9999
-1则禁止访问
1
management.server.port=-1
-
修改management访问路径,添加访问根路径
1 2
# http://localhost:9999/manage/actuator/health management.server.servlet.context-path=/manage