真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

SpringBoot如何使用HikariCP連接池詳解

前言

創(chuàng)新互聯(lián)專注骨干網(wǎng)絡(luò)服務(wù)器租用10余年,服務(wù)更有保障!服務(wù)器租用,服務(wù)器托管 成都服務(wù)器租用,成都服務(wù)器托管,骨干網(wǎng)絡(luò)帶寬,享受低延遲,高速訪問。靈活、實現(xiàn)低成本的共享或公網(wǎng)數(shù)據(jù)中心高速帶寬的專屬高性能服務(wù)器。

Springboot讓Java開發(fā)更加美好,更加簡潔,更加簡單。Spring Boot 2.x中使用HikariCP作為默認的數(shù)據(jù)連接池。 HikariCP使用Javassist字節(jié)碼操作庫來實現(xiàn)動態(tài)代理,優(yōu)化并精簡了字節(jié)碼,同時內(nèi)部使用 com.zaxxer.hikari.util.FastList 代替ArrayList、使用了更好的并發(fā)集合類 com.zaxxer.hikari.util.ConcurrentBag ,“號稱”是目前最快的數(shù)據(jù)庫連接池。

下面話不多說了,來一起看看詳細的介紹吧

基本使用

在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

關(guān)于連接池的具體配置參數(shù)詳見 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);
	}

}

實現(xiàn)原理

Spring Boot使用如下方式整合HikariCP:入口是 org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration ,通過 org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.Hikari 中的 @Bean 方式創(chuàng)建 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.* 相關(guān)的連接池配置信息注入到創(chuàng)建的HikariDataSource實例中。

HikariCP的監(jiān)控和遙測

因為在我們的微服務(wù)體系中使用的監(jiān)控系統(tǒng)是Prometheus,這里以Prometheus為例。

注意spring boot 2.0對spring boot 1.x的metrics進行了重構(gòu),不再向后兼容,主要是在spring-boot-acutator中使用了micrometer,支持了更多的監(jiān)控系統(tǒng):Atlas、Datadog、Ganglia、Graphite、Influx、JMX、NewRelic、Prometheus、SignalFx、StatsD、Wavefront。Spring boot 2.0的metrics對比spring boot 1.x除了引入micrometer外,更大的體現(xiàn)是支持了tag,這也說明Prometheus、Influx等支持Tag的時序監(jiān)控數(shù)據(jù)模型的監(jiān)控系統(tǒng)已經(jīng)成為主流。

在前面示例中的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

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對創(chuàng)新互聯(lián)的支持。


網(wǎng)站題目:SpringBoot如何使用HikariCP連接池詳解
當前鏈接:http://weahome.cn/article/pgpdgi.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部