山石彥 | 作者
urlify.cn/vQzIne | 來源
最近工作中有用到雙資料源,一個項目(中台)中需要操作兩個不同的資料庫。當時考慮到了兩種方式,
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:db1spring.datasource.d1.username=user1spring.datasource.d1.password=123456spring.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=truespring.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=1800000spring.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/db2spring.datasource.d2.username=user2spring.datasource.d2.password=123456spring.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=truespring.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=1800000spring.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等資訊。
@Configuration @MapperScan(value = {"com.test.d1.mapper"}, sqlSessionFactoryRef = "d1SqlSessionFactory") public class Ds1MybatisConfig{ @Primary @Bean(name = "d1DataSource") @ConfigurationProperties(prefix = "spring.datasource.d1") public DataSource d1DataSource() { return new org.apache.tomcat.jdbc.pool.DataSource(); } @Bean(name = "d1SqlSessionFactory") public SqlSessionFactory d1SqlSessionFactoryBean() throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(d1DataSource()); sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:/mybatis/d1/*.xml")); return sqlSessionFactoryBean.getObject(); } @Bean public PlatformTransactionManager d1TransactionManager() { return new DataSourceTransactionManager(d1DataSource()); } }
@Configuration @MapperScan(value = {"com.test.d2.mapper"}, sqlSessionFactoryRef = "d2SqlSessionFactory") public class Ds2MybatisConfig{ @Bean(name = "d2DataSource") @ConfigurationProperties(prefix = "spring.datasource.d2") public DataSource d2DataSource() { return new org.apache.tomcat.jdbc.pool.DataSource(); } @Bean(name = "d2SqlSessionFactory") public SqlSessionFactory d2SqlSessionFactory() throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(d2DataSource()); sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:/mybatis/d2/*Mapper.xml")); return sqlSessionFactoryBean.getObject(); } @Bean public PlatformTransactionManager d2TransactionManager() { return new DataSourceTransactionManager(d2DataSource()); } }
@Configuration @MapperScan(value = {"com.test.d2.mapper"}, sqlSessionFactoryRef = "d2SqlSessionFactory") public class Ds2MybatisConfig{ @Bean(name = "d2DataSource") @ConfigurationProperties(prefix = "spring.datasource.d2") public DataSource d2DataSource() { return new org.apache.tomcat.jdbc.pool.DataSource(); } @Bean(name = "d2SqlSessionFactory") public SqlSessionFactory d2SqlSessionFactory() throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(d2DataSource()); sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:/mybatis/d2/*Mapper.xml")); return sqlSessionFactoryBean.getObject(); } @Bean public PlatformTransactionManager d2TransactionManager() { return new DataSourceTransactionManager(d2DataSource()); } }
注意:1.隻能有一個資料源是主資料源,即隻能有一個資料源上可以标注@Primary注解。
2.d1和d2分别是存放不同的資料mapper的檔案夾,不同的資料源可以通路不同的檔案夾下的資料
3.在啟動類上加上@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) 排除Springboot的資料源自動配置類。
粉絲福利:108本java從入門到大神精選電子書領取