最近工作中有用到雙數據源,一個項目(中台)中需要操作兩個不同的數據庫。當時考慮到了兩種方式,
1.通過http請求訪問(A項目訪問d1數據源,B項目訪問d2數據源,B通過http訪問A的接口)
2.配置雙數據源(A項目訪問d1數據源,B項目配置d1,d2數據源)
兩種方式各有利弊,http簡單,但是效率不如雙數據源。雙數據源相對復雜一些,但效率比較好,最后決定使用雙數據源。
步驟如下:
1.applicationContext.properties配置文件中配置兩個數據庫連接信息
#db1數據庫連接 spring.datasource.d1.url=jdbc:oracle:thin:@localhost:1521:db1 spring.datasource.d1.username=user1 spring.datasource.d1.password=123456 spring.datasource.d1.driver-class-name=oracle.jdbc.OracleDriver #驗證連接的有效性 spring.datasource.d1.test-while-idle=true #獲取連接時候驗證,會影響性能 spring.datasource.d1.test-on-borrow=true #在連接歸還到連接池時是否測試該連接 spring.datasource.d1.test-on-return=true spring.datasource.d1.validation-query=SELECT 1 FROM DUAL #空閑連接回收的時間間隔,與test-while-idle一起使用,設置5分鍾 spring.datasource.d1.time-between-eviction-runs-millis=300000 #連接池空閑連接的有效時間 ,設置30分鍾 spring.datasource.d1.min-evictable-idle-time-millis=1800000 spring.datasource.d1.initial-size=5 #指定連接池中最大的活躍連接數. spring.datasource.d1.max-active=50 #指定連接池等待連接返回的最大等待時間,毫秒單位. spring.datasource.d1.max-wait=60000 #指定必須保持連接的最小值 spring.datasource.d1.min-idle=5 #db2數據庫連接 spring.datasource.d2.url=jdbc:oracle:thin:@localhost:1521/db2 spring.datasource.d2.username=user2 spring.datasource.d2.password=123456 spring.datasource.d2.driver-class-name=oracle.jdbc.OracleDriver #驗證連接的有效性 spring.datasource.d2.test-while-idle=true #獲取連接時候驗證,會影響性能 spring.datasource.d2.test-on-borrow=true #在連接歸還到連接池時是否測試該連接 spring.datasource.d2.test-on-return=true spring.datasource.d2.validation-query=SELECT 1 FROM DUAL #空閑連接回收的時間間隔,與test-while-idle一起使用,設置5分鍾 spring.datasource.d2.time-between-eviction-runs-millis=300000 #連接池空閑連接的有效時間 ,設置30分鍾 spring.datasource.d2.min-evictable-idle-time-millis=1800000 spring.datasource.d2.initial-size=5 #指定連接池中最大的活躍連接數. spring.datasource.d2.max-active=50 #指定連接池等待連接返回的最大等待時間,毫秒單位. spring.datasource.d2.max-wait=60000 #指定必須保持連接的最小值 spring.datasource.d2.min-idle=5
2.配置文件Ds1MybatisConfig.java,Ds2MybatisConfig.java文件,配置數據源,sqlSessionFactory等信息。
1 @Configuration 2 @MapperScan(value = {"com.test.d1.mapper"}, sqlSessionFactoryRef = "d1SqlSessionFactory") 3 public class Ds1MybatisConfig{ 4 5 @Primary 6 @Bean(name = "d1DataSource") 7 @ConfigurationProperties(prefix = "spring.datasource.d1") 8 public DataSource d1DataSource() { 9 return new org.apache.tomcat.jdbc.pool.DataSource(); 10 } 11 12 @Bean(name = "d1SqlSessionFactory") 13 public SqlSessionFactory d1SqlSessionFactoryBean() throws Exception { 14 15 SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); 16 sqlSessionFactoryBean.setDataSource(d1DataSource()); 17 sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:/mybatis/d1/*.xml")); 18 return sqlSessionFactoryBean.getObject(); 19 } 20 21 @Bean 22 public PlatformTransactionManager d1TransactionManager() { 23 return new DataSourceTransactionManager(d1DataSource()); 24 } 25 }
1 @Configuration 2 @MapperScan(value = {"com.test.d2.mapper"}, sqlSessionFactoryRef = "d2SqlSessionFactory") 3 public class Ds2MybatisConfig{ 4 5 @Bean(name = "d2DataSource") 6 @ConfigurationProperties(prefix = "spring.datasource.d2") 7 public DataSource d2DataSource() { 8 return new org.apache.tomcat.jdbc.pool.DataSource(); 9 } 10 11 @Bean(name = "d2SqlSessionFactory") 12 public SqlSessionFactory d2SqlSessionFactory() throws Exception { 13 SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); 14 sqlSessionFactoryBean.setDataSource(d2DataSource()); 15 sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:/mybatis/d2/*Mapper.xml")); 16 return sqlSessionFactoryBean.getObject(); 17 } 18 19 @Bean 20 public PlatformTransactionManager d2TransactionManager() { 21 return new DataSourceTransactionManager(d2DataSource()); 22 } 23 24 }
注意:1.只能有一個數據源是主數據源,即只能有一個數據源上可以標注@Primary注解。
2.d1和d2分別是存放不同的數據mapper的文件夾,不同的數據源可以訪問不同的文件夾下的數據
3.在啟動類上加上@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) 排除Springboot的數據源自動配置類。