Spring Boot提供了良好的服务监控模块,只须要经过简单的配置即可以完成服务监控和管理。可是服务监控这块内容每每是最容易被忽略的一块内容,今天咱们一块儿来学习一下使用spring-boot-actuator
进行服务监控。spring-boot-actuator
提供了监控端点,这些端点直接返回JSON字符串
,经过这些端点能够查询服务运行情况,为了防止端点直接暴露,通常状况下会使用安全框架,如Spring Security来管理这些端点的安全性。git
端点地址 | 描述 | 默认启用 |
---|---|---|
auditevents | 获取当前应用暴露的审计事件信息 | 是 |
beans | 获取应用中全部的bean的完整关系列表 | 是 |
caches | 获取公开能够用的缓存 | 是 |
conditions | 获取自动配置条件信息,记录哪些自动配置条件经过和没经过的缘由 | 是 |
configprops | 获取全部配置属性,包括默认配置,显示一个全部 @ConfigurationProperties 的整理列版本 | 是 |
env | 获取全部环境变量 | 是 |
flyway | 获取已应用的全部Flyway数据库迁移信息,须要一个或多个 Flyway Bean | 是 |
health | 获取应用程序健康指标(运行情况信息) | 是 |
httptrace | 获取HTTP跟踪信息(默认状况下,最近100个HTTP请求-响应交换)。须要 HttpTraceRepository Bean | 是 |
info | 获取应用程序信息 | 是 |
integrationgraph | 显示 Spring Integration 图。须要依赖 spring-integration-core | 是 |
loggers | 显示和修改应用程序中日志的配置 | 是 |
liquibase | 获取应用的全部Liquibase数据库迁移。须要一个或多个 Liquibase Bean | 是 |
metrics | 获取系统度量指标信息 | 是 |
mappings | 显示全部@RequestMapping路径的整理列表 | 是 |
scheduledtasks | 显示应用程序中的计划任务 | 是 |
sessions | 容许从Spring Session支持的会话存储中检索和删除用户会话。须要使用Spring Session的基于Servlet的Web应用程序 | 是 |
shutdown | 关闭应用 | 否 |
threaddump | 获取系统线程转储信息 | 是 |
默认状况下,除了shutdown
,其余端点都是启动状态。github
在项目中引入spring-boot-actuator
的依赖,就能够正常使用了web
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
/actuator+端点地址
例如想要访问health端点,则访问http://ip:port/actuator/health;redis
management: endpoint: # 开启shutdown端点 shutdown: enabled: true
启用/禁用全部端点spring
management: endpoints: enabled-by-default: true
默认状况下,只有health
和info
暴露了http端口,这些端点支持经过http
和JMX
访问,若是须要访问具体的端点则须要配置暴露。数据库
暴露http
端点segmentfault
management: endpoints: web: exposure: include: health,info
暴露JMX
端点缓存
management: endpoints: jmx: exposure: include: health,info
health
包含的健康检查项有DataSourceHealthIndicator
,DiskSpaceHealthIndicator
,MongoHealthIndicator
,ReidsHealthIndicator
,CassandraHealthIndicator
。安全
关闭特定的检查项配置以下,关闭redis检查项:微信
management: health: redis: enabled: false
默认状况下health只是简单的展现了UP
和DOWN
两种状态,若是想要看详细信息,则须要配置
management: endpoint: health: show-details: always
metrics
可使用带PathVariable参数,参数为具体的度量值,如查看cpu大小,http://localhost:8080/actuator/metrics/system.cpu.count;
info
使用的时候须要在配置文件中自定义信息,自定义信息以info
开头。
例如在配置文件中增长以下内容:
info: person: name: Java旅途 age: 18
访问info
端点显示的是去掉info的一个JSON串:
person: name: Java旅途 age: 18
Spring-Boot-acturator
使用起来很方便,可是缺点也很明显,就是没有图形化界面。使用起来也不是很友好,下一章中,咱们将使用有图形化的Spring-Boot-Admin
来进行服务监控。
star
支持一下!spring-boot-route(一)Controller接收参数的几种方式
spring-boot-route(二)读取配置文件的几种方式
spring-boot-route(五)整合swagger生成接口文档
spring-boot-route(六)整合JApiDocs生成接口文档
spring-boot-route(七)整合jdbcTemplate操做数据库
spring-boot-route(八)整合mybatis操做数据库
spring-boot-route(九)整合JPA操做数据库
spring-boot-route(十一)数据库配置信息加密
spring-boot-route(十二)整合redis作为缓存
spring-boot-route(十三)整合RabbitMQ
spring-boot-route(十五)整合RocketMQ
spring-boot-route(十六)使用logback生产日志文件
spring-boot-route(十七)使用aop记录操做日志
spring-boot-route(十八)spring-boot-adtuator监控应用
spring-boot-route(十九)spring-boot-admin监控服务
spring-boot-route(二十)Spring Task实现简单定时任务
spring-boot-route(二十一)quartz实现动态定时任务
spring-boot-route(二十二)实现邮件发送功能
这个系列的文章都是工做中频繁用到的知识,学完这个系列,应付平常开发绰绰有余。若是还想了解其余内容,扫面下方二维码告诉我,我会进一步完善这个系列的文章!