前言
在秒杀案例进入实际生产环境中,需要实时或定期监控服务的可用性。Spring Boot 的 actuator(健康监控)功能提供了很多监控所需的接口,可以对应用系统进行配置查看、相关功能统计等。
集成
pom.xml中引入以下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
application.properties配置如下:
#监控的HTTP端口 (如果不指定,则使用和Server相同的端口)
management.port=20886
#忽略拦截
management.security.enabled=false
#当前应用信息
info.app.version=v1.0.0
info.app.name=爪哇笔记
info.app.email=345849402@qq.com
info.app.url=https://blog.52itstyle.vip
#开启shutdown远程关闭功能
#访问:http://localhost:20886/shutdown 关闭服务
endpoints.shutdown.enabled=true
详细使用说明:
HTTP方法
|
路径
|
描述
|
鉴权
|
GET
|
autoconfig
|
查看自动配置的使用情况
|
true
|
GET
|
configprops
|
查看配置属性,包括默认配置
|
true
|
GET
|
beans
|
查看bean及其关系列表
|
true
|
GET
|
dump
|
打印线程栈
|
true
|
GET
|
env
|
查看所有环境变量
|
true
|
GET
|
env/{name}
|
查看具体变量值
|
true
|
GET
|
health
|
查看应用健康指标
|
false
|
GET
|
info
|
查看应用信息
|
false
|
GET
|
mappings
|
查看所有url映射
|
true
|
GET
|
metrics
|
查看应用基本指标
|
true
|
GET
|
metrics/{name}
|
查看具体指标
|
true
|
POST
|
shutdown
|
关闭应用
|
true
|
GET
|
trace
|
查看基本追踪信息
|
true
|
举例 /info:
{
"app": {
"url": "https://www.duidaima.com",
"email": "2274840169@qq.com",
"name": "堆代码",
"version": "v2.0.0"
}
}
actuator 还会对一些集成的第三方应用进行健康检查,比如秒杀系统中用到的 redis、MySql 等等。
举例 /health:
{
"status": "UP",
"jms": {
"status": "UP",
"provider": "ActiveMQ"
},
"diskSpace": {
"status": "UP",
"total": 150325182464,
"free": 74917441536,
"threshold": 10485760
},
"redis": {
"status": "UP",
"version": "3.2.8"
},
"db": {
"status": "UP",
"database": "MySQL",
"hello": 1
}
}
安全
最重要的安全问题,通过这些 endpoints 会暴露出很多应用的信息,这里总结了一些安全措施:
1.关闭指定的endpoint,在application.properties中配置*.enable=false。
2.通过设置management.port=-1关闭endpoint的HTTP访问接口,或者是设置其他的端口,供内部的admin服务访问。
3.设置本地访问,management.address=127.0.0.1,通过设置management.context-path=/admin,可以设置指定的根路径,然后通过Nginx鉴权代理访问。