StringBoot多数据源支持

多数据元配置

  1. 首先在配置文件中根据spring.xxx前缀配置两个数据源

#center
spring.ds_center.type=com.alibaba.druid.pool.DruidDataSource
spring.ds_center.driver-class-name=com.mysql.jdbc.Driver
spring.ds_center.url=jdbc:mysql://192.168.xx.xx:3306/db1?useSSL=false
spring.ds_center.username=root
spring.ds_center.password=123456

# 下面为连接池的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
spring.ds_center.initialSize=5
spring.ds_center.minIdle=5
spring.ds_center.maxActive=20
# 配置获取连接等待超时的时间
spring.ds_center.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.ds_center.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.ds_center.minEvictableIdleTimeMillis=300000
spring.ds_center.validationQuery=SELECT 1 FROM DUAL
spring.ds_center.testWhileIdle=true
spring.ds_center.testOnBorrow=false
spring.ds_center.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.ds_center.poolPreparedStatements=true
spring.ds_center.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.ds_center.filters=stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.ds_center.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
#spring.datasource.useGlobalDataSourceStat=true


#manage
spring.ds_manage.type=com.alibaba.druid.pool.DruidDataSource
spring.ds_manage.driver-class-name=com.mysql.jdbc.Driver
spring.ds_manage.url=jdbc:mysql://192.168.xx.xx:3306/db2?useSSL=false
spring.ds_manage.username=root
spring.ds_manage.password=123456

# 下面为连接池的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
spring.ds_manage.initialSize=5
spring.ds_manage.minIdle=5
spring.ds_manage.maxActive=20
# 配置获取连接等待超时的时间
spring.ds_manage.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.ds_manage.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.ds_manage.minEvictableIdleTimeMillis=300000
spring.ds_manage.validationQuery=SELECT 1 FROM DUAL
spring.ds_manage.testWhileIdle=true
spring.ds_manage.testOnBorrow=false
spring.ds_manage.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.ds_manage.poolPreparedStatements=true
spring.ds_manage.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.ds_manage.filters=stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.ds_manage.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
#spring.datasource.useGlobalDataSourceStat=true
  1. 然后
package com.xx.xx.dao;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.sql.DataSource;

/**
 * 数据库多实例配置
 */
@Configuration
public class MultipleDBConfig {

    @Bean(name = "centerDb")
    @Primary
    @ConfigurationProperties(prefix = "spring.ds_center")
    public DataSource centerDataSource() {
//        return DataSourceBuilder.create().build();
        return new DruidDataSource();
    }

    @Bean(name = "centerJdbcTemplate")
    public JdbcTemplate centerJdbcTemplate(@Qualifier("centerDb") DataSource dsMySQL) {
        return new JdbcTemplate(dsMySQL);
    }

    @Bean(name = "centerNamedJdbcTemplate")
    public NamedParameterJdbcTemplate centerNamedJdbcTemplate(@Qualifier("centerDb") DataSource dsMySQL) {
        return new NamedParameterJdbcTemplate(dsMySQL);
    }

    @Bean(name = "manageDb")
    @ConfigurationProperties(prefix = "spring.ds_manage")
    public DataSource manageDataSource() {
//        return DataSourceBuilder.create().build();
        return new DruidDataSource();
    }

    @Bean(name = "manageJdbcTemplate")
    public JdbcTemplate manageJdbcTemplate(@Qualifier("manageDb") DataSource dsMySQL) {
        return new JdbcTemplate(dsMySQL);
    }

    @Bean(name = "manageNamedJdbcTemplate")
    public NamedParameterJdbcTemplate manageNamedJdbcTemplate(@Qualifier("manageDb") DataSource dsMySQL) {
        return new NamedParameterJdbcTemplate(dsMySQL);
    }

}
  1. 通过自动注入的方式使用(同其他组件)
    @Autowired
    @Qualifier("manageJdbcTemplate")
    protected JdbcTemplate jdbcTemplate;
    @Autowired
    @Qualifier("manageNamedJdbcTemplate")
    protected NamedParameterJdbcTemplate namedJdbcTemplate;

    @Autowired
    @Qualifier("centerJdbcTemplate")
    protected JdbcTemplate centerJdbcTemplate;
    @Autowired
    @Qualifier("centerNamedJdbcTemplate")
    protected NamedParameterJdbcTemplate centerNamedJdbcTemplate;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值