亿级流量点赞系统-第六章

1. 上期回顾

项目源码地址:https://github.com/ruogu-coder/ruogu-like

在第五章,通过引入了消息队列实现了 Redis + 消息队列异步持久化数据,让点赞和响应和数据的持久化解耦,并且通过对账任务保证数据的一致性。通过消息失败重试和死信队列实现消息的可靠性。

2. 本期目标

引入分布式数据库 TiDB(既能保持关系型数据库的 ACID 特性,又能实现水平扩展的分布式数据库解决方案)

3. 环境搭建

官方文档:https://docs.pingcap.com/zh/tidb/stable/quick-start-with-tidb/

我这里的环境是虚拟机 Ubuntu22.04.5 版本

官方文档中快速上手提供两种方式快速体验部署,如果是生产环境,建议参考:生产环境中部署TiDB指南

我这里选择的是 部署本地测试集群

最基础的 TiDB 测试集群通常由 2 个 TiDB 实例、3 个 TiKV 实例、3 个 PD 实例和可选的 TiFlash 实例构成。通过 TiUP Playground,可以快速搭建出上述的一套基础测试集群,步骤如下:

  1. 下载并安装 TiUP
java
复制代码
curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh

安装完成后会提示如下信息:

shell
复制代码
Successfully set mirror to https://tiup-mirrors.pingcap.com Detected shell: bash Shell profile: /home/user/.bashrc /home/user/.bashrc has been modified to add tiup to PATH open a new terminal or source /home/user/.bashrc to use it Installed path: /home/user/.tiup/bin/tiup =============================================== Have a try: tiup playground ===============================================

请注意上述输出中的 Shell profile 文件路径,下一步中需要使用该路径

TiUP 安装完成后会提示 Shell profile 文件的绝对路径。在执行以下 source 命令前,需要将 ${your_shell_profile} 修改为 Shell profile 文件的实际位置。

java
复制代码
source ${your_shell_profile}

注意

  • 如果按以下方式执行 playground,在结束部署测试后,TiUP 会自动清理掉原集群数据,重新执行命令会得到一个全新的集群。
  • 如果希望持久化数据,需要在启动集群时添加 TiUP 的 --tag 参数,详见启动集群时指定tag以保留数据

TiUP Playground 默认监听 127.0.0.1,服务仅本地可访问;若需要使服务可被外部访问,可使用 --host 参数指定监听网卡绑定外部可访问的 IP。

java
复制代码
tiup playground --tag thumb --host 0.0.0.0 --db.host 0.0.0.0

访问 TiDB Dashboard 页面:http://127.0.0.1:2379/dashboard,默认用户名为 root,密码为空。

开放 2379 端口

java
复制代码
firewall-cmd --query-port=2379/tcp # 查看端口是否开放 firewall-cmd --zone=public --add-port=2379/tcp --permanent # 开放端口 firewall-cmd --reload # 重启防火墙使配置生效

无法远程连接数据库,开放 4000 端口重启

java
复制代码
sudo firewall-cmd --add-port=4000/tcp --permanent sudo firewall-cmd --reload

4. 数据迁移

4.1. 导出 SQL 文件

在 IDEA 中穿件 TiDB 连接

创建数据库

修改 application-local.yml

java
复制代码
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver # url: jdbc:mysql://localhost:3306/ruogu_like?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai # username: root # password: 123456 url: jdbc:mysql://192.168.31.7:4000/ruogu_like username: root password: data: redis: host: localhost port: 6379 database: 0 connect-timeout: 5000 # password:

5. 测试

启动项目进行测试

5.1. 点赞

5.2. 取消点赞

0个评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
下载 APP