Spring Boot + Mybatis 多資料來源配置實現讀寫分離

網易雲社群發表於2018-09-30

本文來自網易雲社群

作者:王超


應用場景:專案中有一些報表統計與查詢功能,對資料實時性要求不高,因此考慮對報表的統計與查詢去操作slave db,減少對master的壓力。

根據網上多份資料測試發現總是使用master資料來源,無法切換到slave,經過多次除錯修改現已完美通過,現整理下詳細步驟和完整程式碼如下:

實現方式:配置多個資料來源,使用Spring AOP實現攔截註解實現資料來源的動態切換。

1. application.yml資料庫配置: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實現多資料來源注入:

@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 的多資料來源的配置

我們自己定義一個DataSource類,來繼承 AbstractRoutingDataSource:
public class MasterSlaveRoutingDataSource extends AbstractRoutingDataSource {
    @Override
    protected Object determineCurrentLookupKey() {
        return DbContextHolder.getDbType();
    }
}這裡通過determineCurrentLookupKey()返回的不同key到sqlSessionFactory中獲取對應資料來源然後使用ThreadLocal來存放執行緒的變數,將不同的資料來源標識記錄在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. 註解實現

@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. 應用方式:

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());
 }
對於未加ReadOnlyConnection註解的預設使用masterDataSource。複製程式碼



網易雲免費體驗館,0成本體驗20+款雲產品!

更多網易研發、產品、運營經驗分享請訪問網易雲社群


相關文章:
【推薦】 Android標題欄(2)


相關文章