# sponge **Repository Path**: ericling666/sponge ## Basic Information - **Project Name**: sponge - **Description**: 集群聊天服务器,docker compose一键部署 - **Primary Language**: Unknown - **License**: GPL-2.0 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 10 - **Forks**: 6 - **Created**: 2023-11-03 - **Last Updated**: 2025-03-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: 聊天服务器, Cpp, muduo, MySQL, Redis ## README ![效果演示](asset/output.gif) 本项目是集群聊天服务器,对应的客户端源代码在[https://gitee.com/ericling666/spongeclient](https://gitee.com/ericling666/spongeclient)。 对本项目的演示,请看视频[【集群聊天服务器与仿微信客户端开发,服务器基于muduo,mysql,redis,客户端基于Qt6和FluentUI,代码已开源】 ](https://www.bilibili.com/video/BV1ZJ4m1L7ur/?share_source=copy_web&vd_source=9ae54c31182cbbfa8dbdc757855a2545) 服务器可用于本地部署,也可以部署到云端,使用docker compose一行代码就能部署所有服务,主要有以下服务: ![docker containers]() 详细的配置信息见[docker compose](docker-compose.yml)文件。 # 服务 - mysql,端口均是3306,方便数据库客户端直连。 - nginx,端口是8000,反向代理了三个服务节点server1、server2、server3。 - redis,端口是6379,方便redis客户端直连。 - server1、server2、server3随机端口,容器内部分别指定为8001、8002、8003,还可以任意添加多个sponge server服务,然后用nginx做负载均衡,这里仅配置了三台。 # 技术特点 - 采用tcp长连接实现数据通信,消息传递直接通过目标用户的tcp长链接发到客户端。 - 对于不在同一台服务器上的两个用户之间的通信,使用redis的发布订阅功能实现消息的转发。redis的connector是[`redis-plus-plus`](https://github.com/sewenew/redis-plus-plus),对redis的连接实现了一个连接池,见[redisconnectionpool.h](src/include/redis/redisconnectionpool.h)。 - 支持离线消息,把离线消息存储到mysql数据库中,对mysql数据库的连接,实现了一个数据库连接池[mysqlconnectionpool](src/include/database/mysql/mysqlconnectionpool.h)。 - 使用[muduo](https://github.com/chenshuo/muduo.git)网络库实现网络通信。 - 使用[json](https://github.com/nlohmann/json)解析数据,有自定义的应用层协议字段。 - 使用[dockerfile](dockerfile)快速构建镜像,对第三方依赖通过git submodule实现版本控制和依赖管理,方便部署。 - 使用nginx实现传输层反向代理、负载均衡,所有服务通过[docker compose](docker-compose.yml)直接启动,方便管理和维护。 - 实现了一个线程安全的异步日志工具类,见[logger.h](src/include/log/logger.h),对INFO和ERROR两种日志类型分别输出到文件和终端。 # 如何使用 有两种部署方式,一种是仅编译spongeserver服务,然后其它服务使用本地安装或者docker容器,另一种办法则是全部用docker部署,只需要使用docker compose一行代码就能启动所有容器。 第一种办法,可以参考[dockerfile](dockerfile)文件给出的编译命令,以下简单给出命令和说明。 ```shell # 下载本仓库,必须带上--recursive,这样会自动克隆muduo,hiredis,redis-plus-plus这几个子模块 git clone --recursive https://gitee.com/ericling666/sponge sudo apt update # 安装mysql的connector sudo apt install libmysqlcppconn-dev # 编译、安装muduo网络库 mkdir -p /path to sponge/dependencies/muduo/build cd /path to sponge/dependencies/muduo/build cmake -DMUDUO_BUILD_EXAMPLES=OFF .. make -j4 sudo make install # 编译、安装hiredis库 mkdir -p /path to sponge/dependencies/hiredis/build cd /path to sponge/dependencies/hiredis/build cmake .. make -j4 sudo make install # 编译、安装redis-plus-plus库 mkdir -p /path to sponge/dependencies/redis-plus-plus/build cd /path to sponge/dependencies/redis-plus-plus/build cmake -DREDIS_PLUS_PLUS_CXX_STANDARD=11 -DREDIS_PLUS_PLUS_BUILD_STATIC=OFF -DREDIS_PLUS_PLUS_BUILD_TEST=OFF .. make -j4 sudo make install # 编译 sponge server mkdir -p /path to sponge/build cd /path to sponge/build cmake -DCMAKE_BUILD_TYPE=release -DSPONGE_DISABLE_EXAMPLES=ON -DSPONGE_DISABLE_TESTS=ON .. make -j4 # 修改mysql和redis的ip地址,(端口号如果不一致也要相应修改),这里是把localhost修改为$mysql和$redis,你需要把$mysql、$redis修改为对应的ip地址 sed -i 's/localhost/$mysql/g' /path to sponge/bin/mysql.cnf sed -i 's/localhost/$redis/g' /path to sponge/bin/redis.cnf cd /path to sponge/bin # 启动server进程,监听8000端口 ./server 0.0.0.0 8000 ``` 此外,还需要导入数据库表,导入脚本见文件[sponge.sql](init-scripts/sponge.sql)。 nginx的配置见文件[nginx.conf](nginx.conf)和文件[sponge.conf](tcp.d/sponge.conf)。 第一种办法,相对麻烦,推荐第二种办法,命令如下,视频中演示的也是这种方案,可以快速部署,上云。 ```shell # 下载本仓库,必须带上--recursive,这样会自动克隆muduo,hiredis,redis-plus-plus这几个子模块 git clone --recursive https://gitee.com/ericling666/sponge # 启动所有服务,这会下载mysql、redis、nginx等镜像,并且下载ubuntu镜像,用于构建sponge server镜像,构建脚本见dockerfile文件 docker compose up -d ``` 服务器启动之后,可以通过`nc `快速验证是否启动成功 ![测试服务器是否成功启动](asset/test.png) 你还可以输入以下json格式的字符串,而不需要客户端,直接与服务器通信: ```json // 用户登录 {"msgType":1, "USERID":6, "password":"6"} // 注册用户 {"msgType":2, "name":"Eric Ling", "password":"whatever"} // 退出登录 {"msgType":3, "USERID":7} // 用户聊天 {"msgType":4,"FROMID":6,"TOID":8,"msg":"你好 8号","sendTime":1703078264002} // 添加好友 {"msgType":6,"U1":1,"U2":2} // 查找好友 {"msgType":8,"FROMID":6} // 群聊消息 {"msgType":9,"FROMID":6,"GROUPID":8,"msg":"你好 8号","sendTime":1703078264002} // 加入群聊 {"msgType":10,"FROMID":6,"GROUPID":7} // 创建群聊 {"msgType":12,"FROMID":6,"name":"cpp","description":"cpp group."} // 查询离线消息 {"msgType":14,"FROMID":1} // 查询账号 {"msgType":15,"keyword":"1"} ``` # 依赖库 其中libmysqlcppconn-dev是通过apt直接安装的,json库是header only,其它三个库都是从源码构建的 - libmysqlcppconn-dev - [muduo](https://github.com/chenshuo/muduo.git) - [hiredis](https://github.com/redis/hiredis.git) - [redis-plus-plus](https://github.com/sewenew/redis-plus-plus.git) - [json](https://github.com/nlohmann/json)