解決mybatis-plus動態數據源切換不生效的問題

一、問題描述

在我們項目中,既要連接mysql,又要連接TDEngine(taos),正確配置後也無法動態切換數據源執行sql

二、環境

1.依賴

<!--連接另外一種數據庫的驅動-->
        <dependency>
            <groupId>com.taosdata.jdbc</groupId>
            <artifactId>taos-jdbcdriver</artifactId>
<!--            <version>2.0.32</version>-->
            <version>3.0.0</version>
        </dependency>
        <!--mybatis plus 動態切換數據源的依賴-->
   		<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>3.3.2</version>
        </dependency>

2.配置

spring:
  datasource:
    dynamic:
      strict: true
      primary: mysql
      datasource:
        mysql:
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://192.xxx.xxx.xxx:3306/db_iot?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
          username: root
          password: 123456
        tdengine:
          driver-class-name: com.taosdata.jdbc.TSDBDriver
          url: jdbc:TAOS://192.xxx.xxx.xxx:6030/iot_data?timezone=UTC-8&charset=UTF-8&locale=en_US.UTF-8
          username: root
          password: taosdata
        mysql1:
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://192.xxx.xxx.xxx:3306/db_portal?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
          username: root
          password: 123456



三、解決方法

直接上代碼,最後會有問題分析,因為涉及到源碼,這裡暫時不講
這裡的dynamicRoutingDataSource()不能有DynamicDataSourceAutoConfiguration裡的那樣命名和返回值,那樣在註入時就會因為其他數據源的註入導致無法註入動態數據源,需要細化到具體的類型

package com.xxx.project.iotconf.configs;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
import com.baomidou.dynamic.datasource.provider.YmlDynamicDataSourceProvider;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.Map;


@Configuration
public class TDEngineConfig {

    @Autowired
    private DynamicDataSourceProperties properties;

    @Bean
    @ConditionalOnMissingBean
    @Primary
    public DynamicDataSourceProvider dynamicDataSourceProvider() {
        Map<String, DataSourceProperty> datasourceMap = this.properties.getDatasource();
        return new YmlDynamicDataSourceProvider(datasourceMap);
    }

    @Bean
    @ConditionalOnMissingBean
    public DynamicRoutingDataSource dynamicRoutingDataSource(DynamicDataSourceProvider dynamicDataSourceProvider) {
        DynamicRoutingDataSource dynamicRoutingDataSource = new DynamicRoutingDataSource();
        dynamicRoutingDataSource.setPrimary(this.properties.getPrimary());
        dynamicRoutingDataSource.setStrict(this.properties.getStrict());
        dynamicRoutingDataSource.setStrategy(this.properties.getStrategy());
        dynamicRoutingDataSource.setProvider(dynamicDataSourceProvider);
        dynamicRoutingDataSource.setP6spy(this.properties.getP6spy());
        dynamicRoutingDataSource.setSeata(this.properties.getSeata());
        Map<String, DataSource> dataSourceMap = dynamicDataSourceProvider.loadDataSources();
        for (String key : dataSourceMap.keySet()) {
            dynamicRoutingDataSource.addDataSource(key, dataSourceMap.get(key));
        }
        return dynamicRoutingDataSource;
    }

}

四、測試

放入TAOS創建超表的SQL,由JdbcTemplate去執行,執行成功

在這裡插入圖片描述

package com.xxx.project.iot.pulsar.handler;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.shandy.project.api.basic.dto.SDevice;
import com.shandy.project.api.basic.dto.Tuple2;
import com.shandy.project.iot.pulsar.utils.ReflectUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class DataHandler implements DsHandler{

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @PostConstruct
    public void init() {

    }

    /**
     * 執行單條DDL sql
     */
    @DS(value = "tdengine")
    public void execute(String sql) {
        jdbcTemplate.execute(sql);
        log.info(sql);
    }

    /**
     * 執行單條DML sql
     */
    @DS(value = "tdengine")
    public List<Map<String, Object>> query(String sql, Object[] args) {
        log.info(String.format("%s,params is {%s}", sql, args));
        return jdbcTemplate.queryForList(sql, args);
    }

    /**
     * 保存設備數據(批量,多表多條)
     */
    @DS(value = "tdengine")
    public void batchInsertDevice(List<SDevice> devices, String table) {
        String[] sqls = null;
        jdbcTemplate.batchUpdate(sqls);
        log.info(sqls.toString());
    }

    /**
     * 單條插入
     *
     * @param device  消息結構體
     * @param mqttObj 模型對象,IMqttR或IMqttS對象
     */
    @DS(value = "tdengine")
    public void insertDevice(SDevice device, Object mqttObj) {
        Tuple2<String, List<Object>> t2 = ReflectUtils.getInsSql(mqttObj.getClass(), device);
        List<Object> list = t2.getField(1);
        String sql = t2.getField(0);
        jdbcTemplate.update(sql, list.toArray());
        log.info(sql);
    }
}

五、問題分析

1.一開始執行時,總是報錯誤的SQL語句,要我檢查Mysql的版本,所以從這個提示來看,是沒有動態切換到我們的taos數據庫的。
什麼原因呢?我們看com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceAutoConfiguration類的方法

   @Bean
    @ConditionalOnMissingBean
    public DataSource dataSource(DynamicDataSourceProvider dynamicDataSourceProvider) {
        DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource();
        dataSource.setPrimary(this.properties.getPrimary());
        dataSource.setStrict(this.properties.getStrict());
        dataSource.setStrategy(this.properties.getStrategy());
        dataSource.setProvider(dynamicDataSourceProvider);
        dataSource.setP6spy(this.properties.getP6spy());
        dataSource.setSeata(this.properties.getSeata());
        return dataSource;
    }

這裡有個@ConditionalOnMissingBean註解,意思是當dataSource對象不存在時才會進行註入。
我發現我除瞭配置瞭動態數據源,也配置瞭druid數據源,在項目啟動是肯定是會註入druid的DataSource對象的,那就導致我們的動態數據源的DataSource對象無法註入,可能這就是切換不瞭的原因。這裡或許可以嘗試把druid數據源去掉,但我沒有往這個方向去深究。

spring.datasource.druid.db-type=mysql
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://192.xxx.xxx.xxx:3306/db_iot?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.druid.username=root
spring.datasource.druid.password=123456

既然沒有註入DynamicRoutingDataSource對象,我自己就寫瞭上文中的TDEngineConfig去註入,其中方法和DynamicDataSourceAutoConfiguration類似,隻是稍微做改造。

在最開始我原封不動的把DynamicDataSourceAutoConfiguration方法抄下來,在執行SQL時報:dynamic-datasource could not find a datasource named tdengine
異常來自於在DynamicRoutingDataSource的getDataSource()方法

public DataSource getDataSource(String ds) {
        if (StringUtils.isEmpty(ds)) {
            return this.determinePrimaryDataSource();
        } else if (!this.groupDataSources.isEmpty() && this.groupDataSources.containsKey(ds)) {
            log.debug("dynamic-datasource switch to the datasource named [{}]", ds);
            return ((GroupDataSource)this.groupDataSources.get(ds)).determineDataSource();
        } else if (this.dataSourceMap.containsKey(ds)) {
            log.debug("dynamic-datasource switch to the datasource named [{}]", ds);
            return (DataSource)this.dataSourceMap.get(ds);
        } else if (this.strict) {
            throw new CannotFindDataSourceException("dynamic-datasource could not find a datasource named" + ds);
        } else {
            return this.determinePrimaryDataSource();
        }
    }

從這裡發現,DynamicRoutingDataSource的dataSourceMap是空的,那自然報錯,但是DynamicDataSourceProperties的dataSourceMap並不是空的,所以配置並沒有出錯,隻是項目在啟動時沒有填充DynamicRoutingDataSource的dataSourceMap,那不妨我們自己來完成這件事情。
在TDEngineConfig的dynamicDataSourceProvider()方法中將properties的datasourceMap封裝到YmlDynamicDataSourceProvider中,再看它的loadDataSources(),不就可以獲取到Map<String, DataSource>類型的一個對象嗎,我們把這個想辦法賦值給DynamicRoutingDataSource的dataSourceMap

   public Map<String, DataSource> loadDataSources() {
        return this.createDataSourceMap(this.dataSourcePropertiesMap);
    }

所以在TDEngineConfig的dynamicRoutingDataSource方法中通過如下代碼獲取到dataSourceMap

 Map<String, DataSource> dataSourceMap = dynamicDataSourceProvider.loadDataSources();

再通過下面代碼就將DynamicRoutingDataSource的dataSourceMap填充好瞭

 for (String key : dataSourceMap.keySet()) {
            dynamicRoutingDataSource.addDataSource(key, dataSourceMap.get(key));
        }

此時完成DynamicRoutingDataSource的註入。
這樣再調用SQL時,就可以獲取@DS註解的value值,充當key去dataSourceMap裡找到對應的數據源進行切換

到此這篇關於解決mybatis-plus動態數據源切換不生效的問題的文章就介紹到這瞭,更多相關mybatis-plus動態數據源切換內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: