1. 整体架构设计
1.1 技术栈选型
原来架构:
-
Java 1,8 + Spring Boot 2.2 + Spring Cloud Hoxtom-SR4
-
服务网关: Spring Cloud Gateway
-
RPC框架: OpenFeign
-
Netty (长连接)
-
Redis Cluster (缓存、分布式锁、消息队列)
-
MySQL 5.7 (分库分表,16主16从)
-
MongoDB (消息存储、存储消息记录)
-
消息中间件: Kafka/RocketMQ(消息队列)
-
FastDFS(对象存储)
-
Eureka (注册中心)
-
Config (配置中心)
-
Zipkin(微服务链路中心)
-
Hystrix(熔断降级限流)
-
日志: ELK
升级架构:
-
Java 25 + Spring Boot 3.3 + Spring Cloud 2023.0
-
服务网关: Spring Cloud Gateway
-
RPC框架: Dubbo 或 gRPC
-
Netty 4.1 (长连接)
-
Redis 7 Cluster (缓存、分布式锁、消息队列)
-
MySQL 8.0 (分库分表,16主16从)
-
MongoDB 7 (消息存储、存储消息记录)
-
Elasticsearch 8 (搜索)
-
消息中间件: Kafka/RocketMQ(消息队列)
-
MinIO (对象存储)
-
Nacos (注册中心、配置中心)
-
Sentinel (流控降级)、Hystrix(熔断降级限流)
-
Seata (分布式事务)
-
Canal(实时数据一致性)
-
监控: Prometheus + Grafana
-
日志: ELK
1.2 微服务拆分
yam
services:
- mt-gateway-service: API网关
- mt-auth-service: 认证服务
- mt-user-service: 用户服务。用户注册、登录、资料管理
- mt-find-service: 发现服务
- mt-give-like:点赞服务
- mt-message-service: 消息服务。单聊、群聊消息处理
- mt-group-service: 群组服务。群组管理
- mt-friend-service: 好友服务。好友关系、通讯录
- mt-live-service: 直播服务
- mt-video-service: 短视频服务
- mt-mall-service: 商城服务
- mt-payment-service: 支付服务
- mt-feed-service: 社交服务(朋友圈、动态、圈子)
- mt-push-service: 推送服务
- mt-file-service: 文件服务
- mt-search-service: 搜索服务
- mt-dj-service: dj服务
- mt-inform-service:举报服务
- mt-monitor-service: 监控服务
1.3 系统架构图
text
用户端 → 负载均衡 → API网关 → 微服务集群
↓
Netty集群(长连接)
↓
Redis集群(缓存、会话)
↓
MySQL集群(业务数据) + MongoDB(消息)
↓
Kafka(消息队列)
↓
Elasticsearch(搜索) + MinIO(存储)
2. 数据库设计
2.1 核心表结构设计
2.1.1 用户表(分库分表,32库×32表)
sql
-- 用户主表
CREATE TABLE `im_user_00` (
`id` bigint(20) NOT NULL COMMENT '用户ID(雪花算法)',
`username` varchar(64) NOT NULL COMMENT '用户名',
`password` varchar(255) NOT NULL COMMENT '密码',
`nickname` varchar(64) NOT NULL COMMENT '昵称',
`avatar` varchar(500) DEFAULT NULL COMMENT '头像URL',
`phone` varchar(20) DEFAULT NULL COMMENT '手机号',
`email` varchar(100) DEFAULT NULL COMMENT '邮箱',
`gender` tinyint(1) DEFAULT '0' COMMENT '性别:0-未知,1-男,2-女',
`birthday` date DEFAULT NULL COMMENT '生日',
`signature` varchar(255) DEFAULT NULL COMMENT '个性签名',
`region` varchar(100) DEFAULT NULL COMMENT '地区',
`status` tinyint(1) DEFAULT '1' COMMENT '状态:0-禁用,1-正常,2-冻结',
`user_type` tinyint(1) DEFAULT '0' COMMENT '用户类型:0-普通,1-主播,2-商家',
`level` int(11) DEFAULT '1' COMMENT '用户等级',
`experience` bigint(20) DEFAULT '0' COMMENT '经验值',
`coins` bigint(20) DEFAULT '0' COMMENT '金币',
`diamonds` bigint(20) DEFAULT '0' COMMENT '钻石',
`vip_level` int(11) DEFAULT '0' COMMENT 'VIP等级',
`vip_expire_time` datetime DEFAULT NULL COMMENT 'VIP过期时间',
`last_login_time` datetime DEFAULT NULL COMMENT '最后登录时间',
`last_login_ip` varchar(50) DEFAULT NULL COMMENT '最后登录IP',
`device_id` varchar(100) DEFAULT NULL COMMENT '设备ID',
`device_type` varchar(20) DEFAULT NULL COMMENT '设备类型',
`app_version` varchar(20) DEFAULT NULL COMMENT 'APP版本',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`version` int(11) DEFAULT '0' COMMENT '版本号',
`is_deleted` tinyint(1) DEFAULT '0' COMMENT '是否删除',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_username` (`username`),
UNIQUE KEY `uk_phone` (`phone`),
UNIQUE KEY `uk_email` (`email`),
KEY `idx_nickname` (`nickname`),
KEY `idx_create_time` (`create_time`),
KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户表';
-- 用户扩展表
CREATE TABLE `im_user_ext_00` (
`user_id` bigint(20) NOT NULL COMMENT '用户ID',
`follow_count` int(11) DEFAULT '0' COMMENT '关注数',
`fans_count` int(11) DEFAULT '0' COMMENT '粉丝数',
`friend_count` int(11) DEFAULT '0' COMMENT '好友数',
`group_count` int(11) DEFAULT '0' COMMENT '群组数',
`post_count` int(11) DEFAULT '0' COMMENT '动态数',
`video_count` int(11) DEFAULT '0' COMMENT '视频数',
`live_count` int(11) DEFAULT '0' COMMENT '直播次数',
`total_live_time` bigint(20) DEFAULT '0' COMMENT '总直播时长(秒)',
`total_spend` decimal(20,2) DEFAULT '0.00' COMMENT '总消费金额',
`total_income` decimal(20,2) DEFAULT '0.00' COMMENT '总收入金额',
`total_gifts_sent` bigint(20) DEFAULT '0' COMMENT '送出礼物总数',
`total_gifts_received` bigint(20) DEFAULT '0' COMMENT '收到礼物总数',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户扩展表';
2.1.2 好友关系表
sql
CREATE TABLE `im_friend_00` (
`id` bigint(20) NOT NULL COMMENT '关系ID',
`user_id` bigint(20) NOT NULL COMMENT '用户ID',
`friend_id` bigint(20) NOT NULL COMMENT '好友ID',
`remark` varchar(64) DEFAULT NULL COMMENT '备注',
`alias` varchar(64) DEFAULT NULL COMMENT '昵称备注',
`source` varchar(50) DEFAULT NULL COMMENT '来源',
`category_id` bigint(20) DEFAULT '0' COMMENT '分组ID',
`status` tinyint(1) DEFAULT '0' COMMENT '状态:0-申请中,1-已好友,2-已拒绝,3-已拉黑',
`is_top` tinyint(1) DEFAULT '0' COMMENT '是否置顶',
`is_disturb` tinyint(1) DEFAULT '0' COMMENT '是否免打扰',
`is_special` tinyint(1) DEFAULT '0' COMMENT '特别关心',
`last_interact_time` datetime DEFAULT NULL COMMENT '最后互动时间',
`add_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '添加时间',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_user_friend` (`user_id`,`friend_id`),
KEY `idx_user_id` (`user_id`),
KEY `idx_friend_id` (`friend_id`),
KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='好友关系表';
-- 好友申请记录
CREATE TABLE `im_friend_apply_00` (
`id` bigint(20) NOT NULL COMMENT '申请ID',
`applicant_id` bigint(20) NOT NULL COMMENT '申请人ID',
`receiver_id` bigint(20) NOT NULL COMMENT '接收人ID',
`apply_reason` varchar(255) DEFAULT NULL COMMENT '申请理由',
`status` tinyint(1) DEFAULT '0' COMMENT '状态:0-待处理,1-已同意,2-已拒绝,3-已过期',
`handle_time` datetime DEFAULT NULL COMMENT '处理时间',
`handle_reason` varchar(255) DEFAULT NULL COMMENT '处理理由',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `idx_receiver_status` (`receiver_id`,`status`),
KEY `idx_applicant` (`applicant_id`),
KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='好友申请表';
2.1.3 消息表(分表按月存储)
消息表分表策略:按时间范围分区+用户ID哈希分片。
sql
-- 单聊消息表
CREATE TABLE `im_message_single_202401` (
`msg_id` bigint(20) NOT NULL COMMENT '消息ID(雪花算法)',
`seq_id` bigint(20) NOT NULL COMMENT '序列号(单聊维度)',
`sender_id` bigint(20) NOT NULL COMMENT '发送者ID',
`receiver_id` bigint(20) NOT NULL COMMENT '接收者ID',
`msg_type` tinyint(2) NOT NULL COMMENT '消息类型:1-文本,2-图片,3-语音,4-视频,5-文件,6-位置,7-名片,8-系统消息',
`content_type` tinyint(2) DEFAULT '1' COMMENT '内容类型:1-普通消息,2-@消息,3-引用消息,4-回复消息',
`content` text COMMENT '消息内容',
`ext_data` json DEFAULT NULL COMMENT '扩展数据',
`is_recalled` tinyint(1) DEFAULT '0' COMMENT '是否已撤回',
`recall_time` datetime DEFAULT NULL COMMENT '撤回时间',
`is_read` tinyint(1) DEFAULT '0' COMMENT '是否已读',
`read_time` datetime DEFAULT NULL COMMENT '已读时间',
`send_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '发送时间',
`arrive_time` datetime DEFAULT NULL COMMENT '送达时间',
PRIMARY KEY (`msg_id`),
UNIQUE KEY `uk_seq` (`sender_id`,`receiver_id`,`seq_id`),
KEY `idx_sender_receiver` (`sender_id`,`receiver_id`,`send_time`),
KEY `idx_receiver_send_time` (`receiver_id`,`send_time`),
KEY `idx_send_time` (`send_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='单聊消息表';
-- 群聊消息表
CREATE TABLE `im_message_group_202401` (
`msg_id` bigint(20) NOT NULL COMMENT '消息ID',
`group_id` bigint(20) NOT NULL COMMENT '群组ID',
`seq_id` bigint(20) NOT NULL COMMENT '序列号(群组维度)',
`sender_id` bigint(20) NOT NULL COMMENT '发送者ID',
`msg_type` tinyint(2) NOT NULL COMMENT '消息类型',
`content_type` tinyint(2) DEFAULT '1' COMMENT '内容类型',
`content` text COMMENT '消息内容',
`at_user_ids` json DEFAULT NULL COMMENT '@的用户ID列表',
`ext_data` json DEFAULT NULL COMMENT '扩展数据',
`is_recalled` tinyint(1) DEFAULT '0' COMMENT '是否已撤回',
`recall_time` datetime DEFAULT NULL COMMENT '撤回时间',
`send_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '发送时间',
PRIMARY KEY (`msg_id`),
UNIQUE KEY `uk_group_seq` (`group_id`,`seq_id`),
KEY `idx_group_send_time` (`group_id`,`send_time`),
KEY `idx_sender` (`sender_id`),
KEY `idx_send_time` (`send_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='群聊消息表';
2.1.4 群组表
sql
CREATE TABLE `im_group_00` (
`group_id` bigint(20) NOT NULL COMMENT '群组ID',
`group_code` varchar(32) NOT NULL COMMENT '群号',
`group_name` varchar(100) NOT NULL COMMENT '群名称',
`avatar` varchar(500) DEFAULT NULL COMMENT '群头像',
`owner_id` bigint(20) NOT NULL COMMENT '群主ID',
`admin_ids` json DEFAULT NULL COMMENT '管理员ID列表',
`group_type` tinyint(1) DEFAULT '1' COMMENT '群类型:1-普通群,2-工作群,3-粉丝群,4-直播群',
`max_members` int(11) DEFAULT '500' COMMENT '最大成员数',
`current_members` int(11) DEFAULT '1' COMMENT '当前成员数',
`announcement` text COMMENT '群公告',
`introduction` varchar(500) DEFAULT NULL COMMENT '群简介',
`join_type` tinyint(1) DEFAULT '0' COMMENT '加群方式:0-直接加入,1-需要验证,2-禁止加入',
`question` varchar(255) DEFAULT NULL COMMENT '加群问题',
`answer` varchar(255) DEFAULT NULL COMMENT '加群答案',
`mute_type` tinyint(1) DEFAULT '0' COMMENT '禁言类型:0-不禁言,1-全员禁言,2-普通成员禁言',
`mute_end_time` datetime DEFAULT NULL COMMENT '禁言结束时间',
`status` tinyint(1) DEFAULT '1' COMMENT '状态:0-解散,1-正常,2-封禁',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`version` int(11) DEFAULT '0' COMMENT '版本号',
PRIMARY KEY (`group_id`),
UNIQUE KEY `uk_group_code` (`group_code`),
KEY `idx_owner_id` (`owner_id`),
KEY `idx_group_name` (`group_name`),
KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='群组表';
-- 群成员表
CREATE TABLE `im_group_member_00` (
`id` bigint(20) NOT NULL COMMENT '关系ID',
`group_id` bigint(20) NOT NULL COMMENT '群组ID',
`user_id` bigint(20) NOT NULL COMMENT '用户ID',
`role` tinyint(1) DEFAULT '3' COMMENT '角色:1-群主,2-管理员,3-普通成员',
`alias` varchar(64) DEFAULT NULL COMMENT '群昵称',
`join_type` tinyint(1) DEFAULT '0' COMMENT '加入方式:0-主动加入,1-邀请加入,2-扫码加入',
`inviter_id` bigint(20) DEFAULT NULL COMMENT '邀请人ID',
`last_read_msg_id` bigint(20) DEFAULT '0' COMMENT '最后读取的消息ID',
`last_read_time` datetime DEFAULT NULL COMMENT '最后读取时间',
`is_disturb` tinyint(1) DEFAULT '0' COMMENT '是否免打扰',
`is_top` tinyint(1) DEFAULT '0' COMMENT '是否置顶',
`is_banned` tinyint(1) DEFAULT '0' COMMENT '是否被禁言',
`ban_end_time` datetime DEFAULT NULL COMMENT '禁言结束时间',
`join_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '加入时间',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_group_user` (`group_id`,`user_id`),
KEY `idx_group_id` (`group_id`),
KEY `idx_user_id` (`user_id`),
KEY `idx_role` (`role`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='群成员表';

最低0.47元/天 解锁文章
1789

被折叠的 条评论
为什么被折叠?



