hikaricp-plus-spring-boot-starter

Spring Boot Starter For HikariCP

说明

基于HikariCP数据源的Spring Boot Starter 实现

  1. HikariDataSource 自动初始化
  2. 基于HikariDataSource的动态数据源实现
  3. 基于AOP+注解实现数据源按需切换
  4. HikariDataSource数据源监控逻辑

Maven

<dependency>
    <groupId>com.github.hiwepy</groupId>
    <artifactId>hikaricp-plus-spring-boot-starter</artifactId>
    <version>${project.version}</version>
</dependency>
spring:
  # 数据源配置:
  datasource:
    name: ${spring.application.name}
    # HiKariCP的数据源配置:https://blog.csdn.net/qq_25027457/article/details/124724042
    hikari:
      enabled: true
      # 连接初始化SQL
      connection-init-sql: select SYSDATE()
      # 连接测试语句
      connection-test-query: select SYSDATE()
      # 连接池初始化失败超时时间;单位 (毫秒)
      initialization-fail-timeout: 30000
      # 内存泄漏侦测入口
      leak-detection-threshold: 30000
      # 一个连接的最大的生命时间(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒以上
      # 根据MySQL wait_timeout参数值,默认值为28800秒(即8小时)(show variables like '%timeout%';) 这里设置时长为 7小时
      max-lifetime: 3600000
      # 连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count)
      max-pool-size: 10
      # 最小闲置线程
      min-idle: 5
      # 生效超时
      validation-timeout: 30000
集成 Prometheus 监控

项目中引入 micrometer-prometheus 依赖,可实现 hikaricp 组件的指标采集

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

采集指标如下:

# HELP hikaricp_connections Total connections
# TYPE hikaricp_connections gauge
hikaricp_connections{application="app-test",pool="master",} 10.0
# HELP hikaricp_connections_timeout_total Connection timeout total count
# TYPE hikaricp_connections_timeout_total counter
hikaricp_connections_timeout_total{application="app-test",pool="master",} 0.0
# HELP hikaricp_connections_idle Idle connections
# TYPE hikaricp_connections_idle gauge
hikaricp_connections_idle{application="app-test",pool="master",} 10.0
# HELP hikaricp_connections_active Active connections
# TYPE hikaricp_connections_active gauge
hikaricp_connections_active{application="app-test",pool="master",} 0.0
# HELP hikaricp_connections_creation_seconds_max Connection creation time
# TYPE hikaricp_connections_creation_seconds_max gauge
hikaricp_connections_creation_seconds_max{application="app-test",pool="master",} 0.0
# HELP hikaricp_connections_creation_seconds Connection creation time
# TYPE hikaricp_connections_creation_seconds summary
hikaricp_connections_creation_seconds_count{application="app-test",pool="master",} 0.0
hikaricp_connections_creation_seconds_sum{application="app-test",pool="master",} 0.0
# HELP hikaricp_connections_min Min connections
# TYPE hikaricp_connections_min gauge
hikaricp_connections_min{application="app-test",pool="master",} 10.0
# HELP hikaricp_connections_usage_seconds Connection usage time
# TYPE hikaricp_connections_usage_seconds summary
hikaricp_connections_usage_seconds_count{application="app-test",pool="master",} 4.0
hikaricp_connections_usage_seconds_sum{application="app-test",pool="master",} 0.105
# HELP hikaricp_connections_usage_seconds_max Connection usage time
# TYPE hikaricp_connections_usage_seconds_max gauge
hikaricp_connections_usage_seconds_max{application="app-test",pool="master",} 0.084
# HELP hikaricp_connections_max Max connections
# TYPE hikaricp_connections_max gauge
hikaricp_connections_max{application="app-test",pool="master",} 10.0
# HELP hikaricp_connections_acquire_seconds Connection acquire time
# TYPE hikaricp_connections_acquire_seconds summary
hikaricp_connections_acquire_seconds_count{application="app-test",pool="master",} 4.0
hikaricp_connections_acquire_seconds_sum{application="app-test",pool="master",} 0.0209152
# HELP hikaricp_connections_acquire_seconds_max Connection acquire time
# TYPE hikaricp_connections_acquire_seconds_max gauge
hikaricp_connections_acquire_seconds_max{application="app-test",pool="master",} 0.011627699
# HELP hikaricp_connections_pending Pending threads
# TYPE hikaricp_connections_pending gauge
hikaricp_connections_pending{application="app-test",pool="master",} 0.0

Sample

https://github.com/vindell/spring-boot-starter-samples/tree/master/spring-boot-sample-hikaricp

作者:Jeebiz  创建时间:2023-03-20 23:58
最后编辑:Jeebiz  更新时间:2024-10-05 00:01