您好,登錄后才能下訂單哦!
前言
Springboot讓Java開發更加美好,更加簡潔,更加簡單。Spring Boot 2.x中使用HikariCP作為默認的數據連接池。 HikariCP使用Javassist字節碼操作庫來實現動態代理,優化并精簡了字節碼,同時內部使用 com.zaxxer.hikari.util.FastList
代替ArrayList、使用了更好的并發集合類 com.zaxxer.hikari.util.ConcurrentBag
,“號稱”是目前最快的數據庫連接池。
下面話不多說了,來一起看看詳細的介紹吧
基本使用
在Spring Boot 2.x中使用HikariCP十分簡單,只需引入依賴 implementation 'org.springframework.boot:spring-boot-starter-jdbc'
:
pluginManagement { repositories { gradlePluginPortal() } } rootProject.name = 'datasource-config' plugins { id 'org.springframework.boot' version '2.1.3.RELEASE' id 'java' } apply plugin: 'io.spring.dependency-management' group = 'spring-boot-guides' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8' repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-jdbc' runtimeOnly 'com.h3database:h3' testImplementation 'org.springframework.boot:spring-boot-starter-test' }
配置文件如下:
spring: datasource: url: jdbc:h3:mem:demodb username: sa password: hikari: # https://github.com/brettwooldridge/HikariCP (uses milliseconds for all time values) maximumPoolSize: 10 minimumIdle: 2 idleTimeout: 600000 connectionTimeout: 30000 maxLifetime: 1800000
關于連接池的具體配置參數詳見 HikariCP 。
示例代碼如下:
package springbootguides.datasourceconfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import javax.sql.DataSource; import java.sql.Connection; @SpringBootApplication public class DatasourceConfigApplication implements CommandLineRunner { @Autowired private DataSource datasource; @Override public void run(String... args) throws Exception { try(Connection conn = datasource.getConnection()) { System.out.println(conn); } } public static void main(String[] args) { SpringApplication.run(DatasourceConfigApplication.class, args); } }
實現原理
Spring Boot使用如下方式整合HikariCP:入口是 org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
,通過 org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.Hikari
中的 @Bean 方式創建 com.zaxxer.hikari.HikariDataSource
:
/** * Hikari DataSource configuration. */ @ConditionalOnClass(HikariDataSource.class) @ConditionalOnMissingBean(DataSource.class) @ConditionalOnProperty(name = "spring.datasource.type", havingValue = "com.zaxxer.hikari.HikariDataSource", matchIfMissing = true) static class Hikari { @Bean @ConfigurationProperties(prefix = "spring.datasource.hikari") public HikariDataSource dataSource(DataSourceProperties properties) { HikariDataSource dataSource = createDataSource(properties, HikariDataSource.class); if (StringUtils.hasText(properties.getName())) { dataSource.setPoolName(properties.getName()); } return dataSource; } }
@ConfigurationProperties(prefix = "spring.datasource.hikari")
會自動把 spring.datasource.hikari.*
相關的連接池配置信息注入到創建的HikariDataSource實例中。
HikariCP的監控和遙測
因為在我們的微服務體系中使用的監控系統是Prometheus,這里以Prometheus為例。
注意spring boot 2.0對spring boot 1.x的metrics進行了重構,不再向后兼容,主要是在spring-boot-acutator中使用了micrometer,支持了更多的監控系統:Atlas、Datadog、Ganglia、Graphite、Influx、JMX、NewRelic、Prometheus、SignalFx、StatsD、Wavefront。Spring boot 2.0的metrics對比spring boot 1.x除了引入micrometer外,更大的體現是支持了tag,這也說明Prometheus、Influx等支持Tag的時序監控數據模型的監控系統已經成為主流。
在前面示例中的build.gradle中加入如下依賴:
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'io.micrometer:micrometer-registry-prometheus'
配置文件applycation.yaml中加入對actuator的配置:
management: endpoints: web: exposure: include: "health,info,prometheus" server: port: 8079 servlet: context-path: /
注意這里引入了web和actuator依賴,通過配置 management.server.port 指定actuator的web端點為8089端口,通過 management.endpoints.include 對外開放 /actuator/prometheus ,在引入 io.micrometer:micrometer-registry-prometheus 依賴之后,端點 /actuator/prometheus 當即生效。
curl http://localhost:8079/actuator/prometheus | grep hikari # TYPE hikaricp_connections_acquire_seconds summary hikaricp_connections_acquire_seconds_count{pool="HikariPool-1",} 3.0 hikaricp_connections_acquire_seconds_sum{pool="HikariPool-1",} 0.001230082 # HELP hikaricp_connections_acquire_seconds_max Connection acquire time # TYPE hikaricp_connections_acquire_seconds_max gauge hikaricp_connections_acquire_seconds_max{pool="HikariPool-1",} 0.0 # HELP hikaricp_connections_min Min connections # TYPE hikaricp_connections_min gauge hikaricp_connections_min{pool="HikariPool-1",} 2.0 # TYPE hikaricp_connections_timeout_total counter hikaricp_connections_timeout_total{pool="HikariPool-1",} 0.0 # HELP hikaricp_connections_pending Pending threads # TYPE hikaricp_connections_pending gauge hikaricp_connections_pending{pool="HikariPool-1",} 0.0 # HELP hikaricp_connections_usage_seconds Connection usage time # TYPE hikaricp_connections_usage_seconds summary hikaricp_connections_usage_seconds_count{pool="HikariPool-1",} 3.0 hikaricp_connections_usage_seconds_sum{pool="HikariPool-1",} 0.06 # HELP hikaricp_connections_usage_seconds_max Connection usage time # TYPE hikaricp_connections_usage_seconds_max gauge hikaricp_connections_usage_seconds_max{pool="HikariPool-1",} 0.0 # HELP hikaricp_connections_max Max connections # TYPE hikaricp_connections_max gauge hikaricp_connections_max{pool="HikariPool-1",} 10.0 # HELP hikaricp_connections Total connections # TYPE hikaricp_connections gauge hikaricp_connections{pool="HikariPool-1",} 2.0 # HELP hikaricp_connections_creation_seconds_max Connection creation time # TYPE hikaricp_connections_creation_seconds_max gauge hikaricp_connections_creation_seconds_max{pool="HikariPool-1",} 0.0 # HELP hikaricp_connections_creation_seconds Connection creation time # TYPE hikaricp_connections_creation_seconds summary hikaricp_connections_creation_seconds_count{pool="HikariPool-1",} 1.0 hikaricp_connections_creation_seconds_sum{pool="HikariPool-1",} 0.001 # HELP hikaricp_connections_idle Idle connections # TYPE hikaricp_connections_idle gauge hikaricp_connections_idle{pool="HikariPool-1",} 2.0 # HELP hikaricp_connections_active Active connections # TYPE hikaricp_connections_active gauge hikaricp_connections_active{pool="HikariPool-1",} 0.0
參考
• HikariCP
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。