springboot中自带监控工具actuator,在对监控要求不高的情况下,使用actuator就可以满足系统监控要求了。使用actuator,需要添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
本文使用2.1.6版本,配置文件如下
management.server.address=127.0.0.1 management.server.port=18082 management.endpoints.web.exposure.include=* management.endpoints.web.base-path=/actuator management.endpoint.shutdown.enabled=true graceful.shutdown.wait=30 management.endpoint.health.show-details=always
上面配置指定了actuator对外暴露的地址是127.0.0.1,端口是18082,对外暴露服务的所有访问点,具体见官网,基础路径是/actuator,允许关闭应用(一般不开启),优雅关闭等待30s,总是允许查看详细的应用健康信息。
下面依次介绍一下各个信息的详情
1.info主要展示一些系统发布的版本信息,配置一下info信息
info.app.name=springboot-mybatis info.app.description=two-datasource-test info.app.version=1.0 info.app.spring-boot-version=2.1.6
启动工程后,浏览器输入:http://127.0.0.1:18082/actuator/info,输出如下:
{ "app": { "name": "springboot-security", "description": "springboot-security-test", "version": "1.0", "spring-boot-version": "2.1.6" } }
2. beans主要展示所有的bean,包括自定义的,以及这些bean之间的关系
输入:http://127.0.0.1:18082/actuator/beans,输入所有的bean,下图截取了一部分
3.health介绍系统的健康状态,
输入:http://127.0.0.1:18082/actuator/health,输出如下结果:
{ "status": "UP", "details": { "db": { "status": "UP", "details": { "firstDataSource": { "status": "UP", "details": { "database": "MySQL", "hello": 1 } }, "secondDataSource": { "status": "UP", "details": { "database": "MySQL", "hello": 1 } } } }, "diskSpace": { "status": "UP", "details": { "total": 499972567040, "free": 396368695296, "threshold": 10485760 } } } }
4.Configuration Properties 主要展示有@ConfigurationProperties注解的bean
浏览器输入:http://127.0.0.1:18082/actuator/configprops,输出部分截图如下,黄色的bean就是我在程序中定义的
5.environment获取应用的所有环境变量
输入http://127.0.0.1:18082/actuator/env,返回结果部分截图如下:
也可以查看具体某一个环境变量的值,比如输入:http://localhost:18082/actuator/env/server.port,输出如下:
{
"property": {
"source": "applicationConfig: [classpath:/application.properties]",
"value": "8080"
},
"activeProfiles": [],
"propertySources": [{
"name": "server.ports"
}, {
"name": "servletConfigInitParams"
}, {
"name": "servletContextInitParams"
}, {
"name": "systemProperties"
}, {
"name": "systemEnvironment"
}, {
"name": "random"
}, {
"name": "applicationConfig: [classpath:/application.properties]",
"property": {
"value": "8080",
"origin": "class path resource [application.properties]:28:13"
}
}]
}
6. Audit Events提供应用的审核事件信息
这个需要配合spring-security使用,具体源码见:https://github.com/jinjunzhu/spring-boot-security
浏览器输入http://127.0.0.1:18082/login,页面如下:
输入用户名:user,密码:password,登录成功后就可以使用了
浏览器输入url:http://127.0.0.1:18082/actuator/auditevents
{ "events": [{ "timestamp": "2020-05-06T02:52:25.880Z", "principal": "anonymousUser", "type": "AUTHORIZATION_FAILURE", "data": { "details": { "remoteAddress": "0:0:0:0:0:0:0:1", "sessionId": null }, "type": "org.springframework.security.access.AccessDeniedException", "message": "Access is denied" } }, { "timestamp": "2020-05-06T02:56:09.375Z", "principal": "user", "type": "AUTHENTICATION_SUCCESS", "data": { "details": { "remoteAddress": "0:0:0:0:0:0:0:1", "sessionId": "8B61F133E87B51975CF5BA2D00EAD790" } } }, { "timestamp": "2020-05-06T03:05:42.352Z", "principal": "anonymousUser", "type": "AUTHORIZATION_FAILURE", "data": { "details": { "remoteAddress": "127.0.0.1", "sessionId": null }, "type": "org.springframework.security.access.AccessDeniedException", "message": "Access is denied" } }, { "timestamp": "2020-05-06T03:07:54.183Z", "principal": "user", "type": "AUTHENTICATION_SUCCESS", "data": { "details": { "remoteAddress": "127.0.0.1", "sessionId": "52D3515D26413A408FB53158A52520FE" } } }] } { "property": { "source": "applicationConfig: [classpath:/application.properties]", "value": "8080" }, "activeProfiles": [], "propertySources": [{ "name": "server.ports" }, { "name": "servletConfigInitParams" }, { "name": "servletContextInitParams" }, { "name": "systemProperties" }, { "name": "systemEnvironment" }, { "name": "random" }, { "name": "applicationConfig: [classpath:/application.properties]", "property": { "value": "8080", "origin": "class path resource [application.properties]:28:13" } }] }
从上面可看到user用户认证成功。
7.Caches用于获取应用的缓存
浏览器输入url:http://localhost:8080/actuator/caches可以返回应用中配置的缓存
也可以向应用发送一个DELETE请求清除缓存,
curl 'http://localhost:8080/actuator/caches' -i -X DELETE
也可以按照名称清除缓存
curl 'http://localhost:8080/actuator/caches/countries?cacheManager=anotherCacheManager' -i -X DELETE
8.Conditions Evaluation Report提供配置和自动配置的条件值,浏览器输入:http://127.0.0.1:18082/actuator/conditions,输出结果如下:
9.Flyway提供数据库使用Flyway迁移数据的情况
curl 'http://localhost:18082/actuator/flyway' -i -X GET
10.Heap Dump提供应用jvm的堆dump信息
url:curl 'http://localhost:18082/actuator/heapdump' -O
11.HTTP Trace提供应用的请求-响应的交互信息
url:curl 'http://localhost:18082/actuator/httptrace' -i -X GET
12.Spring Integration graph用来查看spring集成的所有组件
url:curl 'http://localhost:18082/actuator/integrationgraph' -i -X GET
13.liquibase用来查看使用liquibase工具进行数据库迁移和重构的工具
url:curl 'http://localhost:18082/actuator/liquibase' -i -X GET
14.Log File获取应用日志文件的内容
url:curl 'http://localhost:18082/actuator/logfile' -i -X GET
15.Loggers获取应用的日志配置以及日志级别
url:curl 'http://localhost:18082/actuator/loggers' -i -X GET
16.Mappings用来查看应用的request mapping
url:curl 'http://localhost:44209/actuator/mappings' -i -X GET
17.Metrics用来监控应用的各项指标
url:curl 'http://localhost:18082/actuator/metrics' -i -X GET
返回结果如下:
{ "names": ["jvm.memory.max", "jvm.threads.states", "http.server.requests", "jvm.gc.memory.promoted", "jvm.memory.used", "jvm.gc.max.data.size", "jdbc.connections.max", "jdbc.connections.min", "jvm.gc.pause", "jvm.memory.committed", "system.cpu.count", "logback.events", "tomcat.global.sent", "jvm.buffer.memory.used", "tomcat.sessions.created", "jvm.threads.daemon", "system.cpu.usage", "jvm.gc.memory.allocated", "tomcat.global.request.max", "hikaricp.connections.idle", "hikaricp.connections.pending", "tomcat.global.request", "tomcat.sessions.expired", "hikaricp.connections", "jvm.threads.live", "jvm.threads.peak", "tomcat.global.received", "hikaricp.connections.active", "hikaricp.connections.creation", "process.uptime", "tomcat.sessions.rejected", "process.cpu.usage", "tomcat.threads.config.max", "jvm.classes.loaded", "hikaricp.connections.max", "hikaricp.connections.min", "jvm.classes.unloaded", "tomcat.global.error", "tomcat.sessions.active.current", "tomcat.sessions.alive.max", "jvm.gc.live.data.size", "hikaricp.connections.usage", "tomcat.threads.current", "hikaricp.connections.timeout", "jvm.buffer.count", "jvm.buffer.total.capacity", "tomcat.sessions.active.max", "hikaricp.connections.acquire", "tomcat.threads.busy", "process.start.time"] }
18.Prometheus以Prometheus格式提供springboot应用监控指标值
url: curl 'http://localhost:18082/actuator/prometheus' -i -X GET
19.Scheduled Tasks监控定时任务信息
url:curl 'http://localhost:18082/actuator/scheduledtasks' -i -X GET
返回示例如下:
{
"cron" : [ {
"runnable" : {
"target" : "com.example.Processor.processOrders"
},
"expression" : "0 0 0/3 1/1 * ?"
} ],
"fixedDelay" : [ {
"runnable" : {
"target" : "com.example.Processor.purge"
},
"initialDelay" : 5000,
"interval" : 5000
} ],
"fixedRate" : [ {
"runnable" : {
"target" : "com.example.Processor.retrieveIssues"
},
"initialDelay" : 10000,
"interval" : 3000
} ],
"custom" : [ {
"runnable" : {
"target" : "com.example.Processor$CustomTriggeredRunnable"
},
"trigger" : "com.example.Processor$CustomTrigger@2c6bb64b"
} ]
}
20.Sessions提供使用spring session管理的session
url:curl 'http://localhost:18082/actuator/sessions?username=alice' -i -X GET
21.Thread Dump提供应用jvm的线程dump信息
url:curl 'http://localhost:18082/actuator/threaddump' -i -X GET
22.Shutdown用来关闭应用,一般不建议配置
url:curl 'http://localhost:18082/actuator/shutdown' -i -X POST
最后,springboot的健康检查都是实现了接口HealthIndicator,AbstractHealthIndicator做了一个抽象实现,其他实现类继承AbstractHealthIndicator来进行收集健康信息。
当然,我们也可以自己实现健康检查,只要实现HealthIndicator接口或者继承AbstractHealthIndicator抽象类就可以。
状态收集类都实现了HealthAggregator接口,AbstractHealthAggregator是HealthAggregator的抽象实现,OrderedHealthAggregator继承了AbstractHealthAggregator,
定义了状态收集的顺序,可以进行配置,参数management.health.status.order=。我们可以实现HealthAggregator接口来增加新的Status,也可以在配置中实现,比如
management.health.status.order=Error, DOWN, OUT_OF_SERVICE, UNKNOWN, UP management.health.status.http-mapping.FATAL=500
配置类见HealthIndicatorProperties。