当前位置:   article > 正文

【经验分享】SpringCloud + MyBatis Plus 配置 MySQL,TDengine 双数据源_springboot配置mysql和tdengine的多数据源数据库

springboot配置mysql和tdengine的多数据源数据库

概述

因为项目中采集工厂中的设备码点的数据量比较大,需要集成TDengine时序数据库,所以需要设置双数据源

操作步骤

导入依赖

  1. <!-- 多数据源支持 -->
  2. <dependency>
  3. <groupId>com.baomidou</groupId>
  4. <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
  5. <version>3.3.6</version>
  6. </dependency>
  7. <!-- taos连接驱动 -->
  8. <dependency>
  9. <groupId>com.taosdata.jdbc</groupId>
  10. <artifactId>taos-jdbcdriver</artifactId>
  11. <version>3.2.11</version>
  12. </dependency>

 nacos 配置文件数据源修改

  1. spring:
  2. servlet:
  3. multipart:
  4. max-file-size: 100MB
  5. max-request-size: 100MB
  6. enabled: true
  7. # mysql 配置
  8. datasource:
  9. dynamic:
  10. primary: mysql-server
  11. type: com.alibaba.druid.pool.DruidDataSource
  12. mysql-server:
  13. driver-class-name: com.mysql.cj.jdbc.Driver
  14. jdbc-url: jdbc:mysql://ip:port/db?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
  15. username: username
  16. password: password
  17. initial-size: 10
  18. max-active: 100
  19. min-idle: 10
  20. max-wait: 60000
  21. pool-prepared-statements: true
  22. max-pool-prepared-statement-per-connection-size: 20
  23. time-between-eviction-runs-millis: 60000
  24. min-evictable-idle-time-millis: 300000
  25. test-while-idle: true
  26. test-on-borrow: false
  27. test-on-return: false
  28. stat-view-servlet:
  29. enabled: true
  30. url-pattern: /druid/*
  31. filter:
  32. stat:
  33. log-slow-sql: true
  34. slow-sql-millis: 1000
  35. merge-sql: false
  36. wall:
  37. config:
  38. multi-statement-allow: true
  39. # TDengine 配置
  40. tdengine-server:
  41. driver-class-name: com.taosdata.jdbc.rs.RestfulDriver
  42. jdbc-url: jdbc:TAOS-RS://ip:port/db?timezone=UTC-8&charset=utf-8
  43. username: username
  44. password: password
  45. pool-name: Data_trans_HikariCP
  46. minimum-idle: 10 #最小空闲连接数量
  47. idle-timeout: 600000 #空闲连接存活最大时间,默认600000(10分钟)
  48. maximum-pool-size: 100 #连接池最大连接数,默认是10
  49. auto-commit: true #此属性控制从池返回的连接的默认自动提交行为,默认值:true
  50. max-lifetime: 1800000 #此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟
  51. connection-timeout: 30000 #数据库连接超时时间,默认30秒,即30000

新增自定义数据源配置类

MySQL

  1. /**
  2. * MySQL 双数据源配置
  3. * @author pumpkin
  4. * @date 2024/5/16 14:08
  5. */
  6. @Configuration
  7. @MapperScan(basePackages = {"com.xxx.xxx.xxx.dao", "com.xxx.xxx.xxx.dao"}, sqlSessionTemplateRef = "mysqlSqlSessionTemplate")
  8. public class MysqlServerConfig {
  9. private final MybatisPlusProperties properties;
  10. public MysqlServerConfig(MybatisPlusProperties properties) {
  11. this.properties = properties;
  12. }
  13. @Bean(name = "mysqlDataSource")
  14. @ConfigurationProperties(prefix = "spring.datasource.mysql-server")
  15. @Primary
  16. public DataSource mysqlDataSource() {
  17. return DataSourceBuilder.create().build();
  18. }
  19. @Bean(name = "mysqlSqlSessionFactory")
  20. @Primary
  21. public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource) throws Exception {
  22. // SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
  23. MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
  24. bean.setDataSource(dataSource);
  25. // 指定多个XML映射文件位置
  26. PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
  27. // bean.setMapperLocations(resolver.getResources("classpath*:/mapper/*.xml"));
  28. Resource[] resources1 = resolver.getResources("classpath*:mapper/**/*.xml");
  29. Resource[] resources2 = resolver.getResources("classpath*:mapper/*.xml");
  30. // 将多个资源数组合并为一个
  31. Resource[] mapperLocations = new Resource[resources1.length + resources2.length];
  32. System.arraycopy(resources1, 0, mapperLocations, 0, resources1.length);
  33. System.arraycopy(resources2, 0, mapperLocations, resources1.length, resources2.length);
  34. // 设置合并后的资源数组
  35. bean.setMapperLocations(mapperLocations);
  36. // MybatisConfiguration configuration = this.properties.getConfiguration();
  37. // if (configuration == null && !StringUtils.hasText(this.properties.getConfigLocation())) {
  38. // configuration = new MybatisConfiguration();
  39. // }
  40. MybatisConfiguration configuration = new MybatisConfiguration();
  41. configuration.setMapUnderscoreToCamelCase(true);
  42. configuration.setDefaultFetchSize(100);
  43. configuration.setDefaultStatementTimeout(30);
  44. bean.setConfiguration(configuration);
  45. return bean.getObject();
  46. }
  47. @Bean(name = "mysqlTransactionManager")
  48. @Primary
  49. public DataSourceTransactionManager mysqlTransactionManager(@Qualifier("mysqlDataSource") DataSource dataSource) {
  50. return new DataSourceTransactionManager(dataSource);
  51. }
  52. @Bean(name = "mysqlSqlSessionTemplate")
  53. @Primary
  54. public SqlSessionTemplate mysqlSqlSessionTemplate(@Qualifier("mysqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
  55. return new SqlSessionTemplate(sqlSessionFactory);
  56. }
  57. }

TDengine

  1. /**
  2. * TDengine 双数据源配置
  3. * @author pumpkin
  4. * @date 2024/5/16 14:08
  5. */
  6. @Configuration
  7. @MapperScan(basePackages = {"com.xxx.xxx.xxx.tdengine"}, sqlSessionTemplateRef = "tdengineSqlSessionTemplate")
  8. public class TDengineServerConfig {
  9. @Bean(name = "tdengineDataSource")
  10. @ConfigurationProperties(prefix = "spring.datasource.tdengine-server")
  11. public DataSource tdengineDataSource() {
  12. return DataSourceBuilder.create().build();
  13. }
  14. @Bean(name = "tdengineSqlSessionFactory")
  15. public SqlSessionFactory tdengineSqlSessionFactory(@Qualifier("tdengineDataSource") DataSource dataSource) throws Exception {
  16. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
  17. bean.setDataSource(dataSource);
  18. bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/tdengine/*.xml"));
  19. return bean.getObject();
  20. }
  21. @Bean(name = "tdengineTransactionManager")
  22. public DataSourceTransactionManager tdengineTransactionManager(@Qualifier("tdengineDataSource") DataSource dataSource) {
  23. return new DataSourceTransactionManager(dataSource);
  24. }
  25. @Bean(name = "tdengineSqlSessionTemplate")
  26. public SqlSessionTemplate tdengineSqlSessionTemplate(@Qualifier("tdengineSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
  27. return new SqlSessionTemplate(sqlSessionFactory);
  28. }
  29. }

将访问对应数据源的 Mapper 类放在对应的包下,使用 DAO 或者 Mapper 层的方法的时候就会操作对应的数据源了

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/742433
推荐阅读
相关标签
  

闽ICP备14008679号