一、问题描述
在我们项目中,既要连接mysql,又要连接TDEngine(taos),正确配置后也无法动态切换数据源执行sql
二、环境
1.依赖
<dependency> <groupId>com.taosdata.jdbcgroupId> <artifactId>taos-jdbcdriverartifactId> <version>3.0.0version> dependency> <dependency> <groupId>com.baomidougroupId> <artifactId>dynamic-datasource-spring-boot-starterartifactId> <version>3.3.2version> dependency>
配置
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;@Configurationpublic 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@Slf4jpublic class DataHandler implements DsHandler{ @Autowired private JdbcTemplate jdbcTemplate; @PostConstruct public void init() { } @DS(value = "tdengine") public void execute(String sql) { jdbcTemplate.execute(sql); log.info(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()); } @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=mysqlspring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.druid.url=jdbc:mysql://192.xxx.xxx.xxx:3306/db_iot?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghaispring.datasource.druid.username=rootspring.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
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里找到对应的数据源进行切换
来源地址:https://blog.csdn.net/qq_46552655/article/details/128418714