一、Tcaplus是什么?
游戏数据库 TcaplusDB 是专为游戏设计的 NoSQL 分布式数据存储服务,支持 Protobuf 接口访问,Tcaplus 将 Cache 与硬盘结合,追求高性能的同时,也节省成本,很好地支持全区全服和分区分服,并针对游戏爆发增长和长尾运维特点提供不停机扩缩容、备份容灾、快速回档等全套解决方案,安全可信赖。目前应用于《王者荣耀》、《穿越火线》、《火影忍者》等数百款流行游戏。
二、使用步骤
特别要注意的是,在购买时,连接协议里选择TDR,这样就支持xml表定义了。
如何定义表
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <metalib name="demo_table" tagsetversion="1" version="1"> <struct name="user" version="1" primarykey="user_id,server_id" splittablekey="user_id"> <entry name="user_id" type="string" size="450" desc="用户ID"/> <entry name="server_id" type="uint64" desc="服务器ID" /> <entry name="nick_name" type="string" size="50" desc="昵称"/> <entry name="desc" type="string" size="1024" desc="描述信息"/> <entry name="state" type="Tinyuint" defaultvalue="0" desc="用户状态 0 : AVALIABLE, 1 DELETED"/> <index name="index1" column="user_id"/> <index name="index2" column="user_id,server_id"/> </struct> <struct name="mail" version="1" primarykey="user_id,server_id,mail_id" splittablekey="user_id" > <entry name="user_id" type="string" size="50" desc="用户ID"/> <entry name="server_id" type="uint64" desc="服务器ID" /> <entry name="mail_id" type="uint64" desc="邮件ID" /> <entry name="state" type="Tinyuint" defaultvalue="0" desc="邮件状态(0 未读/1 已读)"/> <entry name="from" type="string" size="50" desc="邮件发送方名字"/> <entry name="subject" type="string" size="100" desc="邮件标题"/> <entry name="content" type="string" size="1024" desc="邮件正文"/> <index name="index1" column="user_id"/> <index name="index2" column="user_id,server_id"/> <index name="index3" column="user_id,mail_id"/> </struct> <struct name="server" version="1" primarykey="server_id" splittablekey="server_id" > <entry name="server_id" type="uint64" desc="服务器ID" /> <entry name="server_name" type="string" size="50" desc="服务器名称"/> <entry name="capacity" type="uint64" desc="服务器最大承载容量"/> <entry name="desc" type="string" size="1024" desc="描述信息"/> <entry name="state" type="Tinyuint" defaultvalue="0" desc="server状态 0 : AVALIABLE, 1 DELETED"/> <index name="index1" column="server_id"/> </struct> </metalib>
通过 client 工具访问 TcaplusDB 数据
1.tcaplus_client 是一个 TcaplusDB 表访问的客户端工具,可通过下表中的下载链接进行下载,Linux x86_64 平台的 TcaplusServiceAPI 发布包包含64位 Linux 版本的 tcaplus_client 工具:
版本 | 发布时间 | 操作系统 | 下载包名 |
3.46.0.199033 | 2020/12/28 | Linux x86_64 | 下载 |
2.下载完成 TcaplusServiceApi 安装包后,将其 通过上传工具 上传至与 TcaplusDB 集群同一VPC,同一子网的云服务器中,然后解压。
tar -xf TcaplusPbApi3.46.0.199033.x86_64_release_20201210.tar.gz -C tcaplus
解压完成后,进入至 tcaplus 的 bin 目录中,并赋予可执行权限:
cd tcaplus/release/x86_64/bin chmod +x tcaplus_client
直接执行./tcaplus_client命令,会提示连接数据库所需的参数信息,用户可以根据自己的集群信息进行填写
参数 | 说明 | 是否必填 |
-a | 接入 ID | 是 |
-z | 表格组 ID | 是 |
-s | 集群密码 | 是 |
-d | 集群 IP 地址及端口 | 是 |
-e | 需要执行的 SQL 语句,需要双引号包含 | 否 |
./tcaplus_client -a 15 -z 1 -s "ooxx" -d 172.16.0.7:9999
返回结果
====== Welcome to use tcaplus_client, use "help" to show usage ====== tcaplus> help; -------------------------------------------------------------------------------- help: show usage of commands, example: "help select;". show: get server status related information. executing "help show;" for details. exit/quit: exit the client. count: print record number in the database. desc: print table field name and type. select: query records from database. insert: insert a new record into database. replace: replace a record into the database. update: update a record in the database. delete: delete record(s) from database. dump: dump records from database. load: load records into the database. --------------------------------------------------------------------------------
查看数据库集群状态
tcaplus> show status; ------------------------------------------------------------------ Access_id(app_id) = 15 ------------------------------------------------------------------ TableGroup_id(zone_id) = 1 ------------------------------------------------------------------ [dir_server_url] dir_server_url[0]: 172.16.0.7:9999 ------------------------------------------------------------------ [Proxys] proxy[0]:tcp://172.16.0.7:10001 CONNECTED Authened proxy[1]:tcp://172.16.0.7:10002 CONNECTED Authened ------------------------------------------------------------------ [The connected dir server] tcp://172.16.0.7:9999 ------------------------------------------------------------------
查看所有表
tcaplus> show tables; ---------------------------------------------------------- | Table Name Type Protocol | ---------------------------------------------------------- | server GENERIC TDR | | user GENERIC TDR | | mail GENERIC TDR | ----------------------------------------------------------
查看表结构
tcaplus> desc mail; Table Structure From: Tcaplus System TableName: mail TableType: GENERIC, IdlType: TDRXML SvrTdrCurrentVersion: 1 +---------+------+--------+-----+----+-------+------------+-----+------+-----------------------+ |Field |Type |Key |Index|Size|Version|DefaultValue|Count|Select|Desc | +---------+------+--------+-----+----+-------+------------+-----+------+-----------------------+ |user_id |string|PRI,SPLT|- |50 |1 |- |1 |- |用户ID | +---------+------+--------+-----+----+-------+------------+-----+------+-----------------------+ |server_id|uint64|PRI |- |8 |1 |- |1 |- |服务器ID | +---------+------+--------+-----+----+-------+------------+-----+------+-----------------------+ |mail_id |uint64|PRI |- |8 |1 |- |1 |- |邮件ID | +---------+------+--------+-----+----+-------+------------+-----+------+-----------------------+ |state |uint8 |- |- |1 |1 |0 |1 |- |邮件状态(0 未读/1 已读)| +---------+------+--------+-----+----+-------+------------+-----+------+-----------------------+ |from |string|- |- |50 |1 |- |1 |- |邮件发送方名字 | +---------+------+--------+-----+----+-------+------------+-----+------+-----------------------+ |subject |string|- |- |100 |1 |- |1 |- |邮件标题 | +---------+------+--------+-----+----+-------+------------+-----+------+-----------------------+ |content |string|- |- |1024|1 |- |1 |- |邮件正文 | +---------+------+--------+-----+----+-------+------------+-----+------+-----------------------+ 7 rows in set (113 us)
插入:
tcaplus> insert into mail (user_id, server_id, mail_id, state,from,subject,content) values ("10001",1,202106301221,0,"系统","新手邮件","欢迎你,来到我们的虚拟世界"); insert success insert time: 6427 us
查询表格的记录总数
tcaplus> count mail; -------------------------------------------------------------------------------- | TableName Count | -------------------------------------------------------------------------------- | mail 1 | --------------------------------------------------------------------------------
查询:
tcaplus> select * from mail where user_id = "10001" and server_id = 1; +-------+---------+------------+-----+------+----------+----------------------------+ |user_id|server_id|mail_id |state|from |subject |content | +-------+---------+------------+-----+------+----------+----------------------------+ |"10001"|1 |202106301221|0 |"系统"|"新手邮件"|"欢迎你,来到我们的虚拟世界"| +-------+---------+------------+-----+------+----------+----------------------------+ 1 records selected, select time: 41306 us
如果要列显示:
tcaplus> select * from mail where user_id = "10001" \G; user_id: "10001" server_id: 1 mail_id: 202106301221 state: 0 from: "系统" subject: "新手邮件" content: "欢迎你,来到我们的虚拟世界" 1 records selected, select time: 5450 us
更新:
tcaplus> update mail set state = 1, from = "系统2" where user_id = "10001" and server_id = 1 and mail_id=202106301221; update success update time: 7497 us
删除:
tcaplus> delete from mail where user_id = "10001" and server_id = 1 and mail_id=202106301221; delete success delete time: 5367 us
如果要通过部分key 删除符合条件的记录,如下使用:
tcaplus> delete from mail where user_id = "10001" and server_id = 1 by partkey; delete success delete time: 8567 us
导出
tcaplus> dump * into mail.txt from mail limit 10;
会导出到本目录下,打开后,内容如下:
[root@VM_0_13_centos bin]# cat mail.txt user_id,server_id,mail_id,state,from,subject,content "10001",1,202106301221,0,"系统","新手邮件","欢迎你,来到我们的虚拟世界"
导入
tcaplus> load mail infile mail.txt; loaded 1 records successful
索引查询
当开启全局索引功能后,TcaplusDB 将支持按照普通字段查询,查询条件中的字段必须是建立了全局索引的字段。
如果是聚合查询,那么聚合查询的字段也必须是建立了全局索引的字段。
一个索引查询请求,最多返回3000条记录。
支持的查询语句
条件查询
支持=, >, >=, <, <=, !=, between, in, not in, like, not like, and, or
聚合查询
当前支持的聚合查询包括:sum, count, max, min, avg,如:
以下两个指令要等到3.51.0才能支持
设置生命周期
毫秒级别
tcaplus> setttl mail ttl=60000 where user_id = "10001" and server_id = 1 and mail_id="202106301221";
读取生命周期
tcaplus> getttl from mail where user_id = "10001" and server_id = 1 and mail_id="202106301221";
退出
tcaplus> exit
当你不知道某个指令如何使用时,可以用help 前缀看帮助内容:
help select
如何安装:
大概有7个G不到的大小
docker load -i tcaplusdb-local-3.53.1.tar.gz
[root@VM-0-15-centos download]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tcaplusdb-local 3.53.1 34c03389c8d3 3 days ago 6.74GB
打tag
sudo docker tag 34c03389c8d3 ccr.ccs.tencentyun.com/jiangguilong/tcaplus:3.53.1
推送到远程镜像仓库
sudo docker push ccr.ccs.tencentyun.com/jiangguilong/tcaplus:3.53.1
创建工作负载 Deployment,特别要指出
运行命令为/bin/sh,运行参数
-c sleep 36000
#TCAPLUS_CONTAINER_PROXY_PUBLIC_IP是指定proxy的ip, 获取方式:进CentOS7 环境,用ifconfig 获取eth0的ip, docker run -itd --privileged -e TCAPLUS_CONTAINER_PROXY_PUBLIC_IP="x.x.x.x" -e TCAPLUS_CONTAINER_OMS_PASSWORD="***" -p 80:80 -p 13755-13765:13755-13765 -p 9999:9999 --shm-size=3G --name test tcaplusdb-local:3.51.1
TCAPLUS_CONTAINER_PROXY_PUBLIC_IP说明:
场景1:Docker部署在远程机器环境,此时指定的IP为远程机器的IP(能被本机访问OK,telnet
9999,13755递增类端口ok)
场景2:Docker部署在本机windows10的wsl2环境,此时指定的ip为进wsl2的Centos7环境,ifconfig获取
的eth0的ip
场景3:Docker部署在腾讯云CVM环境,此时指定的IP为CVM机器的外网IP(安全组放通9999,13755-137xx
范围内的端口)
查看开通的
端口
[root@tcaplus-d57ffc6c8-n2tfq tcaplus]# netstat -nltp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 2205/httpd tcp 0 0 172.16.0.28:6000 0.0.0.0:* LISTEN 1451/./swiftd tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1382/httpd tcp 0 0 172.16.0.28:9200 0.0.0.0:* LISTEN 966/java tcp 0 0 127.0.0.1:9010 0.0.0.0:* LISTEN 1351/tconnd tcp 0 0 172.16.0.28:4500 0.0.0.0:* LISTEN 2128/tcapdb tcp 0 0 172.16.0.28:9300 0.0.0.0:* LISTEN 966/java tcp 0 0 172.16.0.28:21 0.0.0.0:* LISTEN 825/vsftpd tcp 0 0 172.16.0.28:9017 0.0.0.0:* LISTEN 1478/base_agent_net tcp 0 0 172.16.0.28:13755 0.0.0.0:* LISTEN 2026/swiftd tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 1382/httpd tcp 0 0 172.16.0.28:13756 0.0.0.0:* LISTEN 2029/swiftd tcp 0 0 172.16.0.28:9020 0.0.0.0:* LISTEN 1478/base_agent_net tcp 0 0 0.0.0.0:36000 0.0.0.0:* LISTEN 853/sshd tcp 0 0 172.16.0.28:8899 0.0.0.0:* LISTEN 1367/tcenterd tcp 0 0 172.16.0.28:9898 0.0.0.0:* LISTEN 2109/swiftd tcp 0 0 172.16.0.28:3306 0.0.0.0:* LISTEN 699/mysqld tcp 0 0 172.16.0.28:9999 0.0.0.0:* LISTEN 2109/swiftd
对pod的环境配置要求,如果内存不够用会导致启动部分service失败
CPU | 内存 | 本地存储 | 实例数量(最低要求) |
4 核 | 8 GB | 50 GB | 1 |
如何在TKE下设置pod配置:
工作负载类型:Deployment 镜像:ccr.ccs.tencentyun.com/yorha-kf-demo/tcaplus 镜像版本:3.53.1 环境变量: TCAPLUS_CONTAINER_OMS_PASSWORD=Yorha888 TCAPLUS_CONTAINER_PROXY_PUBLIC_IP=42.192.666.252 工作目录:/data/tcaplus/ 运行命令:/bin/sh 运行参数: -c sh tcaplus_run.sh > run.log && sleep 36000 特权级容器:开启 容器开启特权级,将拥有宿主机的root权限
截图如下:
我们通过负载均衡来访问,服务和路由设置:
如果遇到license无效需要你上传时,只需要设置下,
系统管理–>系统配置->TOKEN绑定IP 设置否
游戏进程和Tcaplus之间的连接一般是一个进程一个连接,用异步
参考链接:游戏数据库TcaplusDB
总结
TcaplusDB是腾讯出品的分布式NoSQL数据库,存储和调度的代码完全自研。具备缓存+落地融合架构、PB级存储、毫秒级时延、无损水平扩展和复杂数据结构等特性。同时具备丰富的生态、便捷的迁移、极低的运维成本和五个九高可用等特点。客户覆盖游戏、互联网、政务、金融、制造和物联网等领域。