# go语言封装的聊天服务系统 **Repository Path**: hlw422/go_chat_server ## Basic Information - **Project Name**: go语言封装的聊天服务系统 - **Description**: go语言封装的聊天服务系统,采用微服务架构,docker进行服务安装 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-06-16 - **Last Updated**: 2026-06-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Go Chat Server 一个基于Go语言微服务架构的完整聊天系统,支持单聊、群聊、多媒体消息、音视频通话等功能。 ## 功能特性 - **用户系统**:注册、登录、用户资料管理、用户搜索 - **好友系统**:发送好友请求、接受/拒绝、好友列表管理 - **即时通讯**:单聊、群聊、消息历史记录 - **多媒体消息**:支持图片、文件、音频、视频消息 - **离线消息**:离线时自动存储消息,上线后自动推送 - **音视频通话**:WebRTC信令服务,支持音频/视频通话 - **实时通知**:基于SSE(Server-Sent Events)的实时消息推送 - **消息状态**:已发送、已送达、已读状态追踪 ## 架构设计 ``` ┌─────────────────────────────────────────────────────────────┐ │ Client (Web/App) │ └─────────────────────────┬───────────────────────────────────┘ │ HTTP/HTTPS + SSE ▼ ┌─────────────────────────────────────────────────────────────┐ │ API Gateway (:8080) │ │ (Gin + JWT Auth + CORS + SSE) │ └─────────────────────────┬───────────────────────────────────┘ │ gRPC ┌───────────────┼───────────────┐ ▼ ▼ ▼ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ User │ │ Friend │ │ Chat │ │ Service │ │ Service │ │ Service │ │ (:50051) │ │ (:50052) │ │ (:50053) │ └──────────┘ └──────────┘ └──────────┘ │ │ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ Message │ │ Offline │ │ Media │ │ Service │ │ Service │ │ Service │ │ (:50054) │ │ (:50055) │ │ (:50056) │ └──────────┘ └──────────┘ └──────────┘ │ │ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ Notif. │ │ Call │ │ Shared │ │ Service │ │ Service │ │ Utils │ │ (:50057) │ │ (:50058) │ │ │ └──────────┘ └──────────┘ └──────────┘ │ │ │ └───────────────┼───────────────┘ ▼ ┌─────────────────────────────────────────┐ │ Infrastructure │ │ MongoDB | Redis | MinIO | Nginx │ └─────────────────────────────────────────┘ ``` ## 技术栈 | 组件 | 技术 | |------|------| | 语言 | Go 1.21+ | | Web框架 | Gin | | RPC框架 | gRPC + Protocol Buffers | | 数据库 | MongoDB | | 缓存 | Redis | | 对象存储 | MinIO (S3兼容) | | 认证 | JWT | | 日志 | Zap | | 配置 | Viper | | 容器化 | Docker + Docker Compose | ## 项目结构 ``` chatserver/ ├── api-gateway/ # API网关服务 ├── user-service/ # 用户服务 ├── friend-service/ # 好友服务 ├── chat-service/ # 会话服务 ├── message-service/ # 消息服务 ├── offline-service/ # 离线消息服务 ├── media-service/ # 媒体服务 ├── call-service/ # 通话服务 ├── notification-service/ # 通知服务 ├── shared/ # 共享工具库 ├── deploy/ # 部署配置 ├── docs/ # 文档 ├── docker-compose.yml # Docker编排 ├── Makefile # 构建脚本 └── go.work # Go工作空间 ``` ## 快速开始 ### 前置条件 - Go 1.21+ - Docker & Docker Compose - MongoDB (可选,使用Docker) - Redis (可选,使用Docker) ### 1. 克隆项目 ```bash git clone https://gitee.com/hlw422/go_chat_server.git cd go_chat_server ``` ### 2. 启动基础设施 ```bash # 启动 MongoDB、Redis、MinIO docker-compose up -d mongodb redis minio ``` ### 3. 启动服务 **方式一:逐个启动(开发调试)** ```bash # 终端1 - 用户服务 cd user-service && go run ./cmd # 终端2 - 好友服务 cd friend-service && go run ./cmd # 终端3 - 会话服务 cd chat-service && go run ./cmd # 终端4 - 消息服务 cd message-service && go run ./cmd # 终端5 - 离线服务 cd offline-service && go run ./cmd # 终端6 - 媒体服务 cd media-service && go run ./cmd # 终端7 - 通知服务 cd notification-service && go run ./cmd # 终端8 - 通话服务 cd call-service && go run ./cmd # 终端9 - API网关 cd api-gateway && go run ./cmd ``` **方式二:Docker Compose(一键启动)** ```bash docker-compose up -d ``` ### 4. 验证服务 ```bash # 检查API网关是否运行 curl http://localhost:8080/api/v1/users/register -X POST \ -H "Content-Type: application/json" \ -d '{"username":"testuser","email":"test@example.com","password":"123456"}' ``` ## API 文档 详细的API文档请参考 [OpenAPI规范](docs/api/openapi.yaml) ### 核心API端点 #### 用户服务 | 方法 | 路径 | 描述 | |------|------|------| | POST | `/api/v1/users/register` | 用户注册 | | POST | `/api/v1/users/login` | 用户登录 | | GET | `/api/v1/users/profile` | 获取用户资料 | | PUT | `/api/v1/users/profile` | 更新用户资料 | | GET | `/api/v1/users/search` | 搜索用户 | #### 好友服务 | 方法 | 路径 | 描述 | |------|------|------| | POST | `/api/v1/friends/request` | 发送好友请求 | | POST | `/api/v1/friends/accept` | 接受好友请求 | | POST | `/api/v1/friends/reject` | 拒绝好友请求 | | DELETE | `/api/v1/friends/:id` | 删除好友 | | GET | `/api/v1/friends` | 获取好友列表 | | GET | `/api/v1/friends/requests` | 获取好友请求列表 | #### 会话服务 | 方法 | 路径 | 描述 | |------|------|------| | POST | `/api/v1/conversations` | 创建会话 | | GET | `/api/v1/conversations` | 获取会话列表 | | GET | `/api/v1/conversations/:id` | 获取会话详情 | | DELETE | `/api/v1/conversations/:id` | 删除会话 | | POST | `/api/v1/conversations/:id/participants` | 添加群成员 | | DELETE | `/api/v1/conversations/:id/participants/:uid` | 移除群成员 | #### 消息服务 | 方法 | 路径 | 描述 | |------|------|------| | POST | `/api/v1/conversations/:id/messages` | 发送消息 | | GET | `/api/v1/conversations/:id/messages` | 获取消息历史 | | DELETE | `/api/v1/messages/:id` | 删除消息 | | PUT | `/api/v1/messages/:id/status` | 更新消息状态 | #### 媒体服务 | 方法 | 路径 | 描述 | |------|------|------| | POST | `/api/v1/media/upload` | 上传文件 | | GET | `/api/v1/media/:id` | 获取文件 | | DELETE | `/api/v1/media/:id` | 删除文件 | #### 通话服务 | 方法 | 路径 | 描述 | |------|------|------| | POST | `/api/v1/calls/initiate` | 发起通话 | | POST | `/api/v1/calls/accept` | 接听通话 | | POST | `/api/v1/calls/reject` | 拒绝通话 | | POST | `/api/v1/calls/end` | 结束通话 | #### 通知服务 | 方法 | 路径 | 描述 | |------|------|------| | GET | `/api/v1/notifications` | 获取通知列表 | | GET | `/api/v1/notifications/unread/count` | 获取未读数量 | | POST | `/api/v1/notifications/:id/read` | 标记已读 | | POST | `/api/v1/notifications/read-all` | 全部标记已读 | #### SSE 实时推送 | 方法 | 路径 | 描述 | |------|------|------| | GET | `/api/v1/sse/connect` | 建立SSE连接 | **SSE事件类型**: - `message` - 新消息 - `typing` - 输入状态 - `status` - 用户状态变更 - `call` - 通话事件 - `notification` - 通知 - `friend_request` - 好友请求 ## 使用示例 ### 1. 用户注册 ```bash curl -X POST http://localhost:8080/api/v1/users/register \ -H "Content-Type: application/json" \ -d '{ "username": "zhangsan", "email": "zhangsan@example.com", "password": "password123" }' ``` ### 2. 用户登录 ```bash curl -X POST http://localhost:8080/api/v1/users/login \ -H "Content-Type: application/json" \ -d '{ "email": "zhangsan@example.com", "password": "password123" }' ``` 响应: ```json { "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...", "user": { "id": "665f1a2b3c4d5e6f7a8b9c0d", "username": "zhangsan", "email": "zhangsan@example.com", "status": "online" } } ``` ### 3. 发送消息 ```bash # 使用登录返回的token curl -X POST http://localhost:8080/api/v1/conversations/{conversation_id}/messages \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_TOKEN" \ -d '{ "type": "text", "content": "你好!" }' ``` ### 4. 建立SSE连接 ```javascript // JavaScript 示例 const eventSource = new EventSource('http://localhost:8080/api/v1/sse/connect', { headers: { 'Authorization': 'Bearer YOUR_TOKEN' } }); eventSource.onmessage = (event) => { const data = JSON.parse(event.data); console.log('收到消息:', data); }; eventSource.addEventListener('message', (event) => { const message = JSON.parse(event.data); console.log('新消息:', message); }); ``` ## 配置说明 每个服务都有独立的配置文件 `config/config.yaml`: ```yaml server: port: 50051 host: "0.0.0.0" database: uri: "mongodb://admin:password@localhost:27017" database: "chatserver" redis: addr: "localhost:6379" password: "" db: 0 jwt: secret: "your-secret-key-change-in-production" expiration: 24 # 小时 ``` ## 开发指南 ### 运行测试 ```bash # 运行所有测试 make test # 运行单个服务的测试 cd user-service && go test ./... ``` ### 代码检查 ```bash make lint ``` ### 生成Proto代码 ```bash make proto ``` ### 构建二进制 ```bash make build ``` ## 部署 ### Docker部署 ```bash # 构建所有镜像 docker-compose build # 启动所有服务 docker-compose up -d # 查看日志 docker-compose logs -f api-gateway ``` ### 环境变量 生产环境建议通过环境变量配置敏感信息: ```bash export MONGO_URI="mongodb://user:password@host:port/database" export REDIS_ADDR="redis-host:6379" export JWT_SECRET="your-secure-secret-key" export MINIO_ACCESS_KEY="your-access-key" export MINIO_SECRET_KEY="your-secret-key" ``` ## 监控 ### 健康检查 ```bash # 检查各服务状态 curl http://localhost:8080/health ``` ### 日志 服务使用结构化日志(JSON格式),可通过以下方式查看: ```bash # Docker环境 docker-compose logs -f user-service # 本地环境 # 日志输出到stdout ``` ## 常见问题 ### Q: 如何修改数据库连接? 修改对应服务的 `config/config.yaml` 文件中的 `database.uri` 字段。 ### Q: 如何添加新的消息类型? 1. 在 `message-service` 的 model 中添加新类型 2. 更新 proto 定义 3. 在 handler 中添加处理逻辑 ### Q: 如何扩展为分布式部署? 1. 使用服务发现(Consul/etcd) 2. 配置负载均衡(Nginx) 3. 使用消息队列(Nats/Kafka)处理跨服务通信 ## 贡献指南 1. Fork 本仓库 2. 创建特性分支 (`git checkout -b feature/your-feature`) 3. 提交更改 (`git commit -m 'Add some feature'`) 4. 推送到分支 (`git push origin feature/your-feature`) 5. 创建 Pull Request ## 许可证 本项目采用 MIT 许可证 - 详见 [LICENSE](LICENSE) 文件 ## 联系方式 - 作者:hlw42 - 仓库:https://gitee.com/hlw422/go_chat_server ## 致谢 感谢以下开源项目: - [Go](https://golang.org/) - [Gin](https://github.com/gin-gonic/gin) - [gRPC](https://grpc.io/) - [MongoDB](https://www.mongodb.com/) - [Redis](https://redis.io/) - [MinIO](https://min.io/)