• docker环境连接tdengine服务


    1.开发app项目

    
    <dependency>
        <groupId>com.taosdata.jdbcgroupId>
        <artifactId>taos-jdbcdriverartifactId>
        <version>3.0.0version>
    dependency>
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-jdbcartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    spring:
      datasource:
        url: jdbc:TAOS://:/ciot
        driver-class-name: com.taosdata.jdbc.TSDBDriver
        username: root
        password: taosdata
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    @RestController
    public class AppController {
    
        @Autowired
        private JdbcTemplate jdbcTemplate;
    
        @GetMapping("create")
        public String create() {
            jdbcTemplate.execute("use ciot");
            jdbcTemplate.execute("create table if not exists weather_1(" +
                    "ts timestamp ,temporary float ,humidity int)");
    
    
            List<String> tables = jdbcTemplate.query("show tables", (rs, rowNum) ->
                    rs.getString(1));
            return tables.toString();
        }
    
        @GetMapping("hello")
        public String test() {
            return "hello world";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    运行项目,请求接口 http:127.0.0.1:8080/create 获取响应结果

    2.根据TDengine Linux Client构建基础镜像

    2.1.准备安装包

    ​ 官网下载TDengine-client-3.0.1.4-Linux-x64-Lite.tar.gz,并放置在./target目录中

    2.2.编写Dockerfile

    ### 基础镜像
    FROM openjdk:8
    ### 作者
    MAINTAINER zml<17610907265@sina.cn>
    ### 系统编码
    ENV LANG=C.UTF-8 LC_ALL=C.UTF-8
    WORKDIR /
    ### 将TDengine-client添加进入镜像
    ADD target/TDengine-client-3.0.1.4-Linux-x64-Lite.tar.gz /
    ### 解压缩并安装client
    WORKDIR /TDengine-client-3.0.1.4/
    RUN ./install_client.sh
    ### 安装时间工具ntp
    #RUN echo ' \
    #    deb http://mirrors.aliyun.com/debian/ buster main non-free contrib \
    #    deb http://mirrors.aliyun.com/debian-security buster/updates main \
    #    deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib \
    #    deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib \
    #    deb-src http://mirrors.aliyun.com/debian-security buster/updates main \
    #    deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib \
    #    deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib \
    #    deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib \
    #    ' > /etc/apt/sources.list
    #RUN apt-get clean && apt-get update --fix-missing
    #RUN apt-get install -y ntp ntpdate
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    2.3.构建tdengine client基础镜像

    docker build -t tdengine-client:0.0.1 -f Dockerfile .
    
    • 1

    2.4.查看构建的基础镜像

    在这里插入图片描述

    3.根据基础镜像构建工程镜像

    3.1 准备工程jar

    ​ 执行mvn clean package 获得app-0.0.1-SNAPSHOT.jar,并放置在./target目录中。

    3.2 编写Dockerfile

    ### 基础镜像
    FROM tdengine-client:0.0.1
    ### 同步系统时间
    RUN cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
    #RUN ntpdate cn.pool.ntp.org
    ### 修改基础镜像中taos.cfg
    RUN echo "firstEp  hasaki:6030" >> /usr/local/taos/cfg/taos.cfg
    ### 修改hosts
    RUN echo "192.168.177.137 hasaki" >> /etc/hosts
    ### 应用构建成功以后jar文件被复制到镜像内,名字也改为app.jar
    ADD target/app-0.0.1-SNAPSHOT.jar /app.jar
    ### 启动容器时的进程
    ENTRYPOINT ["java", "-jar", "/app.jar"]
    ### 暴露的端口
    EXPOSE 8080
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    3.3 构建工程镜像

    docker build -t app:0.0.1 -f Dockerfile .
    
    • 1

    3.4 查看构建的工程镜像

    在这里插入图片描述

    3.5 根据镜像运行容器

    docker run -d -p 8083:8080  app:0.0.1
    
    • 1

    请求接口查看结果:http://192.168.177.131:8083/create

    4. 问题记录

    4.1 tdengine创建普通用户并授权

    4.1.1 执行命令并查看结果:
    CREATE USER ciot PASS '123321' sysinfo 1;
    
    • 1
    show users;
    
    • 1

    在这里插入图片描述

    4.1.2 用户授权
    GRANT ALL ON ciot.* TO ciot;
    
    • 1
    4.1.3 结论

    经测试,授权过某一数据库权限的用户仍然具有其他数据库的操作权限,授权失败。

    4.2 新版tdengine解决了什么问题

    	略。请及时更新最新版本
    
    • 1

    4.3 集群环境如何保证高可用

    4.3.1 show dnodes

    首先集群节点设置要正确,通过show dnodes命令查看,如下图:

    在这里插入图片描述

    如果节点缺少,需要通过 create dnode "hostname:port"创建。

    4.3.2 show mnodes

    如果需要保证服务高可用,需要在注册节点taos cli中手动执行以下命令:

    create mnode on dnode 2;
    
    • 1

    其中,dnode 2是指对应tdengine节点的id值。
    此时当leader宕机,follower会自动升级为leader,提供服务。

    4.4.3 vnode设置多副本保证高可用
    CREATE DATABASE demo replica 3;
    
    • 1

    如果此时有四个dnode,关掉两个dnode服务(这两个服务至多有一个leader,否则mnode不可用),vnode group 仍然可以正常使用。
    此时,关闭hasaki-3,数据库操作无法执行。

    4.4 时间不同步问题解决

    4.4.1 非docker环境
     Client and server's time is not synchronized
    
    • 1

    执行如下命令:

    yum -y install ntp ntpdate
    ntpdate cn.pool.ntp.org
    
    • 1
    • 2
    4.4.2 docker环境

    tdengine client基础镜像系统为debain,执行ntpdate cn.pool.ntp.org Can't adjust the time of day: Operation not permitted;

    原因分析:猜测在一些虚拟vps上,可能存在一些限制,导致此处报错。

    经测试,使用RUN cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

    可以解决docker环境项目接口访问数据库,Client and server’s time is not synchronized问题。

  • 相关阅读:
    oracle从入门到精通第三篇(条件取值语句|约束|子查询|自查询|连表查询)
    ACM-概率题(其一)
    FFmpeg入门详解之6:VLC播放器简介
    【图像去噪】基于非线性扩散PM算法实现图像去噪附matlab代码
    【什么是区块链】
    【SA8295P 源码分析】107 - AIS Camera 美信max96712解串器 - max9295加串器 寄存器初始化及工作过程详解
    从理解js双重递归执行顺序到用递归方式实现二叉树中序遍历
    【嵌入式C语言】图解C语言指针变量
    Docker部署Nacos注册中心
    【大前端攻城狮之路】百度爱番番前端性能监控体系方案设计
  • 原文地址:https://blog.csdn.net/MonkeySun123321/article/details/127796241