概述
HikariCP提供了一些监控指标,他的监控指标都是基于MicroMeter提供出来的,而后支持Prometheus和Dropwizard。本次咱们将讨论一下HikariCp的监控指标有哪些,为何提供这些指标,以及我们如何去作监控。web
监控指标
就像com.zaxxer.hikari.metrics.PoolStats
提供的那样,几个重要的指标都存储在poolState中。spring
-
totalConnections 总链接数,包括空闲的链接和使用中的链接。数据库
-
idleConnections 空闲链接数服务器
-
activeConnections 活跃链接数app
totalConnections = activeConnection + idleConnections框架
-
pendingThreads 正在等待链接的线程数量。排查性能问题时,这个指标是一个重要的参考指标,若是正在等待链接的线程在至关一段时间内数量较多,能够考虑扩大数据库链接池的size。(即HikariCP的maxPoolSize)spring-boot
-
maxConnections 最大链接数,统计指标,统计到目前为止链接的最大数量。性能
-
minConnections 最小链接数,统计指标,统计到目前为止链接的最小数量。大数据
-
usageTime 每一个链接使用的时间,当链接被回收的时候会记录此指标:
com.zaxxer.hikari.pool.HikariPool#recycle
ui -
acquireTime 获取每一个链接须要等待时间,一个请求获取数据库链接后或者由于超时失败后,会记录此指标。
-
connectionCreateTime 链接建立时间
如何监控
这里拿咱们比较熟悉的SpringBoot项目为例,同时使用prometheus和grafana,项目中加入promethues的依赖:
implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'io.micrometer:micrometer-registry-prometheus:1.3.0'
同时在SpringBoot项目的application.properties配置文件中加入如下参数:
management.endpoints.web.exposure.include=prometheus 暴露prometheus格式化的指标,这样能够被promethues服务器抓取
接下来咱们须要启动一个prometheus的服务,https://prometheus.io/download/ 而后更改下peometheus服务的默认配置再启动,追加配置以下:
- job_name: 'prometheus-test' # job名称 scrape_interval: 5s # 抓取时间间隔,这里每5s像数据源请求一次 metrics_path: '/actuator/prometheus' static_configs: - targets: ['127.0.0.1:8080'] # 这里是springBoot项目的地址
接着启动一个grafana的服务,https://grafana.com/get 启动后添加prometheus数据源,制做仪表盘等。
扩展知识
-
Micrometer Vendor-neutral application metrics facade(与供应商无关的应用程序指标外观)。咱们能够把它类比于日志框架中的slf4j。把promethues类比于logback。应用程序直接依赖Micrometer来暴露指标。Micrometer比较受欢迎,已经做为SpringBoot2.0内置的指标门面库。
-
Dropwizard 一个Java框架,相似于SpringBoot,但国内使用较少。其中有提供metrics相关的功能。
-
Prometheus 一个监控器的实现,基于pull模型,定时像prometheus数据源拉取指标信息。作分析、处理和展现。