不废话,直接上代码
application.yml配置文件
spring:
#clickhouse数据源配置
clickhouse:
driver-class-name: ru.yandex.clickhouse.ClickHouseDriver
jdbcurl: jdbc:clickhouse://xxx.xxx.xxx.xxx:xxx/
username: xxxxx
password: xxxxxx
type: com.alibaba.druid.pool.DruidDataSource
postgresql:
jdbcurl: jdbc:postgresql://xx.xxx.xxx.xxx:5432/xxx
username: xxxxx
password: xxxxxxx
driver-class-name: org.postgresql.Driver
type: com.alibaba.druid.pool.DruidDataSource
#数据源配置
datasource:
commonConfig: #连接池统一配置,应用到所有的数据源
#连接池初始化时初始化的数据库连接数
initialSize: 1
#最小连接池数量
minIdle: 1
#已经不再使用,配置了也没效果
maxIdle: 5
#最大连接池数量
maxActive: 50
#获取连接时最大等待时间,单位毫秒。配置了maxWait之后,缺省启用公平锁,并发效率会有所下降,如果需要可以通过配置useUnfairLock属性为true使用非公平锁。
maxWait: 10000
#建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
testWhileIdle: true
#有两个含义:1) Destroy线程会检测连接的间隔时间 2) testWhileIdle的判断依据,详细看testWhileIdle属性的说明
timeBetweenEvictionRunsMillis: 10000
#配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 300000
#验证连接有效与否的SQL,不同的数据配置不同
validationQuery: select 'x'
#申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
testOnBorrow: false
#归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
testOnReturn: false
#打开PSCache,并且指定每个连接上PSCache的大小
poolPreparedStatements: true
#要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。在Druid中,不会存在Oracle下PSCache占用内存过多的问题,可以把这个数值配置大一些,比如说100
maxOpenPreparedStatements: 20
#属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有:监控统计用的filter:stat日志用的filter:log4j防御sql注入的filter:wall
filters: stat
数据源配置类
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
@Component
public class DataSourceConfig {
@Value("${spring.datasource.commonConfig.initialSize}")
private int initialSize;
@Value("${spring.datasource.commonConfig.minIdle}")
private int minIdle;
@Value("${spring.datasource.commonConfig.maxActive}")
private int maxActive;
@Value("${spring.datasource.commonConfig.maxWait}")
private int maxWait;
@Value("${spring.datasource.commonConfig.timeBetweenEvictionRunsMillis}")
private int timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.commonConfig.minEvictableIdleTimeMillis}")
private int minEvictableIdleTimeMillis;
@Value("${spring.datasource.commonConfig.validationQuery}")
private String validationQuery;
@Value("${spring.datasource.commonConfig.testWhileIdle}")
private boolean testWhileIdle;
@Value("${spring.datasource.commonConfig.testOnBorrow}")
private boolean testOnBorrow;
@Value("${spring.datasource.commonConfig.testOnReturn}")
private boolean testOnReturn;
@Value("${spring.datasource.commonConfig.poolPreparedStatements}")
private boolean poolPreparedStatements;
@Value("${spring.datasource.commonConfig.maxOpenPreparedStatements}")
private int maxOpenPreparedStatements;
@Value("${spring.datasource.commonConfig.filters}")
private String filters;
public int getInitialSize() {
return initialSize;
}
public void setInitialSize(int initialSize) {
this.initialSize = initialSize;
}
public int getMinIdle() {
return minIdle;
}
public void setMinIdle(int minIdle) {
this.minIdle = minIdle;
}
public int getMaxActive() {
return maxActive;
}
public void setMaxActive(int maxActive) {
this.maxActive = maxActive;
}
public int getMaxWait() {
return maxWait;
}
public void setMaxWait(int maxWait) {
this.maxWait = maxWait;
}
public int getTimeBetweenEvictionRunsMillis() {
return timeBetweenEvictionRunsMillis;
}
public void setTimeBetweenEvictionRunsMillis(int timeBetweenEvictionRunsMillis) {
this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
}
public int getMinEvictableIdleTimeMillis() {
return minEvictableIdleTimeMillis;
}
public void setMinEvictableIdleTimeMillis(int minEvictableIdleTimeMillis) {
this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
}
public String getValidationQuery() {
return validationQuery;
}
public void setValidationQuery(String validationQuery) {
this.validationQuery = validationQuery;
}
public boolean isTestWhileIdle() {
return testWhileIdle;
}
public void setTestWhileIdle(boolean testWhileIdle) {
this.testWhileIdle = testWhileIdle;
}
public boolean isTestOnBorrow() {
return testOnBorrow;
}
public void setTestOnBorrow(boolean testOnBorrow) {
this.testOnBorrow = testOnBorrow;
}
public boolean isTestOnReturn() {
return testOnReturn;
}
public void setTestOnReturn(boolean testOnReturn) {
this.testOnReturn = testOnReturn;
}
public boolean isPoolPreparedStatements() {
return poolPreparedStatements;
}
public void setPoolPreparedStatements(boolean poolPreparedStatements) {
this.poolPreparedStatements = poolPreparedStatements;
}
public int getMaxOpenPreparedStatements() {
return maxOpenPreparedStatements;
}
public void setMaxOpenPreparedStatements(int maxOpenPreparedStatements) {
this.maxOpenPreparedStatements = maxOpenPreparedStatements;
}
public String getFilters() {
return filters;
}
public void setFilters(String filters) {
this.filters = filters;
}
@Primary //主数据源
@Bean(name = "clickhouse")
@Qualifier("clickhouse")
@ConfigurationProperties(prefix = "spring.clickhouse")
public DataSource clickHouseDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "postgresql")
@Qualifier("postgresql")
@ConfigurationProperties(prefix = "spring.postgresql")
public DataSource postgreSqlDataSource(){
return DataSourceBuilder.create().build();
}
}
prefix 指向配置文件中数据源的配置路径
jdbc配置类
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import javax.sql.DataSource;
@Repository
public class JDBCTemplateConfig {
@Bean(name="ckJdbcTemplate")
public JdbcTemplate clickHouseJDBCTemplate(@Qualifier("clickhouse")DataSource dataSource){
return new JdbcTemplate(dataSource);
}
@Bean(name="pgJdbcTemplate")
public JdbcTemplate postgreSqlJDBCTemplate(@Qualifier("postgresql")DataSource dataSource){
return new JdbcTemplate(dataSource);
}
}
连接使用
@Repository
public class ClickHouseDao {
@Autowired
@Qualifier("ckJdbcTemplate")
private JdbcTemplate jdbcTemplate;
}
@Repository
@Transactional
public class PgDao {
@Autowired
@Qualifier("pgJdbcTemplate")
private JdbcTemplate jdbcTemplate;
}