springBoot + gradle 配置Druid數(shù)據(jù)連接池
首先在build.gradle文件中加入兩個依賴:
然后直接在配置文件中柑肴,配置需要監(jiān)控的數(shù)據(jù)庫信息就好了惶岭,不需要在application.yml中添加配置均牢,(配置文件需要自己另外新建的)
packagecom.etong.government
importcom.alibaba.druid.pool.DruidDataSource;
importorg.slf4j.Logger;
importorg.slf4j.LoggerFactory
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.context.annotation.Primary;
importjavax.sql.DataSource;
importjava.sql.SQLException;
@Configuration
public classDruidConfig {
private static finalLoggerLOGGER= LoggerFactory.getLogger(DruidConfig.class);
//開發(fā)
@Bean(destroyMethod ="close", initMethod ="init")
@Primary
publicDataSource dataSource() {
DruidDataSource datasource =newDruidDataSource();
datasource.setDriverClassName("oracle.jdbc.OracleDriver");
datasource.setUrl("jdbc:oracle:thin:@192.168.10.41:1521:sems");
datasource.setUsername("chargePile");
datasource.setPassword("chargePile");
//其它配置
datasource.setInitialSize(5);
datasource.setMinIdle(5);
datasource.setMaxActive(50);
datasource.setMaxWait(10000);
datasource.setTimeBetweenEvictionRunsMillis(5000);
datasource.setMinEvictableIdleTimeMillis(20000);
datasource.setValidationQuery("SELECT 1 FROM DUAL");
datasource.setTestWhileIdle(true);
datasource.setTestOnBorrow(true);
datasource.setTestOnReturn(false);
datasource.setPoolPreparedStatements(true);
datasource.setMaxPoolPreparedStatementPerConnectionSize(20);
try{
datasource.setFilters("stat,wall,log4j");
}catch(SQLException e) {
LOGGER.error("druid configuration initialization filter", e);
}
returndatasource;
}
@Bean(destroyMethod ="close", initMethod ="init")
publicDataSource dataSource1() {
DruidDataSource datasource =newDruidDataSource();
datasource.setDriverClassName("oracle.jdbc.OracleDriver");
datasource.setUrl("jdbc:oracle:thin:@10.8.20.8:1521:testdb");
datasource.setUsername("cs_wx_db");
datasource.setPassword("cswx");
//其它配置
datasource.setInitialSize(5);
datasource.setMinIdle(5);
datasource.setMaxActive(50);
datasource.setMaxWait(10000);
datasource.setTimeBetweenEvictionRunsMillis(5000);
datasource.setMinEvictableIdleTimeMillis(20000);
datasource.setValidationQuery("SELECT 1 FROM DUAL");
datasource.setTestWhileIdle(true);
datasource.setTestOnBorrow(true);
datasource.setTestOnReturn(false);
datasource.setPoolPreparedStatements(true);
datasource.setMaxPoolPreparedStatementPerConnectionSize(20);
try{
datasource.setFilters("stat,wall,log4j");
}catch(SQLException e) {
LOGGER.error("druid configuration initialization filter", e);
}
returndatasource;
}
}
配置多數(shù)據(jù)源的區(qū)別在于,主數(shù)據(jù)源多了一個@Primary的注解
主:
從: