欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Spring boot 连接多数据源过程详解

时间:2020-12-17 01:52:45|栏目:JAVA代码|点击:

1.application.yml中添加两个datasource

server:
 port: 8080
spring:
 application:
  name: king
 datasource:
  master:
   type: com.zaxxer.hikari.HikariDataSource
   jdbc-url: jdbc:mysql://localhost:3306/king?useUnicode=yes&characterEncoding=UTF-8&serverTimezone=UTC
   driver-class-name: com.mysql.cj.jdbc.Driver
   username: root
   password: 123456
   idle-timeout: 10000
   minimum-idle: 1
   maximum-pool-size: 10
   pool-name: master
   auto-commit: false
   connection-test-query: SELECT 1
  slave:
   type: com.zaxxer.hikari.HikariDataSource
   jdbc-url: jdbc:mysql://localhost:3306/king?useUnicode=yes&characterEncoding=UTF-8&serverTimezone=UTC
   driver-class-name: com.mysql.cj.jdbc.Driver
   username: root
   password: 123456
   idle-timeout: 10000
   minimum-idle: 1
   maximum-pool-size: 10
   pool-name: slave
   auto-commit: false
   connection-test-query: SELECT 1

2.手动初始化数据源masterDataSource

package cn.bj.king.config;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
/**
* @author ARongking
* @date 2019-03-28
*/
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = {"cn.bj.king.mapper.master"},
    sqlSessionFactoryRef = "masterSqlSessionFactory")
public class WriteDataSourceConfig {

  @Bean(name = "masterDataSource")
  @Primary
  @Qualifier("masterDataSource")
  public DataSource masterDataSource(@Qualifier("masterHikariConfig")HikariConfig hikariConfig) {
    System.out.println("实例化主库");
    HikariDataSource hikariDataSource=new HikariDataSource(hikariConfig);
    return hikariDataSource;
  }
  /**
  * 配置连接池信息
  * @return
  */
  @ConfigurationProperties(prefix = "spring.datasource.master")
  @Bean("masterHikariConfig")
  public HikariConfig masterHikariConfig(){
    HikariConfig hikariConfig=new HikariConfig();
    return hikariConfig;
  }
  /**
   * SqlSessionFactory配置
   *
   * @return
   * @throws Exception
   */
  @Bean(name = "masterSqlSessionFactory")
  @Primary
  public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSource);
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    // 配置mapper文件位置
    sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mapper/master/*.xml"));
    sqlSessionFactoryBean.setTypeAliasesPackage("cn.bj.king.entity");
    return sqlSessionFactoryBean.getObject();
  }

  /**
   * 配置事物管理器
   *
   * @return
   */
  @Bean(name = "masterTransactionManager")
  @Primary
  public DataSourceTransactionManager masterTransactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
    DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
    dataSourceTransactionManager.setDataSource(dataSource);
    return dataSourceTransactionManager;
  }
}

3.手动初始化数据源ReadDataSource

package cn.bj.king.config;
import com.github.pagehelper.PageInterceptor;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = {"cn.bj.king.mapper.slave"},
    sqlSessionFactoryRef = "slaveSqlSessionFactory")
public class ReadDataSourceConfig {
  @Autowired
  Environment environment;

  @Bean(name = "slaveDataSource")
  @Qualifier("slaveDataSource")
  public DataSource slaveDataSource(@Qualifier("slaveHikariConfig")HikariConfig hikariConfig) {
    System.out.println("实例化从库");
    HikariDataSource hikariDataSource=new HikariDataSource(hikariConfig);
    return hikariDataSource;
  }

  /**
   * 配置连接池信息
   * @return
   */
  @ConfigurationProperties(prefix = "spring.datasource.slave")
  @Bean("slaveHikariConfig")
  public HikariConfig slaveHikariConfig(){
    HikariConfig hikariConfig=new HikariConfig();
    return hikariConfig;
  }

  /**
   * SqlSessionFactory配置
   *
   * @return
   * @throws Exception
   */
  @Bean(name = "slaveSqlSessionFactory")
  public SqlSessionFactory slaveSqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception {
    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSource);
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    // 配置mapper文件位置
    sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mapper/slave/*.xml"));
    sqlSessionFactoryBean.setTypeAliasesPackage("cn.bj.king.entity");
    return sqlSessionFactoryBean.getObject();
  }
  /**
   * 配置事物管理器
   *
   * @return
   */
  @Bean(name = "slaveTransactionManager")
  public DataSourceTransactionManager slaveTransactionManager(@Qualifier("slaveDataSource") DataSource dataSource) {
    DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
    dataSourceTransactionManager.setDataSource(dataSource);
    return dataSourceTransactionManager;
  }
}

4.测试启动是否正常初始化master和slave 两个数据源

5.这里需要注意几个地方:

  • 配置数据库连接池的时候,如果不是手动设置HikariConfig ,即通过Environment 去一个个获取数据库连接池在yml中的配置的话,数据库连接池的(idle-timeout,minimum-idle, maximum-pool-size,pool-name,auto-commit,connection-test-query)等,一定要与slave节点平级,不然 HikariConfig 是读取不到这些信息的。
  • 如果手动赋值HikariConfig 的话,那就没有什么限制了,写到哪里都行,只需要用Environment 去读取配置文件的值,手动设置到HikariConfig 就行了。

上一篇:如何使用Spring Boot ApplicationRunner解析命令行中的参数

栏    目:JAVA代码

下一篇:Java爬虫抓取视频网站下载链接

本文标题:Spring boot 连接多数据源过程详解

本文地址:http://www.codeinn.net/misctech/33618.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有