Spring Boot 提供运行时的应用监控和管理功能。本文,咱们经过 HTTP 实现对应用的监控和管理。javascript
博客地址:blog.720ui.com/java
Spring Boot 监控核心是 spring-boot-starter-actuator 依赖,增长依赖后, Spring Boot 会默认配置一些通用的监控,好比 jvm 监控、类加载、健康监控等。git
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>复制代码
若是,使用 HTTP 调用的方式,还须要 spring-boot-starter-web 依赖。github
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>复制代码
以最新的 Spring Boot 的版本 1.4.3 为准,列出目前支持的监控。web
HTTP方法 | 路径 | 描述 |
---|---|---|
GET | /actuator | 查看全部EndPoint的列表,须要加入 Spring HATEOAS 支持 |
GET | /autoconfig | 查看应用的自动配置的使用状况 |
GET | /beans | 查看应用的全部Bean的信息 |
GET | /configprops | 查看应用的全部配置属性 |
GET | /dump | 查看应用的线程状态信息 |
GET | /env | 查看应用的全部环境信息 |
GET | /flyway | 查看已经有迁徙路线数据库迁移 |
GET | /health | 查看应用健康指标 |
GET | /info | 查看应用信息 |
GET | /liquibase | 查看已经有liquibase数据库迁移应用 |
GET | /mappings | 查看全部url映射 |
GET | /metrics | 查看应用基本指标 |
POST | /shutdown | 容许优雅关闭当前应用(默认状况下不启用) |
GET | /trace | 查看基本的HTTP跟踪信息 |
GET | /docs | 查看文档,须要依赖 spring-boot-actuator-docs |
GET | /heapdump | 返回一个gzip压缩 hprof 堆转储文件 |
GET | /jolokia | 暴露JMX bean(当jolokia路径) |
GET | /logfile | 查看日志文件的内容(若是logging.file或logging.path属性已设置)。支持使用对HTTP范围标头到日志文件的部分恢复内容。 |
咱们能够经过 http://localhost:8080/health , 统计系统的情况,默认里面目前只有系统情况和磁盘情况。这些检测器都经过 HealthIndicator 接口实现,下篇文章,我会讲解到经过 HealthIndicator 接口实现自定义健康检测。spring
默认状况下,只会返回一个空的 json 内容。咱们能够在 application.properties 配置文件中经过 info 前缀来设置一些属性。数据库
info.author.realname=梁桂钊
info.author.nickname=LiangGzone复制代码
咱们也能够在 application.yml 配置文件中设置一些属性。json
info.author:
email: lianggzone@163.com
blog: http://blog.720ui.com复制代码
咱们能够经过 http://localhost:8080/metrics, 获取当前应用的各种重要度量指标,好比:内存信息、线程信息、垃圾回收信息等。安全
咱们还能够经过 /metrics/{name} 接口来更细粒度的获取度量信息,好比咱们能够经过访问 /metrics/mem.free 来获取当前可用内存数量。springboot
查看基本的 HTTP 跟踪信息。默认状况下,跟踪信息的存储采用 org.springframework.boot.actuate.trace.InMemoryTraceRepository 实现的内存方式,始终保留最近的 100 条请求记录。
shutdown 端点默认是不启用的,咱们能够在 application.properties 中开启。此外,shutdown 端点不支持 GET 请求,咱们须要经过 POST 方法访问。
endpoints.shutdown.enabled=true复制代码
端点能够在 Spring Boot 配置文件中进行定制。例如,上面,咱们开启 shutdown 端点。
endpoints.shutdown.enabled=true复制代码
端点的前缀是,endpoints + “.”+ 端点名。
默认的端点访问路径是根目录下,咱们能够经过修改配置,进行定制。
management.context-path=/manage复制代码
此时,咱们的访问地址就变成: http://localhost:8080/manage/info
此外,默认监控的接口端口和业务的端口是一致的,咱们出于安全性考虑,能够改变端点的访问的端口。
management.port=9090复制代码
咱们甚至能够关闭 http 端点。
management.port=-1复制代码
相关示例完整代码: springboot-action
(完)
更多精彩文章,尽在「服务端思惟」微信公众号!