本篇內(nèi)容主要講解“怎么用SpringBoot + Mybatis多數(shù)據(jù)源配置實(shí)現(xiàn)讀寫分離”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“怎么用SpringBoot + Mybatis多數(shù)據(jù)源配置實(shí)現(xiàn)讀寫分離”吧!
創(chuàng)新互聯(lián)建站堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站制作、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的博興網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
應(yīng)用場景:項(xiàng)目中有一些報(bào)表統(tǒng)計(jì)與查詢功能,對(duì)數(shù)據(jù)實(shí)時(shí)性要求不高,因此考慮對(duì)報(bào)表的統(tǒng)計(jì)與查詢?nèi)ゲ僮鱯lave db,減少對(duì)master的壓力。
根據(jù)網(wǎng)上多份資料測試發(fā)現(xiàn)總是使用master數(shù)據(jù)源,無法切換到slave,經(jīng)過多次調(diào)試修改現(xiàn)已完美通過,現(xiàn)整理下詳細(xì)步驟和完整代碼如下:
實(shí)現(xiàn)方式:配置多個(gè)數(shù)據(jù)源,使用Spring AOP實(shí)現(xiàn)攔截注解實(shí)現(xiàn)數(shù)據(jù)源的動(dòng)態(tài)切換。
1. application.yml數(shù)據(jù)庫配置:
druid: type: com.alibaba.druid.pool.DruidDataSource master: url: jdbc:MySQL://127.0.0.1:3306/test?characterEncoding=UTF-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useUnicode=true driver-class-name: com.mysql.jdbc.Driver username: test password: 123 initial-size: 5 max-active: 10 min-idle: 5 max-wait: 60000 time-between-eviction-runs-millis: 3000 min-evictable-idle-time-millis: 300000 validation-query: SELECT 'x' FROM DUAL test-while-idle: true test-on-borrow: true test-on-return: false filters: stat,wall,log4j2 slave: url: jdbc:mysql://127.0.0.1:3307/test?characterEncoding=UTF-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useUnicode=true driver-class-name: com.mysql.jdbc.Driver username: test password: 123 initial-size: 5 max-active: 10 min-idle: 5 max-wait: 60000 time-between-eviction-runs-millis: 3000 min-evictable-idle-time-millis: 300000 validation-query: SELECT 'x' FROM DUAL test-while-idle: true test-on-borrow: true test-on-return: false filters: stat,wall,log4j2
2. 通過MybatisAutoConfiguration實(shí)現(xiàn)多數(shù)據(jù)源注入:
@Configuration @EnableTransactionManagement public class DataSourceConfiguration extends MybatisAutoConfiguration { @Value("${druid.type}") private Class<? extends DataSource> dataSourceType; @Bean(name = "masterDataSource") @Primary @ConfigurationProperties(prefix = "druid.master") public DataSource masterDataSource(){ return DataSourceBuilder.create().type(dataSourceType).build(); } @Bean(name = "slaveDataSource") @ConfigurationProperties(prefix = "druid.slave") public DataSource slaveDataSource(){ return DataSourceBuilder.create().type(dataSourceType).build(); } @Bean @Override public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { return super.sqlSessionFactory(dataSource()); } @Bean(name = "dataSource") public AbstractRoutingDataSource dataSource() { MasterSlaveRoutingDataSource proxy = new MasterSlaveRoutingDataSource(); Map<Object, Object> targetDataResources = new HashMap<>(); targetDataResources.put(DbContextHolder.DbType.MASTER, masterDataSource()); targetDataResources.put(DbContextHolder.DbType.SLAVE, slaveDataSource()); proxy.setDefaultTargetDataSource(masterDataSource()); proxy.setTargetDataSources(targetDataResources); proxy.afterPropertiesSet(); return proxy; } }
3. 基于 AbstractRoutingDataSource 和 AOP 的多數(shù)據(jù)源的配置
我們自己定義一個(gè)DataSource類,來繼承 AbstractRoutingDataSource:
public class MasterSlaveRoutingDataSource extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { return DbContextHolder.getDbType(); } }
這里通過determineCurrentLookupKey()返回的不同key到sqlSessionFactory中獲取對(duì)應(yīng)數(shù)據(jù)源然后使用ThreadLocal來存放線程的變量,將不同的數(shù)據(jù)源標(biāo)識(shí)記錄在ThreadLocal中
public class DbContextHolder { public enum DbType{ MASTER, SLAVE } private static final ThreadLocal<DbType> contextHolder = new ThreadLocal<>(); public static void setDbType(DbType dbType){ if (dbType==null) { throw new NullPointerException(); } contextHolder.set(dbType); } public static DbType getDbType(){ return contextHolder.get()==null?DbType.MASTER:contextHolder.get(); } public static void clearDbType(){ contextHolder.remove(); } }
4. 注解實(shí)現(xiàn)
@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface ReadOnlyConnection { }@Aspect @Component public class ReadOnlyConnectionInterceptor implements Ordered { @Around("@annotation(readOnlyConnection)") public Object proceed(ProceedingJoinPoint proceedingJoinPoint, ReadOnlyConnection readOnlyConnection) throws Throwable { try { DbContextHolder.setDbType(DbContextHolder.DbType.SLAVE); Object result = proceedingJoinPoint.proceed(); return result; }finally { DbContextHolder.clearDbType(); } } @Override public int getOrder() { return 0; } }
5. 應(yīng)用方式:
service層接口增加ReadOnlyConnection注解即可:
@ReadOnlyConnectionpublic CommonPagingVO<GroupGoodsVO> pagingByCondition(GroupGoodsCondition condition, int pageNum, int pageSize) { Page<GroupGoodsVO> page = PageHelper.startPage(pageNum, pageSize).doSelectPage(() -> groupGoodsMapper.listByCondition(condition)); return CommonPagingVO.get(page,page.getResult()); }
對(duì)于未加ReadOnlyConnection注解的默認(rèn)使用masterDataSource。
到此,相信大家對(duì)“怎么用SpringBoot + Mybatis多數(shù)據(jù)源配置實(shí)現(xiàn)讀寫分離”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
當(dāng)前標(biāo)題:怎么用SpringBoot+Mybatis多數(shù)據(jù)源配置實(shí)現(xiàn)讀寫分離
網(wǎng)頁地址:http://aaarwkj.com/article44/igjohe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)站制作、手機(jī)網(wǎng)站建設(shè)、建站公司、定制開發(fā)、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)