开发随手记-Java整合Tdengine多数据源配置

此文档连接MySQL,TDengine(时序库)

Maven:

<!-- TDengine 依赖 -->

<dependency>

    <groupId>com.taosdata.jdbc</groupId>

    <artifactId>taos-jdbcdriver</artifactId>

    <version>3.2.4</version>

</dependency>

<!--Druid  方便控制台查看-->

<dependency>

    <groupId>com.alibaba</groupId>

    <artifactId>druid-spring-boot-starter</artifactId>

    <version>1.2.16</version>

</dependency>

<!-- hutool工具 -->

<dependency>

    <groupId>cn.hutool</groupId>

    <artifactId>hutool-all</artifactId>

    <version>5.8.5</version>

</dependency>

<!--  mybatis plus 动态数据源 多数据源 -->

<dependency>

    <groupId>com.baomidou</groupId>

    <artifactId>dynamic-datasource-spring-boot-starter</artifactId>

    <version>3.5.1</version>

</dependency>

YML配置:

spring:
  lifecycle:
    timeout-per-shutdown-phase: 30s #设置缓冲时间 默认也是30s
  # Mysql配置
  datasource:
    druid:
      # 接下来(one,two)其实就都是自定义配置了,springBoot是识别不了的(当然你也可以另起其它行,到其它位置),我们需要将这些配置映射到对应的类上,springBoot不会帮我们做
      one:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxx?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
        username: root
        password: xxxx
        type: com.alibaba.druid.pool.DruidDataSource
        name: mysqlDataSource  # 在druid 内数据源的名称
        # springboot2.0整合了hikari ,据说这是目前性能最好的java数据库连接池,但是 druid 有控制面板方便查看
        # 手动配置数据源
        validation-query: SELECT 1 FROM DUAL  # 连接是否有效的查询语句
        validation-query-timeout: 60000 # 连接是否有效的查询超时时间
        # 建议 连接数 = ((核心数 * 2) + 有效磁盘数)
        initial-size: 40  #初始化时建立物理连接的个数,初始化发生在显示调用 init 方法,或者第一次 getConnection 时
        min-idle: 40  # 最小连接池数量
        max-active: 100  #最大连接池数量
        test-on-borrow: false  #申请连接时会执行validationQuery检测连接是否有效,开启会降低性能,默认为true
        test-on-return: false #归还连接时会执行validationQuery检测连接是否有效,开启会降低性能,默认为true
        time-between-eviction-runs-millis: 60000  # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
        min-evictable-idle-time-millis: 300000  # 配置一个连接在池中最小生存的时间,单位是毫秒
      two:
        driver-class-name: com.taosdata.jdbc.TSDBDriver
        # 这里指定了具体的数据库 需要注意,
        # 如果换成不指定具体数据库名称 jdbc:TAOS://192.168.172.129:6030?charset=UTF-8&locale=en_US.UTF-8&timezone=UTC-8 则在sql中使用必须要指定数据库的名称 dba.table_b
        url: jdbc:TAOS://xxx.xxx.xxx.xxx:6041/dev?charset=UTF-8&locale=en_US.UTF-8&timezone=UTC-8
        username: root
        password: xxxx
        type: com.alibaba.druid.pool.DruidDataSource
        name: tdengineDataSource # 在druid 内数据源的名称
        # springboot2.0整合了hikari ,据说这是目前性能最好的java数据库连接池,但是 druid 有控制面板方便查看
        # 手动配置数据源
        validation-query: select server_status()  # 连接是否有效的查询语句
        validation-query-timeout: 60000 # 连接是否有效的查询超时时间
        # 建议 连接数 = ((核心数 * 2) + 有效磁盘数)
        # initial-size 该属性只有在调用接口初始化init方法的时候才会建立连接
        # initial-size: 10  #初始化时建立物理连接的个数,初始化发生在显示调用 init 方法,或者第一次 getConnection 时
        min-idle: 10  # 最小连接池数量
        max-active: 20  #最大连接池数量
        test-on-borrow: false  #申请连接时会执行validationQuery检测连接是否有效,开启会降低性能,默认为true
        test-on-return: false #归还连接时会执行validationQuery检测连接是否有效,开启会降低性能,默认为true
        time-between-eviction-runs-millis: 60000  # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
        min-evictable-idle-time-millis: 300000  # 配置一个连接在池中最小生存的时间,单位是毫秒

配置类:

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.servlet.Filter;
import javax.sql.DataSource;

/**
 * 多数据源配置
 */
@Component
@Slf4j
public class DataSourceConfig {

    public static final String MYSQL_DATA_SOURCE = "mysqlDataSource";
    public static final String TDENGINE_DATA_SOURCE = "tdengineDataSource";

    /**
     * http://127.0.0.1:8090/druid/index.html
     * 配置Druid的监控视图
     *
     * @return
     */
    @Bean
    public ServletRegistrationBean<StatViewServlet> druidStatViewServlet() {
        ServletRegistrationBean<StatViewServlet> registrationBean =
                new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        // 配置Druid监控页面的登录用户名和密码
        registrationBean.addInitParameter("loginUsername", "admin");
        registrationBean.addInitParameter("loginPassword", "123456");

        // 设置 IP 白名单,允许访问的 IP,多个 IP 用逗号分隔
        registrationBean.addInitParameter("allow", "127.0.0.1");

        // 设置 IP 黑名单,拒绝访问的 IP,多个 IP 用逗号分隔(当 IP 在黑名单中同时又在白名单中时,优先于白名单)
        // servletRegistrationBean.addInitParameter("deny", "192.168.1.100");

        // 是否能够重置数据
        registrationBean.addInitParameter("resetEnable", "false");
        return registrationBean;
    }

    /**
     * 配置Druid的WebStatFilter
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean<Filter> druidWebStatFilter() {
        FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new WebStatFilter());

        // 添加过滤规则
        registrationBean.addUrlPatterns("/*");
        // 配置不拦截的路径
        registrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return registrationBean;
    }

    /**
     * 自动配置的数据源,使用Druid连接池
     * druid 会管理这数据源
     *
     * @return
     */
    @Bean(name = MYSQL_DATA_SOURCE)
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.druid.one")
    public DataSource dataSource() {
        return new DruidDataSource();
    }

    /**
     * 手动配置的数据源,也使用Druid连接池
     * druid 会管理这数据源
     * <p>
     * 这里不直接集成tdengine 到 mybatisPlus 的原因有两个
     * 1、集成到 mybatisPlus 时需要多数据源,无论是使用dynamic-datasource的方案,还是自己去写,都需要去处理做逻辑代码,可能后期需要版本的维护就又要考虑这个维护
     * 2、就是无论这个tdengine 如何发展,它一定会支持 jdbc 的方案,但是可能不会支持 mybatis和 springBoot,
     * 3、效率问题
     *
     * @return
     */
    @Bean(name = TDENGINE_DATA_SOURCE)
    @ConfigurationProperties(prefix = "spring.datasource.druid.two")
    public DataSource customDataSource() {
        return new DruidDataSource();
    }
}

多数据源连接监控:

http://127.0.0.1:8090/druid/datasource.html

MySQL:

TDengine:

 错误解决:

spring cloud项目集成可能会以下错误找不到数据源

### Error querying database.  Cause: java.sql.SQLSyntaxErrorException: Table 'xxx.dev' doesn't exist
### The error may exist in file [E:\workspace\xxxxxx\target\classes\mapper\tdengine\DevMapper.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: select *         from dev;
### Cause: java.sql.SQLSyntaxErrorException: Table 'xxx.dev' doesn't exist
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: Table 'xxx.dev' doesn't exist] with root cause
java.sql.SQLSyntaxErrorException: Table 'xxx.dev' doesn't exist

需要绑定默认数据源@Primary

也需要注意mapper的层级

  • 42
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
快速排序(QuickSort)是一种常用的排序算法,它的核心思想是通过一趟排序将待排序数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。 以下是Java实现的快速排序代码: ```java public class QuickSort { public void quickSort(int[] nums, int left, int right) { if (left < right) { int pivotIndex = partition(nums, left, right); quickSort(nums, left, pivotIndex - 1); quickSort(nums, pivotIndex + 1, right); } } private int partition(int[] nums, int left, int right) { int pivot = nums[left]; int i = left + 1; int j = right; while (i <= j) { while (i <= j && nums[i] < pivot) { i++; } while (i <= j && nums[j] >= pivot) { j--; } if (i <= j) { swap(nums, i, j); } } swap(nums, left, j); return j; } private void swap(int[] nums, int i, int j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } } ``` 使用方法: ```java int[] nums = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}; QuickSort quickSort = new QuickSort(); quickSort.quickSort(nums, 0, nums.length - 1); System.out.println(Arrays.toString(nums)); ``` 输出结果: ```java [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9] ``` 以上代码中,`quickSort` 方法是快速排序的主体,它接受一个待排序的整数数组、数组的左右边界作为参数。在方法中,我们首先判断左右边界是否交叉,如果没有交叉,我们就通过分区函数 `partition` 进行分治。对于分区函数 `partition` ,它接受一个待排序的整数数组、数组的左右边界作为参数,它的核心思想是通过两个指针 i 和 j 一起遍历数组,当 i 指向的数小于等于 pivot 时,i 向右移动,当 j 指向的数大于 pivot 时,j 向左移动,直到 i 和 j 相遇。在遍历的过程中,如果 i 指向的数大于 pivot 并且 j 指向的数小于等于 pivot ,我们就交换 i 和 j 指向的数。最后,我们将 pivot 与 j 指向的数交换,并返回 j 作为分割点。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值