﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-Jack Jiang</title><link>http://www.blogjava.net/jb2011/</link><description>我的最新工程MobileIMSDK：http://git.oschina.net/jackjiang/MobileIMSDK</description><language>zh-cn</language><lastBuildDate>Sun, 14 Jun 2026 18:25:23 GMT</lastBuildDate><pubDate>Sun, 14 Jun 2026 18:25:23 GMT</pubDate><ttl>60</ttl><item><title>直播系统聊天技术(十)：基于实时消息总线的活动系统架构设计</title><link>http://www.blogjava.net/jb2011/archive/2026/06/08/451786.html</link><dc:creator>Jack Jiang</dc:creator><author>Jack Jiang</author><pubDate>Mon, 08 Jun 2026 03:52:00 GMT</pubDate><guid>http://www.blogjava.net/jb2011/archive/2026/06/08/451786.html</guid><wfw:comment>http://www.blogjava.net/jb2011/comments/451786.html</wfw:comment><comments>http://www.blogjava.net/jb2011/archive/2026/06/08/451786.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jb2011/comments/commentRss/451786.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jb2011/services/trackbacks/451786.html</trackback:ping><description><![CDATA[<p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">本文由云音乐技术团队燕十三分享，有修订和改动。</p><h1>1、引言</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">本文分享的是网易云音乐技术团队基于实时消息总线技术，解决了直播活动系统的模块灵活组合、消息治理与异步履约等问题，希望能给你带来启发。</p><div img-uploading-status=""  image-package-1"="" data-index="1" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="cover-opti" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202606/1834368-20260607220850523-776800620.png" src="https://img2024.cnblogs.com/blog/1834368/202606/1834368-20260607220850523-776800620.png"  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><h1>2、系列文章</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>本文是系列文章中的第 <em>10</em> 篇：</strong></p><ol style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-1236-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">直播系统聊天技术(一)：百万在线的美拍直播弹幕系统的实时推送技术实践之路</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-3252-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">直播系统聊天技术(二)：阿里电商IM消息平台，在群聊、直播场景下的技术实践</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-3376-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">直播系统聊天技术(三)：微信直播聊天室单房间1500万在线的消息架构演进之路</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-3515-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">直播系统聊天技术(四)：百度直播的海量用户实时消息系统架构演进实践</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-3594-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">直播系统聊天技术(五)：微信小游戏直播在Android端的跨进程渲染推流实践</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-3799-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">直播系统聊天技术(六)：百万人在线的直播间实时聊天消息分发技术实践</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-3835-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">直播系统聊天技术(七)：直播间海量聊天消息的架构设计难点实践</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-3994-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">直播系统聊天技术(八)：vivo直播系统中IM消息模块的架构实践</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-4299-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">直播系统聊天技术(九)：千万级实时直播弹幕的技术实践</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-4914-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">直播系统聊天技术(十)：基于实时消息总线的活动系统架构设计</a>》（<span style="color: #ff0000;"><strong>* 本文</strong></span>）</li></ol><h1>3、技术背景</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">所谓组装，就离不开老生常谈的复用，我们可以对大部分认为比较共性的场景做好系统级别的封装，封装成一个个复用度较高的服务，然后通过接口和扩展点的方式进行一部分的能力开放，但是有一种场景是解决不了的，就是当一个功能级别的代码执行结束后，希望触发到另外一个功能，同时希望这个功能是可以通过配置去解决的，并且不需要通过开发的手段去解决这类问题。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">例如，用户送了一个礼物给一个主播，直播间的贡献榜上对该用户做了积分 +1，这是一个很典型的「履约」类场景，与我们在直播间内下单购买，履约给仓储系统道理是一样的，但是这都建立在这个流程模式是固化的。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">而活动往往不是这样的，活动相比这些固化的流程是更为灵活的。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>笔者的团队曾经开发过这样一个场景：</strong></p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;">用户通过送礼，去帮助主播完成一个直播间虚拟能量条的冲击，而每充满这个能量条都希望做一件事情，这个事情就彰显出业务侧的脑洞大开，第一次的活动是希望给某个榜单加上分数，第二次的活动希望是给主播掉落一个虚拟的宝箱，第三次的活动是给用户发送一些抽奖券，依次类推，只要是做过的功能，他都希望这个事情可以去用，时间久了，就会面临频繁去修改这个模块的代码，当它「结束」后，if 条件式的去触发各个代码，或者策略模式的去魔改代码，对于长期建设来看这种方式并不友好，就我看来这只是这个模块结束后要做这么多事情，那么下一个模块如果从这么多事情挑两件岂不是还要再写一堆代码？</p></blockquote><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">因此我们想到了一个相对比较原始的解决方案：总线式服务。</p><h1>4、信鸽服务的组合能力</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">与其说组合，更倾向于用「履约」这个词形容会比较恰当一点，因为对于活动来说，信鸽只做「履约」这一侧的功能比较重要，他要解决的是异步场景的分发类问题，而不是对一些系统做一些 adapter 组合集成的能力。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">这里以一个活动场景来做示例：</p><div img-uploading-status=""  image-package-2"="" data-index="2" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="1" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202606/1834368-20260607220857544-2079633592.png" src="https://img2024.cnblogs.com/blog/1834368/202606/1834368-20260607220857544-2079633592.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;"><strong>音乐人活动的一个简单的页面主要包含了几个模块功能：</strong></p></div></div></div><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>用户通过送礼、观看等行为完成相关任务；</li><li style="list-style: inherit;"><em>2）</em>为喜爱的音乐人加进度条积分；</li><li style="list-style: inherit;"><em>3）</em>当进度条积分完成某个档位后，触发宝箱的掉落、飘屏送礼等。每个档位的类型是不同的；</li><li style="list-style: inherit;"><em>4）</em>幸运锦鲤的模块。</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">对于这样一个活动，从开发的角度来看，其实它是由几个模块进行组装的，除了 4 是需要独立开发的，1、2、3 都可以通过现有的系统进行组装，这里 1 抽象为任务系统，2 抽象为进度条系统，3 也可以抽象为宝箱系统、送礼系统等。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">下图是对场景抽象化模块的概念：</p><div img-uploading-status=""  image-package-3"="" data-index="3" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="2" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202606/1834368-20260607220904408-23033239.png" src="https://img2024.cnblogs.com/blog/1834368/202606/1834368-20260607220904408-23033239.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;">那么难点来了，通过什么手段可以来将这些模块进行组装呢？</p></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">通过图中可以看到，当一个模块完成了他的生命周期，可以发送一份数据，路由系统收到了这个数据之后，会帮我们做一层路由的转发，决定数据会路由到下一个系统上去。下一个系统可能是「奖励系统」也可以是「进度条系统」。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">同时可以对这个行为进一步细化的抽象，我们希望这个路由系统充当一个「总线」的角色，「信件」代表了每一个系统希望收到的数据，同时对每一个系统都抽象化成一个目的地，如果我们配置了一份路由关系（抽象为信鸽配置），那么这只「信鸽」可以作为将数据信件为我们带到我们想去的任何目的地，那么对于系统的好处是，系统只需要提供自己接入这个路由系统的能力即可，下一次随便是什么活动，可以直接做一些组合关系。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>有了信鸽路由这种思路，针对音乐人活动这种场景，我们可以将一个流程的完整链路梳理出来：</strong></p><div img-uploading-status=""  image-package-4"="" data-index="4" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="3" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202606/1834368-20260607220911532-500319752.png" src="https://img2024.cnblogs.com/blog/1834368/202606/1834368-20260607220911532-500319752.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;">图中可以看到，我们对任务、进度条等基础模块，做了一些扩展点（EXT-POINT）来满足业务流程的编排，这在中台里是比较常规的解决问题的手段之一，而在系统模块组组合的场景中，多了一层信鸽服务的概念，在【2】、【3】、【4】的处理流程中，都可以由信鸽来决定数据流向哪个系统，事实证明这个方案是可行也并且有效的。</p></div></div></div><h1>5、整体架构设计</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">那么如何设计这样一个组合能力的架构呢？</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>研发的重心可以分为四个层面：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>履约能力：当代码结束后做到末端的触发。</li><li style="list-style: inherit;"><em>2）</em>SDK 接入能力：Interface 级别的包装，天然的 Autoconfigure 能力。</li><li style="list-style: inherit;"><em>3）</em>全局的标识字典：一份数据如何让所有接入的系统都可以达成共识。</li><li style="list-style: inherit;"><em>4）</em>系统的自动注册能力：接入 SDK 后，自动上报到信鸽服务的统一管理，自动激活，不需要人工的介入开启。</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>基于这四个层面，架构设计如下图：</strong></p><div img-uploading-status=""  image-package-5"="" data-index="5" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="4" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202606/1834368-20260607220918134-803980143.png" src="https://img2024.cnblogs.com/blog/1834368/202606/1834368-20260607220918134-803980143.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;"><strong>从图中可以看到：</strong>左侧虚线框代表了一个系统的触发，右边虚线框代表了最终另外一个系统的触达。信鸽系统充当了代理的服务，触发的系统只需要接入信鸽 SDK，当执行完自己的职责之后，对数据做一层组装发送给信鸽系统，信鸽系统会根据发送的信鸽 id，寻找到信鸽的相关配置，信鸽 id 决定了数据会流转到那个接入的子系统上，这个流转可以是同步的也可以是异步的（当然大部分都是异步场景），异步主要依赖「RocketMQ」的投递能力，当转发失败后，会对投递的数据结果集进行备份存储，用于定时步长的重试操作。在以往的实践过程中，大部分场景都是异步的链路，不需要获得下一个子系统提供的返回的结果集，并且「RocketMQ」本身投递消息的出错率也是小概率事件（毕竟4台 broker，出错3台的可能性是极低的），相比 RPC 这类通信级别的接口有绝对的优势。</p></div></div></div><h1>6、提供的SDK能力</h1><h2>6.1 路由触发</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">在上图中，我们可以看到对于子系统分成了大概 2 类场景，一种是活动业务域，另外一种是非活动业务域，这本身与业务的场景有关，我们希望所有的子系统都可以按照标准去接入 SDK，但是并不能保证每个子系统提供能力都依靠 SDK，对于一些非活动业务域使用了定制化开发的模式来进行桥接工作，这种桥接工作更像是传统的 adapter 和 ESB 总线思路。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">而活动业务域的子系统都可以采取接入 SDK 的模式，这里主要会介绍一下异步的设计思路，当一个子系统接入 SDK 后，会在 Spring 容器创建 bean 的时候，默认创建一个 PushConsumer 的 bean ，添加监听信鸽 「fly」 的 「Listener」 ，这样能做到自动消费到路由的消息，对消息进行解析，假设这个系统可以承担的模块能力分别是 S1 / S2 ..., SN 等功能，那么整体实现图如下图所示。</p><div img-uploading-status=""  image-package-6"="" data-index="6" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="5" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202606/1834368-20260607220924083-1773808322.png" src="https://img2024.cnblogs.com/blog/1834368/202606/1834368-20260607220924083-1773808322.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><h2>6.2 自动注册</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">每一个 Provider 的 Server 都是一个独立的应用服务集群，对于每一个 Provider 来说他提供的能力并不是单一的，正如上文所说，一个领域服务（活动 TOY 服务：主要提供各类积木式玩法领域），他能够提供的模块是非常多样的，诸如宝箱、进度条、留言板等等，「act-toy」 服务他具备了许多功能，当 「act-toy」 服务接入 SDK 后，就需要把自己子域需要发布成 Provider 的功能注册到信鸽上，注册的方案如下图。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">当一个 Server 拉取 SDK 的启动后，会定时的拉取定义好的 Interface ，对实现的 Class ，获取自定义注解的 Type 类型，通过顺序消息的方式注册到信鸽服务上，采用了启动 + 定时推送的方式，信鸽服务收到相关的注册信息可以后会将其存储。</p><div img-uploading-status=""  image-package-7"="" data-index="7" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="6" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202606/1834368-20260607220929979-857766167.png" src="https://img2024.cnblogs.com/blog/1834368/202606/1834368-20260607220929979-857766167.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><h1>7、与ESB消息总线方案的比较</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">从整个组合的能力上看，整体的设计思路是符合总线式服务，而总线式服务业内比较经典的，就是 ESB 总线。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">ESB 总线这个技术用今天互联网的角度来看是比较过时的，因为他本身更适应用于大型 IT 企业内部的一些跨语言类服务架构方案，而他本身承接了系统和系统之间的桥接能力。但是信鸽本身的设计不同于 ESB 总线，他更注重的特点是「哑管道」的概念，忽略集成的适配转换，也不适合做中心化同步的集成。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>那么具体的比较如下：</strong></p><div img-uploading-status=""  image-package-8"="" data-index="8" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="7" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202606/1834368-20260607220935469-861389729.png" src="https://img2024.cnblogs.com/blog/1834368/202606/1834368-20260607220935469-861389729.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;"><strong>那么可以从2个 ESB 场景来介绍信鸽的优势所在，笔者理解的 ESB 总线技术大致分两类：</strong></p></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><em>1）</em>开源的 ESB 解决方案：这类是提供开源的一种框架技术，部署在同一个 Tomcat 容器中，开发若干个 Bundle 可以运行在容器中，并且能做到热替换的作用，但是每个 bundle 需要通信的模式需要定制私有协议。由于这类开源框架较早，提供的通信协议大部分是 WebService 类的，因此在开发过程中，开发的成本会非常高，例如 Apache 早期开源的 ServiceMix 。关于协议，需要写大量的 「wsdl」 做通信，大多数也是依赖 WebService 发布的接口。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><em>2）</em>自研的 ESB 集成能力：笔者曾经参加过一个企业级的项目，大致是 ESB 的总线集成了各路系统，包括了 Rest 协议接口、C++ 服务发布的 WebService 协议、海外第三方公司发布的 WebService 协议，为了打通公司内部 C++ 服务、Java Web 服务与海外第三方系统，采用的就是做一个集成的总线，这里需要太多的协议转换，包括如何解析 「wsdl」 文件读取节点数据，转换成 Json 等等，这类场景在企业级服务是常见的事情，本身不适用互联网场景，侧重点更偏向于同步接口协议的转换，同时还不知道编排的能力，大部分依靠硬编码来解决。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">无论 1 还是 2 与我们信鸽设计思路都是不同的，信鸽本质上强调的是异步去做末端的事件，选用轻量的协议结构，在技术上属于同语言系，因此关于字典的定义也是标准化的，也就是说，userId、anchorId 分别代表了用户id和主播id，并不需要任何一方突发奇想定义 uid、aid 这类的字样，由信鸽来定义统一化的字典，同时编排统一收拢到信鸽服务来确定，是可视化的，不需要编写复杂的 XML 节点文件。</p><h1>8、与Pipeline流水线方案的比较</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">从总线这个概念来看，或多或少同「管道」是类似的，Pipeline 的思想被广泛应用在诸多技术领域中。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>比如：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>CI/CD 持续集成的场景；</li><li style="list-style: inherit;"><em>2）</em>开源框架中的流水线设计模式，例如 Netty框架中的网络字节流处理等；</li><li style="list-style: inherit;"><em>3）</em>业务自定义的一些工作流流转技术。</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>第一种场景：</strong>更倾向于 DevOps 的解决方案，从持续集成，持续交付，持续部署，为了快速、自动化、可重复的方式的去处理工程，与我们今天要解决的上层业务编排场景是完全不同的两个领域。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>第二种场景：</strong>本质上是一种代码实现的设计模式，像 Netty 中，采用的是「责任链」的设计模式去实现，网络字节流经过「工厂流水线」后，进行包装，最后得到一个成品，与我们今天要解决的业务同样不是一个领域。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>第三种场景：</strong>是业务开发过程中经常遇到的问题，尤其是有复杂流程的场景中，这里包含了对流程的编排、服务的编排，每个代码块和服务都可能作为一种处理的「节点」，在整个流水线中进行串排完成业务的实现。与我们的信鸽有什么不同呢？</p><div img-uploading-status=""  image-package-9"="" data-index="9" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="8" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202606/1834368-20260607220941960-470757752.png" src="https://img2024.cnblogs.com/blog/1834368/202606/1834368-20260607220941960-470757752.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;">在我看来，两种技术方案是可以同时存在的，我们对已经稳定的领域场景，做一些灵活自定义的一些流程编排，这些流程可以作为「流水线」的思路去实现，在「流水线」末尾的一个流程节点上可以定位为「信鸽」的节点，这个节点可以再继续自由组合定制化的活动场景。</p></div></div></div><h1>9、信鸽服务的消息转发能力</h1><h2>9.1 面临的问题</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">直播的活动与营销活动的不同，大部分触发的场景偏向于平台的下游，因此需要监听许多 topic，去实现自己的业务编码，而活动在非成熟的形式背景下，必然要面临大量的短期代码，短期代码的生命周期往往只局限于活动周期内，这类代码代表了探索，代表了拓荒。直播活动在领域建模上是具备双面性的，一方面要在历史的经验去做经验复制和沉淀，另一方面要具备快速展开短期代码的能力。而随着服务数量的增多，就面临着许多服务要监听 topic，而这些 topic 对于 A 服务可能是监听过的，对于 B 服务可能也是监听过的，面临着这类问题，我们需要把 topic 接入的代码做一次代码的 Copy 或者做一些包来解决，但是这并不是一个友好的解决方案。同时，这也面临着另外一个问题，另外一个问题就是当一个代码块完成他的使命后，再也没有开启的那天，他接入的 topic 会一直进行空转的消费，我们不可能经常的对 「Nydus」（云音乐版 RocketMQ ）管控平台去做一些消费下线，时间久了，消息治理就变得比较棘手起来，就如同我们去分层解决服务循环依赖，没成想有一天异步链路中也出现了一团乱麻。下面这个图代表了 「dev」 过程中遇到的问题：</p><div img-uploading-status=""  image-package-10"="" data-index="10" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="9" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202606/1834368-20260607220947393-1150952851.png" src="https://img2024.cnblogs.com/blog/1834368/202606/1834368-20260607220947393-1150952851.png"  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><h2>9.2 解决思路</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">我们希望信鸽可以继续发挥他的优势，它不仅仅是一个只做业务末端履约的一个总线服务，他更应该帮助我们活动域内的服务做好消息治理这方面的工作，首先需要有一个研发思路的转变，就是开发同学需要为自己 「dev」 的模块去提供他的 topic，这个 topic 与自己 「dev」 的模块是一个技术闭环的，如果每一个 「dev」 的模块都具备这样的能力，信鸽只需要发挥他的优势，对本来需要监听的 topic 变成由信鸽转发给模块的 topic。</p><div img-uploading-status=""  image-package-11"="" data-index="11" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="10" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202606/1834368-20260607220952870-1482314690.png" src="https://img2024.cnblogs.com/blog/1834368/202606/1834368-20260607220952870-1482314690.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">按照这个解决思路的前提下，我们希望信鸽需要具备的就是消息转发的能力，而这个转发的场景可以抽象为三种。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>具体是：</strong></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><em>1）</em>业务自定义的分发场景：这个场景是一定要做一些特定业务处理的，通过原始消息，做一些业务清洗工作，再去转发到一些业务场景中，属于定制化的场景。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><em>2）</em>注册上报自动分发的场景：这个场景只要保证信鸽服务的 receiver 监听的 topic 足够，那么信鸽就可以自动分发到各个业务模块的 topic 上，其中分发的消息结构是具备异样的，可以通过 tag 的不同让 consumer 自住选择性拉取，根据 tag 的区分，同时也能解决某类 topic 消息过多，导致饿死的场景，犹如上图提到的一样。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><em>3）</em>消息存储的场景：并不是所有的消息都需要存储，在活动中我们认为较为重要的消息是需要做备份，方便后期去做回放，例如礼物消息，是任何场景使用率较高的，为了提升写入的能力，可以采用批量消费的消费方式，做batch的写入，在写入这里初步选型是用TiDB，TiDB相对DDB（网易分布式数据库）比较适合去存这类消息，同时，这类消息大部分时候是不需要做读操作。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>按照提到的三个场景，整体的架构如下：</strong></p><div img-uploading-status=""  image-package-12"="" data-index="12" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="11" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202606/1834368-20260607220959994-40099209.png" src="https://img2024.cnblogs.com/blog/1834368/202606/1834368-20260607220959994-40099209.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;">我们的 receiver 可以根据配置过的 topic 动态的在 spring 容器里创建 consumer 的 bean，兼容了新接入 topic，需要修改信鸽服务代码的问题，同时在收到了 topic 消息的那一刻，消息转发如图中所描述一样，分成了三条路。</p></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>这三条路代表了：</strong>自动转发、自定义转发、消息存储，这里选用三个不同的 consumer，保证消费线程池的足够宽，和转发 topic 队列足够可消费，自动转发收到消息后会根据源 topic 的类型再一次做 Tag 区分的转发到配置的 topic 关系上，同时这个转发关系是可以让研发自助管理的，也可以配置他的存活周期，非常适用于活动短期场景，在活动结束后，减少了服务的空转消费的情况。当然毕竟也要考虑到另外一个问题，多了一跳的操作会导致到转发的失败，对于这种场景，我们会对失败的消息做 exception 的存储，进行重试处理。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>同时：</strong>我们对信鸽的转发能力做了压力测试，队列的长度设置的足够宽，不考虑写表链路的场景下，单纯的转发能力，消耗 io 的点主要还是集中在 broker 通信的场景上，如果考虑消息都采用异步落盘的情况下，系统的吞吐量会更优，选用了 「8U16G」 的服务器配置，在 32 台 docker 云容器的支撑下，receiver 是可以承担到 300w/min 的消息量，并且 cpu 还能保持到 45% 左右。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">虽然信鸽的转发能力解决了我们的问题，但并不代表这是个最优解，我希望的最优解是可以让信鸽搭载 FaaS 平台，毕竟 FaaS 可以提供很多关于消息清洗的场景，而且 FaaS 在机器资源调度上会有更好表现。FaaS + BaaS 这样的组合，是未来系统技术转型的趋势。</p><h1>10、本文小结</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">花费时间做一个系统到底带来什么样的好处，又遇到什么难点呢？</p><h2>10.1 开发维护性</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">这里用一个 case 来描述研发在使用信鸽服务做业务开发后会带来怎样的好处，大勇作为一名业务研发同学，今天他需要开发一个活动，活动涉及到榜单晋级、直播间杀怪、直播间飘屏、主播任务。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>这个活动的流程思路是：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>主播完成了 「xx」 任务后在直播间掉落一个「怪物」，并发送飘屏通知。</li><li style="list-style: inherit;"><em>2）</em>主播完成了 「xx」 任务后给榜单加上一些分数。</li><li style="list-style: inherit;"><em>3）</em>榜单跨日晋级 topN，topN 发送飘屏通知。</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">很幸运的是、以上涉及到的功能，以前都开发过了，这一次只需要完成组合，很不幸的是，大勇需要开发这类组合能力。直到他遇到了信鸽，一切迎刃而解了，每个功能都接入了信鸽，那么大勇只需要通过信鸽后台，配置好任务完成要飞向「杀怪」、「飘屏」等等信鸽，对于大勇可能只需要很短的时间提测了，发布流程也减少了，一次性的胶水组装代码也不需要，毕竟完成任务掉落怪物，这个逻辑写在任务系统好像也不是很合适。</p><h2>10.2 遇到的困难</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">在做消息管理这类问题处理的时候，实现业务覆盖的时候遇到了很多难点。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">我们要对现有系统已经接入的 topic 做一些改造，在新业务场景中是屡试不爽的，但是在旧系统中（例如任务系统），新提供了一个任务的 topic，收发路由的旧消息，虽然我们也按照 SDK 的方式做了根据 tag（源 topic）做一些不同 service 的区分，但是这里依然避免不了关于数据清洗和数据结构的协议转换问题，这类问题可能与任务系统本身的清洗思路是有问题的，而这类问题最佳的解决方案一般可以选用脚本语言去做消息清洗会更加灵活一点。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">同时在做容量评估的时候，初期的压测并不是很顺利，由于写表链路与信鸽服务存在一个服务中进行压测，如果对某些消息进行数据写入 TiDB 的话，即便是批量消费写入整体服务的吞吐量也是难以压上去的，因此对写TiDB链路的服务单独进行独立，单独进行这方面的压测，信鸽原始服务只做转发，而写存储这一块可以单独去做写能力的评估。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">去掉了写 TiDB 链路的场景，单独对服务的进行吞吐量压测，起初选用的 「4U8G」 的服务器资源，由于整体的转发性能较为吃 cpu ，当提升了规格之后，整体的吞吐量有了很明显翻倍，而介于线上消息量的评估，100w/min 的消息可能是我们现有业务的极限状况，我们分别按照不同消息量做了压测，最后输出了一个压测结果集，会根据不同的消息量区间做适当的扩容和缩减。</p><h2>10.3 未来展望</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">本文就云音乐大直播活动中台技术团队在日常研发过程中遇到关于业务场景组合、消息管理这类问题，提供了一种系统设计的思路，希望可以帮助读者在日常开发提供一些参考的意见。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">目前主要还是围绕着解决现有技术侧问题所展开的，后期会考虑对消息回放这一块作为修复数据的一个重要手段和解决方案，站在异常处理的视角上，如何帮助研发同学快速修复线上问题。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">同时，面对未来国际化的场景下，对于消息用户地区机房不够敏感的场景下，希望可以通过一些手段来帮助业务侧消息转发到相关机房，协助 「Nydus」国际化后，解决业务侧路由的不清晰之处，而在未来的国际化路由机房的基础上，如果做到模块之间消息可以准确的路由到用户所在机房也是我们需要更加深入思考的问题。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">我们希望信鸽可以作为直播相关产品在活动业务域的重要解决手段之一，帮助更多相关同学解决「复用」、「组合」的烦恼，同时希望它可以国际化，适应更多的产品场景之中。</p><h1>11、参考资料</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[0] <a href="http://www.52im.net/thread-853-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">移动端实时音视频直播技术详解（一）：开篇</a>》</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[1] <a href="http://www.52im.net/thread-1562-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">海量实时消息的视频直播系统架构演进之路(视频+PPT)</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[2] <a href="http://www.52im.net/thread-1236-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">百万在线的美拍直播弹幕系统的实时推送技术实践之路</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[3] <a href="http://www.52im.net/thread-3252-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">阿里电商IM消息平台，在群聊、直播场景下的技术实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[4] <a href="http://www.52im.net/thread-3376-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">微信直播聊天室单房间1500万在线的消息架构演进之路</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[5] <a href="http://www.52im.net/thread-3515-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">百度直播的海量用户实时消息系统架构演进实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[6] <a href="http://www.52im.net/thread-3799-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">百万人在线的直播间实时聊天消息分发技术实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[7] <a href="http://www.52im.net/thread-3835-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">直播间海量聊天消息的架构设计难点实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[8] <a href="http://www.52im.net/thread-3994-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">vivo直播系统中IM消息模块的架构实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[9] <a href="http://www.52im.net/thread-3687-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">万人群聊消息投递方案的思考和实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[10] <a href="http://www.52im.net/thread-1562-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">海量实时消息的视频直播系统架构演进之路(视频+PPT)[附件下载]</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[11] <a href="http://www.52im.net/thread-4905-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">社交场景下的统一即时通讯im消息流交互层模块化技术实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[12] <a href="http://www.52im.net/thread-4012-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">阿里IM技术分享(八)：深度解密钉钉即时消息服务DTIM的技术设计</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[13] <a href="http://www.52im.net/thread-4886-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">B站IM消息系统的新架构升级实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[14] <a href="http://www.52im.net/thread-3875-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">视频直播技术干货(九)：千万级直播系统后端架构设计的方方面面</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[15] <a href="http://www.52im.net/thread-3922-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">视频直播技术干货(十)：一文读懂主流视频直播系统的推拉流架构、传输协议等</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[16] <a href="http://www.52im.net/thread-4785-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">视频直播技术干货(十三)：B站实时视频直播技术实践和音视频知识入门</a></p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;"><strong>即时通讯技术学习：</strong></p><p style="margin: 10px auto;">- 移动端IM开发入门文章：《<a href="http://www.52im.net/thread-464-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">新手入门一篇就够：从零开发移动端IM</a>》</p><p style="margin: 10px auto;">- 开源IM框架源码：<a href="https://github.com/JackJiang2011/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">https://github.com/JackJiang2011/MobileIMSDK</a>（<a href="https://gitee.com/jackjiang/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">备用地址点此</a>）</p></blockquote><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">（本文同步发布于：<a href="http://www.52im.net/thread-4914-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">http://www.52im.net/thread-4914-1-1.html</a>）</p><img src ="http://www.blogjava.net/jb2011/aggbug/451786.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jb2011/" target="_blank">Jack Jiang</a> 2026-06-08 11:52 <a href="http://www.blogjava.net/jb2011/archive/2026/06/08/451786.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>轻量级Web端IM即时通讯框架MobileIMSDK-Web，基于Socket.io库</title><link>http://www.blogjava.net/jb2011/archive/2026/06/01/451784.html</link><dc:creator>Jack Jiang</dc:creator><author>Jack Jiang</author><pubDate>Mon, 01 Jun 2026 02:51:00 GMT</pubDate><guid>http://www.blogjava.net/jb2011/archive/2026/06/01/451784.html</guid><wfw:comment>http://www.blogjava.net/jb2011/comments/451784.html</wfw:comment><comments>http://www.blogjava.net/jb2011/archive/2026/06/01/451784.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jb2011/comments/commentRss/451784.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jb2011/services/trackbacks/451784.html</trackback:ping><description><![CDATA[<h1>一、基本介绍</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><img src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260531223405684-1587595240.jpg" alt="1" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">MobileIMSDK-Web是一套纯JS编写的Web端IM即时通讯框架(含服务端)：</p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>超轻量级、极少依赖；</li><li style="list-style: inherit;"><em>2）</em>纯JS编写、高度提炼，简单易用；</li><li style="list-style: inherit;"><em>3）</em>基于著名的socket.io网络库实现，浏览器兼容性好、服务端并发性能好；</li><li style="list-style: inherit;"><em>4）</em>支持运行于iOS、Android等移动端浏览器和各种PC端浏览器；</li><li style="list-style: inherit;"><em>5）</em>能与<a href="https://gitee.com/jackjiang/MobileIMSDK" target="_blank" style="color: #1d58d1; text-decoration: none;">MobileIMSDK的APP版</a>（原生移动端代码编写）完美互通；</li><li style="list-style: inherit;"><em>6）</em>可应用于手机端/PC端的网页聊天应用、企业OA、Web端消息推送等场景。</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">&#9758; 补充说明：MobileIMSDK-Web是 <a href="https://gitee.com/jackjiang/MobileIMSDK" target="_blank" style="color: #1d58d1; text-decoration: none;">MobileIMSDK</a> 的姊妹工程，MobileIMSDK-Web专注于Web端网页聊天（或推送），而MobileIMSDK用于原生代码编写的移动端IM（或推送）应用，但二者可完美互通&#8212;&#8212;从而实现原生代码编写的移动端与基于html的网页聊天完美互通。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">&#9758; 关于为何使用的是Socket.io而不是Netty作为MobileIMSDK-Web的网络层，详见：<span style="color: gray;">《</span><a href="http://www.52im.net/thread-1248-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">MobileIMSDK-Web的网络层框架为何使用的是Socket.io而不是Netty？</a><span style="color: gray;">》。</span></p><h1>二、与MobileIMSDK的区别</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><img src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260426210610330-1388464167.jpg" alt="2" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>&#9758; 关于<a href="https://gitee.com/jackjiang/MobileIMSDK" target="_blank" style="color: #1d58d1; text-decoration: none;">MobileIMSDK</a>：</strong></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">MobileIMSDK主要使用原生代码编写，应用于非Web网页方式的移动端即时通讯场景下（<span style="color: #808080;">当然最新的MobileIMSDK框架也支持基于HTML5的WebSocket客户端</span>）。</p><strong style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">⭐️ 同步开源地址：</strong><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;">❶ <strong>GitHub：</strong><a href="https://github.com/JackJiang2011/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">https://github.com/JackJiang2011/MobileIMSDK</a></li><li style="list-style: inherit;">❷ <strong>码云gitee：</strong><a href="https://gitee.com/jackjiang/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">https://gitee.com/jackjiang/MobileIMSDK</a></li><li style="list-style: inherit;">❸ <strong>Gitcode：</strong><a href="https://gitcode.com/hellojackjiang2011/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">https://gitcode.com/hellojackjiang2011/MobileIMSDK</a></li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>&#9758; 关于MobileIMSDK-Web：</strong><br />MobileIMSDK-Web完全使用JavaScript编写，主要应用于不支持HTML5的需要兼容旧式浏览器的Web网页方式的即时通讯场景下（包括但不限于手机端、PC端的网页聊天（或消息推送）等）。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>&#9758; MobileIMSDK与MobileIMSDK-Web的互通：</strong></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">基于MobileIMSDK-Web开发的开发的网页聊天等和基于MobileIMSDK开发的移动端IM等<strong>可以无缝地进行消息互通，两个框架之间的通信协议完全兼容</strong>，从而实现您的网页聊天（或推送）与手机端原生代码开发的IM（或推送）进行完美协作，实现多端通信。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>&#9758; 我该如何选择？</strong></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>选择一：</strong>如果您的应用是用原生代码编写，如移动端是原生代码编写或者不需要兼容旧式浏览器，那么您可以将 <a href="https://gitee.com/jackjiang/MobileIMSDK" target="_blank" style="color: #1d58d1; text-decoration: none;">MobileIMSDK</a> 引入到您的项目中从而实现IM（或推送）应用；</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>选择二：</strong>如果您的应用是基于Web网页且需要兼容旧式浏览器，那么您的最佳选择就是使用MobileIMSDK-Web来开发您的网页端聊天（或消息推送）。</p><h1>三、设计目标&nbsp;</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">原生的WebSocket代码或者原始的socket.io代码，使得网络通信代码与大量前端UI界面代码混在一起，使得UI界面的重构、维护、改版都非常困难。而MobileIMSDK-Web工程将让开发者专注于UI应用层的开发，网络通信层专业的代码交由SDK开发人员，从而解偶Web端IM的UI前端和通信层的耦合性，同时大大降低复杂性。<br /><br /><strong>总结一下，MobileIMSDK-Web的设计目标是为您的Web端IM带来以下便利：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>前端UI代码与网络通信代码解耦：UI界面的重构、维护、改版都非常容易和优雅；</li><li style="list-style: inherit;"><em>2）</em>服务端网络通信代码与业务代码解耦：使得服务端的业务逻辑实现起来清晰简单；</li><li style="list-style: inherit;"><em>3）</em>浏览器端的高兼容性：受益于socket.io框架，MobileIMSDK-Web在不支持WebSocket的旧式浏览器上仍可很好地工作；</li><li style="list-style: inherit;"><em>4）</em>服务端的高并发、高性能：得益于Nodejs的异步编程模型和高并发特性，基于MobileIMSDK-Web编写的IM服务端拥有极高的并发处理性能。</li></ul><h1>四、框架组成&nbsp;</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">整套MobileIMSDK-Web框架由以下2部分组成：</p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>浏览器端SDK：用于开发浏览器端页面，纯JS编写，极少依赖，方便对接基于原生JS、Angular、EmberJS、VUE等各种前端框架；</li><li style="list-style: inherit;"><em>2）</em>服务器端SDK：用于开发Web端IM的服务端，支持高性能和高并发。</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><img src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260531224502945-759635755.jpg" alt="目录+精编源码" width="1421" height="1220" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></p><h1>五、技术亮点</h1><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;">✅️ 轻量易使用：超轻量级&#8212;&#8212;纯JS编写且极少依赖，高度提炼&#8212;&#8212;简单易用；</li><li style="list-style: inherit;">✅️ 兼容性好：基于socket.io网络框架，浏览器兼容性好，在不支持WebSocket的旧式浏览器上仍可很好地工作；</li><li style="list-style: inherit;">✅️ QoS机制：完善的消息送达保证机制（<span style="color: sienna;">真正ACK应答机制</span>），确保不漏过每一条消息；</li><li style="list-style: inherit;">✅️ 断网恢复能力：拥有网络状况自动检测、断网自动治愈的能力；</li><li style="list-style: inherit;">✅️ 支持多种设备：支持运行于iOS、Android等移动端浏览器和各种PC端浏览器；</li><li style="list-style: inherit;">✅️ 封装的通信协议：实现了一个对上层透明的即时通讯通信协议模型；</li><li style="list-style: inherit;">✅️ 身份认证机制：实现了简单合理的身份认证机制（socket.io官方并未实现之，资料也几乎没有）；</li><li style="list-style: inherit;">✅️ 全消息路径：实现了client to server、server to client、client to client 共3种消息路径（socket.io官方只演示了广播消息，一对一发送无资料）；</li><li style="list-style: inherit;">✅️ 服务端慢io解偶：开发者可通过使用MQ进行DB等慢io的读、写解偶，保证IM实时消息高吞吐和性能；</li><li style="list-style: inherit;">✅️ 服务端代码解偶：实现了上层应用代码与sdk核心代码的解偶，上线、下线、c2s消息、c2c消息、身份认证等的回调通知；</li><li style="list-style: inherit;">✅️ 实现了在线列表：服务端实现了一个高性能的在线用户列表机制；</li><li style="list-style: inherit;">✅️ 完善的log记录：服务端接入了log4js日志框架，确保MobileIMSDK-Web中的每一个关键步骤都有日志输出，让您的运行调试更为便利；</li><li style="list-style: inherit;">✅️ 浏览器端代码解耦：实现了UI前端代码与sdk网络通信代码解偶，防止前端代码跟IM核心代码混在一起，不利于持续升级、重用和维护；</li><li style="list-style: inherit;">✅️ 轻松开启数据加密：一个参数即可开启SSL/TLS通信加密；</li><li style="list-style: inherit;">✅️ 聊天协议兼容：实现了与<a href="https://gitee.com/jackjiang/MobileIMSDK" target="_blank" style="color: #1d58d1; text-decoration: none;">MobileIMSDK</a> 完全兼容的协议模型。</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>MobileIMSDK-Web的浏览器兼容性：</strong></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><img src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260531224628339-940085843.png" alt="x1" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></strong></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><span style="color: gray;">（</span>&#9758; <span style="color: gray;">MobileIMSDK-Web的兼容性由socket.io网络框架决定：</span><a href="https://socket.io/docs/v4/client-installation/#browser-support" target="_blank" style="color: #1d58d1; text-decoration: none;">点此查看兼容性说明</a><span style="color: gray;">）</span></p><h1>六、性能负载</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">得益于socket.io网络框架的高性能和Nodejs的异步编程模型，MobileIMSDK-Web可支持单机数万甚至上十万并发连接。当然，每种应用场景都有各自的特点和差异，请视具体场景具体评估之，性能数据仅供参考。<span style="color: gray;">（关于为何使用的是Socket.io而不是Netty作为MobileIMSDK-Web的网络层，详见《</span><a href="http://www.52im.net/thread-1248-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">MobileIMSDK-Web的网络层框架为何使用的是Socket.io而不是Netty？</a><span style="color: gray;">》）</span><br /><br /><strong>&#9758; socket.io性能测试讨论：</strong><a href="https://cnodejs.org/topic/5492ba8b61491ead0cc7c018" target="_blank" style="color: #1d58d1; text-decoration: none;">socket.io 高并发实战</a>、<a href="https://www.zhihu.com/question/31950731" target="_blank" style="color: #1d58d1; text-decoration: none;">socket.io保持6万+连接测试？</a>、<a href="https://www.zhihu.com/question/20831000" target="_blank" style="color: #1d58d1; text-decoration: none;">如何实现单服务器300万个长连接的？</a>。</p><h1>七、开发者手册</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><img src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260531225035199-1551909552.jpg" alt="缩略清单-无阴影" width="897" height="890" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></p><h1>八、Demo运行截图</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><img src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260531224933909-2000002215.jpg" alt="运行效果all_in_one拼合图-opti" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></p><h1>九、产品案例</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">以下为基于MobileIMSDK-Web的Web端IM产品案例 <a href="http://www.52im.net/thread-2483-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">RainbowChat-Web</a> 的产品情况。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><img src="https://img2024.cnblogs.com/blog/1834368/202511/1834368-20251101000357185-1361468844.png" alt="rbpw_tb_main1" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>下图为RainbowChat-Web的主界面</strong>（<a href="http://www.52im.net/thread-2470-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">更多截图</a>、<a href="http://www.52im.net/thread-2491-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">更多演示视频</a>）：</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><img src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260531225851689-770087619.jpg" alt="main1" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>下图为RainbowChat-Web的主界面[聊天窗全屏时]</strong> （<a href="http://www.52im.net/thread-2470-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">更多截图</a>、<a href="http://www.52im.net/thread-2491-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">更多演示视频</a>）：</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><em><img src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260531225901053-419797023.jpg" alt="main2" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></em></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">（<strong>本文内容引用自：</strong><a href="http://www.52im.net/thread-959-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">http://www.52im.net/thread-959-1-1.html</a>）</p><img src ="http://www.blogjava.net/jb2011/aggbug/451784.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jb2011/" target="_blank">Jack Jiang</a> 2026-06-01 10:51 <a href="http://www.blogjava.net/jb2011/archive/2026/06/01/451784.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>详解AI大模型实时通信为什么选SSE，而不是WebSocket和WebRTC</title><link>http://www.blogjava.net/jb2011/archive/2026/05/28/451783.html</link><dc:creator>Jack Jiang</dc:creator><author>Jack Jiang</author><pubDate>Thu, 28 May 2026 03:57:00 GMT</pubDate><guid>http://www.blogjava.net/jb2011/archive/2026/05/28/451783.html</guid><wfw:comment>http://www.blogjava.net/jb2011/comments/451783.html</wfw:comment><comments>http://www.blogjava.net/jb2011/archive/2026/05/28/451783.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jb2011/comments/commentRss/451783.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jb2011/services/trackbacks/451783.html</trackback:ping><description><![CDATA[<p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">本文由ArchSynapse AI分享，有修订和重新排版。</p><h1>1、引言</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">在为大型语言模型（LLM）应用构建实时前后端通信系统时，选择正确的底层技术至关重要。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>本章节将深入剖析三种主流技术的核心原理：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>Server-Sent Events (SSE)：它作为服务器主导的单向数据流的黄金标准；</li><li style="list-style: inherit;"><em>2）</em>WebSocket：作为通用的全双工通信解决方案；</li><li style="list-style: inherit;"><em>3）</em>WebRTC：专注于媒体流和点对点数据交换。</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">理解它们的技术设计哲学和技术细节，是做出明智技术选型的关键基础。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>本文将为读者剖析 SSE、WebSocket、WebRTC 的技术原理，并对比三者在性能、安全与架构方面的优劣势，详解了AI大模型（LLM）在实时通信协议方面的综合技术考量以及最终选择。</strong></p><div img-uploading-status=""  image-package-1"="" data-index="1" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260526180712892-706354511.jpg" alt="cover-opti2" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><h1>2、AI大模型实时通信技术专题</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>技术专题系列文章目录如下，本文是第&nbsp;<em>5</em>&nbsp;篇：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-4797-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">全民AI时代，大模型客户端和服务端的实时通信到底用什么协议？</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-4831-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">大模型时代多模型AI网关的架构设计与实现</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-4856-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">通俗易懂：AI大模型基于SSE的实时流式响应技术原理和实践示例</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-4872-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">ChatGPT如何实现聊天一样的实时交互？快速读懂SSE实时&#8220;推&#8221;技术</a>&nbsp;》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-4885-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">AI大模型爆火的SSE技术到底是什么？万字长文，一篇读懂SSE！</a>&nbsp;》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-4909-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">详解AI大模型实时通信为什么选SSE，而不是WebSocket和WebRTC</a>》（<span style="color: red;">&#9756; 本文</span>）</li></ul><h1>3、SSE是什么？</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">Server-Sent Events (SSE) 是一种基于标准 HTTP 协议的技术，专为服务器向客户端推送单向事件流而设计。</p><div img-uploading-status=""  image-package-2"="" data-index="2" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260526180723207-320310494.png" alt="1" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;">其工作流程始于客户端创建一个&nbsp;EventSource&nbsp;对象，该对象会自动发起一个持久的 HTTP GET 请求。服务器接收到请求后，不立即关闭连接，而是保持响应打开，并以特定格式发送数据。</p></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">这个格式要求服务器设置 Content-Type 头部为&nbsp;<em>text/event-stream</em>。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>每个事件由一系列字段组成，包括：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>必需的&nbsp;<em>data:&nbsp;</em>字段用于承载消息内容；</li><li style="list-style: inherit;"><em>2）</em>可选的&nbsp;<em>event:&nbsp;</em>字段用于定义事件类型；</li><li style="list-style: inherit;"><em>3）</em>id: 字段用于标识事件以便在连接中断后恢复；</li><li style="list-style: inherit;"><em>4）</em>以及&nbsp;<em>retry:&nbsp;</em>字段用于指定重新连接前的等待时间。</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">消息之间必须以两个换行符&nbsp;<em>\n\n</em>&nbsp;分隔。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">这种机制使得服务器能够在一个长连接上持续不断地推送增量数据，非常适合 LLM 流式传输等场景。SSE 的一大优势在于其天然地融入了现有的 HTTP 生态，能够无缝通过反向代理、负载均衡器和防火墙，无需特殊配置。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>&#9758; 进一步学习：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><a href="http://www.52im.net/thread-336-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">Web端即时通讯技术盘点：短轮询、Comet、Websocket、SSE</a></li><li style="list-style: inherit;"><a href="http://www.52im.net/thread-335-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">SSE技术详解：一种全新的HTML5服务器推送事件技术</a></li><li style="list-style: inherit;"><a href="http://www.52im.net/thread-907-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">使用WebSocket和SSE技术实现Web端消息推送</a></li><li style="list-style: inherit;"><a href="http://www.52im.net/thread-1038-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">详解Web端通信方式的演进：从Ajax、JSONP 到 SSE、Websocket</a></li><li style="list-style: inherit;"><a href="http://www.52im.net/thread-907-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">使用WebSocket和SSE技术实现Web端消息推送</a></li><li style="list-style: inherit;"><a href="http://www.52im.net/thread-3555-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">网页端IM通信技术快速入门：短轮询、长轮询、SSE、WebSocket</a></li><li style="list-style: inherit;"><a href="http://www.52im.net/thread-3695-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">搞懂现代Web端即时通讯技术一文就够：WebSocket、socket.io、SSE</a></li></ul><h1>4、WebSocket是什么？</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">WebSocket&nbsp;则提供了一种更为强大和灵活的全双工通信协议。它通过一次性的 HTTP 握手来&#8220;升级&#8221;一个标准的 TCP 连接。</p><div img-uploading-status=""  image-package-3"="" data-index="3" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260526180730137-813513241.png" alt="2" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;">握手过程涉及客户端发送一个包含&nbsp;<em>Upgrade: websocket</em>&nbsp;和<em>&nbsp;Connection: Upgrade</em>&nbsp;等特定头部的 HTTP 请求。如果服务器支持 WebSocket，它会返回一个状态码为 101 (Switching Protocols) 的响应，从而完成协议切换。</p></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">一旦连接建立，客户端和服务器就可以随时独立地向对方发送消息，而无需像 HTTP 那样每次都发起新的请求。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">WebSocket 消息被封装在轻量级的帧中进行传输，支持文本和二进制数据，极大地降低了网络开销。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">这种持久且双向的特性使其成为聊天应用、在线游戏和实时协作工具的理想选择。然而，其复杂性也远超 SSE，因为它是一个独立的协议栈，需要专门的服务器库和客户端处理逻辑。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>&#9758; 进一步学习：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><a href="http://www.52im.net/thread-831-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">新手快速入门：WebSocket简明教程</a></li><li style="list-style: inherit;"><a href="http://www.52im.net/thread-3134-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">WebSocket从入门到精通，半小时就够！</a></li><li style="list-style: inherit;"><a href="http://www.52im.net/thread-331-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">初步认识WebSocket技术</a></li><li style="list-style: inherit;"><a href="http://www.52im.net/thread-1258-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">刨根问底HTTP与WebSocket的关系(上篇)</a></li><li style="list-style: inherit;"><a href="http://www.52im.net/thread-1273-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">刨根问底WebSocket与Socket的关系</a></li></ul><h1>5、WebRTC是什么？</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">WebRTC (Web Real-Time Communication) 是一个更为复杂的生态系统，旨在实现浏览器间的直接音视频通话和任意数据交换。</p><div img-uploading-status=""  image-package-4"="" data-index="4" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260526180736611-509078980.png" alt="3" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;">它的核心思想是尽可能地建立点对点（Peer-to-Peer, P2P）连接，从而绕过中间服务器传输大量媒体数据，以达到最低的延迟。</p></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>WebRTC 的整个通信过程分为三个阶段：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>首先是通过信令服务器交换初始信息；</li><li style="list-style: inherit;"><em>2）</em>其次是通过 ICE (Interactive Connectivity Establishment) 框架进行 NAT 和防火墙穿越；</li><li style="list-style: inherit;"><em>3）</em>最后才是建立 P2P 数据通道。</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">ICE&nbsp;框架依赖于 STUN (Session Traversal Utilities for NAT) 服务器来帮助设备发现自己的公网 IP 地址，如果直接连接失败，则会降级到 TURN (Traversal Using Relays around NAT) 服务器进行中继。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">TURN&nbsp;服务器虽然能保证连接成功，但会消耗大量带宽，因为所有流量都需要经过它转发。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">WebRTC 的另一个关键组件是&nbsp;RTCDataChannel API，它允许在两个浏览器之间建立一个类似 WebSocket 的 P2P 数据通道，用于传输非媒体数据。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">由于其架构的复杂性，WebRTC 通常需要一个额外的信令层（常由 WebSocket 或 SSE 实现）来协调连接的建立。</p><div img-uploading-status=""  image-package-5"="" data-index="5" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260526180745318-1967992419.png" alt="4" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;">&nbsp;</p></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>&#9758; 进一步学习：</strong></p><ol style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><a href="http://www.52im.net/thread-227-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">访谈WebRTC标准之父：WebRTC的过去、现在和未来</a></li><li style="list-style: inherit;"><a href="http://www.52im.net/thread-284-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">WebRTC实时音视频技术的整体架构介绍</a></li><li style="list-style: inherit;"><a href="http://www.52im.net/thread-356-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">新手入门：到底什么是WebRTC服务器，以及它是如何联接通话的？</a></li><li style="list-style: inherit;"><a href="http://www.52im.net/thread-442-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">WebRTC实时音视频技术基础：基本架构和协议栈</a></li><li style="list-style: inherit;"><a href="http://www.52im.net/thread-3804-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">实时音视频入门学习：开源工程WebRTC的技术原理和使用浅析</a></li><li style="list-style: inherit;"><a href="http://www.52im.net/thread-4184-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">零基础快速入门WebRTC：基本概念、关键技术、与WebSocket的区别等</a></li></ol><h1>6、兼容性、性能与开发复杂度对比</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">在为 LLM 应用选择实时通信技术时，必须在兼容性、性能和开发复杂度这三个关键维度上进行权衡。本章节将对 Server-Sent Events (SSE)、WebSocket 和 WebRTC 进行全面的横向对比，以揭示它们在不同方面的优劣，从而为技术选型提供数据驱动的依据。</p><h2>6.1 兼容性与平台支持</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">首先，在兼容性与平台支持方面，SSE 和 WebSocket 拥有非常广泛的浏览器支持，几乎覆盖了所有现代浏览器，包括 Chrome、Firefox、Safari 和 Edge。Opera Mini 是少数不支持 WebSocket 和 SSE 的例外。IE 浏览器则完全不支持这两种技术。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">相比之下，WebRTC 的支持范围稍窄一些，IE 不支持，旧版的 Edge 也不支持，但同样广泛应用于其他现代浏览器及其移动端版本。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">对于 LLM 应用而言，这意味着 SSE 和 WebSocket 能够触及更广泛的用户群体。然而，值得注意的是，随着 HTTPS 成为网页标配，HTTP/2 得到了普及，这彻底解决了过去 SSE 在 HTTP/1.1 下因浏览器并发连接数限制（通常是6个）而导致的性能瓶颈，使得 SSE 在现代 Web 架构中表现得更加稳健和高效。</p><h2>6.2 延迟性能</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">其次，延迟性能是衡量实时通信技术优劣的核心指标。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">理论上，WebSocket 具有最低的延迟，因为它在握手完成后就建立了一个纯粹的、无头开销的数据通道。WebRTC 在 P2P 模式下可以实现极低的延迟，因为它避免了服务器中转。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">然而，WebRTC 的延迟受到 NAT/防火墙穿越过程的影响，可能需要 fallback 到较慢的 TURN 中继服务器。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">SSE 的延迟取决于底层的 HTTP 版本。在 HTTP/1.1 下，由于队头阻塞（Head-of-Line Blocking），多个 SSE 连接可能会相互阻塞。但在 HTTP/2 下，得益于其强大的多路复用能力，SSE 的性能得到了极大提升，可以与常规 HTTP 请求并行，延迟接近 WebSocket。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">一项针对真实世界数据流的测试表明，SSE 和 WebSocket 在 EPS（每秒事件数）方面表现出相当的性能，WebSocket 在高并发下略占优势，但差异在实践中往往可以忽略不计。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">因此，对于大多数 LLM 文本流式传输场景，SSE 在 HTTP/2 下已经提供了足够低的延迟。</p><h2>6.3 开发复杂度</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">最后，开发复杂度是决定项目成本和上线速度的关键因素。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">在这三者中，SSE 的开发复杂度最低。它基于标准的 HTTP，开发者可以利用现有的 Web 服务器和工具链，客户端代码也极为简洁，只需几行 JavaScript 即可启动 EventSource 并监听事件。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">WebSocket 的复杂度显著更高。除了协议本身，开发者还需要处理连接管理、状态同步、手动实现心跳包和自动重连逻辑、应对粘性会话（Sticky Sessions）带来的负载均衡挑战，以及在分布式系统中使用 Redis Pub/Sub 等消息队列来广播消息。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">WebRTC 的开发复杂度最高，它不仅包含了上述 WebSocket 的所有挑战，还引入了信令、ICE 候选者收集、STUN/TURN 服务器的配置和维护等一系列全新的难题。此外，WebRTC 的安全模型也更为复杂，需要同时保护信令通道和媒体通道。</p><div img-uploading-status=""  image-package-6"="" data-index="6" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260526180753070-318268009.png" alt="5" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;"><strong>综上所述：</strong>SSE 在兼容性、性能和开发复杂度之间取得了最佳平衡，尤其是在 HTTP/2 得到普及的今天。WebSocket 提供了更高的灵活性和更低的理论延迟，但代价是巨大的开发和运维复杂性。WebRTC 则因其固有的复杂性，仅适用于特定的 P2P 和媒体流场景。</p></div></div></div><h1>7、安全性考量与风险缓解策略</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">在 LLM 应用中采用实时通信技术时，安全性是不可忽视的核心要素。本章节将深入探讨 Server-Sent Events (SSE)、WebSocket 和 WebRTC 在安全性方面的关键考量、潜在风险及相应的缓解策略。</p><h2>7.1 Server-Sent Events</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">对于 Server-Sent Events (SSE)，其安全性很大程度上继承自标准的 HTTP 安全模型。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">首要的安全措施是强制使用 HTTPS，因为 SSE 的长期连接窗口使其更容易受到中间人攻击（Man-in-the-Middle），窃听或篡改流中的数据。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">身份验证是另一个关键环节。由于 EventSource API 不支持自定义 HTTP 头部，传统的 JWT 令牌传递变得困难。常见的解决方案是通过 URL 查询参数传递短时效的访问令牌，但这存在安全隐患，因为令牌可能被记录在服务器日志或浏览器历史中。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">另一种更安全的方式是利用带有 SameSite=Strict 属性的 Cookie 来进行认证，这种方式能自动随请求发送，但需要注意 CSRF（跨站请求伪造）攻击的风险，可以通过检查 Origin 头部来缓解。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">此外，SSE 端点容易受到 DoS（拒绝服务）攻击，攻击者可以通过建立大量连接耗尽服务器资源。对此，应实施严格的速率限制，限制来自同一 IP 或用户的并发连接数。</p><h2>7.2 WebSocket</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">WebSocket 的安全性挑战更为复杂。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">虽然 WSS (WebSocket Secure) 使用 TLS 加密来保护数据传输，但其状态化的性质带来了新的攻击面。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">首先，WebSocket 缺乏标准化的身份验证机制，开发者必须自行实现，例如在握手阶段通过 Cookie 或查询参数传递凭证。如果未正确验证 Origin 头部，应用将面临 Cross-Site WebSocket Hijacking (CSWSH) 攻击，恶意网站可以在未经授权的情况下建立 WebSocket 连接并访问敏感数据。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">其次，WebSocket 容易受到 DoS 攻击，特别是通过 Flood 攻击（发送大量小消息或建立海量连接）来耗尽服务器 CPU 或内存。有效的防御措施包括要求连接前进行认证、限制消息大小和频率、以及实施精细的速率控制 [89]。输入数据也可能导致注入攻击，如 SQL 注入或 XSS，因此所有传入的消息都必须经过严格的验证和清理。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">最后，由于 WebSocket 连接是持久的，如果令牌在连接期间过期，可能会导致会话劫持，因此需要实现 token 刷新机制。</p><h2>7.3 WebRTC</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">WebRTC 的安全性设计更为严格，其核心理念是端到端加密。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">所有媒体流都默认使用 SRTP (Secure Real-time Transport Protocol) 加密，而数据通道则使用 DTLS (Datagram Transport Layer Security) 加密，确保只有通信双方可以解密数据。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">然而，WebRTC 的安全性并非万无一失，其脆弱性主要集中在基础设施层面。最著名的安全问题是 IP 地址泄露。由于 ICE 协商需要交换候选者的 IP 地址，即使用户使用了 VPN，其真实的本地和公网 IP 仍可能暴露给通信的另一方，这严重损害了隐私。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">缓解此问题的方法是在网络拓扑中强制使用 TURN 服务器中继，从而隐藏客户端的真实 IP。信令通道的安全性至关重要，如果信令服务器未使用 WSS (WebSocket Secure)&nbsp;或 HTTPS，攻击者可以拦截信令消息，从而发起会话劫持或拒绝服务攻击。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">此外，TURN 服务器若配置不当，可能被滥用进行 DDoS 攻击或数据窃取。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">最后，尽管 WebRTC 协议本身很安全，但其浏览器实现可能存在漏洞，需要及时更新浏览器以修补已知的安全问题。</p><div img-uploading-status=""  image-package-7"="" data-index="7" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260526180759305-467863608.png" alt="6" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;">&nbsp;</p></div></div></div><h1>8、架构设计与可扩展性挑战</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">为 LLM 应用选择实时通信技术不仅关乎功能实现，更深刻地影响着系统的整体架构设计和未来的可扩展性。本章节将深入探讨使用 Server-Sent Events (SSE)、WebSocket 和 WebRTC 时面临的架构挑战，特别是在大规模生产环境下的可扩展性问题。</p><h2>8.1 Server-Sent Events</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">使用 Server-Sent Events (SSE) 的架构相对简单且更具弹性。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">由于 SSE 基于标准的 HTTP，它可以轻松地与现有的无状态微服务架构集成。服务器可以水平扩展，因为每个 SSE 连接都是独立的，负载均衡器可以将新连接分发到任何可用的服务器实例。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">然而，当需要向所有订阅用户广播消息时，简单的无状态架构会遇到挑战。解决这个问题的常见模式是引入一个中央消息代理，如 Redis Pub/Sub 或 Kafka。所有服务器实例都订阅同一个 Redis 频道，当有新消息需要广播时，只需将消息发布到该频道，Redis 会将其高效地分发给所有相关的 SSE 客户端。这种发布/订阅（Pub/Sub）模式极大地简化了广播逻辑，并提高了系统的可扩展性。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">此外，SSE 的 stateless nature 使其非常适合在云原生环境中运行，例如 Kubernetes 或 serverless 平台，因为它不需要在节点间共享会话状态。</p><h2>8.2 WebSocket</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">相比之下，WebSocket 的架构设计则充满了挑战，其根本原因在于连接的有状态性。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">每个 WebSocket 连接都会占用服务器的一个文件描述符，并持续消耗内存和 CPU 资源。当用户数量从数百增加到数万甚至数百万时，这种状态化特性会迅速成为瓶颈。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">传统的负载均衡策略（如轮询或最少连接）无法有效分配 WebSocket 连接，因为客户端断线重连后很可能需要回到最初的服务器实例以保持会话状态。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">为了解决这个问题，通常需要采用&#8220;粘性会话&#8221;（Sticky Sessions）或&#8220;会话亲缘性&#8221;（Session Affinity）的负载均衡策略，但这会导致负载分布不均，并形成单点故障。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">更高级的解决方案是构建一个全局状态管理系统，例如将所有连接状态存储在 Redis 等外部数据库中。当服务器 A 接收到需要路由到服务器 B 上某个用户的 WebSocket 消息时，它可以从 Redis 获取目标用户的连接信息并进行转发。这种方法虽然可行，但极大地增加了系统的复杂性和延迟。因此，许多团队最终会选择使用专门的实时通信平台（如 Ably 或 Pusher）来托管这部分基础设施，从而将焦点重新放回业务逻辑上。</p><h2>8.3 WebRTC</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">WebRTC 的可扩展性问题则源于其点对点（P2P）的本质。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">纯 P2P 架构在小规模群组（例如，最多 4-5 人）内工作良好，因为每个参与者只需要与其他成员建立连接即可。然而，当参与者数量增加时，连接数量会呈二次方增长（N&#178; problem），导致每个客户端的带宽和 CPU 负载急剧上升，很快就会超出个人设备的处理能力。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">为了实现 WebRTC 的规模化，必须引入中心化的媒体服务器。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>目前主要有两种架构：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>MCU（Multipoint Control Unit，多点控制单元）；</li><li style="list-style: inherit;"><em>2）</em>SFU（Selective Forwarding Unit，选择性转发单元）。</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">MCU 会接收所有参与者的媒体流，进行解码、混音/拼接，然后重新编码成一个新的流再分发给所有人，这样客户端只需接收一个流，但 MCU 承担了巨大的计算压力。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">SFU 则是一种更高效的架构，它只负责将每个参与者发送的媒体流转发给其他所有人，而不进行任何处理，从而将计算负担分散到各个客户端。SFU 是现代大规模 WebRTC 应用（如 Zoom、Google Meet）的首选架构。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">然而，无论是 MCUs 还是 SFUs，都需要大量的服务器资源和复杂的网络基础设施来支持，这对于许多团队来说是一笔巨大的投资。</p><div img-uploading-status=""  image-package-8"="" data-index="8" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260526180806009-1638170641.png" alt="7" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;"><strong>总而言之：</strong></p></div></div></div><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>SSE 在可扩展性方面提供了最大的灵活性和成本效益；</li><li style="list-style: inherit;"><em>2）</em>WebSocket 的扩展性挑战巨大，通常需要借助第三方服务或投入大量内部工程资源；</li><li style="list-style: inherit;"><em>3）</em>WebRTC 的扩展性则最为昂贵和复杂，是专门为媒体流设计的，不适合用于普通的文本数据通信。</li></ul><h1>9、最终决策与综合建议</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">在对 Server-Sent Events (SSE)、WebSocket 和 WebRTC 进行了全面的原理、性能、安全性和可扩展性分析之后，本章节将整合所有信息，为 LLM 应用的前后端实时通信提供一个清晰的决策框架，并给出具体的选型建议。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">选择何种技术，最终取决于您的 LLM 应用的具体需求、团队的技术栈和预算。我们可以将 LLM 应用的实时通信场景划分为几个典型的层级，每个层级对应不同的技术最优解。</p><h2>9.1 第一层级：流式文本展示</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">这是最常见的 LLM 应用场景，用户输入一个问题，然后看到答案逐字或逐词地动态呈现出来。这种模式的核心特征是服务器主导的单向数据流。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">在这个场景下，Server-Sent Events (SSE) 是无可争议的最佳选择。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">其理由非常充分：</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><em>1）</em>首先，SSE 的设计初衷就是为此类单向推送而优化的，它比 WebSocket 更加轻量和简单。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><em>2）</em>其次，得益于现代浏览器对 EventSource API 的原生支持，客户端实现极其简单，且内置了可靠的自动重连机制，极大地提升了用户体验和应用的健壮性。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><em>3）</em>再者，SSE 基于标准 HTTP，在部署和运维上非常友好，可以无缝融入现有的 web 服务生态，无需为实时通信单独搭建一套复杂的基础设施。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">Shopify 在其 2022 年黑色星期五活动中的 BFCM Live Map 成功案例，就是一个有力的证明，他们通过 SSE 将毫秒级的数据更新推送给数十万用户，展现了其在生产环境下的强大能力和可扩展性。</p><h2>9.2 第二层级：双向实时互动</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">当应用的需求超越了单向展示，进入真正的&#8220;对话&#8221;或&#8220;协作&#8221;阶段时，就需要选择能够支持客户端和服务器双向通信的技术。在这种情况下，WebSocket 是唯一的选择。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>典型的用例包括：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>语音 I/O：OpenAI 的 Realtime API 就是一个绝佳的例子，它利用 WebSocket 实现了低延迟的语音对话，允许用户在 AI 说话时打断，并即时获得反馈。这种场景需要双向的音频流和命令流，是 WebSocket 的典型应用。</li><li style="list-style: inherit;"><em>2）</em>多智能体系统：当多个 AI Agent 需要在后台协同工作，并需要向前端实时同步各自的思考过程、工具调用结果或状态变化时，WebSocket 提供了必要的双向通道。</li><li style="list-style: inherit;"><em>3）</em>实时协作编辑：当 LLM 作为协作者与用户共同编写代码、文档或进行头脑风暴时，WebSocket 可以确保用户的每一次修改都能被实时同步给 LLM，反之亦然。</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">选择 WebSocket 的代价是显著增加的开发和运维复杂度。您需要投入资源来构建和维护一个能够处理成千上万个并发长连接的系统，并解决由此带来的一系列可扩展性问题。</p><h2>9.3 第三层级：点对点媒体流与数据交换</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">这是 WebRTC 的专属领域。虽然 WebRTC 也能传输数据，但它的核心价值在于绕过服务器直接传输高质量的音视频流。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>在 LLM 应用中，WebRTC 的应用场景非常具体且有限：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>高级语音交互：当需要处理原始的、高保真的音频流，而不是经过编码的文本或语音片段时，WebRTC 可能是必要的底层传输协议。</li><li style="list-style: inherit;"><em>2）</em>P2P 文件共享：如果 LLM 应用涉及到用户之间直接通过浏览器共享由 LLM 生成的大文件，WebRTC 的 DataChannel 可以提供一条绕过服务器的高速通道。</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">除非您的应用明确属于上述范畴，否则不应轻易选择 WebRTC。它的高昂复杂性和成本使其成为一个非常规的选择。</p><h2>9.4 综合决策框架</h2><div img-uploading-status=""  image-package-9"="" data-index="9" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260526180812249-756717547.png" alt="8" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;">&nbsp;</p></div></div></div><h2>9.5 最终结论与建议</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">对于绝大多数 LLM 应用的前后端通信需求，我们强烈建议从 Server-Sent Events (SSE) 开始。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">它足够强大，足以满足当前主流的流式文本展示需求，同时又足够简单，能让团队快速迭代产品。不要为了所谓的&#8220;先进性&#8221;或过度设计而一开始就选择更复杂的 WebSocket。只有当业务发展到确实需要双向、低延迟的实时互动时，才应该评估并投入资源迁移到 WebSocket。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">记住，没有一种技术是完美的，最好的技术是那个能够以最低的成本和复杂度解决你当前问题的技术。</p><h1>10、参考资料</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[0]&nbsp;<a href="https://developer.mozilla.org/zh-CN/docs/Web/API/EventSource" target="_blank" style="color: #1d58d1; text-decoration: none;">EventSource API Docs</a><br />[1]&nbsp;<a href="http://www.52im.net/thread-336-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">Web端即时通讯技术盘点：短轮询、Comet、Websocket、SSE</a><br />[2]&nbsp;<a href="http://www.52im.net/thread-335-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">SSE技术详解：一种全新的HTML5服务器推送事件技术</a><br />[3]&nbsp;<a href="http://www.52im.net/thread-907-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">使用WebSocket和SSE技术实现Web端消息推送</a><br />[4]&nbsp;<a href="http://www.52im.net/thread-1038-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">详解Web端通信方式的演进：从Ajax、JSONP 到 SSE、Websocket</a><br />[5]&nbsp;<a href="http://www.52im.net/thread-907-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">使用WebSocket和SSE技术实现Web端消息推送</a><br />[6]&nbsp;<a href="http://www.52im.net/thread-2719-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">一文读懂前端技术演进：盘点Web前端20年的技术变迁史</a><br />[7]&nbsp;<a href="http://www.52im.net/thread-3134-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">WebSocket从入门到精通，半小时就够！</a><br />[8]&nbsp;<a href="http://www.52im.net/thread-3555-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">网页端IM通信技术快速入门：短轮询、长轮询、SSE、WebSocket</a><br />[9]&nbsp;<a href="http://www.52im.net/thread-3695-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">搞懂现代Web端即时通讯技术一文就够：WebSocket、socket.io、SSE</a><br />[10]&nbsp;<a href="http://www.52im.net/thread-442-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">WebRTC实时音视频技术基础：基本架构和协议栈</a><br />[11]&nbsp;<a href="http://www.52im.net/thread-3680-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">零基础入门：基于开源WebRTC，从0到1实现实时音视频聊天功能</a><br />[12]&nbsp;<a href="http://www.52im.net/thread-3804-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">实时音视频入门学习：开源工程WebRTC的技术原理和使用浅析</a><br />[13]&nbsp;<a href="http://www.52im.net/thread-4184-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">零基础快速入门WebRTC：基本概念、关键技术、与WebSocket的区别等</a><br />[14]&nbsp;<a href="http://www.52im.net/thread-4831-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">大模型时代多模型AI网关的架构设计与实现</a><br />[15]&nbsp;<a href="http://www.52im.net/thread-4797-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">全民AI时代，大模型客户端和服务端的实时通信到底用什么协议？</a><br />[16]&nbsp;<a href="http://www.52im.net/thread-4856-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">通俗易懂：AI大模型基于SSE的实时流式响应技术原理和实践示例</a><br />[17]&nbsp;<a href="http://www.52im.net/thread-4832-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">Web端实时通信技术SSE在携程机票业务中的实践应用</a><br />[18]&nbsp;<a href="http://www.52im.net/thread-4872-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">ChatGPT如何实现聊天一样的实时交互？快速读懂SSE实时&#8220;推&#8221;技术</a></p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;"><strong>即时通讯技术学习：</strong></p><p style="margin: 10px auto;">- 移动端IM开发入门文章：《<a href="http://www.52im.net/thread-464-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">新手入门一篇就够：从零开发移动端IM</a>》</p><p style="margin: 10px auto;">- 开源IM框架源码：<a href="https://github.com/JackJiang2011/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">https://github.com/JackJiang2011/MobileIMSDK</a>（<a href="https://gitee.com/jackjiang/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">备用地址点此</a>）</p></blockquote><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">（本文同步发布于：<a href="http://www.52im.net/thread-4909-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">http://www.52im.net/thread-4909-1-1.html</a>）</p><img src ="http://www.blogjava.net/jb2011/aggbug/451783.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jb2011/" target="_blank">Jack Jiang</a> 2026-05-28 11:57 <a href="http://www.blogjava.net/jb2011/archive/2026/05/28/451783.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>微信IM聊天消息序列号生成算法技术原理</title><link>http://www.blogjava.net/jb2011/archive/2026/05/19/451782.html</link><dc:creator>Jack Jiang</dc:creator><author>Jack Jiang</author><pubDate>Tue, 19 May 2026 10:07:00 GMT</pubDate><guid>http://www.blogjava.net/jb2011/archive/2026/05/19/451782.html</guid><wfw:comment>http://www.blogjava.net/jb2011/comments/451782.html</wfw:comment><comments>http://www.blogjava.net/jb2011/archive/2026/05/19/451782.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jb2011/comments/commentRss/451782.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jb2011/services/trackbacks/451782.html</trackback:ping><description><![CDATA[<h1>1、写在前面</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>对于IM系统来说，如何做到IM聊天消息离线差异拉取（<span style="color: #808080;">差异拉取是为了节省流量</span>）、消息多端同步、消息顺序保证等，是典型的IM技术难点。总结下来其实就是要解决好一个问题：即如何保证聊天消息的唯一性判定和顺序判定。</strong></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">很多读者在讨论这个问题的时候，普遍考虑的是使用整型自增序列号作为消息ID（<span style="color: #808080;">即MsgId</span>）：这样确实能保证消息的唯一性又方便保证顺序性，但问题是在分布式情况下是很难保证消息id的唯一性且顺序递增的，维护id生成的一致性难度太大了（<span style="color: #808080;">网络延迟、调试出错等等都可能导致不同的机器取到的消息id存在碰撞的可能</span>）。</p><div img-uploading-status=""  image-package-7"="" data-index="7" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="1" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260518221952974-304066111.jpg" src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260518221952974-304066111.jpg"  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;"><strong>微信消息序列号实际上是解决消息的唯一性、顺序性问题，可以将一个技术点分解成两个</strong>：即将原先每条消息一个自增且唯一的消息ID分拆成两个关键属性&#8212;&#8212;消息ID（<span style="color: #808080;">msgId</span>）和消息序列号（<span style="color: #808080;">seqId</span>）。消息ID只要保证唯一性而不需要兼顾顺序性（<span style="color: #808080;">比如直接用UUID</span>）、消息序列号只要保证顺序性而不需要兼顾唯一性，这样的技术分解就能很好的解决原本一个消息ID既要保证唯一性又要保证顺序性的难题。</p><p style="margin: 10px auto;">那么，如何优雅地解决&#8220;消息序列号只要保证顺序性而不需要兼顾唯一性&#8221;的问题呢？这就是本文所要分享的内容，强烈建议深入理解和阅读。</p></div></div></div><h1>2、本文分篇</h1><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;">上篇：《<a href="http://www.52im.net/thread-1998-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">微信技术分享：微信的海量IM聊天消息序列号生成实践（算法原理篇）</a>》（&#9756;&nbsp;&nbsp;本文）</li><li style="list-style: inherit;">下篇：《<a href="http://www.52im.net/thread-1999-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">微信技术分享：微信的海量IM聊天消息序列号生成实践（容灾方案篇）</a>》</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>本文是&#8220;IM消息ID技术专题&#8221;系列文章的第1篇，总目录如下：</strong></p><ol style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-1998-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">IM消息ID技术专题(一)：微信的海量IM聊天消息序列号生成实践（算法原理篇）</a>》（&#9756;&nbsp;&nbsp;本文）</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-1999-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">IM消息ID技术专题(二)：微信的海量IM聊天消息序列号生成实践（容灾方案篇）</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-2747-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">IM消息ID技术专题(三)：解密融云IM产品的聊天消息ID生成策略</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-2751-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">IM消息ID技术专题(四)：深度解密美团的分布式ID生成算法</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-2953-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">IM消息ID技术专题(五)：开源分布式ID生成器UidGenerator的技术实现</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-3129-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">IM消息ID技术专题(六)：深度解密滴滴的高性能ID生成器(Tinyid)</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-4378-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">IM消息ID技术专题(七)：深度解密vivo的自研分布式ID服务(鲁班)</a>》</li></ol><h1>3、技术背景</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">微信在立项之初，就已确立了利用数据版本号（注：具体的实现也就是本文要分享的消息序列号）实现终端与后台的数据增量同步机制，确保发消息时消息可靠送达对方手机，避免了大量潜在的家庭纠纷。时至今日，这套同步机制仍然在消息收发、朋友圈通知、好友数据更新等需要数据同步的地方发挥着核心的作用。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">而在这同步机制的背后，需要一个高可用、高可靠的消息序列号生成器来产生同步数据用的版本号（注：因为序列号天生的递增特性，完全可以当版本号来使用，但又不仅限于版本号的用途）。这个消息序列号生成器我们微信内部称之为 seqsvr&nbsp;，目前已经发展为一个每天万亿级调用的重量级系统，其中每次申请序列号平时调用耗时1ms，99.9%的调用耗时小于3ms，服务部署于数百台4核 CPU 服务器上。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">本篇将重点介绍微信的消息序列号生成器 seqsvr 的算法原理、架构核心思想，以及 seqsvr 随着业务量快速上涨所做的架构演变（下篇《<a href="http://www.52im.net/thread-1999-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">微信技术分享：微信的海量IM聊天消息序列号生成实践（容灾方案篇）</a>》会着重讨论分布式容灾方案）。</p><div img-uploading-status=""  image-package-8"="" data-index="8" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="2" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260518222001594-1795592302.png" src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260518222001594-1795592302.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><h1>4、关于作者</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>曾钦松：</strong>微信高级工程师，负责过微信基础架构、微信翻译引擎、微信围棋PhoenixGo，致力于高可用高性能后台系统的设计与研发。2011年毕业于西安电子科技大学，早先曾在腾讯搜搜从事检索架构、分布式数据库方面的工作。</p><h1>5、技术思路</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">微信服务器端为每一份需要与客户端同步的数据（例如聊天消息）都会赋予一个唯一的、递增的序列号（后文称为 sequence&nbsp;），作为这份数据的版本号（这是利用了序列号递增的特性）。在客户端与服务器端同步的时候，客户端会带上已经同步下去数据的最大版本号，后台会根据客户端最大版本号与服务器端的最大版本号，计算出需要同步的增量数据，返回给客户端。这样不仅保证了客户端与服务器端的数据同步的可靠性，同时也大幅减少了同步时的冗余数据（就像这篇文章中讨论的一样：《<a href="http://www.52im.net/thread-714-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">如何保证IM实时消息的&#8220;时序性&#8221;与&#8220;一致性&#8221;？</a>》）。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>这里不用乐观锁机制来生成版本号，而是使用了一个独立的 seqsvr 来处理序列号操作：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>一方面因为业务有大量的 sequence 查询需求&#8212;&#8212;查询已经分配出去的最后一个 sequence ，而基于 seqsvr 的查询操作可以做到非常轻量级，避免对存储层的大量 IO 查询操作；</li><li style="list-style: inherit;"><em>2）</em>另一方面微信用户的不同种类的数据存在不同的 Key-Value 系统中，使用统一的序列号有助于避免重复开发，同时业务逻辑可以很方便地判断一个用户的各类数据是否有更新。</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>从 seqsvr 申请的、用作数据版本号的 sequence ，具有两种基本的性质：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>递增的64位整型变量；</li><li style="list-style: inherit;"><em>2）</em>每个用户都有自己独立的64位 sequence 空间。</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>举个例子</strong>，小明当前申请的 sequence 为100，那么他下一次申请的 sequence ，可能为101，也可能是110，总之一定大于之前申请的100。而小红呢，她的 sequence 与小明的 sequence 是独立开的，假如她当前申请到的 sequence 为50，然后期间不管小明申请多少次 sequence 怎么折腾，都不会影响到她下一次申请到的值（很可能是51）。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">这里用了每个用户独立的64位 sequence 的体系，而不是用一个全局的64位（或更高位） sequence ，很大原因是全局唯一的 sequence 会有非常严重的申请互斥问题，不容易去实现一个高性能高可靠的架构。对微信业务来说，每个用户独立的64位 sequence 空间已经满足业务要求。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">目前 sequence 用在终端与后台的数据同步外，同时也广泛用于微信后台逻辑层的基础数据一致性cache中，大幅减少逻辑层对存储层的访问。虽然一个用于终端&#8212;&#8212;后台数据同步，一个用于后台cache的一致性保证，场景大不相同。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">但我们仔细分析就会发现，两个场景都是利用 sequence 可靠递增的性质来实现数据的一致性保证，这就要求我们的 seqsvr 保证分配出去的 sequence 是稳定递增的，一旦出现回退必然导致各种数据错乱、消息消失；另外，这两个场景都非常普遍，我们在使用微信的时候会不知不觉地对应到这两个场景：小明给小红发消息、小红拉黑小明、小明发一条失恋状态的朋友圈，一次简单的分手背后可能申请了无数次 sequence。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">微信目前拥有数亿的活跃用户，每时每刻都会有海量 sequence 申请，这对 seqsvr 的设计也是个极大的挑战。那么，既要 sequence 可靠递增，又要能顶住海量的访问，要如何设计 seqsvr 的架构？我们先从 seqsvr 的架构原型说起。</p><h1>6、具体的技术架构原型</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">不考虑 seqsvr 的具体架构的话，它应该是一个巨大的64位数组，而我们每一个微信用户，都在这个大数组里独占一格8 bytes 的空间，这个格子就放着用户已经分配出去的最后一个 sequence：cur_seq。每个用户来申请sequence的时候，只需要将用户的cur_seq+=1，保存回数组，并返回给用户。</p><div img-uploading-status=""  image-package-9"="" data-index="9" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="3" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260518222013768-659902586.jpg" src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260518222013768-659902586.jpg" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><span style="color: #888888;">&#9650; 图1：小明申请了一个sequence，返回101</span></p><h2>6.1 预分配中间层</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">任何一件看起来很简单的事，在海量的访问量下都会变得不简单。前文提到，seqsvr 需要保证分配出去的sequence 递增（数据可靠），还需要满足海量的访问量（每天接近万亿级别的访问）。满足数据可靠的话，我们很容易想到把数据持久化到硬盘，但是按照目前每秒千万级的访问量（~10^7 QPS），基本没有任何硬盘系统能扛住。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">后台架构设计很多时候是一门关于权衡的哲学，针对不同的场景去考虑能不能降低某方面的要求，以换取其它方面的提升。仔细考虑我们的需求，我们只要求递增，并没有要求连续，也就是说出现一大段跳跃是允许的（例如分配出的sequence序列：1,2,3,10,100,101）。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>于是我们实现了一个简单优雅的策略：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>内存中储存最近一个分配出去的sequence：cur_seq，以及分配上限：max_seq；</li><li style="list-style: inherit;"><em>2）</em>分配sequence时，将cur_seq++，同时与分配上限max_seq比较：如果cur_seq &gt; max_seq，将分配上限提升一个步长max_seq += step，并持久化max_seq；</li><li style="list-style: inherit;"><em>3）</em>重启时，读出持久化的max_seq，赋值给cur_seq。</li></ul><div img-uploading-status=""  image-package-10"="" data-index="10" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="4" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260518222022232-313771115.jpg" src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260518222022232-313771115.jpg" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;"><span style="color: #888888;">&#9650; 图2：小明、小红、小白都各自申请了一个sequence，但只有小白的max_seq增加了步长100</span></p></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">这样通过增加一个预分配 sequence 的中间层，在保证 sequence 不回退的前提下，大幅地提升了分配 sequence 的性能。实际应用中每次提升的步长为10000，那么持久化的硬盘IO次数从之前~10^7 QPS降低到~10^3 QPS，处于可接受范围。在正常运作时分配出去的sequence是顺序递增的，只有在机器重启后，第一次分配的 sequence 会产生一个比较大的跳跃，跳跃大小取决于步长大小。</p><h2>6.2 分号段共享存储</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">请求带来的硬盘IO问题解决了，可以支持服务平稳运行，但该模型还是存在一个问题：重启时要读取大量的max_seq数据加载到内存中。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">我们可以简单计算下，以目前 uid（用户唯一ID）上限2^32个、一个 max_seq 8bytes 的空间，数据大小一共为32GB，从硬盘加载需要不少时间。另一方面，出于数据可靠性的考虑，必然需要一个可靠存储系统来保存max_seq数据，重启时通过网络从该可靠存储系统加载数据。如果max_seq数据过大的话，会导致重启时在数据传输花费大量时间，造成一段时间不可服务。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">为了解决这个问题，我们引入号段 Section 的概念，uid 相邻的一段用户属于一个号段，而同个号段内的用户共享一个 max_seq，这样大幅减少了max_seq 数据的大小，同时也降低了IO次数。</p><div img-uploading-status=""  image-package-11"="" data-index="11" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="5" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260518222027384-1683669861.jpg" src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260518222027384-1683669861.jpg" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;"><span style="color: #888888;">&#9650; 图3：小明、小红、小白属于同个Section，他们共用一个max_seq。在每个人都申请一个sequence</span></p></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">的时候，只有小白突破了max_seq上限，需要更新max_seq并持久化</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">目前 seqsvr 一个 Section 包含10万个 uid，max_seq 数据只有300+KB，为我们实现从可靠存储系统读取max_seq 数据重启打下基础。</p><h2>6.3 工程实现</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>工程实现在上面两个策略上做了一些调整，主要是出于数据可靠性及灾难隔离考虑：</strong></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><em>1）</em>把存储层和缓存中间层分成两个模块 StoreSvr 及 AllocSvr 。StoreSvr 为存储层，利用了多机 NRW 策略来保证数据持久化后不丢失； AllocSvr 则是缓存中间层，部署于多台机器，每台 AllocSvr 负责若干号段的 sequence 分配，分摊海量的 sequence 申请请求。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><em>2）</em>整个系统又按 uid 范围进行分 Set，每个 Set 都是一个完整的、独立的 StoreSvr+AllocSvr 子系统。分 Set 设计目的是为了做灾难隔离，一个 Set 出现故障只会影响该 Set 内的用户，而不会影响到其它用户。</p><div img-uploading-status=""  image-package-12"="" data-index="12" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="6" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260518222112836-73128927.jpg" src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260518222112836-73128927.jpg" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;"><span style="color: #888888;">&#9650; 图4：原型架构图</span></p></div></div></div><h1>7、本篇小结</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">写到这里把 seqsvr 基本原型讲完了，正是如此简单优雅的模型，可靠、稳定地支撑着微信五年来的高速发展。五年里访问量一倍又一倍地上涨，seqsvr 本身也做过大大小小的重构，但 seqsvr 的分层架构一直没有改变过，并且在可预见的未来里也会一直保持不变。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">原型跟生产环境的版本存在一定差距，最主要的差距在于容灾上。像微信的 IM 类应用，对系统可用性非常敏感，而 seqsvr 又处于收发消息、朋友圈等功能的关键路径上，对可用性要求非常高，出现长时间不可服务是分分钟写故障报告的节奏。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><img alt="7" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260518223553428-708359023.jpg" src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260518223553428-708359023.jpg" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">本文的下篇《<a href="http://www.52im.net/thread-1999-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">微信技术分享：微信的海量IM聊天消息序列号生成实践（容灾方案篇）</a>》会讲讲 seqsvr 的容灾方案演变。另：《<a href="http://www.52im.net/thread-4636-1-1.html#7" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">微信团队分享：来看看微信十年前的IM消息收发架构，你做到了吗</a>》一文中提到的利用sequence序列号实现消息防丢机制的原理，也可以一并阅读之。</p><h1>8、相关资料</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[1]&nbsp;<a href="http://www.52im.net/thread-3189-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">零基础IM开发入门(四)：什么是IM聊天系统的消息时序一致性？</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[2]&nbsp;<a href="http://www.52im.net/thread-294-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">IM消息送达保证机制实现(一)：保证在线实时消息的可靠投递</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[3]&nbsp;<a href="http://www.52im.net/thread-594-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">IM消息送达保证机制实现(二)：保证离线消息的可靠投递</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[4]&nbsp;<a href="http://www.52im.net/thread-714-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">如何保证IM实时消息的&#8220;时序性&#8221;与&#8220;一致性&#8221;？</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[5]&nbsp;<a href="http://www.52im.net/thread-715-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">IM单聊和群聊中的在线状态同步应该用&#8220;推&#8221;还是&#8220;拉&#8221;？</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[6]&nbsp;<a href="http://www.52im.net/thread-753-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">IM群聊消息如此复杂，如何保证不丢不重？</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[7]&nbsp;<a href="http://www.52im.net/thread-867-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">浅谈移动端IM的多点登陆和消息漫游原理</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[8]&nbsp;<a href="http://www.52im.net/thread-1616-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">IM群聊消息究竟是存1份(即扩散读)还是存多份(即扩散写)？</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[9]&nbsp;<a href="http://www.52im.net/thread-3445-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">一套亿级用户的IM架构技术干货(下篇)：可靠性、有序性、弱网优化等</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[10]&nbsp;<a href="http://www.52im.net/thread-3638-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">融云技术分享：全面揭秘亿级IM消息的可靠投递机制</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[11]&nbsp;<a href="http://www.52im.net/thread-3856-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">阿里IM技术分享(七)：闲鱼IM的在线、离线聊天数据同步机制优化实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[12]&nbsp;<a href="http://www.52im.net/thread-4764-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">转转平台IM系统架构设计与实践(一)：整体架构设计</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[13]&nbsp;<a href="http://www.52im.net/thread-4887-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">如何保障分布式IM聊天系统的消息有序性（即消息不乱）</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[14]&nbsp;<a href="http://www.52im.net/thread-4636-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">微信团队分享：来看看微信十年前的IM消息收发架构，你做到了吗</a></p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;"><strong>即时通讯技术学习：</strong></p><p style="margin: 10px auto;">- 移动端IM开发入门文章：《<a href="http://www.52im.net/thread-464-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">新手入门一篇就够：从零开发移动端IM</a>》</p><p style="margin: 10px auto;">- 开源IM框架源码：<a href="https://github.com/JackJiang2011/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">https://github.com/JackJiang2011/MobileIMSDK</a>（<a href="https://gitee.com/jackjiang/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">备用地址点此</a>）</p></blockquote><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">（本文同步发布于：<a href="http://www.52im.net/thread-1998-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">http://www.52im.net/thread-1998-1-1.html</a>）</p><img src ="http://www.blogjava.net/jb2011/aggbug/451782.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jb2011/" target="_blank">Jack Jiang</a> 2026-05-19 18:07 <a href="http://www.blogjava.net/jb2011/archive/2026/05/19/451782.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>socket长连接在手游场景下的技术实践</title><link>http://www.blogjava.net/jb2011/archive/2026/05/06/451780.html</link><dc:creator>Jack Jiang</dc:creator><author>Jack Jiang</author><pubDate>Wed, 06 May 2026 14:27:00 GMT</pubDate><guid>http://www.blogjava.net/jb2011/archive/2026/05/06/451780.html</guid><wfw:comment>http://www.blogjava.net/jb2011/comments/451780.html</wfw:comment><comments>http://www.blogjava.net/jb2011/archive/2026/05/06/451780.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jb2011/comments/commentRss/451780.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jb2011/services/trackbacks/451780.html</trackback:ping><description><![CDATA[<p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">本文由37手游黄子键分享，有排版和内容优化等。</p><h1>1、引言</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">本文介绍了37手游基于B站goim框架自研长连接系统的实践。系统采用分层设计，支持多协议和发布/订阅机制，用于直播弹幕、实时推送等场景，实现了高性能与业务适配。</p><div img-uploading-status=""  image-package-1"="" data-index="1" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="cover_opti" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201852477-1068335934.jpg" src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201852477-1068335934.jpg" medium-zoom-image="" ls-is-cached=""  lazyloaded"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><h1>2、长连接对于大部分公司的意义</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">实时的响应总是让人兴奋的，就如你在微信里看到对方正在输入，如你在王者峡谷里一呼百应，如你们在直播弹幕里不约而同的 666，它们的背后都离不开长连接技术的加持。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">每个互联网公司里几乎都有一套长连接系统，它们被应用在消息提醒、即时通讯、推送、直播弹幕、游戏、共享定位、股票行情等等场景。而当公司发展到一定规模，业务场景变得更复杂后，更有可能是多个业务都需要同时使用长连接系统。</p><h1>3、长连接是什么?</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>长连接，在百科上的定义：</strong>指在一个连接上可以连续发送多个数据包，在连接保持期间，如果没有数据包发送，需要双方发链路检测包。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>严格上来说：</strong>长连接是一种概念，它指的是在网络发送，接收双方保持一个持续连接的状态，双方都可以发送或接收消息（全双工）。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">当然，长连接系统听起来好像高深莫测，但实际不可能说完全脱离于我们实际公司的业务去出发设计，这就引出来我们今天的主题&#8212;&#8212; 长连接技术在37手游内是如何设计以及实践的？</p><h1>4、技术痛点</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>从服务端而言：</strong>缺乏在SDK内实时推送通知用户的能力，如防沉迷的弹窗通过http定时轮询来实现，给服务造成很大的压力</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>从客户端而言：</strong>也缺乏低成本告知服务端自身在线的能力，如维持用户在线状态靠客户端定时上报心跳到防沉迷服务实现。</p><h1>5、搭建背景</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>业务背景：</strong>手游的SDK需要提供内嵌直播弹幕的功能，供玩家在看直播的时候进行沟通发言，业界一般会用到长连接来进行实时的推送，以降低服务的轮训和请求。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>从业务上而言：</strong>直播弹幕需求由于初始量级很少，不用通过长连接的形式也能实现具体业务逻辑；但出于平台的拓展和能力考虑，长连接的能力是必须具备的。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">长连接系统的这次搭建，本质是通过SDK内嵌直播弹幕为切入点，从0到1，为平台提供了完整的一套实时推送触达用户的能力。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>从以上角度出发：</strong>我们便着手想要设计一个高可用且高性能的长连接系统提供给我们使用。</p><h1>6、方案选型</h1><div img-uploading-status=""  image-package-2"="" data-index="2" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="1" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201914846-1664838654.png" src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201914846-1664838654.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;">&nbsp;</p></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">从实际上而言，我们考虑三个方向。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><span style="color: #3366ff;"><strong><em>1）</em>云服务：</strong></span></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">市面上可购买的服务如环信等，大多数都以即时聊天通讯为主，长连接往往只是附带产品，过于偏向于社交业务，在花钱的同时也很难适应到自身游戏业务。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><span style="color: #3366ff;"><em>2）</em>开源框架：</span></strong></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">b站的<a href="https://github.com/Terry-Mao/goim" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">goim</a>框架、<a href="https://github.com/FreddyChen/NettyChat" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">NettyChat</a>框架、<a href="https://gitee.com/jackjiang/MobileIMSDK" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">MobileIMSDK</a>框架等，好处是免费且能快速接入，但业务还是不相适应，且语言栈和技术栈不一定相契合。</p><div img-uploading-status=""  image-package-3"="" data-index="3" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="2" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201920865-1888031507.png" src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201920865-1888031507.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;">&nbsp;</p></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><span style="color: #3366ff;"><em>3）</em>自研：</span></strong></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>弊</em>：</strong>开发成本高，且容易踩坑。<strong><em>利</em>：</strong>基础组件完善，统一框架好进行监控和问题查询。且能充分契合自身业务进行拓展，并将数据源掌控在自己手中。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><span style="color: #3366ff;"><strong><em>4）</em>结论：</strong></span></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">出于扩展性的考虑，肯定是自研的方式更加合适，但完全自研相应的成本和不确定性会非常高，因此最终的选型方案为借鉴b站的goim框架设计和部分代码，并做了符合自身技术栈和业务架构的改善。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">可认为是基于开源框架的自研方案的形式完成了长连接这个系统的落地。</p><h1>7、goim技术概况</h1><h2>7.1 goim是什么？为什么借鉴它？</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">goim是b站开源研发的一个支持集群的im及实时推送服务。业务上为直播间的弹幕发送场景。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>主要考虑到：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>技术栈契合：语言栈为Golang ，消息队列为kafka，缓存设计为redis；</li><li style="list-style: inherit;"><em>2）</em>高性能：有压测报告，性能设计有保障；</li><li style="list-style: inherit;"><em>3）</em>多协议支持：websocket、tcp。</li></ul><h2>7.2 goim的模块？</h2><div img-uploading-status=""  image-package-4"="" data-index="4" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="3" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201927412-901970329.png" src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201927412-901970329.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>如上图所示：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>comet：用于跟端上保连，在内存中存储订阅信息；</li><li style="list-style: inherit;"><em>2）</em>logic：用于处理鉴权，在线房间等业务逻辑；</li><li style="list-style: inherit;"><em>3）</em>business：业务服务；</li><li style="list-style: inherit;"><em>4）</em>balancer：负载均衡模块；</li><li style="list-style: inherit;"><em>5）</em>discovery：服务发现模块；</li><li style="list-style: inherit;"><em>6）</em>job：kafka用于削峰。redis用于缓存房间信息；</li><li style="list-style: inherit;"><em>7）</em>logic和comet的通讯：采用rpc，优化性能。</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">从上面的goim的设计而言，我们可以总结出长连接的设计原则。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>长连接架构的设计总是大同小异的，通用的有这几点：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>业务层次分明：分接入层，逻辑层，存储层，服务发现层；通过这几层，实现业务解耦</li><li style="list-style: inherit;"><em>2）</em>协议通用：传输的数据协议必须在所有服务内进行通讯，并支持扩展</li><li style="list-style: inherit;"><em>3）</em>上行和下行收敛：上行和下行都会有一个对应的网关来进行收敛消息的收敛</li><li style="list-style: inherit;"><em>4）</em>消息削峰：通过队列来将发送信息削峰。并使接入层和逻辑层解藕</li></ul><h1>8、手游长连接系统的设计实践</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">根据以上的通用设计原则，我们不难得出，需要针对goim的架构如何做适配才能实际满足我们的业务需求，下面我们将会一一介绍。</p><h2>8.1 架构改动</h2><div img-uploading-status=""  image-package-5"="" data-index="5" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="4" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201936024-1704414491.png" src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201936024-1704414491.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;"><strong>从架构而言，我们改动的点不多：</strong></p></div></div></div><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>cient获取连接节点：手游内无统一的服务发现模块，但有外层LB，因此通过LB来实现comet节点负载均衡</li><li style="list-style: inherit;"><em>2）</em>推送消息：同上，消费者获取节点时需要到logic服务中查询，而不是discovery</li><li style="list-style: inherit;"><em>3）</em>服务通讯协议：由于手游内架构通讯协议的统一，接入层和逻辑层的通讯由RPC转为http</li></ul><h2>8.2 逻辑改动</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>1）</em>鉴权机制：</strong>客户端第一次连接上comet的时候，会发送鉴权上行。comet会解析客户端传输的数据，若包体协议或对应的签名错误，comet会将会直接主动断连</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>2）</em>心跳机制：</strong>comet会有心跳计时器，若客户端无定时上报心跳，则认为该连接已经超时，直接断开。这种业务的心跳主要是为了防止僵尸连接的存在。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>3）</em>回包机制：</strong>客户端每发送一次上行操作，comet都会有对应的消息回包给端上。端上可根据回包，来知道自己鉴权，心跳，或者订阅，退订的操作是否成功，从而决定是否进行重试。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>4）</em>发布/订阅机制：</strong></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">我们在观看goim的源码时，发现goim适用于直播，群体推送以房间为维度，带有强业务属性，因此我们针对该部分抽象出发布/订阅机制。将房间抽象为topic，修改进房/出房动作为&nbsp;&nbsp;订阅/退订。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">将所有的通知抽象为业务事件，客户端想要接受到哪个事件过来的消息时，可发送对应的订阅上行。单个连接订阅的事件不做限制，对某个用户或某个事件范围内的用户推送消息时，comet会根据事件去取到推送的用户，只有用户订阅了才会收到消息。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>5）</em>内存设计：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;">a. bucket维护消息通道和事件的信息；</li><li style="list-style: inherit;">b. 一个session对应一个用户连接；</li><li style="list-style: inherit;">c. 根据sessionid做一致性哈希来选择落到那个bucket上；</li><li style="list-style: inherit;">d. bucket有两个map，一个是session map，一个是topic map；</li><li style="list-style: inherit;">e. 所有bucket都会开启一个chan做监听，广播的时候，会通知到所有bucket，所有bucket再取出某个事件的所有连接进行下发。</li></ul><div img-uploading-status=""  image-package-6"="" data-index="6" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="5" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201943167-1033383934.jpg" src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201943167-1033383934.jpg" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><h2>8.3 总体交互</h2><div img-uploading-status=""  image-package-7"="" data-index="7" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="6" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201950322-1618265421.jpg" src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201950322-1618265421.jpg" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><h2>8.4 总体特点</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>因此，我们可以总结出来，我们手游长连接系统的总体特点：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>纯golang实现；</li><li style="list-style: inherit;"><em>2）</em>多协议支持：websocket和tcp；</li><li style="list-style: inherit;"><em>3）</em>可拓扑结构：主要模块均无状态，可横向扩展；</li><li style="list-style: inherit;"><em>4）</em>消息支持单推/群推，消息协议业务可自定义；</li><li style="list-style: inherit;"><em>5）</em>发布/订阅机制，事件可业务自定义。</li></ul><h1>9、性能评估</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">在说性能之前，我们先抛出一个疑问：协程数过多实际占用的是什么？连接数过多是否影响CPU？</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>从实际上来说：</strong>长连接的性能瓶颈一般卡在接入层，因此以接入层为评估维度，通过全链路压测得出来结果。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>如下：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>压测参数：3000连接+ 3000 qps的实时推送；</li><li style="list-style: inherit;"><em>2）</em>推送内容：{msg:test}&nbsp;&nbsp;；</li><li style="list-style: inherit;"><em>3）</em>推送类型：群推；</li><li style="list-style: inherit;"><em>4）</em>推送持续时长：5分钟 ；</li><li style="list-style: inherit;"><em>5）</em>资源使用：1核2G的容器；</li><li style="list-style: inherit;"><em>6）</em>CPU的使用：达到100%。</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">目前采用的长连接系统，通过压测结果发现，本身的连接数的维持和实时推送实际影响到是机器两个维度的性能，推送量影响到的是CPU，连接数影响到的是内存。分别提高两个参数对相应的内存，CPU的影响并不大。</p><h1>10、实际应用情况</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>直播弹幕：</strong></p><div img-uploading-status=""  image-package-8"="" data-index="8" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="7" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506202001563-268800190.jpg" src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506202001563-268800190.jpg"  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;">&nbsp;</p></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>悬浮球实时红点：</strong></p><div img-uploading-status=""  image-package-9"="" data-index="9" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="8" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506202006981-45886119.jpg" src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506202006981-45886119.jpg"  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;">从实际上来说，接入的业务虽小，但基础能力已经具备。</p></div></div></div><h1>11、本文小结</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">长连接系统从根本上来说，对应的设计都是大同小异的，我们应该更加关注的是对于自身业务的适配以及实现。</p><h1>12、参考资料</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[1]&nbsp;<a href="http://www.52im.net/thread-1915-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">以网游服务端的网络接入层设计为例，理解实时通信的技术挑战</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[2]&nbsp;<a href="http://www.52im.net/thread-2737-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">知乎千万级并发的高性能长连接网关技术实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[3]&nbsp;<a href="http://www.52im.net/thread-3110-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">手淘亿级移动端接入层网关的技术演进之路</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[4]&nbsp;<a href="http://www.52im.net/thread-3564-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">喜马拉雅自研亿级API网关技术实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[5]&nbsp;<a href="http://www.52im.net/thread-3757-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">石墨文档单机50万WebSocket长连接架构实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[6]&nbsp;<a href="http://www.52im.net/thread-3860-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">小米小爱单机120万长连接接入层的架构演进</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[7]&nbsp;<a href="http://www.52im.net/thread-3941-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">B站基于微服务的API网关从0到1的演进之路</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[8]&nbsp;<a href="http://www.52im.net/thread-4618-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">去哪儿网酒店高性能业务网关技术实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[9]&nbsp;<a href="http://www.52im.net/thread-4623-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">百度基于Go的千万级统一长连接服务架构实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[10]&nbsp;<a href="http://www.52im.net/thread-4641-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">揭秘腾讯公网TGW网关系统的技术架构演进</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[11]&nbsp;<a href="http://www.52im.net/thread-4854-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">基于Netty的携程高性能网关异步改造实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[12]&nbsp;<a href="http://www.52im.net/thread-4623-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">手把手教你写基于TCP的Socket长连接</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[13]&nbsp;<a href="http://www.52im.net/thread-2799-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">正确理解IM长连接、心跳及重连机制，并动手实现</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[14]&nbsp;<a href="http://www.52im.net/thread-3908-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">万字长文：手把手教你实现一套高效的IM长连接自适应心跳保活机制</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[15]&nbsp;<a href="http://www.52im.net/thread-2106-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1;">用JWT技术解决IM系统Socket长连接的身份认证痛点</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[16]&nbsp;<a href="http://www.52im.net/topic-tcpipvol1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">TCP/IP详解</a>&nbsp;-&nbsp;<a href="http://docs.52im.net/extend/docs/book/tcpip/vol1/11/" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">第11章&#183;UDP：用户数据报协议</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[17]&nbsp;<a href="http://www.52im.net/topic-tcpipvol1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">TCP/IP详解</a>&nbsp;-&nbsp;<a href="http://docs.52im.net/extend/docs/book/tcpip/vol1/17/" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">第17章&#183;TCP：传输控制协议</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[18]&nbsp;<a href="http://www.52im.net/thread-3134-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">WebSocket从入门到精通，半小时就够！</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[19]&nbsp;<a href="http://www.52im.net/thread-1107-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">快速理解TCP协议一篇就够</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[20]&nbsp;<a href="http://www.52im.net/thread-1160-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">快速理解TCP和UDP的差异</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[21]&nbsp;<a href="http://www.52im.net/thread-3793-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">一泡尿的时间，快速搞懂TCP和UDP的区别</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[22]&nbsp;<a href="http://www.52im.net/thread-3821-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">到底什么是Socket？一文即懂！</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[23]&nbsp;<a href="http://www.52im.net/thread-1732-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">我们在读写Socket时，究竟在读写什么？</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[24]&nbsp;<a href="http://www.52im.net/thread-3339-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">假如你来设计TCP协议，会怎么做？</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[25]&nbsp;<a href="http://www.52im.net/thread-4146-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">深入操作系统，一文搞懂Socket到底是什么</a></p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;"><strong>即时通讯技术学习：</strong></p><p style="margin: 10px auto;">- 移动端IM开发入门文章：《<a href="http://www.52im.net/thread-464-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">新手入门一篇就够：从零开发移动端IM</a>》</p><p style="margin: 10px auto;">- 开源IM框架源码：<a href="https://github.com/JackJiang2011/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">https://github.com/JackJiang2011/MobileIMSDK</a>（<a href="https://gitee.com/jackjiang/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">备用地址点此</a>）</p></blockquote><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">（<strong>本文同步发布于：</strong><a href="http://www.52im.net/thread-4907-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">http://www.52im.net/thread-4907-1-1.html</a>）</p><img src ="http://www.blogjava.net/jb2011/aggbug/451780.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jb2011/" target="_blank">Jack Jiang</a> 2026-05-06 22:27 <a href="http://www.blogjava.net/jb2011/archive/2026/05/06/451780.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>基于开源MobileIMSDK框架，即时通讯IM产品RainbowChat v12.0已发布</title><link>http://www.blogjava.net/jb2011/archive/2026/04/27/451779.html</link><dc:creator>Jack Jiang</dc:creator><author>Jack Jiang</author><pubDate>Mon, 27 Apr 2026 09:22:00 GMT</pubDate><guid>http://www.blogjava.net/jb2011/archive/2026/04/27/451779.html</guid><wfw:comment>http://www.blogjava.net/jb2011/comments/451779.html</wfw:comment><comments>http://www.blogjava.net/jb2011/archive/2026/04/27/451779.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jb2011/comments/commentRss/451779.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jb2011/services/trackbacks/451779.html</trackback:ping><description><![CDATA[<div id="cnblogs_post_body"  blogpost-body-html"="" style="margin-bottom: 20px; word-break: break-word; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><h1>1、关于RainbowChat</h1><p style="margin: 10px auto;">&nbsp;<img src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260426210602156-1614496586.jpg" alt="1" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></p><p style="margin: 10px auto;">RainbowChat是一套基于开源IM即时通讯聊天框架&nbsp;<a href="http://www.52im.net/thread-52-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">MobileIMSDK</a>&nbsp;的<strong>产品级移动端IM系统</strong>。<strong>RainbowChat源于真实运营的产</strong>品，解决了大量的屏幕适配、细节优化、机器兼容问题。<strong>RainbowChat可能是市面上提供im即时通讯聊天源码的，唯一一款同时支持TCP、UDP、WebSocket三种通信协议的IM产品</strong>。与姊妹产品&nbsp;<a href="http://www.52im.net/thread-4824-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">RainbowTalk</a>&nbsp;和&nbsp;<a href="http://www.52im.net/thread-2483-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">RainbowChat-Web</a>&nbsp;技术同源，历经考验。</p><ul style="margin-left: 2.5rem; padding-left: 0px;"><li style="list-style: inherit;"><strong><strong>&#9758;</strong>&nbsp;详细介绍：</strong><a href="http://www.52im.net/thread-19-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">http://www.52im.net/thread-19-1-1.html</a></li><li style="list-style: inherit;"><strong><strong>&#9758;</strong>&nbsp;版本日志：</strong><a href="http://www.52im.net/thread-2735-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">http://www.52im.net/thread-2735-1-1.html</a></li><li style="list-style: inherit;"><strong><strong>&#9758;</strong>&nbsp;运行截图：</strong><a href="http://www.52im.net/thread-20-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">Android端</a>、<a href="http://www.52im.net/thread-2730-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">iOS端</a></li><li style="list-style: inherit;"><strong><strong>&#9758;</strong>&nbsp;下载体验：</strong><a href="https://apps.apple.com/cn/app/rainbowchat/id1423024212" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">App Store安装地址</a>&nbsp;（另：Android端下载体验&nbsp;<a href="http://www.52im.net/thread-4739-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">点此查看</a>）</li></ul><h1>2、关于MobileIMSDK开源工程</h1><p style="margin: 10px auto;"><img src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260426210610330-1388464167.jpg" alt="2" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></p><div align="left"><p style="margin: 10px auto;"><strong>MobileIMSDK</strong>&nbsp;是一套全平台开源IM即时通讯聊天框架，超轻量级、高度提炼，一套API优雅支持<strong>UDP</strong>&nbsp;、<strong>TCP</strong>&nbsp;、<strong>WebSocket</strong>&nbsp;三种协议，客户端支持<strong>iOS、Android、H5、小程序、Uniapp、标准Java、纯血鸿蒙</strong>等，服务端基于Netty编写，性能卓越、易于扩展。</p><p style="margin: 10px auto;"><img src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260426210620811-1338174721.png" alt="3" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></p></div><p data-pid="9Wt-ivaI" style="margin: 10px auto;"><strong>工程同步开源地址：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px;"><li style="list-style: inherit;">❶&nbsp;<strong>GitHub：</strong><a href="https://github.com/JackJiang2011/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">https://github.com/JackJiang2011/MobileIMSDK</a></li><li style="list-style: inherit;">❷&nbsp;<strong>码云gitee：</strong>&nbsp;<a href="https://gitee.com/jackjiang/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">https://gitee.com/jackjiang/MobileIMSDK</a></li><li style="list-style: inherit;">❸&nbsp;<strong>Gitcode：</strong><a href="https://gitcode.com/hellojackjiang2011/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">https://gitcode.com/hellojackjiang2011/MobileIMSDK</a></li></ul><h1>2、v12.0 版更新内容</h1><p style="margin: 10px auto;"><strong>此版更新内容</strong>（<a href="http://www.52im.net/thread-1217-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">更多历史更新日志</a>）：<br /><br /><strong>（1）Android端主要更新内容</strong>【<span style="color: #888888;">全面适配Android 16、适配16KB page size、适配全面屏特性等</span>】：</p><ul style="margin-left: 2.5rem; padding-left: 0px;"><li style="list-style: inherit;"><em>1）</em>[<span style="color: #ff4d4e;">bug</span>] 解决了两个表情占位符重复的问题；</li><li style="list-style: inherit;"><em>2）</em>[<span style="color: #ff4d4e;">bug</span>] 解决了好友列表删除唯一好友后，一直转圈的问题；</li><li style="list-style: inherit;"><em>3）</em>[<span style="color: #ff4d4e;">bug</span>] 优化了搜索聊天记录时，当首页&#8220;消息&#8221;中不存在该陌生人时，搜出的群聊详细中消息发送者昵称会用uid显示的问题；</li><li style="list-style: inherit;"><em>4）</em>[<span style="color: #ff4d4e;">bug</span>] 解决了不支持分区存储的老手机转发的大文件消息，在新系统上无法下载的问题；</li><li style="list-style: inherit;"><em>5）</em>[<span style="color: #ff4d4e;">bug</span>] 优化了存在多条置顶消息时，不是按置顶时间而是消息时间排序的问题；</li><li style="list-style: inherit;"><em>6）</em>[<span style="color: #00b050;">新增</span>] 二维码生成界面下方增加功能按钮；</li><li style="list-style: inherit;"><em>7）</em>[<span style="color: #00b050;">新增</span>] &#8220;用户信息&#8221;界面增加了&#8220;查看用户资料&#8221;按钮；</li><li style="list-style: inherit;"><em>8）</em>[<span style="color: #00b050;">新增</span>] 优化了世界频道的打开入口等；</li><li style="list-style: inherit;"><em>9）</em>[<span style="color: #00b050;">新增</span>] 去掉了&#8220;商城&#8221;模块，增加了&#8220;发现&#8221;页面；</li><li style="list-style: inherit;"><em>10）</em>[<span style="color: #909296;">优化</span>] 将核心层提炼成独立的chatkit模块；</li><li style="list-style: inherit;"><em>11）</em>[<span style="color: #909296;">优化</span>] 解决了独立chatkit后，好友信息中删除对方时，无法自动跳转到主页的问题；</li><li style="list-style: inherit;"><em>12）</em>[<span style="color: #909296;">优化</span>] 现在不能删除首页列表中的&#8220;确认提醒&#8221;这个item了；</li><li style="list-style: inherit;"><em>13）</em>[<span style="color: #909296;">优化</span>] 升级腾讯Bugly至4.1.9.3，解决上架国内应用市场的隐私合规问题；</li><li style="list-style: inherit;"><em>14）</em>[<span style="color: #909296;">优化</span>] 登录和退出登录接口中废弃了osType字段；</li><li style="list-style: inherit;"><em>15）</em>[<span style="color: #909296;">优化</span>] 优化了注册界面中关于服务端返回邮箱格式不正确的错误码的处理；</li><li style="list-style: inherit;"><em>16）</em>[<span style="color: #909296;">优化</span>] 支持小窗、分屏显示；</li><li style="list-style: inherit;"><em>17）</em>[<span style="color: #909296;">优化</span>] 只有好友才能查看对方的注册和登录时间；</li><li style="list-style: inherit;"><em>18）</em>[<span style="color: #909296;">优化</span>] 查找好友时不再显示对方的在线状态；</li><li style="list-style: inherit;"><em>19）</em>[<span style="color: #909296;">优化</span>]&nbsp;<span style="color: #ff8c00;">提升targetSdkVersion至36，全面兼容Android 16</span>；</li><li style="list-style: inherit;"><em>20）</em>[<span style="color: #909296;">优化</span>]&nbsp;<span style="color: #ff8c00;">开发工程升级适配AGP 9.1最新版</span>；</li><li style="list-style: inherit;"><em>21）</em>[<span style="color: #909296;">优化</span>] 升级权限框架以适配最新Android 16系统；</li><li style="list-style: inherit;"><em>22）</em>[<span style="color: #909296;">优化</span>] 针对<span style="color: #ff8c00;">全部界面适配系统强制的Edge to Edge全面屏特性</span>；</li><li style="list-style: inherit;"><em>23）</em>[<span style="color: #909296;">优化</span>] 解决了Android 16下聊天界面输入法弹出时会挡住消息输入框的问题；</li><li style="list-style: inherit;"><em>24）</em>[<span style="color: #909296;">优化</span>] 解决基于PopupWindow实现的弹出界面底部在Edge to Edge全面屏特性下的显示问题；</li><li style="list-style: inherit;"><em>25）</em>[<span style="color: #909296;">优化</span>] 加固一处因多线程安全问题导致的可能崩溃风险；</li><li style="list-style: inherit;"><em>26）</em>[<span style="color: #909296;">优化</span>] 升级高德地图SDK至最新v11.1等，<span style="color: #ff8c00;">适配google play强制16KB page size问题</span>；</li><li style="list-style: inherit;"><em>27）</em>[<span style="color: #909296;">优化</span>] 优化了位置消息搜索界面的搜索组件ui并提升了细节体验；</li><li style="list-style: inherit;"><em>28）</em>[<span style="color: #909296;">优化</span>] 解决了进入了主页搜索界面在Android 16下不能自动弹出输入法，及优化了点击背景可收起软键盘；</li><li style="list-style: inherit;"><em>29）</em>[<span style="color: #909296;">优化</span>] 删减了APP首次启动时的权限申请内容；</li><li style="list-style: inherit;"><em>30）</em>[<span style="color: #909296;">优化</span>] 解决了Android 16下返回按钮事件捕获失败的问题；</li><li style="list-style: inherit;"><em>31）</em>[<span style="color: #909296;">优化</span>] 聊天界面下方的功能面板图标美化等；</li><li style="list-style: inherit;"><em>32）</em>[<span style="color: #909296;">优化</span>] 聊天文本框自动换行；</li><li style="list-style: inherit;"><em>33）</em>[<span style="color: #909296;">优化</span>] 其它更具现代感的UI细节优化和体验等；</li></ul><p style="margin: 10px auto;"><strong>（2）服务端主要更新内容</strong>【<span style="color: #888888;">安全加固、新增接口等</span>】：</p><ul style="margin-left: 2.5rem; padding-left: 0px;"><li style="list-style: inherit;"><em>1）</em>[<span style="color: #ff4d4e;">bug</span>] 解决了对接RainbowChat-Web产品时，网页端无法正常登录的问题；</li><li style="list-style: inherit;"><em>2）</em>[<span style="color: #909296;">优化</span>] 加固了后端SQL防注入逻辑；</li><li style="list-style: inherit;"><em>3）</em>[<span style="color: #909296;">优化</span>] 开启了WebSocket协议支持；</li><li style="list-style: inherit;"><em>4）</em>[<span style="color: #909296;">优化</span>] 对离线数据表中的消息指纹字段增加了索引，提升查询性能；</li><li style="list-style: inherit;"><em>5）</em>[<span style="color: #909296;">优化</span>] 优化了文件下载服务中存在利用文件名进行越权文件操作的安全隐患；</li><li style="list-style: inherit;"><em>6）</em>[<span style="color: #909296;">优化</span>] 提供了一个校验token与uid一致性的安全性实现示例；</li><li style="list-style: inherit;"><em>7）</em>[<span style="color: #909296;">优化</span>] 优化了原Android专用的登录接口【接口1009】，使之同时支持验证码、密码登录；</li><li style="list-style: inherit;"><em>8）</em>[<span style="color: #909296;">优化</span>] 【接口1008-10-22】新增了&#8220;preview_count&#8221;字段；</li><li style="list-style: inherit;"><em>9）</em>[<span style="color: #909296;">优化</span>] 将IDEA工程中applicationContextRoot改成了rainbowchat_pro/（方便开发环境跟生产环境一致）;</li><li style="list-style: inherit;"><em>10）</em>[<span style="color: #909296;">优化</span>] 优化了注册接口【接口1008-1-7】，增加了手机号和短信验证码支持；</li><li style="list-style: inherit;"><em>11）</em>[<span style="color: #00b050;">新增</span>] 数据库新增了注销登录相关字段；</li><li style="list-style: inherit;"><em>12）</em>[<span style="color: #00b050;">新增</span>] 新增注销登录接口；</li><li style="list-style: inherit;"><em>13）</em>[<span style="color: #00b050;">新增</span>] 新增获取验证码接口【接口1008-1-27】；</li><li style="list-style: inherit;"><em>14）</em>[<span style="color: #00b050;">新增</span>] 新增新的登录接口【接口1017】，同时支持ios等客户端的验证码、密码登录；</li><li style="list-style: inherit;"><em>15）</em>[<span style="color: #00b050;">新增</span>] 新增对接鸿蒙NEXT产品时支持华为Push Kit离线推送；</li></ul><h2>3、升级后的主要UI运行截图</h2><p style="margin: 10px auto;">（<strong><strong>&#9758;</strong>&nbsp;</strong>更多截图请查看：<a href="http://www.52im.net/thread-20-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">Android端运行截图</a>、<a href="http://www.52im.net/thread-2730-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">iOS端运行截图</a>）</p><p style="margin: 10px auto;"><img src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260426210722057-491636925.jpg" alt="4_s100_p75" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></p><h2>4、真机运行视频</h2><p style="margin: 10px auto;">（&#9758;&nbsp;<a href="https://www.bilibili.com/video/BV1ZHoMBbEvC/" target="_blank" style="color: #1d58d1; text-decoration: none;">新窗口中查看真机运行视频</a>）</p><p style="margin: 10px auto;"><img alt="5" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260428145800484-1457756756.gif" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260428145800484-1457756756.gif"  medium-zoom-image"="" style="border: 0px; max-width: 100%; cursor: zoom-in; height: auto !important; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><br /></p><h2>5、真机实拍截图</h2><p style="margin: 10px auto;"><img src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260426210751290-767045076.jpg" alt="6_pct85" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></p></div><div id="MySignature" role="contentinfo" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div id="AllanboltSignature"></div></div><img src ="http://www.blogjava.net/jb2011/aggbug/451779.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jb2011/" target="_blank">Jack Jiang</a> 2026-04-27 17:22 <a href="http://www.blogjava.net/jb2011/archive/2026/04/27/451779.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>社交场景下的统一即时通讯im消息流交互层模块化技术实践</title><link>http://www.blogjava.net/jb2011/archive/2026/04/20/451777.html</link><dc:creator>Jack Jiang</dc:creator><author>Jack Jiang</author><pubDate>Mon, 20 Apr 2026 10:43:00 GMT</pubDate><guid>http://www.blogjava.net/jb2011/archive/2026/04/20/451777.html</guid><wfw:comment>http://www.blogjava.net/jb2011/comments/451777.html</wfw:comment><comments>http://www.blogjava.net/jb2011/archive/2026/04/20/451777.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jb2011/comments/commentRss/451777.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jb2011/services/trackbacks/451777.html</trackback:ping><description><![CDATA[<p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">网易技术团队旭风分享，有排版优化和修订。</p><h1>1、引言</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">一款社交产品的诞生，离不开即时通讯（IM）场景。随着团队业务版图在社交领域的布局，诞生了多个社交场景APP，涉及的IM场景，包含私聊、群聊、聊天室等。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>这些IM场景，在消息流的展示形式上是极为相似的，同时每个业务又有着自己特殊的交互需求。基于此，我们对IM消息流能力做了标准化的构建，来减少IM功能的业务接入成本；同时也是为了统一各个业务的技术方案，减少跨业务开发的理解和维护成本。</strong>本文主要针对iOS端在IM消息流交互层的设计上，提供一些实践思路。</p><div img-uploading-status=""  image-package-1"="" data-index="1" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="cover_opti" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172238382-1940825930.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172238382-1940825930.png" medium-zoom-image="" ls-is-cached=""  lazyloaded"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><h1>2、业界的实现方案</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">目前业界有各种即时通讯服务商提供的配套交互层解决方案，其大多以牺牲灵活性来满足快速集成需要，在定制能力上远不能胜任我们业务需要。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">再诸如 MessageKit之类的社区IM框架，其在视觉交互表现上功能完备，能帮助我们快速、灵活搭建IM消息流结构，但业务需要的是一套完整的携带消息交互能力的方案，因此对此类框架，仍需要做不小的改造才能适应我们的业务（另一参考方案：<a href="https://gitee.com/jackjiang/MobileIMSDK" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">MobileIMSDK</a>（<a href="https://www.cnblogs.com/imteck4713/p/gitee.com/jackjiang/MobileIMSDK" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">Gitee源码托管地址</a>））。</p><h1>3、我们的想法</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>对于一个IM消息流交互层方案，主要考虑几个方面：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>规范的消息流结构：提供消息流视图结构规范化的构建方式；</li><li style="list-style: inherit;"><em>2）</em>标准的消息交互能力：统一消息交互能力，业务方按需使用，快速集成；</li><li style="list-style: inherit;"><em>3）</em>业务拓展性：针对数据源、消息交互能力提供业务灵活拓展点；</li><li style="list-style: inherit;"><em>4）</em>业务接入成本：内置通用交互方案，降低业务接入成本。</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">目前，我们存量业务中的IM场景，底层IM能力主要由云信引擎提供。同时又存在基于业务服务端，通过HTTP去交互的场景。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">另外，还需要预留后期切换IM引擎的可能性，因此需要将交互层IM能力抽象出来。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">此外，为了适应团队现状，减小业务接入成本，考虑将云信提供的交互能力内置在方案中。</p><h1>4、整体设计</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>设计愿景：</strong>提供标准化的能力，同时对拓展开放。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">我们期望一套通用的IM消息流能力，能够在方案上标准化。这里的标准化，主要包含消息流结构构建的标准化，以及消息交互能力的标准化。同时，方案需要在交互能力上适应不同业务场景，因此采用依赖注入的方式，提供业务定制能力。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>按照职能划分，将框架整体分为了两层：</strong></p><div img-uploading-status=""  image-package-2"="" data-index="2" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="1" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172248072-572198573.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172248072-572198573.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;">&nbsp;</p></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>1）</em></strong>消息流结构层：负责消息流结构的构建，定义消息视图、布局、数据上的规范，提供业务层分别在「消息」、「会话」两个维度的配置能力。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>2）</em></strong>消息交互层：提供消息能力、消息流、消息数据方面的交互能力，向下依赖交互接口，内置标准交互能力的同时，也支持业务按需注入交互实现。</p><h1>5、聊天消息流的显示结构</h1><h2>5.1 消息组件</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">不同的业务场景，消息流样式表现必然有所差异。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>下面列出了我们几个业务中的消息流界面：</strong></p><div img-uploading-status=""  image-package-3"="" data-index="3" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="2" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172253118-172076407.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172253118-172076407.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">如何设计一套通用的消息流视图结构，满足不同业务需要？经过对各个业务以及一些主流IM工具的观察，将消息视图结构设计成如下结构，是能够满足我们各个IM场景需要的（见下图）。</p><div img-uploading-status=""  image-package-4"="" data-index="4" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="3" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172300245-2086082730.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172300245-2086082730.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">我将消息结构拆分成了5部分，对应5个消息组件&nbsp;&nbsp;MessageView ，每个消息组件都支持业务对其「样式」、「显隐」、「布局」进行配置，从而满足不同场景定制需要。</p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;">MessageView作为基础消息组件，提供了一些标准能力，例如是否响应菜单动作 canPerformMenuAction 、视图重用回调时机 prepareForReuse 、尺寸策略等。</p><p style="margin: 10px auto;">open&nbsp;class&nbsp;MessageView: MessageAbstractView {</p><p style="margin: 10px auto;">&nbsp;&nbsp;public&nbsp;var&nbsp;canPerformMenuAction =&nbsp;false</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;open func refresh(with&nbsp;message: Message) {}</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;open func prepareForReuse() {}</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;open&nbsp;class&nbsp;func createSizeStrategy(message: Message, fittingSize: CGSize) -&gt; MessageLayoutSizeStrategy? {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;// ...</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;}</p><p style="margin: 10px auto;">}</p></blockquote><h2>5.2 尺寸策略</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">消息组件尺寸作为消息流布局上不可或缺的要素，方案提供了多种尺寸计算策略 MessageLayoutSizeStrategy 。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>具体是：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>自动布局计算策略：业务方对消息组件使用 AutoLayout 布局时使用，内部会依据约束自动计算好组件尺寸；</li><li style="list-style: inherit;"><em>2）</em>SizeThatFit 策略：依据组件 SizeThatFit 方法返回的尺寸进行布局；</li><li style="list-style: inherit;"><em>3）</em>自定义策略：提供自定义尺寸计算方式。</li></ul><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;">public protocol MessageLayoutSizeStrategy {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;func caclulateSize(_ sizeViewType: MessageView.Type,</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;message: Message,</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fittingSize: CGSize) -&gt; CGSize</p><p style="margin: 10px auto;">}</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">public struct MessageAutoLayoutSizeStrategy: MessageLayoutSizeStrategy {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;public func caclulateSize(_ sizeViewType: MessageView.Type,</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;message: Message,</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fittingSize: CGSize) -&gt; CGSize {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;// ...省略其他代码</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;sizeView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;}</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">}</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">public struct MessageSizeThatFitsStrategy: MessageLayoutSizeStrategy {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;public func caclulateSize(_ sizeViewType: MessageView.Type,</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;message: Message,</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fittingSize: CGSize) -&gt; CGSize&nbsp; {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// ...省略其他代码</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;sizeView.sizeThatFits(fittingSize)</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;}</p><p style="margin: 10px auto;">}</p></blockquote><h2>5.3 布局快照</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">我们还针对消息组件维度支持了布局快照。通常当一个消息组件尺寸固定，在交互过程中尺寸不会发生的情况下，打开布局快照，以减少布局计算消耗。同时也提供了快照清除的能力。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">我们对多个消息流在快速滚动过程中的CPU峰值做了统计，在使用自动布局尺寸策略的情况下，开启布局快照，峰值降低了10%~20%。</p><div img-uploading-status=""  image-package-5"="" data-index="5" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="4" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172308798-1399375821.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172308798-1399375821.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><h2>5.4 交互事件</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">另外在手势交互上，对外暴露了各个消息组件的一系列交互事件。常见的场景例如单击浏览消息内容，长按展示消息菜单等。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">方案内部提供了基于系统样式的长按菜单，并提供上层菜单配置能力，同时也可以基于暴露的长按手势事件来自定义菜单。</p><h2>5.5 消息流</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">一个会话对应一个流，方案也提供了消息流在会话维度上的一些标准化配置。例如消息分页数量、是否自动拉取历史消息、是否开启增量刷新，以及在时间展示上的样式配置等。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">此外为了减少列表重绘，消息流也支持增量刷新。通常情况下业务层不需要主动刷新列表，只需对消息数据进行增删改操作，内部会触发对数据源的「diff-update」计算，从而驱动列表的增量更新。</p><div img-uploading-status=""  image-package-6"="" data-index="6" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="5" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172314511-1003407726.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172314511-1003407726.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><h1>6、聊天消息交互层</h1><h2>6.1 概述</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>对于业务方而言，在消息交互上通常关心这么几点：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>提供了哪些标准化的交互能力；</li><li style="list-style: inherit;"><em>2）</em>如何拓展自定义的交互实现；</li><li style="list-style: inherit;"><em>3）</em>如何对交互流程进行干预。</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">结合团队现状，我们在方案内部内置了基于某信的IM交互能力，同时定义了相关交互接口，供业务方按需注入实现。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">在实际业务中，一个APP内可能存在多个IM场景，因此交互能力支持按会话维度进行注入，各个会话之间的交互是相互隔离的。</p><h2>6.2 消息源</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">不同的IM场景，消息数据来源可能存在差异。例如我们私聊、群聊的数据源来自云信数据同步服务，聊天室数据需要通过云信提供的历史消息接口拉取，另外也存在诸如通过业务服务端接口来拉取消息数据的场景。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">因此方案上设置了数据源接口 SessionMessageProvider ，提供不同场景消息源的定制能力。</p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;">public protocol SessionMessageProvider {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;func messages(in&nbsp;session: Session,</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;anchorMessage: Message?,</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;limit: Int,</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;completion: @escaping ([Message]) -&gt; Void)</p><p style="margin: 10px auto;">}</p></blockquote><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">方案设置了一个负责管理消息数据源的 DataManager 实例， 其依赖 SessionMessageProvider 提供的数据源。同时内置了基于云信的数据源获取实现，能够根据当前会话类型，获取私聊、群聊、聊天室的数据源。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">如果当前场景是通过HTTP拉取消息的，则需要业务上层手动注入一个从接口获取数据源的 SessionMessageProvider 实例。</p><div img-uploading-status=""  image-package-7"="" data-index="7" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="6" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172322020-1289552147.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172322020-1289552147.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><h2>6.3 交互源</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">方案提供了IM标准交互能力，例如消息收发、消息撤回、保存等，以统一各业务交互姿势。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">具体的交互源除了要考虑目前包含的云信及业务服务端，也要适应其他交互源，因此将交互实现部分也抽象出了接口 MessageServiceInterface 。业务根据当前实际场景，注入具体的交互实现即可。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>下面列出了一些交互申明：</strong></p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;">public protocol MessageServiceInterface {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;func send(message: Message,&nbsp;in&nbsp;session: Session, completion: @escaping MessageServiceInterfaceCompletion)</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;func resend(message: Message, completion: @escaping MessageServiceInterfaceCompletion)</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;func forward(message: Message, to session: Session, completion: @escaping MessageServiceInterfaceCompletion)</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;func revoke(message: Message, completion: @escaping MessageServiceInterfaceCompletion)</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;func save(message: Message,&nbsp;in&nbsp;session: Session, completion: @escaping MessageServiceInterfaceCompletion)</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;func&nbsp;delete(message: Message, completion: @escaping MessageServiceInterfaceCompletion)</p><p style="margin: 10px auto;">}</p></blockquote><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">同样，我们也内置了一些通用交互方案，例如支持云信提供的私聊群聊交互能力，以及由中台提供的通用聊天室服务交互能力，以支持相关场景下快速接入。</p><div img-uploading-status=""  image-package-8"="" data-index="8" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="7" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172331316-289718956.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172331316-289718956.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><h2>6.4 交互钩子</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">在实际IM业务开发过程中，往往需要对交互流程做一些干预，或是在交互过程中做一些定制化的动作。因此方案也提供了一些交互钩子，支持「交互前置校验」、「交互前准备」。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>以消息发送流程为例，提供了「发送前校验」、「发送准备」两个消息发送过程的回调钩子：</strong></p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;">public protocol MessageServicePrechecker {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;// 消息发送前置校验&nbsp;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;func shouldSend(message: Message,&nbsp;in&nbsp;session: Session) -&gt; Bool</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;// ...省略其他代码</p><p style="margin: 10px auto;">}</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">public protocol MessageServicePreparation {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;/// 准备发送准备</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;func prepareSend(message: Message,&nbsp;in&nbsp;session: Session, callback: @escaping MessageServicePreparationCallback)</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;// ...省略其他代码</p><p style="margin: 10px auto;">}</p></blockquote><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>整体的发送流程如图所示：</strong></p><div img-uploading-status=""  image-package-9"="" data-index="9" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="8" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172339025-249421311.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172339025-249421311.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">前置校验阶段，用来作消息发送前的校验工作，根据实际状态决定消息是否可以发送。发送准备阶段，则可以在消息投递前做最后的准备工作，例如海外业务可以在这里处理消息资源附件上传Amazon，或是在此处对消息塞入一些客户端信息、反作弊Token等，支持异步操作。</p><h1>7、业务接入能力</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">业务只需要在上层提供针对消息以及会话两个维度的配置，就能基于内置的交互能力，构建出一套基础的IM消息流能力。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">在具体的消息样式呈现上，则通常需要业务层维护一组关于「消息类型-消息组件类型-消息结构」的映射关系。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>具体关联如下：</strong></p><div img-uploading-status=""  image-package-10"="" data-index="10" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="9" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172344348-320868519.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172344348-320868519.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">在交互能力上，提供了IM场景的标准能力，业务可以按需使用。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">另外，实际IM场景可能需要一些更为丰富的定制能力，则可以依据方案提供的消息数据源接口、消息交互接口来对具体交互实现进行定制。同时也可以使用相关的交互钩子对交互过程进行干预，以适应自己的业务。</p><h1>8、本文小结</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">本文对团队IM场景的现状做了简单介绍，撇开具体实现细节，就如何搭建一套能够适应多业务需要的通用IM消息流交互层方案，提供了一些思考和实践经验。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">从结果来看，该方案稳定支撑了团队多个IM场景，抹除各场景实现差异，有效降低了维护成本和新业务接入成本。</p><h1>9、参考资料</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[1]&nbsp;<a href="http://www.52im.net/thread-3065-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">零基础IM开发入门(一)：什么是IM聊天系统？</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[2]&nbsp;<a href="http://www.52im.net/thread-812-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">一套海量在线用户的移动端IM架构设计实践分享(含详细图文)</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[3]&nbsp;<a href="http://www.52im.net/thread-151-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">一套原创分布式即时通讯(IM)系统理论架构方案</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[4]&nbsp;<a href="http://www.52im.net/thread-2796-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">从游击队到正规军(二)：马蜂窝旅游网的IM客户端架构演进和实践总结</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[5]&nbsp;<a href="http://www.52im.net/thread-2966-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">社交软件红包技术解密(十)：手Q客户端针对2020年春节红包的技术实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[6]&nbsp;<a href="http://www.52im.net/thread-4636-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">微信团队分享：来看看微信十年前的IM消息收发架构，你做到了吗</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[7]&nbsp;<a href="http://www.52im.net/thread-4690-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">携程技术分享：亿级流量的办公IM及开放平台技术实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[8]&nbsp;<a href="http://www.52im.net/thread-4707-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">百度公共IM系统的Andriod端IM SDK组件架构设计与技术实现</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[9]&nbsp;<a href="http://www.52im.net/thread-4764-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">转转平台IM系统架构设计与实践(一)：整体架构设计</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[10]&nbsp;<a href="http://www.52im.net/thread-4812-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">一年撸完百万行代码，企业微信的全新鸿蒙NEXT客户端架构演进之路</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[11]&nbsp;<a href="http://www.52im.net/thread-4874-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">转转客服IM聊天系统背后的技术挑战和实践分享</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[12]&nbsp;<a href="http://www.52im.net/thread-4886-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">B站IM消息系统的新架构升级实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[13]&nbsp;<a href="http://www.52im.net/thread-4437-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">企业微信针对百万级组织架构的客户端性能优化实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[14]&nbsp;<a href="http://www.52im.net/thread-3631-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">企业微信的IM架构设计揭秘：消息模型、万人群、已读回执、消息撤回等</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[15]&nbsp;<a href="http://www.52im.net/thread-1470-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">从客户端的角度来谈谈移动端IM的消息可靠性和送达机制</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[16]&nbsp;<a href="http://www.52im.net/thread-1413-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">现代移动端网络短连接的优化手段总结：请求速度、弱网适应、安全保障</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[17]&nbsp;<a href="http://www.52im.net/thread-1998-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">IM消息ID技术专题(一)：微信的海量IM聊天消息序列号生成实践（算法原理篇）</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[18]&nbsp;<a href="http://www.52im.net/thread-3088-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">IM开发干货分享：有赞移动端IM的组件化SDK架构设计实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[19]&nbsp;<a href="http://www.52im.net/thread-3615-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">阿里技术分享：闲鱼IM基于Flutter的移动端跨端改造实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[20]&nbsp;<a href="http://www.52im.net/thread-3732-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">IM开发干货分享：万字长文，详解IM&#8220;消息&#8220;列表卡顿优化实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[21]&nbsp;<a href="http://www.52im.net/thread-4202-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">IM开发干货分享：IM客户端不同版本兼容运行的技术思路和实践总结</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[22]&nbsp;<a href="http://www.52im.net/thread-4359-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">百度统一socket长连接组件从0到1的技术实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[23]&nbsp;<a href="http://www.52im.net/thread-4470-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">淘宝移动端统一网络库的架构演进和弱网优化技术实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[24]&nbsp;<a href="http://www.52im.net/thread-4620-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1;">抖音技术分享：飞鸽IM桌面端基于Rust语言进行重构的技术选型和实践总结</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[25]&nbsp;<a href="http://www.52im.net/thread-4633-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">大型IM工程重构实践：企业微信Android端的重构之路</a></p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;"><strong>即时通讯技术学习：</strong></p><p style="margin: 10px auto;">- 移动端IM开发入门文章：《<a href="http://www.52im.net/thread-464-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">新手入门一篇就够：从零开发移动端IM</a>》</p><p style="margin: 10px auto;">- 开源IM框架源码：<a href="https://github.com/JackJiang2011/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">https://github.com/JackJiang2011/MobileIMSDK</a>（<a href="https://gitee.com/jackjiang/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">备用地址点此</a>）</p></blockquote><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">（<strong>本文同步发布于：</strong><a href="http://www.52im.net/thread-4905-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">http://www.52im.net/thread-4905-1-1.html</a>）</p><img src ="http://www.blogjava.net/jb2011/aggbug/451777.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jb2011/" target="_blank">Jack Jiang</a> 2026-04-20 18:43 <a href="http://www.blogjava.net/jb2011/archive/2026/04/20/451777.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>现代IM系统中聊天消息的同步和存储方案探讨</title><link>http://www.blogjava.net/jb2011/archive/2026/04/14/451773.html</link><dc:creator>Jack Jiang</dc:creator><author>Jack Jiang</author><pubDate>Tue, 14 Apr 2026 03:39:00 GMT</pubDate><guid>http://www.blogjava.net/jb2011/archive/2026/04/14/451773.html</guid><wfw:comment>http://www.blogjava.net/jb2011/comments/451773.html</wfw:comment><comments>http://www.blogjava.net/jb2011/archive/2026/04/14/451773.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jb2011/comments/commentRss/451773.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jb2011/services/trackbacks/451773.html</trackback:ping><description><![CDATA[<p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">本文作者阿里云高级技术专家木洛，有优化和修订。</p><h1>1、前言</h1><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">IM全称是&#8220;Instant Messaging&#8221;，中文名是即时通讯。在这个高度信息化的移动互联网时代，生活中IM类产品已经成为必备品，比较有名的如钉钉、微信、QQ等以IM为核心功能的产品。当然目前微信已经成长为一个生态型产品，但其核心功能还是IM。还有一些非以IM系统为核心的应用，最典型的如一些在线游戏、社交应用，IM也是其重要的功能模块。可以说，带有社交属性的应用，IM功能一定是必不可少的。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">IM系统在互联网初期即存在，其基础技术架构在这十几年的发展中更新迭代多次，从早期的CS、P2P架构，到现在后台已经演变为一个复杂的分布式系统，涉及移动端、网络、安全和存储等技术的方方面面。其支撑的规模也从早期的少量日活，到现在微信这个巨头最新公布的达到9亿的日活的体量。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>IM系统中最核心的是消息系统，消息系统最核心的是消息的同步和存储：</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>1）</em>消息的同步：</strong>将消息完整的、快速的从发送方传递到接收方，就是消息的同步。消息同步系统最重要的衡量指标就是消息传递的实时性、完整性以及能支撑的消息规模。从功能上来说，一般至少要支持在线和离线推送，高级的IM系统还支持&#8220;多端同步&#8221;；</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>2）</em>消息的存储：</strong>消息存储即消息的持久化保存，这里不是指消息在客户端本地的保存，而是指云端的保存，功能上对应的就是&#8220;消息漫游&#8221;。&#8220;消息漫游&#8221;的好处是可以实现账号在任意端登陆查看所有历史消息，这也是高级IM系统特有的功能之一。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>本文内容主要涉及IM系统中的消息系统架构，探讨一种适用于大用户量的消息同步以及存储系统的架构实现，能够支持消息系统中的高级特性&#8220;多端同步&#8221;以及&#8220;消息漫游&#8221;。在性能和规模上，能够做到全量消息云端存储，百万TPS以及毫秒级延迟的消息同步能力。</strong></p><h1>2、架构设计</h1><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">本章主要会介绍基于TableStore的现代IM消息系统的架构设计，在详细介绍架构设计之前，会先介绍一种Timeline逻辑模型，来抽象和简化对IM消息同步和存储模型的理解。理解了Timeline模型后，会介绍如何基于此模型对消息的同步以及存储进行建模。基于Timeline模型，在实现消息同步和存储时还会有各方面的技术权衡，例如如何对消息同步常见的读扩散和写扩散两种模型进行对比和选择，以及针对Timeline模型的特征如何来选择底层数据库。</p><div img-uploading-status=""  image-package-1"="" data-index="1" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="1" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260413181023972-940776420.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260413181023972-940776420.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin-top: 10px; margin-bottom: 10px;"><span style="color: #888888;">&nbsp;&#9650; 上图是消息系统传统架构与现代架构的简单对比</span></p></div></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>传统架构下，消息是先同步后存储：</strong></p><blockquote style="background-image: none; border-top: none; border-right: none; border-bottom: none; border-left-width: 3px; border-left-color: #e2dfdf; margin-right: 0px; margin-left: 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin-top: 10px; margin-bottom: 10px;">对于在线的用户，消息会直接实时同步到在线的接收方，消息同步成功后，并不会进行持久化。而对于离线的用户或者消息无法实时同步成功时，消息会持久化到离线库，当接收方重新连接后，会从离线库拉取所有未读消息。当离线库中的消息成功同步到接收方后，消息会从离线库中删除。传统的消息系统，服务端的主要工作是维护发送方和接收方的连接状态，并提供在线消息同步和离线消息缓存的能力，保证消息一定能够从发送方传递到接收方。服务端不会对消息进行持久化，所以也无法支持消息漫游。</p></blockquote><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>现代架构下，消息是先存储后同步：</strong></p><blockquote style="background-image: none; border-top: none; border-right: none; border-bottom: none; border-left-width: 3px; border-left-color: #e2dfdf; margin-right: 0px; margin-left: 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin-top: 10px; margin-bottom: 10px;">先存储后同步的好处是，如果接收方确认接收到了消息，那这条消息一定是已经在云端保存了。并且消息会有两个库来保存，一个是消息存储库，用于全量保存所有会话的消息，主要用于支持消息漫游。另一个是消息同步库，主要用于接收方的多端同步。</p><p style="margin-top: 10px; margin-bottom: 10px;">消息从发送方发出后，经过服务端转发，服务端会先将消息保存到消息存储库，后保存到消息同步库。完成消息的持久化保存后，对于在线的接收方，会直接选择在线推送。但在线推送并不是一个必须路径，只是一个更优的消息传递路径。</p><p style="margin-top: 10px; margin-bottom: 10px;">对于在线推送失败或者离线的接收方，会有另外一个统一的消息同步方式。接收方会主动的向服务端拉取所有未同步消息，但接收方何时来同步以及会在哪些端来同步消息对服务端来说是未知的，所以要求服务端必须保存所有需要同步到接收方的消息，这是消息同步库的主要作用。对于新的同步设备，会有消息漫游的需求，这是消息存储库的主要作用，在消息存储库中，可以拉取任意会话的全量历史消息。</p></blockquote><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">以上是传统架构和现代架构的一个简单的对比，现代架构上整个消息的同步和存储流程，并没有变复杂太多，但是其能实现多端同步以及消息漫游。现代架构中最核心的就是两个消息库&#8220;消息同步库&#8221;和&#8220;消息存储库&#8221;，是消息同步和存储最核心的基础。而本篇文章接下来的部分，都是围绕这两个库的设计和实现来展开。</p><h1>3、Timeline模型</h1><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">在分析&#8220;消息同步库&#8221;和&#8220;消息存储库&#8221;的设计和实现之前，在本章会先介绍一个逻辑模型-Timeline。Timeline模型会帮助我们简化对消息同步和存储模型的理解，而消息库的设计和实现也是围绕Timeline的特性和需求来展开。</p><div img-uploading-status=""  image-package-2"="" data-index="2" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="2" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260413181031449-115397958.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260413181031449-115397958.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin-top: 10px; margin-bottom: 10px;"><span style="color: #888888;">&#9650; Timeline模型</span></p></div></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>如图是Timeline模型的一个抽象表述，Timeline可以简单理解为是一个消息队列，但这个消息队列有如下特性：</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>1）</em></strong>每个消息拥有一个顺序ID（SeqId），在队列后面的消息的SeqId一定比前面的消息的SeqId大，也就是保证SeqId一定是增长的，但是不要求严格递增；</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>2）</em></strong>新的消息永远在尾部添加，保证新的消息的SeqId永远比已经存在队列中的消息都大；</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>3）</em></strong>可根据SeqId随机定位到具体的某条消息进行读取，也可以任意读取某个给定范围内的所有消息。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">有了这些特性后，消息的同步可以拿Timeline来很简单的实现。图中的例子中，消息发送方是A，消息接收方是B，同时B存在多个接收端，分别是B1、B2和B3。A向B发送消息，消息需要同步到B的多个端，待同步的消息通过一个Timeline来进行交换。A向B发送的所有消息，都会保存在这个Timeline中，B的每个接收端都是独立的从这个Timeline中拉取消息。每个接收端同步完毕后，都会在本地记录下最新同步到的消息的SeqId，即最新的一个位点，作为下次消息同步的起始位点。服务端不会保存各个端的同步状态，各个端均可以在任意时间从任意点开始拉取消息。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">消息漫游也是基于Timeline，和消息同步唯一的区别是，消息漫游要求服务端能够对Timeline内的所有数据进行持久化。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">基于Timeline，从逻辑模型上能够很简单的理解在服务端如何去实现消息同步和存储，并支持多端同步和消息漫游这些高级功能。落地到实现的难点主要在如何将逻辑模型映射到物理模型，Timeline的实现对数据库会有哪些要求？我们应该选择何种数据库去实现？这些是接下来会讨论到的问题。</p><h1>4、消息存储模型</h1><div img-uploading-status=""  image-package-3"="" data-index="3" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="3" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260413181037019-819894174.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260413181037019-819894174.png"  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin-top: 10px; margin-bottom: 10px;"><span style="color: #888888;">&#9650; 基于Timeline的消息存储模型</span></p></div></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">如图是基于Timeline的消息存储模型，消息存储要求每个会话都对应一个独立的Timeline。如图例子所示，A与B/C/D/E/F均发生了会话，每个会话对应一个独立的Timeline，每个Timeline内存有这个会话中的所有消息，服务端会对每个Timeline进行持久化。服务端能够对所有会话Timeline中的全量消息进行持久化，也就拥有了消息漫游的能力。</p><h1>5、消息同步模型</h1><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">消息同步模型会比消息存储模型稍复杂一些，消息的同步一般有读扩散和写扩散两种不同的方式，分别对应不同的Timeline物理模型。</p><div img-uploading-status=""  image-package-4"="" data-index="4" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="4" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260413181044257-470653877.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260413181044257-470653877.png"  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin-top: 10px; margin-bottom: 10px;"><span style="color: #888888;">&#9650; 读扩散和写扩散两种不同同步模式下对应的不同的Timeline模型</span></p></div></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">如图是读扩散和写扩散两种不同同步模式下对应的不同的Timeline模型，按图中的示例，A作为消息接收者，其与B/C/D/E/F发生了会话，每个会话中的新的消息都需要同步到A的某个端，看下读扩散和写扩散两种模式下消息如何做同步。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>读扩散：</strong></p><blockquote style="background-image: none; border-top: none; border-right: none; border-bottom: none; border-left-width: 3px; border-left-color: #e2dfdf; margin-right: 0px; margin-left: 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin-top: 10px; margin-bottom: 10px;">消息存储模型中，每个会话的Timeline中保存了这个会话的全量消息。读扩散的消息同步模式下，每个会话中产生的新的消息，只需要写一次到其用于存储的Timeline中，接收端从这个Timeline中拉取新的消息。</p><p style="margin-top: 10px; margin-bottom: 10px;">优点是消息只需要写一次，相比写扩散的模式，能够大大降低消息写入次数，特别是在群消息这种场景下。但其缺点也比较明显，接收端去同步消息的逻辑会相对复杂和低效。接收端需要对每个会话都拉取一次才能获取全部消息，读被大大的放大，并且会产生很多无效的读，因为并不是每个会话都会有新消息产生。</p></blockquote><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>写扩散：</strong></p><blockquote style="background-image: none; border-top: none; border-right: none; border-bottom: none; border-left-width: 3px; border-left-color: #e2dfdf; margin-right: 0px; margin-left: 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin-top: 10px; margin-bottom: 10px;">写扩散的消息同步模式，需要有一个额外的Timeline来专门用于消息同步，通常是每个接收端都会拥有一个独立的同步Timeline，用于存放需要向这个接收端同步的所有消息。</p><p style="margin-top: 10px; margin-bottom: 10px;">每个会话中的消息，会产生多次写，除了写入用于消息存储的会话Timeline，还需要写入需要同步到的接收端的同步Timeline。在个人与个人的会话中，消息会被额外写两次，除了写入这个会话的存储Timeline，还需要写入参与这个会话的两个接收者的同步Timeline。而在群这个场景下，写入会被更加的放大，如果这个群拥有N个参与者，那每条消息都需要额外的写N次。</p><p style="margin-top: 10px; margin-bottom: 10px;">写扩散同步模式的优点是，在接收端消息同步逻辑会非常简单，只需要从其同步Timeline中读取一次即可，大大降低了消息同步所需的读的压力。其缺点就是消息写入会被放大，特别是针对群这种场景。</p></blockquote><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>在IM这种应用场景下，通常会选择写扩散这种消息同步模式。</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">IM场景下，一条消息只会产生一次，但是会被读取多次，是典型的读多写少的场景，消息的读写比例大概是10:1。若使用读扩散同步模式，整个系统的读写比例会被放大到100:1。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">一个优化的好的系统，必须从设计上去平衡这种读写压力，避免读或写任意一维触碰到天花板。所以IM系统这类场景下，通常会应用写扩散这种同步模式，来平衡读和写，将100:1的读写比例平衡到30:30。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">当然写扩散这种同步模式，还需要处理一些极端场景，例如万人大群。针对这种极端写扩散的场景，会退化到使用读扩散。一个简单的IM系统，通常会在产品层面限制这种大群的存在，<strong>而对于一个高级的IM系统，会采用读写扩散混合的同步模式，来满足这类产品的需求。</strong>采用混合模式，会根据数据的不同类型和不同的读写负载，来决定用写扩散还是读扩散。</p><h1>6、典型架构设计</h1><div img-uploading-status=""  image-package-5"="" data-index="5" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="8" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202511/1834368-20251119161427966-1874426070.jpg" src="https://img2024.cnblogs.com/blog/1834368/202511/1834368-20251119161427966-1874426070.jpg"  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin-top: 10px; margin-bottom: 10px;">如上图所示，是一个典型的消息系统架构。</p></div></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>该典型的消息系统架构中包含几个重要组件：</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><em>1）</em>端：作为消息的发送和接收端，通过连接消息服务器来发送和接收消息。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><em>2）</em>消息服务器：一组无状态的服务器，可水平扩展，处理消息的发送和接收请求，连接后端消息系统。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><em>3）</em>消息队列：新写入消息的缓冲队列，消息系统的前置消息存储，用于削峰填谷以及异步消费。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><em>4）</em>消息处理：一组无状态的消费处理服务器，用于异步消费消息队列中的消息数据，处理消息的持久化和写扩散同步。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><em>5）</em>消息存储和索引库：持久化存储消息，每个会话对应一个 Timeline 进行消息存储，存储的消息建立索引来实现消息检索。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><em>6）</em>消息同步库：</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">写扩散形式同步消息，每个用户的收件箱对应一个 Timeline，同步库内消息不需要永久保存，通常对消息设定一个生命周期。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">新消息会由端发出，通常消息体中会携带消息 ID（用于去重）、逻辑时间戳（用于排序）、消息类型（控制消息、图片消息或者文本消息等）、消息体等内容。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">消息会先写入消息队列，作为底层存储的一个临时缓冲区。消息队列中的消息会由消息处理服务器消费，可以允许乱序消费。消息处理服务器对消息先存储后同步，先写入发件箱 Timeline（存储库），后写扩散至各个接收端的收件箱（同步库）。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">消息数据写入存储库后，会被近实时的构建索引，索引包括文本消息的全文索引以及多字段索引（发送方、消息类型等）。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">对于在线的设备，可以由消息服务器主动推送至在线设备端。对于离线设备，登录后会主动向服务端同步消息。每个设备会在本地保留有最新一条消息的顺序 ID，向服务端同步该顺序 ID 后的所有消息。</p><h1>7、消息库设计</h1><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">基于Timeline模型，以及Timeline模型在消息存储和消息同步的应用，我们看下消息同步库和消息存储库的设计。</p><div img-uploading-status=""  image-package-6"="" data-index="6" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="6" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260413181057300-191979039.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260413181057300-191979039.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div><div><span style="color: #888888;">&#9650; 基于Timeline的消息库设计</span></div></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>消息同步库：</strong></p><blockquote style="background-image: none; border-top: none; border-right: none; border-bottom: none; border-left-width: 3px; border-left-color: #e2dfdf; margin-right: 0px; margin-left: 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin-top: 10px; margin-bottom: 10px;">消息同步库用于存储所有用于消息同步的Timeline，每个Timeline对应一个接收端，主要用作写扩散模式的消息同步。</p><p style="margin-top: 10px; margin-bottom: 10px;">这个库不需要永久保留所有需要同步的消息，因为消息在同步到所有端后其生命周期就可以结束，就可以被回收。但是如前面所介绍的，一个实现简单的多端同步消息系统，在服务端不会保存有所有端的同步状态，而是依赖端自己主动来做同步。</p><p style="margin-top: 10px; margin-bottom: 10px;">所以服务端不知道消息何时可以回收，通常的做法是为这个库里的消息设定一个固定的生命周期，例如一周或者一个月，生命周期结束可被淘汰。</p></blockquote><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>消息存储库：</strong></p><blockquote style="background-image: none; border-top: none; border-right: none; border-bottom: none; border-left-width: 3px; border-left-color: #e2dfdf; margin-right: 0px; margin-left: 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin-top: 10px; margin-bottom: 10px;">消息存储库用于存储所有会话的Timeline，每个Timeline包含了一个会话中的所有消息。这个库主要用于消息漫游时拉取某个会话的所有历史消息，也用于读扩散模式的消息同步。</p><p style="margin-top: 10px; margin-bottom: 10px;">消息同步库和消息存储库，对数据库有不同的要求，如何对数据库做选型，在下面会讨论。</p></blockquote><h1>8、数据库选型</h1><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>消息系统最核心的两个库是消息同步库和消息存储库，两个库对数据库有不同的要求：</strong></p><div img-uploading-status=""  image-package-7"="" data-index="7" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="7" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260413181102987-1731984220.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260413181102987-1731984220.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin-top: 10px; margin-bottom: 10px;"><strong>总结下来，对数据库的要求有如下几点：</strong></p></div></div></div><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>表结构设计能够满足Timeline模型的功能要求：不要求关系模型，能够实现队列模型，并能够支持生成自增的SeqId；</li><li style="list-style: inherit;"><em>2）</em>能够支持高并发写和范围读，规模在十万级TPS；</li><li style="list-style: inherit;"><em>3）</em>能够保存海量数据，百TB级；</li><li style="list-style: inherit;"><em>4）</em>能够为数据定义生命周期。</li></ul><h1>9、本文小结</h1><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">本文主要介绍了现代IM系统中消息推送和存储架构的实现，基于逻辑的Timeline模型，我们可以很清晰明了的理解整个消息推送和存储的架构。而基于Timeline的消息存储和推送模型，其实不光可以应用在IM消息系统中，还可应用在例如Feeds流、实时消息同步、直播弹幕等场景。</p><h1>10、参考资料</h1><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[1]&nbsp;<a href="http://www.52im.net/thread-307-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">浅谈IM系统的架构设计</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[2]&nbsp;<a href="http://www.52im.net/thread-289-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">简述移动端IM开发的那些坑：架构设计、通信协议和客户端</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[3]&nbsp;<a href="http://www.52im.net/thread-812-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">一套海量在线用户的移动端IM架构设计实践分享(含详细图文)</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[4]&nbsp;<a href="http://www.52im.net/thread-151-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">一套原创分布式即时通讯(IM)系统理论架构方案</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[5]&nbsp;<a href="http://www.52im.net/thread-152-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">从零到卓越：京东客服即时通讯系统的技术架构演进历程</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[6]&nbsp;<a href="http://www.52im.net/thread-31-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">蘑菇街即时通讯/IM服务器开发之架构选择</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[7]&nbsp;<a href="http://www.52im.net/thread-158-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">腾讯QQ1.4亿在线用户的技术挑战和架构演进之路PPT</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[8]&nbsp;<a href="http://www.52im.net/thread-1221-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">移动端IM中大规模群消息的推送如何保证效率、实时性？</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[9]&nbsp;<a href="http://www.52im.net/thread-1961-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">子弹短信光鲜的背后：网易云信首席架构师分享亿级IM平台的技术实践</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[10]&nbsp;<a href="http://www.52im.net/thread-1998-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">微信技术分享：微信的海量IM聊天消息序列号生成实践（算法原理篇）</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[11]&nbsp;<a href="http://www.52im.net/thread-2015-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">一套高可用、易伸缩、高并发的IM群聊、单聊架构方案设计实践</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[12]&nbsp;<a href="http://www.52im.net/thread-2202-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">社交软件红包技术解密(一)：全面解密QQ红包技术方案&#8212;&#8212;架构、技术实现等</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[13]&nbsp;<a href="http://www.52im.net/thread-2519-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">社交软件红包技术解密(二)：解密微信摇一摇红包从0到1的技术演进</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[14]&nbsp;<a href="http://www.52im.net/thread-2675-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">从游击队到正规军(一)：马蜂窝旅游网的IM系统架构演进之路</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[15]&nbsp;<a href="http://www.52im.net/thread-2807-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">瓜子IM智能客服系统的数据架构设计（整理自现场演讲，有配套PPT）</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[16]&nbsp;<a href="http://www.52im.net/thread-2848-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">阿里钉钉技术分享：企业级IM王者&#8212;&#8212;钉钉在后端架构上的过人之处</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[17]&nbsp;<a href="http://www.52im.net/thread-3742-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">IM开发基础知识补课(十)：大型IM系统有多难？万字长文，搞懂异地多活！</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[18]&nbsp;<a href="http://www.52im.net/thread-3252-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">阿里技术分享：电商IM消息平台，在群聊、直播场景下的技术实践</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[19]&nbsp;<a href="http://www.52im.net/thread-3393-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">一套亿级用户的IM架构技术干货(上篇)：整体架构、服务拆分等</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[20]&nbsp;<a href="http://www.52im.net/thread-3472-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">从新手到专家：如何设计一套亿级消息量的分布式IM系统</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[21]&nbsp;<a href="http://www.52im.net/thread-3631-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">企业微信的IM架构设计揭秘：消息模型、万人群、已读回执、消息撤回等</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[22]&nbsp;<a href="http://www.52im.net/thread-3638-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">融云技术分享：全面揭秘亿级IM消息的可靠投递机制</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[23]&nbsp;<a href="http://www.52im.net/thread-3675-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">IM开发技术学习：揭秘微信朋友圈这种信息推流背后的系统设计</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[24]&nbsp;<a href="http://www.52im.net/thread-3699-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">阿里IM技术分享(三)：闲鱼亿级IM消息系统的架构演进之路</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[25]&nbsp;<a href="http://www.52im.net/thread-3752-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">基于实践：一套百万消息量小规模IM系统技术要点总结</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[26]&nbsp;<a href="http://www.52im.net/thread-3816-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">跟着源码学IM(十)：基于Netty，搭建高性能IM集群（含技术思路+源码）</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[27]&nbsp;<a href="http://www.52im.net/thread-3954-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">一套十万级TPS的IM综合消息系统的架构实践与思考</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[28]&nbsp;<a href="http://www.52im.net/thread-3994-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">直播系统聊天技术(八)：vivo直播系统中IM消息模块的架构实践</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[29]&nbsp;<a href="http://www.52im.net/thread-4153-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">得物从0到1自研客服IM系统的技术实践之路</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[30]&nbsp;<a href="http://www.52im.net/thread-4404-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">海量用户IM聊天室的架构设计与实践</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[31]&nbsp;<a href="http://www.52im.net/thread-4437-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">企业微信针对百万级组织架构的客户端性能优化实践</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[32]&nbsp;<a href="http://www.52im.net/thread-4564-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">一套分布式IM即时通讯系统的技术选型和架构设计</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[33]&nbsp;<a href="http://www.52im.net/thread-4627-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">陌陌技术分享：陌陌IM在后端KV缓存架构上的技术实践</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[34]&nbsp;<a href="http://www.52im.net/thread-4636-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">微信团队分享：来看看微信十年前的IM消息收发架构，你做到了吗</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[35]&nbsp;<a href="http://www.52im.net/thread-4690-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">携程技术分享：亿级流量的办公IM及开放平台技术实践</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[36]&nbsp;<a href="http://www.52im.net/thread-4764-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">转转平台IM系统架构设计与实践(一)：整体架构设计</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[37]&nbsp;<a href="http://www.52im.net/thread-4789-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">支持百万人超大群聊的Web端IM架构设计与实践</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[38]&nbsp;<a href="http://www.52im.net/thread-4812-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">一年撸完百万行代码，企业微信的全新鸿蒙NEXT客户端架构演进之路</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[39]&nbsp;<a href="http://www.52im.net/thread-4874-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">转转客服IM聊天系统背后的技术挑战和实践分享</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[40]&nbsp;<a href="http://www.52im.net/thread-4886-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">B站IM消息系统的新架构升级实践</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[41]&nbsp;<a href="http://www.52im.net/thread-4887-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">如何保障分布式IM聊天系统的消息有序性（即消息不乱）</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[42]&nbsp;<a href="http://www.52im.net/thread-464-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">新手入门一篇就够：从零开发移动端IM</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[43]&nbsp;<a href="http://www.52im.net/thread-1587-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">移动端IM开发者必读(一)：通俗易懂，理解移动网络的&#8220;弱&#8221;和&#8220;慢&#8221;</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[44]&nbsp;<a href="http://www.52im.net/thread-3065-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">零基础IM开发入门(一)：什么是IM聊天系统？</a></p><blockquote style="background-image: none; border-top: none; border-right: none; border-bottom: none; border-left-width: 3px; border-left-color: #e2dfdf; margin-right: 0px; margin-left: 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin-top: 10px; margin-bottom: 10px;"><strong>即时通讯技术学习：</strong></p><p style="margin-top: 10px; margin-bottom: 10px;">- 移动端IM开发入门文章：《<a href="http://www.52im.net/thread-464-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration-line: none;">新手入门一篇就够：从零开发移动端IM</a>》</p><p style="margin-top: 10px; margin-bottom: 10px;">- 开源IM框架源码：<a href="https://github.com/JackJiang2011/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration-line: none;">https://github.com/JackJiang2011/MobileIMSDK</a>（<a href="https://gitee.com/jackjiang/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration-line: none;">备用地址点此</a>）</p></blockquote><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">（<strong>本文内容引用自：</strong><a href="http://www.52im.net/thread-1230-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">http://www.52im.net/thread-1230-1-1.html</a>）</p><img src ="http://www.blogjava.net/jb2011/aggbug/451773.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jb2011/" target="_blank">Jack Jiang</a> 2026-04-14 11:39 <a href="http://www.blogjava.net/jb2011/archive/2026/04/14/451773.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>即时通讯安全篇（十六）：对称加密 vs 非对称加密？一文搞懂！</title><link>http://www.blogjava.net/jb2011/archive/2026/04/09/451768.html</link><dc:creator>Jack Jiang</dc:creator><author>Jack Jiang</author><pubDate>Thu, 09 Apr 2026 04:29:00 GMT</pubDate><guid>http://www.blogjava.net/jb2011/archive/2026/04/09/451768.html</guid><wfw:comment>http://www.blogjava.net/jb2011/comments/451768.html</wfw:comment><comments>http://www.blogjava.net/jb2011/archive/2026/04/09/451768.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jb2011/comments/commentRss/451768.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jb2011/services/trackbacks/451768.html</trackback:ping><description><![CDATA[<p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">本文由vivo 互联网服务器团队Deng Qian分享，有排版和内容优化。</p><h1>1、引言</h1><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>在了解加密原理前，我们来看看这样一个故事：</strong></p><blockquote style="background-image: none; border-top: none; border-right: none; border-bottom: none; border-left-width: 3px; border-left-color: #e2dfdf; margin-right: 0px; margin-left: 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin-top: 10px; margin-bottom: 10px;">小红和小明是情侣，一天，小红给小明发短信说：&#8220;亲爱的，我银行卡上没有钱了，你给我转1万块钱吧。&#8221;有过上当受骗经历的人都知道这有可能是小偷偷了小红手提包，然后拿手机发的短信。</p><p style="margin-top: 10px; margin-bottom: 10px;">不过我们小明学过加密原理，于是他回复说：&#8220;你直接拿我的银行卡刷吧，密码加上我们第一次约会的日期就是663156。&#8221;</p><p style="margin-top: 10px; margin-bottom: 10px;">很明显，只有小明和小红知道他们第一次约会是什么时候，假设是2008年4月1号，那么小红就可以根据计算663156-200841=462315得到银行卡密码，就可以消费了。</p></blockquote><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>这就是加密的本质：</strong>将信息与密钥相加得到加密后的信息，只有知道密钥的人才能解密。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>本文将以通俗案例讲解加密本质，介绍对称加密（含 AES、迪菲&#8211;赫尔曼密钥交换）与非对称加密（RSA）原理、特点及应用，并阐释其数学基础。</strong></p><div img-uploading-status=""  image-package-1"="" data-index="1" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><img alt="cover-opti" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260407164625913-1412249552.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260407164625913-1412249552.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div><h1>2、系列文章</h1><ol style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-216-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">即时通讯安全篇（一）：正确地理解和使用Android端加密算法</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-217-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">即时通讯安全篇（二）：探讨组合加密算法在IM中的应用</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-219-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">即时通讯安全篇（三）：常用加解密算法与通讯安全讲解</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-312-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">即时通讯安全篇（四）：实例分析Android中密钥硬编码的风险</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-642-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">即时通讯安全篇（五）：对称加密技术在Android平台上的应用实践</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-653-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">即时通讯安全篇（六）：非对称加密技术的原理与应用实践</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-2106-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">即时通讯安全篇（七）：用JWT技术解决IM系统Socket长连接的身份认证痛点</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-1890-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">即时通讯安全篇（八）：如果这样来理解HTTPS原理，一篇就够了</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-2866-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">即时通讯安全篇（九）：你知道，HTTPS用的是对称加密还是非对称加密？</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-3897-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">即时通讯安全篇（十）：为什么要用HTTPS？深入浅出，探密短连接的安全性</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-4015-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">即时通讯安全篇（十一）：IM聊天系统安全手段之通信连接层加密技术</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-4026-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">即时通讯安全篇（十二）：IM聊天系统安全手段之传输内容端到端加密技术</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-4552-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">即时通讯安全篇（十三）：信创必学，一文读懂什么是国密算法</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-4644-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">即时通讯安全篇（十四）：网络端口的安全防护技术实践</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-4781-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">即时通讯安全篇（十五）：详解硬编码密码的泄漏风险及其扫描原理和工具</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-4899-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">即时通讯安全篇（十六）：对称加密 vs 非对称加密？一文搞懂！</a>》（<span style="color: #ff0000;"><strong>&#9756; 本文</strong></span>）</li></ol><h1>3、什么是秘钥</h1><div img-uploading-status=""  image-package-2"="" data-index="2" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="1" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260407164638282-1077976364.jpg" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260407164638282-1077976364.jpg"  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin-top: 10px; margin-bottom: 10px;"><strong>既然加密需要密钥，那么密钥是什么呢？</strong></p></div></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">密钥是作用于加密时的一串密码，通过密钥进行信息加密，传输，到达接收者和监听者，由于接收者也有密钥，所以接收者可以根据密钥进行解密。从而防止通讯信息泄露。</p><h1>4、什么是对称加密</h1><div img-uploading-status=""  image-package-3"="" data-index="3" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="2" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260407164644792-430788659.jpg" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260407164644792-430788659.jpg" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin-top: 10px; margin-bottom: 10px;"><strong>前言讲的故事就是一个对称式加密</strong>，小明和小红都知道第一次约会的日期。所以传统的对称式加密需要通讯双方都保存同一份密钥，通过这份密钥进行加密和解密。所以对称加密也称为单密钥加密。</p></div></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>对称加密的优势在于加解密速度快，但是安全性较低</strong>，密钥一旦泄露，所有的加密信息都会被破解。同时密钥的传输和保密也成为难题。为了解决密钥传输的问题，出现通过密钥交换建立共享密钥的技术。具体如何建立共享密钥呢？我们往下看。</p><h1>5、对称加密之建立共享密匙</h1><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">在小明、小红和小偷的三人世界中，由于小明是学过加密原理的，知道迪菲&#8211;赫尔曼密钥交换（Diffie-Hellman Key Exchange），所以他知道如何建立共享密钥。</p><h2>5.1 颜料混合把戏</h2><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">接下来我们看看如何通过颜料混合把戏建立共享密钥吧。</p><blockquote style="background-image: none; border-top: none; border-right: none; border-bottom: none; border-left-width: 3px; border-left-color: #e2dfdf; margin-right: 0px; margin-left: 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin-top: 10px; margin-bottom: 10px;">假设在房间中有小明、小红和小偷三个人，每个人各自拥有相同颜色的颜料。在房间的正中间也有这些颜料。接下来，小明要和小红建立共享密钥了。</p><p style="margin-top: 10px; margin-bottom: 10px;">此时，小明对大家说：&#8220;我要用蓝色。&#8221;然后小明从自己的颜料里选择了黄色，这个黄色就是小明的私钥，小红和小偷都不知道。</p><p style="margin-top: 10px; margin-bottom: 10px;">小明将自己的私钥黄色与公钥蓝色混合后，得到了一种不能分解的颜色，我们就叫&#8220;小明-蓝色&#8221;吧（虽然大家都知道黄+蓝变绿，但是这里我们为了知道是谁的混合色，还是以名字加公钥颜色来称呼），然后小明将&#8220;小明-蓝色&#8221;公布了出来。</p><p style="margin-top: 10px; margin-bottom: 10px;">同样，小红听到了小明说用蓝色后，也选择了自己的私钥红色与公钥蓝色混合，得到了&#8220;小红-蓝色&#8221;并公布了出来。</p></blockquote><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>此时，房间中小明、小红、小偷三人都知道了几个信息：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>他们都用了蓝色；</li><li style="list-style: inherit;"><em>2）</em>小明公布了&#8220;小明-蓝色&#8221;（小红和小偷不知道是什么颜料与蓝色的混合）；</li><li style="list-style: inherit;"><em>3）</em>小红公布了&#8220;小红-蓝色&#8221;（小红和小偷不知道是什么颜料与蓝色的混合）。</li></ul><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>接下来，见证奇迹的时刻到了：</strong>小明拿到&#8220;小红-蓝色&#8221;与自己的私钥&#8220;黄色&#8221;混合，得到&#8220;小红-蓝色-小明&#8221;的新颜料。同样的，小红拿到&#8220;小明-蓝色&#8221;与自己的私钥&#8220;红色&#8221;混合，得到&#8220;小明-蓝色-小红&#8221;。大家发现了吗？&#8220;小红-蓝色-小明&#8221;和&#8220;小明-蓝色-小红&#8221;是一模一样的颜色。而小偷不知道小明和小红的私钥颜色，无法混合出与他们相同的颜色。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>至此，共享密钥建立起来了。</strong>在了解了共享密钥的建立过程后，我们将告别实体颜料，采用数字的方式来建立共享密钥。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>注：</strong>大家可能想到了，小偷可以根据自己的颜料与公钥&#8220;蓝色&#8221;混合，尝试得出&#8220;小明-蓝色&#8221;和&#8220;小红-蓝色&#8221;。这样的方法称之为穷举法，也就是尝试所有的可能性，进行信息破解，所以加密算法在理论上都是可以通过穷举法破解的，只不过实际上，超级计算机都需要计算万亿年才能穷举出所有可能性。</p><h2>5.2 乘法把戏</h2><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">首先，我们假设乘法如同颜料混合一样，是不能分解的，看看如何用乘法与数字建立共享密钥。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">小明公开了一个数字5，然后小明选择了一个私人数字4，然后利用乘法将两者混合起来，得到&#8220;小明-5&#8221;（20），接下来小红也选择了一个私人数字7得到&#8220;小红-5&#8221;（35），小明拿到35*4=140，小红拿到20*7=140。共享密钥建立完成。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">大家也发现了，小偷知道20,35,5这三个数字后，用除法就能算出小明和小红的私钥。所以，接下来我们将了解实际使用中的如何使用乘法把戏来防止私钥被计算出来的。</p><h1>6、对称加密之迪菲&#183;赫尔曼密钥交换算法</h1><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">我们都知道幂运算，但是要让计算机计算就比较难了。所以，我们会用幂运算作为建立共享密钥的乘法把戏。同时，我们还要了解钟算的原理，这里的钟可以理解成我们经常看到的时钟，我们常见的时钟最大是12，如果当前是10点，过了4个小时后，就变成了下午2点。也就是<em>(10+4)mod12=2</em>。了解了钟算和幂运算后，就开始进入正题吧。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">还是小明、小红和小偷的房间，小明声明了钟为11，幂运算的底为2，接下来小明和小红分别选择了自己的私钥4和7。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>1）</em>第一步：</strong>小明混合自己的&#8220;小明-11,2&#8221;得到，小红混合自己的&#8220;小红-11,2&#8221;得到。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>2）</em>第二步：</strong>小明拿到&#8220;小红-11,2&#8221;（7）进行计算，小红拿到&#8220;小明-11,2&#8221;（5）进行计算。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>大家注意到了吗：</strong>小明和小红建立了共享密钥3，而小偷无法根据已知的11、2、5、7这几个数字计算出密钥或小明小红的私钥。有了共享密钥后，小明和小红就可以安全进行加密传输了。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>迪菲－赫尔曼密钥交换：</strong></p><div img-uploading-status=""  image-package-4"="" data-index="4" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="3" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260407164653497-2010761922.jpg" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260407164653497-2010761922.jpg" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><h1>7、 对称加密之AES加密过程</h1></div></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">AES&nbsp;的全称是 Advanced Encryption Standard ，是最流行的对称加密算法，其加解密速度快。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">AES支持128位，192位，256位三种长度的密钥，密钥越长安全性越高。AES加密时会把明文切分成许多小块的明文，然后对每块明文单独加密，将加密后的密文传送出去，接收方再将密文切块解密，得到明文。如下图所示。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>AES加密原理：</strong></p><div img-uploading-status=""  image-package-5"="" data-index="5" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="4" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260407164701309-1650213049.jpg" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260407164701309-1650213049.jpg" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin-top: 10px; margin-bottom: 10px;">上一步中小明和小红已经协商好了密钥3。接下来就可以通过对称加密进行通信了。</p></div></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>在小明、小红和小偷的房间中，小明想把密码&#8220;462315&#8221;告诉小红，于是：</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>1）</em>第一步：</strong>将密码按照一位的长度进行切分（实际中通常按128位进行切分）；就变成了&#8220;4&#8221;&#8220;6&#8221;&#8220;2&#8221;&#8220;3&#8221;&#8220;1&#8221;&#8220;5&#8221;；</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>2）</em>第二步：</strong>对每块明文通过密钥3进行加密，结果就是&#8220;795648&#8221;，然后小明告诉小红和小偷：&#8220;我的密码是795648&#8221;；</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>3）</em>第三步：</strong>小红拿到密文后，对密文进行切块，对每块通过密钥3进行解密，就得到了正确的密码&#8220;462315&#8221;，而小偷由于不知道密钥，就无法解密出正确的信息。</p><h1>8、什么是非对称加密</h1><h2>8.1 概述</h2><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>在对称加密中，加密和解密使用的是同一份密钥。所以，在非对称加密中，加密和解密使用的是不同的密钥。</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">非对称加密中的密钥分为公钥和私钥。公钥顾名思义就是公开的，任何人都可以通过公钥进行信息加密，但是只有用户私钥的人才能完成信息解密。非对称加密带来了一个好处，避免了对称式加密需要传输和保存同一份密钥的痛苦。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">现在最流行的非对称加密算法就是RSA加密算法，具体是怎么做的呢，我们继续往下看。</p><h2>8.2 RSA加密过程</h2><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>百科是这么解释的：</strong></p><blockquote style="background-image: none; border-top: none; border-right: none; border-bottom: none; border-left-width: 3px; border-left-color: #e2dfdf; margin-right: 0px; margin-left: 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin-top: 10px; margin-bottom: 10px;">RSA加密算法是一种非对称加密算法，在公开密钥加密和电子商业中被广泛使用。RSA是由罗纳德&#183;李维斯特（Ron Rivest）、阿迪&#183;萨莫尔（Adi Shamir）和伦纳德&#183;阿德曼（Leonard Adleman）在1977年一起提出的。当时他们三人都在麻省理工学院工作。RSA 就是他们三人姓氏开头字母拼在一起组成的。</p></blockquote><div img-uploading-status=""  image-package-6"="" data-index="6" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="5" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260407164708427-640238836.jpg" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260407164708427-640238836.jpg" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin-top: 10px; margin-bottom: 10px;">&#9650; RSA算法的作者合影（照片拍摄于1978年）</p></div></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">前面我们讲了如何通过钟算和幂函数建立不可逆（计算机可以通过穷举法计算出私钥，实际场景中就算是超级计算机也要计算几万亿年之久）的共享密钥。由于小红是小明的女朋友，小明天天在小红面前给她讲RSA加密算法的原理，所以小红也知道怎么得出自己的公钥和私钥。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>接下来我们一起跟着小红的脚步，看看RSA加密的公钥和私钥是怎么计算出来的：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>第一步：小红选择了两个很大的质数p和q，这里为了便于计算，选择2和11;</li><li style="list-style: inherit;"><em>2）</em>第二步：计算p和q的乘积n=p*q=2*11=22；</li><li style="list-style: inherit;"><em>3）</em>第三部：计算n的欧拉函数&#966;(n)=(p-1)*(q-1)=10；</li><li style="list-style: inherit;"><em>4）</em>第四步：选择一个小于&#966;(n)且与&#966;(n)互质的整数e，{1,3,7,9}，这里选择e=7；</li><li style="list-style: inherit;"><em>5）</em>第五步：计算e对于&#966;(n)的模反元素（ed mode &#966;(n) = 1）d，d=3。</li></ul><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">到这里小红就得到了他自己的公钥(n,e)和私钥(n,d)。其中n就是钟大小，e和d就是幂函数的幂。接下来就通过计算出来的公钥和私钥进行数据的加解密。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">还是小明、小红和小偷三个人，小红对大家说，我的公钥是（22,7），小明知道了小红的公钥后，想讲自己的信息&#8220;14&#8221;告诉小红，于是就用小红公开的公钥进行加密。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>具体步骤如下：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>第一步：小明根据要加密的信息14进行计算，得到加密后的信息20，然后将20告诉小红和小偷；</li><li style="list-style: inherit;"><em>2）</em>第二步：小红有自己的私钥，将加密信息20进行解密，，得到了小明想传递给小红的信息。而小偷呢，知道22,7,20，但是不知道小红的密钥（22,3），无法解密出正确的信息。</li></ul><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>RSA加密算法在数字签名中也发挥着巨大的作用：</strong>假设小偷可以假冒小红，说小红的公钥是（22,9），而小明不知道是小偷假扮的，按照小偷的公钥加密后，结果被小偷解密了。数字签名的作用就是防止信息被篡改，小红说她的公钥是（22,7）的同时，使用私钥给这段信息（通常使用MD5值计算签名）加上签名，小明得到公钥（22,7）和签名13，小明拿到签名后利用公钥计算出信息是否被篡改。</p><div img-uploading-status=""  image-package-7"="" data-index="7" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="6" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260407164715242-1848354139.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260407164715242-1848354139.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><h1>9、加密的实际作用</h1></div></div></div><div img-uploading-status=""  image-package-8"="" data-index="8" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="7" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260407164720866-1967554200.jpg" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260407164720866-1967554200.jpg" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin-top: 10px; margin-bottom: 10px;">本文使用的很小的数来进行加密原理的讲解，为了是读者可以方便进行计算。</p></div></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">在实际使用中（n,e）都是特别大的数，其中n的长度都在768以上，1024长度被认为是基本安全的。</p><blockquote style="background-image: none; border-top: none; border-right: none; border-bottom: none; border-left-width: 3px; border-left-color: #e2dfdf; margin-right: 0px; margin-left: 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin-top: 10px; margin-bottom: 10px;">（1230186684530117755130494958384962720772853569595334792197322452151726400507263657518745202199786469389956474942774063845925192557326303453731548268507917026122142913461670429214311602221240479274737794080665351419597459856902143413=</p><p style="margin-top: 10px; margin-bottom: 10px;">33478071698956898786044169848212690817704794983713768568912431388982883793878002287614711652531743087737814467999489</p><p style="margin-top: 10px; margin-bottom: 10px;">&#215;</p><p style="margin-top: 10px; margin-bottom: 10px;">36746043666799590428244633799627952632279158164343087642676032283815739666511279233373417143396810270092798736308917）</p></blockquote><h1>10、写在最后</h1><div img-uploading-status=""  image-package-9"="" data-index="9" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="8" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260407164726898-1771510767.jpg" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260407164726898-1771510767.jpg" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin-top: 10px; margin-bottom: 10px;"><strong>或许看到这里，大家心里还有许多疑惑：</strong></p></div></div></div><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>为什么小明和小红建立共享密钥时，通过几次幂运算和钟算就能得到一样的共享密钥？</li><li style="list-style: inherit;"><em>2）</em>为什么RSA加密算法要用两个质数？</li><li style="list-style: inherit;"><em>3）</em>为什么通过公钥加密的信息可以通过私钥解开？</li></ul><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">加密算法的背后，是一道道迷人的数学难题。<strong>而RSA加密算法之所以被广泛运用，是因为一个名为整数分解的古老数学问题，你可以轻易找到两个很大的质数相乘得到一个结果n，但是要将这个结果n分解回两个质数就变得极其困难</strong>。尽管这个所谓的&#8220;整数分解&#8221;问题被研究了数个世纪，还没人能找到一个足够高效的通用方法解决它，并对标准RSA钟大小造成危害。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">数学史中充满了未解决的问题，尽管这些迷人的问题缺乏任何实际应用，却单靠其美学特质就吸引了数学家进行深入探究。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">令人颇感惊讶的是，许多这类迷人但显然无用的问题后来都有了很大的实用价值，这一价值只有在问题被研究数个世纪后才得以破解。整数分解这一问题由来已久。对其最早的严肃研究似乎是在17世纪，由数学家费马（Fermat）和梅森（Mersenne）进行的。欧拉（Euler）和高斯（Gauss）两位数学&#8220;泰斗&#8221;也在接下来的世纪里对这一问题做出了贡献。但直到公钥加密于20世纪70年代被发明，分解大数字的困难才成为一个实际应用的关键。</p><h1>11、本文小结</h1><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">最后总结一下。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>首先：</strong>我们通过一个诈骗短信的例子，引出了加密的原理就是信息+密钥，密钥就是对信息进行加解密的一串数字。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>然后：</strong>通过颜料混合把戏形象的演示了如何建立共享密钥。在使用乘法建立共享密钥的过程中，学习了钟算和幂运算，接着我们了解了RSA加密算法的过程，通过两个质数生成公钥和私钥。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>最后：</strong>我们根据公钥进行信息加密，再通过私钥完成信息解密。</p><h1>12、参考资料</h1><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[1]&nbsp;<a href="http://www.52im.net/thread-217-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">探讨组合加密算法在IM中的应用</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[2]&nbsp;<a href="http://www.52im.net/thread-219-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">一文读懂常用加解密算法与网络通讯安全</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[3]&nbsp;<a href="http://www.52im.net/thread-653-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">非对称加密技术的原理与应用实践</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[4]&nbsp;<a href="http://www.52im.net/thread-2866-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">你知道，HTTPS用的是对称加密还是非对称加密？</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[5]&nbsp;<a href="http://www.52im.net/thread-4015-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">IM聊天系统安全手段之通信连接层加密技术</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[6]&nbsp;<a href="http://www.52im.net/thread-4026-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">IM聊天系统安全手段之传输内容端到端加密技术</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[7]&nbsp;<a href="http://www.52im.net/thread-4552-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">信创必学，一文读懂什么是国密算法的</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[8]&nbsp;<a href="http://www.52im.net/thread-327-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">传输层安全协议SSL/TLS的Java平台实现简介和Demo演示</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[9]&nbsp;<a href="http://www.52im.net/thread-283-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">理论联系实际：一套典型的IM通信协议设计详解（含安全层设计）</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[10]&nbsp;<a href="http://www.52im.net/thread-310-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">微信新一代通信安全解决方案：基于TLS1.3的MMTLS详解</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[11]&nbsp;<a href="http://www.52im.net/thread-215-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">来自阿里OpenIM：打造安全可靠即时通讯服务的技术实践分享</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[12]&nbsp;<a href="http://www.52im.net/thread-763-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">简述实时音视频聊天中端到端加密（E2EE）的工作原理</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[13]&nbsp;<a href="http://www.52im.net/thread-764-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">移动端安全通信的利器&#8212;&#8212;端到端加密（E2EE）技术详解</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[14]&nbsp;<a href="http://www.52im.net/thread-970-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">通俗易懂：一篇掌握即时通讯的消息传输安全原理</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[15]&nbsp;<a href="http://www.52im.net/thread-2027-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">一分钟理解 HTTPS 到底解决了什么问题</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[16]&nbsp;<a href="http://www.52im.net/thread-2446-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">一篇读懂HTTPS：加密原理、安全逻辑、数字证书等</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[17]&nbsp;<a href="http://www.52im.net/thread-4104-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">基于Netty的IM聊天加密技术学习：一文理清常见的加密概念、术语等</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[18]&nbsp;<a href="http://www.52im.net/thread-4142-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">手把手教你为基于Netty的IM生成自签名SSL/TLS证书</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[19]&nbsp;<a href="http://www.52im.net/thread-4783-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">即时通讯初学者必知必会的20个网络编程和通信安全知识点</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[20]&nbsp;<a href="http://www.52im.net/thread-4792-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">零基础IM开发入门(五)：什么是IM系统的端到端加密？</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[21]&nbsp;<a href="http://www.52im.net/thread-4636-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">微信团队分享：来看看微信十年前的IM消息收发架构，你做到了吗</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[22]&nbsp;<a href="http://www.52im.net/thread-3065-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">零基础IM开发入门(一)：什么是IM系统？</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[23]&nbsp;<a href="http://www.52im.net/thread-3393-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">一套亿级用户的IM架构技术干货(上篇)：整体架构、服务拆分等</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[24]&nbsp;<a href="http://www.52im.net/thread-4764-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">转转平台IM系统架构设计与实践(一)：整体架构设计</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[25]&nbsp;<a href="http://www.52im.net/thread-4564-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">一套分布式IM即时通讯系统的技术选型和架构设计</a></p><blockquote style="background-image: none; border-top: none; border-right: none; border-bottom: none; border-left-width: 3px; border-left-color: #e2dfdf; margin-right: 0px; margin-left: 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin-top: 10px; margin-bottom: 10px;"><strong>即时通讯技术学习：</strong></p><p style="margin-top: 10px; margin-bottom: 10px;">- 移动端IM开发入门文章：《<a href="http://www.52im.net/thread-464-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration-line: none;">新手入门一篇就够：从零开发移动端IM</a>》</p><p style="margin-top: 10px; margin-bottom: 10px;">- 开源IM框架源码：<a href="https://github.com/JackJiang2011/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration-line: none;">https://github.com/JackJiang2011/MobileIMSDK</a>（<a href="https://gitee.com/jackjiang/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration-line: none;">备用地址点此</a>）</p></blockquote><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">（<strong>本文已同步发布于：</strong><a href="http://www.52im.net/thread-4899-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration-line: none;">http://www.52im.net/thread-4899-1-1.html</a>）</p><img src ="http://www.blogjava.net/jb2011/aggbug/451768.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jb2011/" target="_blank">Jack Jiang</a> 2026-04-09 12:29 <a href="http://www.blogjava.net/jb2011/archive/2026/04/09/451768.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>网络编程入门如此简单(五)：UDP跟TCP相比，到底差了什么？</title><link>http://www.blogjava.net/jb2011/archive/2026/03/17/451762.html</link><dc:creator>Jack Jiang</dc:creator><author>Jack Jiang</author><pubDate>Tue, 17 Mar 2026 07:35:00 GMT</pubDate><guid>http://www.blogjava.net/jb2011/archive/2026/03/17/451762.html</guid><wfw:comment>http://www.blogjava.net/jb2011/comments/451762.html</wfw:comment><comments>http://www.blogjava.net/jb2011/archive/2026/03/17/451762.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jb2011/comments/commentRss/451762.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jb2011/services/trackbacks/451762.html</trackback:ping><description><![CDATA[<p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">本文由悟空聊架构分享，有修订和排版优化。</p><h1>1、引言</h1><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>本文将通俗易懂地为你类比解释UDP与TCP的核心差异，包括如何基于UDP实现TCP的可靠传输：通过模拟三次握手、添加序列号与确认机制解决顺序和丢包问题，利用滑动窗口控制流量，并引入拥塞控制算法来动态调整发送速率等。</strong></p><div img-uploading-status=""  image-package-1"="" data-index="1" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img src="https://img2024.cnblogs.com/blog/1834368/202603/1834368-20260316161230715-1577906690.jpg" alt="cover-opti" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><h1>2、系列文章</h1><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>本文是该系列文章中的第&nbsp;<em>5</em>&nbsp;篇：</strong></p><ol style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-3330-1-1.html" target="_blank" style="color: #1d58d1; text-decoration-line: none;">网络编程入门如此简单(一)：假如你来设计网络，会怎么做？</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-3339-1-1.html" target="_blank" style="color: #1d58d1; text-decoration-line: none;">网络编程入门如此简单(二)：假如你来设计TCP协议，会怎么做？</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-3868-1-1.html" target="_blank" style="color: #1d58d1; text-decoration-line: none;">网络编程入门如此简单(三)：什么是IPv6？漫画式图文，一篇即懂！</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-4702-1-1.html" target="_blank" style="color: #1d58d1; text-decoration-line: none;">网络编程入门如此简单(四)：一文搞懂localhost和127.0.0.1</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-4897-1-1.html" target="_blank" style="color: #1d58d1; text-decoration-line: none;">网络编程入门如此简单(五)：UDP跟TCP相比，到底差了什么？</a>》（<span style="color: #ff6600;"><strong>* 本文</strong></span>）</li></ol><h1>3、写在前面</h1><div img-uploading-status=""  image-package-2"="" data-index="2" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img src="https://img2024.cnblogs.com/blog/1834368/202603/1834368-20260316161239768-68924467.jpg" alt="1、背景" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">本题是我在面试中，技术总监问我的一道真题，当时答得不太好，所以把它揪出来总结了下。后来问了下总监，总监说这是阿里的面试题。。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">其实面试官主要是想让我说出 UDP 和 TCP 的原理上的区别，怎么给 UDP 加些功能实现 TCP。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">看好去很容易就能说出一两个 TCP 和 UDP 的区别，<strong>但如果能用女朋友都能听懂的方式该怎么说呢？</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>女朋友：</strong>我不想听课本上讲的！我听不懂呀~</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">下面我会以大白话的方式来解答上面的问题。</p><h1>4、UDP协议的主要特点</h1><div img-uploading-status=""  image-package-3"="" data-index="3" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img src="https://img2024.cnblogs.com/blog/1834368/202603/1834368-20260316161247440-1253798853.jpg" alt="2、UDP-的特点" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin-top: 10px; margin-bottom: 10px;">UDP 让我想起了刚毕业参加工作那会，一名毕业菜鸟。</p></div></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>1）沟通简单：</em></strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">领导安排的任务，直接干就完了。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">UDP 也是，相信网络世界永远是美好的，我发送的包是很容易送到的，接收方也是很容易组装的。数据结构也很简单，不需要大量的数据结构、处理逻辑、包头字段。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>2）轻信他人：</em></strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">测试人员报的 bug 我也不会和她争论什么，永远相信测试人员是对的，测试人员说啥就是啥，我改就是。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">UDP 也是，不会建立连接，有个端口号，谁都可以监听这个端口号往上面发数据。也可以从这个端口号传给任何人数据。反正我只管发就是。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>3）不会讨价还价：</em></strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">产品经理昨天说手机壳需要根据心情变色，测试人员说这个 bug 要把关联的两个 bug 一起修掉。那就按照他们说的做吧！</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">UDP 也是，不懂坚持和退让。也就是根据网络情况进行拥塞控制。无论网络丢包多严重，我还是照样发~</p><h1>5、UDP协议的使用场景</h1><div img-uploading-status=""  image-package-4"="" data-index="4" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img src="https://img2024.cnblogs.com/blog/1834368/202603/1834368-20260316161304900-958505871.jpg" alt="3、UDP-使用场景" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin-top: 10px; margin-bottom: 10px;">针对像我那时候毕业菜鸟的情况，领导给我安排了三种工作环境让我选。</p></div></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>1）</em></strong>内部系统，任务简单，模块单一，不需要考虑代码的关联影响，即使失败了也没有关系。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">UDP 也是，需要资源少，网络情况比较好的内网，或者对于丢包不敏感的应用。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>2）</em></strong>有一个强力的团队支持，都是中高级开发、测试人员，团队成员打过很多年交道，互相信任。有什么问题，吼一嗓子就可以了！</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">UDP 也是，不需要一对一沟通来建立连接，可以广播的应用。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>3）</em></strong>一个新项目，需要有激情，对于刚毕业的菜鸟，都是有很强的自主能动性的，也不会耍滑头，躲在厕所玩手机，带薪拉shi ？即使项目不忙，我也抓紧时间干。项目忙，还是一样干！</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">UDP 也是，猛着发包就是，主要应用在需要处理速度快，时延低，可以容忍少数丢包的情况。即使网络情况不佳，发包就是~</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>针对上面的三大场景，UDP 常用在实时竞技游戏，IoT 物联网，移动通信领域。</strong></p><h1>6、TCP协议的主要特点&nbsp;&nbsp;</h1><div img-uploading-status=""  image-package-5"="" data-index="5" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img src="https://img2024.cnblogs.com/blog/1834368/202603/1834368-20260316161311366-169133040.jpg" alt="4、TCP-的特点？" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><h2>6.1 面向连接</h2><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">TCP 和 UDP 是传输层里面比较重要的两个协议。大部分面试的时候都会问到两者的区别。而大部分都会两句，比如 TCP 是面向连接的，UDP 是面向无连接。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>那什么是面向连接？</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">TCP 三次握手是我们常常念叨和背诵的。而在这三次握手成功后，就是建立连接成功。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>那什么又叫面向呢？</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">我们也常听到面向对象编程、面向切面编程、面向服务编程。那到底什么是面向？</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">在我看来 面向 就是遵循一定的协议、规范、数据结构等来做一系列事情。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">比如面向连接，就是为了在客户端和服务端维护连接，而建立一定的数据结构来维护双方交互的状态，用这样的数据来保证所谓的面向连接的特性。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">知道了 TCP 的是用三次握手来建立连接，那我们是否可以让 UDP 也发三个包来模拟 TCP 建立连接？可以是可以，但是如果只是建立，而不是面向连接，其实意义不大。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>那 TCP 面向连接做了哪些事情？</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">TCP 提供可靠交付，通过 TCP 连接传输的数据，可以无差错、不丢失、不重复、并且按序到达。而 UDP 继承了 IP 包的特性，不保证不丢失，不保证按顺序到达。</p><h2>6.2 面向字节流</h2><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">TCP 是面向字节流，所谓字节流，就是发的是一个流，没头没尾。TCP 自己维护流状态。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">UDP 基于 IP 数据报，一个一个地发，一个一个地收。</p><h3>6.3 拥塞控制</h3><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">TCP 拥有拥塞控制，如果包丢弃了或者网络环境不好了，就会根据网络情况自行控制自己的行为，看下是发快点还是发慢点。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">UDP 则没有这么智能了， 你让我发，我就发呗，反正是你让我发的，其他的一概不管~</p><h2>6.4 有状态服务</h2><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">TCP 是一个有状态的服务，有状态可以理解为：我记录了哪些发送了，哪些没有发送，哪些接收到了，哪些没接收到，应该接收哪个了，一点差错都不行。TCP 干的事情可真多！</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">而 UDP 则不是有状态的服务，我只管发，其他的就交给接收端吧，有点任性是吧？</p><h1>7、如何让UDP追上TCP的能力？</h1><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">建立连接上面已经讲到了，三次握手和四次握手，UDP 也可以模拟去做。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>那下面还有几个问题：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>顺序问题；</li><li style="list-style: inherit;"><em>2）</em>丢包问题；</li><li style="list-style: inherit;"><em>3）</em>流量控制；</li><li style="list-style: inherit;"><em>4）</em>拥塞控制。</li></ul><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>TCP 的数据结构长这样：</strong></p><div img-uploading-status=""  image-package-6"="" data-index="6" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img src="https://img2024.cnblogs.com/blog/1834368/202603/1834368-20260316161320459-1316881742.png" alt="5" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin-top: 10px; margin-bottom: 10px;">其实如果你能把这些结构讲清楚，就已经理解了 TCP 的核心功能。下面我还是用大白话的方式来讲解上面的四个问题。</p></div></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">顺序问题和丢包问题可以利用确认与重发的机制。假如包收到了，可以做一个确认，发送一个 ACK 给发送端，告诉他我收到了。假如有的包提前到了，就缓存着。假如有包丢失了，就可以超时重试。超时重试不宜过短，时间必须大于往返时间 RTT，否则会引起不必要的重传。也不宜过长，如果超时时间过长，访问就变慢了。那怎么确定这个时间，可以通过采样 RTT 的时间，进行加权平均。还需要根据网络状况，动态变化。可以了解下自适应重传算法。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">流量控制就是根据网络情况调整发包的速率。利用的是滑动窗口。在对于包的确认中，同时会携带一个窗口的大小，只要利用好这个窗口大小，就能很好地调整发包速率，发的报文段不要超过窗口的大小就 OK。</p><div img-uploading-status=""  image-package-7"="" data-index="7" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img src="https://img2024.cnblogs.com/blog/1834368/202603/1834368-20260316161325818-140795623.png" alt="6" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin-top: 10px; margin-bottom: 10px;">拥塞控制主要用来避免包丢失和超时重传，如果出现了这两种现象，就说明发的速率太快了。那最开始怎么知道发送速率呢？其实开始时只发送一个报文段数据，如果收到一个确认，则倍增报文段，依次类推。当发现超时重传时，就又回到只发送一个报文段的情况，这个就是慢启动，这种方式不合适。其实还有一种快速重传算法，简单来说就是拥塞窗口减半，后续线性增速。针对于算法怎么实现的，这里就不展开讲述了。</p></div></div></div><div img-uploading-status=""  image-package-8"="" data-index="8" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img src="https://img2024.cnblogs.com/blog/1834368/202603/1834368-20260316161331492-684083785.png" alt="7" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin-top: 10px; margin-bottom: 10px;">至此，我用大白话的方式讲解了 UDP 和 TCP 的区别，以及 UDP 缺什么功能，需要怎么去弥补才能实现 TCP 的功能。相信这样回答的思路可以让面试官觉得还是有点东西的。</p></div></div></div><h1>8、参考资料</h1><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[1]&nbsp;<a href="http://www.52im.net/topic-tcpipvol1.html" target="_blank" style="color: #1d58d1; text-decoration-line: none;">TCP/IP详解</a>&nbsp;-&nbsp;<a href="http://docs.52im.net/extend/docs/book/tcpip/vol1/11/" target="_blank" style="color: #1d58d1; text-decoration-line: none;">第11章&#183;UDP：用户数据报协议</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[2]&nbsp;<a href="http://www.52im.net/topic-tcpipvol1.html" target="_blank" style="color: #1d58d1; text-decoration-line: none;">TCP/IP详解</a>&nbsp;-&nbsp;<a href="http://docs.52im.net/extend/docs/book/tcpip/vol1/17/" target="_blank" style="color: #1d58d1; text-decoration-line: none;">第17章&#183;TCP：传输控制协议</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[3]&nbsp;<a href="http://www.52im.net/thread-513-1-1.html" target="_blank" style="color: #1d58d1; text-decoration-line: none;">通俗易懂-深入理解TCP协议（上）：理论基础</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[4]&nbsp;<a href="http://www.52im.net/thread-515-1-1.html" target="_blank" style="color: #1d58d1; text-decoration-line: none;">通俗易懂-深入理解TCP协议（下）：RTT、滑动窗口、拥塞处理</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[5]&nbsp;<a href="http://www.52im.net/thread-1107-1-1.html" target="_blank" style="color: #1d58d1; text-decoration-line: none;">快速理解TCP协议一篇就够</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[6]&nbsp;<a href="http://www.52im.net/thread-1160-1-1.html" target="_blank" style="color: #1d58d1; text-decoration-line: none;">快速理解TCP和UDP的差异</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[7]&nbsp;<a href="http://www.52im.net/thread-1277-1-1.html" target="_blank" style="color: #1d58d1; text-decoration-line: none;">快速理解为什么说UDP有时比TCP更有优势</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[8]&nbsp;<a href="http://www.52im.net/thread-3793-1-1.html" target="_blank" style="color: #1d58d1; text-decoration-line: none;">一泡尿的时间，快速搞懂TCP和UDP的区别</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[9]&nbsp;<a href="http://www.52im.net/thread-1729-1-1.html" target="_blank" style="color: #1d58d1; text-decoration-line: none;">跟着动画来学TCP三次握手和四次挥手</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[10]&nbsp;<a href="http://www.52im.net/thread-3330-1-1.html" target="_blank" style="color: #1d58d1; text-decoration-line: none;">假如你来设计网络，会怎么做？</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[11]&nbsp;<a href="http://www.52im.net/thread-3339-1-1.html" target="_blank" style="color: #1d58d1; text-decoration-line: none;">假如你来设计TCP协议，会怎么做？</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[12]&nbsp;<a href="http://www.52im.net/thread-1024-1-1.html" target="_blank" style="color: #1d58d1; text-decoration-line: none;">深入地理解UDP协议并用好它</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[13]&nbsp;<a href="http://www.52im.net/thread-1293-1-1.html" target="_blank" style="color: #1d58d1; text-decoration-line: none;">如何让不可靠的UDP变的可靠？</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[14]&nbsp;<a href="http://www.52im.net/thread-4724-1-1.html" target="_blank" style="color: #1d58d1; text-decoration-line: none;">UDP比TCP高效？还真不一定！</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[15]&nbsp;<a href="http://www.52im.net/thread-4868-1-1.html" target="_blank" style="color: #1d58d1; text-decoration-line: none;">可靠传输的TCP协议send成功就意味着数据一定发出去了？</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[16]&nbsp;<a href="http://www.52im.net/thread-281-1-1.html" target="_blank" style="color: #1d58d1; text-decoration-line: none;">为何基于TCP协议的移动端IM仍然需要心跳保活机制？</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[17]&nbsp;<a href="http://www.52im.net/thread-1309-1-1.html" target="_blank" style="color: #1d58d1; text-decoration-line: none;">技术扫盲：新一代基于UDP的低延时网络传输层协议&#8212;&#8212;QUIC详解</a></p><blockquote style="background-image: none; border-top: none; border-right: none; border-bottom: none; border-left-width: 3px; border-left-color: #e2dfdf; margin-right: 0px; margin-left: 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin-top: 10px; margin-bottom: 10px;"><strong>即时通讯技术学习：</strong></p><p style="margin-top: 10px; margin-bottom: 10px;">- 移动端IM开发入门文章：《<a href="http://www.52im.net/thread-464-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration-line: none;">新手入门一篇就够：从零开发移动端IM</a>》</p><p style="margin-top: 10px; margin-bottom: 10px;">- 开源IM框架源码：<a href="https://github.com/JackJiang2011/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration-line: none;">https://github.com/JackJiang2011/MobileIMSDK</a>（<a href="https://gitee.com/jackjiang/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration-line: none;">备用地址点此</a>）</p></blockquote><p style="margin-top: 10px; margin-bottom: 10px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">（<strong>本文已同步发布于：</strong><a href="http://www.52im.net/thread-4897-1-1.html" target="_blank" style="color: #1d58d1; text-decoration-line: none;">http://www.52im.net/thread-4897-1-1.html</a>）</p><img src ="http://www.blogjava.net/jb2011/aggbug/451762.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jb2011/" target="_blank">Jack Jiang</a> 2026-03-17 15:35 <a href="http://www.blogjava.net/jb2011/archive/2026/03/17/451762.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>