第九章:springboot2 配置Hikari数据库连接池,开
2023-12-10 本文已影响0人
菜出意料
默认的配置无法自定义hikari的配置数据,只能使用默认值
示例中开启了MicrometerMetricsTrackerFactory
添加mysql jdbc依赖
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
自定义Hikari配置类
@ConfigurationProperties("spring.datasource.hikari")
public class HikariProperties extends HikariConfig {
/**
* metrics日志输出时间间隔,单位分钟
*/
@Value("${loggingMeterPushMetricsStep:15}")
private int loggingMeterPushMetricsStep;
public int getLoggingMeterPushMetricsStep() {
return loggingMeterPushMetricsStep;
}
public void setLoggingMeterPushMetricsStep(int loggingMeterPushMetricsStep) {
this.loggingMeterPushMetricsStep = loggingMeterPushMetricsStep;
}
}
配置类
@Configuration
@EnableConfigurationProperties({HikariProperties.class})
public class HikariConfiguration {
private final Logger log = LoggerFactory.getLogger(HikariConfiguration.class);
private final DataSourceProperties dataSourceProperties;
private final HikariProperties hikariProperties;
public HikariConfiguration(DataSourceProperties dataSourceProperties, HikariProperties hikariProperties) {
this.dataSourceProperties = dataSourceProperties;
this.hikariProperties = hikariProperties;
}
@Bean
public DataSource dataSource() {
log.info("初始化数据源,使用Hikari数据库连接池");
hikariProperties.setJdbcUrl(dataSourceProperties.getUrl());
hikariProperties.setUsername(dataSourceProperties.getUsername());
hikariProperties.setPassword(dataSourceProperties.getPassword());
hikariProperties.setDriverClassName(dataSourceProperties.getDriverClassName());
hikariProperties.setMetricsTrackerFactory(initMicrometerMetricsTrackerFactory());
HikariDataSource dataSource = new HikariDataSource(hikariProperties);
return dataSource;
}
private MicrometerMetricsTrackerFactory initMicrometerMetricsTrackerFactory() {
log.info("使用LoggingMeterRegistry构建Hikari数据连接池的MicrometerMetricsTrackerFactory");
final LoggingMeterRegistry registry = LoggingMeterRegistry
.builder(new LoggingRegistryConfig() {
@Override
public String get(String key) {
return null;
}
@Override
public Duration step() {
return getDuration(this, "step").orElse(Duration.ofMinutes(hikariProperties.getLoggingMeterPushMetricsStep()));
}
})
.threadFactory(new NamedThreadFactory("hikari-logging-metrics-publisher"))
.build();
return new MicrometerMetricsTrackerFactory(registry);
}
}
yaml配置
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:mysql://192.168.110.108:3306/aus-call-center?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
username: root
password: shanglian123
driver-class-name: com.mysql.cj.jdbc.Driver
# 参照https://github.com/brettwooldridge/HikariCP#gear-configuration-knobs-baby
hikari:
# 默认值30000
connectionTimeout: 10000
# 默认值5000
validationTimeout: 1000
# 空闲时间 默认值10000
idleTimeout: 10000
maxLifetime: 30000
# 默认值10
maximumPoolSize: 5
minimumIdle: 3
poolName: "mysql-auscallcenter-pool"
loggingMeterPushMetricsStep: 15
# mysql性能调优参数,参照https://github.com/brettwooldridge/HikariCP/wiki/MySQL-Configuration
data-source-properties:
cachePrepStmts: true
prepStmtCacheSize: 250
prepStmtCacheSqlLimit: 2048
useServerPrepStmts: true
useLocalSessionState: true
# 批量增删改数据时效率高
rewriteBatchedStatements: true
cacheResultSetMetadata: true
cacheServerConfiguration: true
elideSetAutoCommits: true
maintainTimeStats: false