TDengine taosAdapter — 多协议网关详解

分类:14.生态 | 篇章:01 taosAdapter
在这里插入图片描述
免费详情

taosAdapter 是 TDengine 的多协议网关,作为 WebSocket/REST/InfluxDB/OpenTSDB/Telnet/StatsD/collectd/Prometheus 等协议的统一接入层。本文讲解架构、协议、配置、性能调优。

协议速查表

协议端口用途
REST6041HTTP/JSON 查询写入
WebSocket6041WS 二进制
InfluxDB Line6041 (/influxdb)InfluxDB 兼容
OpenTSDB Telnet6046Telnet 协议
OpenTSDB HTTP6041 (/opentsdb)OpenTSDB JSON
collectd6045collectd 协议
StatsD6044StatsD UDP
Prometheus6041 (/metrics)指标暴露
icinga26048icinga2 协议
node_exporter6044node_exporter scrape

详细解析

1. 架构定位

TDengine 多协议接入:

  各种数据源 → taosAdapter → taosd
  
       Web App / 浏览器
            │
            ▼
       REST / WebSocket
            │
            ▼
       ┌──────────────┐
       │ taosAdapter  │
       │   (HTTP/WS)  │
       └──────────────┘
            │
        TCP 6030
            │
            ▼
         taosd
            
  
  IoT 设备 / Prometheus / 监控系统
            │
            ▼
       InfluxDB / OpenTSDB / collectd
            │
            ▼
       taosAdapter
            │
            ▼
         taosd


好处:
  ① 协议归一(应用按需选)
  ② 单端口跨防火墙
  ③ 与 taosd 解耦
  ④ 可横向扩展

2. 安装与启动

# 通常随 TDengine 安装
which taosadapter
# /usr/bin/taosadapter

# 配置
/etc/taos/taosadapter.toml

# 启动
systemctl start taosadapter
systemctl enable taosadapter

# 日志
/var/log/taos/taosadapter.log

# 状态
systemctl status taosadapter
curl http://localhost:6041/-/ping

3. 核心配置

# /etc/taos/taosadapter.toml

debug = false
taosConfigDir = "/etc/taos"

# TCP 监听
[http]
host = "0.0.0.0"
port = 6041

# 跨域
[cors]
allowAllOrigins = true
allowOrigins = ["*"]

# 连接池(到 taosd)
[pool]
maxConnect = 4000
maxIdle = 4000
idleTimeout = "1h"

# SSL
[ssl]
enable = false
certFile = ""
keyFile = ""

# 日志
[log]
path = "/var/log/taos"
rotationCount = 30
rotationSize = "1GB"
level = "info"

# 监控
[monitor]
collectDuration = "3s"
incgroup = false
writeToTD = true
identity = "taosadapter"

4. REST 接口

# 标准 SQL 执行
POST /rest/sql HTTP/1.1
Host: localhost:6041
Authorization: Basic <base64(user:pass)>

Body: 
SELECT * FROM mydb.meters LIMIT 10

Response (JSON):
{
  "code": 0,
  "column_meta": [["ts", "TIMESTAMP", 8], ["current", "FLOAT", 4]],
  "data": [
    ["2026-06-04 12:00:00.000", 25.3],
    ...
  ],
  "rows": 10
}
# curl 示例
curl -X POST http://localhost:6041/rest/sql \
  -u root:taosdata \
  -d 'SELECT * FROM mydb.meters LIMIT 10'


# 指定数据库
curl -X POST http://localhost:6041/rest/sql/mydb \
  -u root:taosdata \
  -d 'SELECT * FROM meters LIMIT 10'


# 高精度时间戳
curl -X POST http://localhost:6041/rest/sql?tz=UTC \
  -u root:taosdata \
  -d '...'

5. InfluxDB Line 协议

# InfluxDB Line Protocol 写入
curl -X POST \
  "http://localhost:6041/influxdb/v1/write?db=mydb" \
  -u root:taosdata \
  -d "meters,location=Beijing current=25.3,voltage=220i 1717488000000"


# 批量
curl -X POST \
  "http://localhost:6041/influxdb/v1/write?db=mydb&precision=ms" \
  -u root:taosdata \
  --data-binary @data.txt
# data.txt:
# meters,location=Beijing current=25.3,voltage=220i 1717488000000
# meters,location=Shanghai current=26.1,voltage=219i 1717488001000


效果:
  - 自动创建超级表
  - 自动创建子表(按 Tag 唯一组合)
  - Schemaless 写入

6. OpenTSDB 协议

# OpenTSDB Telnet (端口 6046)
echo "put meters.current 1717488000 25.3 location=Beijing" | nc localhost 6046


# OpenTSDB HTTP JSON
curl -X POST http://localhost:6041/opentsdb/v1/put \
  -u root:taosdata \
  -H "Content-Type: application/json" \
  -d '{
    "metric": "meters.current",
    "timestamp": 1717488000,
    "value": 25.3,
    "tags": {"location": "Beijing"}
  }'

7. Prometheus 集成

# taosadapter.toml
[prometheus]
enable = true


# Prometheus scrape
scrape_configs:
  - job_name: 'tdengine'
    static_configs:
      - targets: ['taosadapter:6041']
    metrics_path: '/metrics'


# 输出 Prometheus 格式指标
# 包括 taosAdapter 自身和 TDengine 状态

8. 横向扩展

单 taosAdapter 性能:
  - REST: 几万 QPS
  - WebSocket: 十几万行/秒/连接
  - 高负载需横向扩展

  
扩展架构:

  ┌─ taosAdapter-1 ─┐
  │                 │
  ├─ taosAdapter-2 ─┤── LB ──→ Clients
  │                 │
  └─ taosAdapter-3 ─┘
       │
       ▼
     taosd 集群


负载均衡:
  - Nginx
  - HAProxy
  - K8s Service
  - 云 LB


注意:
  - 多 adapter 实例独立无状态
  - 每个连同一 taosd 集群
  - 连接亲和性(WebSocket 长连接)

代码示例

Docker 部署

# docker-compose.yml
services:
  taosd:
    image: tdengine/tdengine:3.x.x
    hostname: taosd
    
  adapter:
    image: tdengine/tdengine:3.x.x
    hostname: adapter
    command: ["taosadapter"]
    environment:
      - TAOS_ADAPTER_TAOSD_FQDN=taosd
    ports:
      - "6041:6041"
      - "6045:6045"
      - "6046:6046"
    depends_on:
      - taosd

Nginx 反代 + TLS + 负载均衡

upstream tdadapter {
  least_conn;
  server adapter1:6041;
  server adapter2:6041;
  server adapter3:6041;
  
  # WebSocket 长连接亲和
  ip_hash;
}

server {
  listen 443 ssl http2;
  server_name taos.example.com;
  
  ssl_certificate /etc/ssl/certs/cert.pem;
  ssl_certificate_key /etc/ssl/private/key.pem;
  
  location /rest/ {
    proxy_pass http://tdadapter;
  }
  
  location /ws {
    proxy_pass http://tdadapter;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }
  
  location /influxdb/ {
    proxy_pass http://tdadapter;
  }
}

性能考量

各协议性能

协议单实例吞吐
REST几万 QPS
WebSocket十几万行/秒
InfluxDB Line几十万行/秒
OpenTSDB Telnet几十万行/秒
StatsD几十万点/秒

调优

参数调整建议
pool.maxConnect4000~10000
pool.idleTimeout1h~3h
多实例高并发
SSL 终止用 Nginx

FAQ

Q1: 一定要部署 taosAdapter 吗?

REST/WebSocket/其他协议必须。Native 客户端不需要。建议总是部署,扩展性更好。

Q2: taosAdapter 与 taosd 必须同机?

不必。可独立部署,但建议同 VPC/低延迟。

Q3: taosAdapter 高可用?

部署多实例 + LB。每个实例无状态,任意节点故障不丢数据(短时连接断开)。

Q4: 跨域 (CORS) 怎么配?

[cors]
allowAllOrigins = true
# 或限定特定域名
allowOrigins = ["https://app.example.com"]

Q5: 监控 taosAdapter 自身?

启用 [monitor] 段,指标会写入 log 数据库。Grafana Dashboard 可视化。

参考

系统构架篇

数据模型

存储引擎

查询引擎

数据写入

数据订阅

预聚合

索引

SQL 语句

客户端与连接器

运维

安全

关于 TDengine

TDengine 专为物联网IoT平台、工业大数据平台设计。其中,TDengine TSDB 是一款高性能、分布式的时序数据库(Time Series Database),同时它还带有内建的缓存、流式计算、数据订阅等系统功能;TDengine IDMP 是一款AI原生工业数据管理平台,它通过树状层次结构建立数据目录,对数据进行标准化、情景化,并通过 AI 提供实时分析、可视化、事件管理与报警等功能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TDengine (老段)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值