石墨文檔Websocket百萬長連接技術實踐

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Web 服務端推送技術經過了長輪詢、短輪詢的發展,最終到 HTML5 標準帶來的 WebSocket 規範逐步成爲了目前業內主流技術方案。它使得消息推送、消息通知等功能的實現變得異常簡單,那麼百萬級別連接下的 Websocket 網關該如何實踐呢?本文整理自石墨文檔資深工程師杜旻翔在重構石墨websocket網關的技術實踐。"}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"1 引言"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在石墨文檔的部分業務中,例如文檔分享、評論、幻燈片演示和文檔表格跟隨等場景,涉及到多客戶端數據同步和服務端批量數據推送的需求,一般的 HTTP 協議無法滿足服務端主動 Push 數據的場景,因此選擇採用 WebSocket 方案進行業務開發。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"隨着石墨文檔業務發展,目前日連接峯值已達百萬量級,日益增長的用戶連接數和不符合目前量級的架構設計導致了內存和 CPU 使用量急劇增長,因此我們考慮對網關進行重構。"}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"2 網關 1.0"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"網關 1.0 是使用 Node.js 基於 Socket.IO 進行修改開發的版本,很好的滿足了當時用戶量級下的業務場景需求。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2.1 架構"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"網關 1.0 版本架構設計圖:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/29\/29ecb547e8148d8b00938726ffa28b81.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"網關 1.0 客戶端連接流程:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"用戶通過 NGINX 連接網關,該操作被業務服務感知;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"業務服務感知到用戶連接後,會進行相關用戶數據查詢,再將消息 Pub 到 Redis;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"網關服務通過 Redis Sub 收到消息;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"查詢網關集羣中的用戶會話數據,向客戶端進行消息推送。"}]}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2.2 痛點"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"雖然 1.0 版本的網關在線上運行良好,但是不能很好的支持後續業務的擴展,並且有以下幾個問題需要解決:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"資源消耗:Nginx 僅使用證書,大部分請求被透傳,產生了一定的資源浪費,同時之前的 Node 網關性能不好,消耗大量的 CPU、內存。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"維護與觀測:未接入石墨的監控體系,無法和現有監控告警聯通,維護上存在一定的困難;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"業務耦合問題:業務服務與網關功能被集成到了同一個服務中,無法針對業務部分性能損耗進行鍼對性水平擴容,爲了解決性能問題,以及後續的模塊擴展能力,都需要進行服務解耦。"}]}]}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"3 網關 2.0"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"網關 2.0 需要解決很多問題:石墨文檔內部有很多組件:文檔、表格、幻燈片和表單等等。在 1.0 版本中組件對網關的業務調用可以通過:Redis、Kafka 和 HTTP 接口,來源不可查,管控困難。此外,從性能優化的角度考慮也需要對原有服務進行解耦合,將 1.0 版本網關拆分爲網關功能部分和業務處理部分,網關功能部分爲 WS-Gateway:集成用戶鑑權、TLS 證書驗證和 WebSocket 連接管理等;業務處理部分爲 WS-API:組件服務直接與該服務進行 gRPC 通信。可針對具體的模塊進行鍼對性擴容;服務重構加上 Nginx 移除,整體硬件消耗顯著降低;服務整合到石墨監控體系。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3.1 整體架構"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"網關 2.0 版本架構設計圖:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/4d\/4d9f02fdb5d3f82329a2208e3e823400.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"網關 2.0 客戶端連接流程:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"客戶端與 WS-Gateway 服務通過握手流程建立 WebSocket 連接;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"連接建立成功後,WS-Gateway 服務將會話進行節點存儲,將連接信息映射關係緩存到 Redis 中,並通過 Kafka 向 WS-API 推送客戶端上線消息;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"WS-API 通過 Kafka 接收客戶端上線消息及客戶端上行消息;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"WS-API 服務預處理及組裝消息,包括從 Redis 獲取消息推送的必要數據,並進行完成消息推送的過濾邏輯,然後 Pub 消息到 Kafka;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"WS-Gateway 通過 Sub Kafka 來獲取服務端需要返回的消息,逐個推送消息至客戶端。"}]}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3.2 握手流程"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"網絡狀態良好的情況下,完成如下圖所示步驟 1 到步驟 6 之後,直接進入 WebSocket 流程;網絡環境較差的情況下,WebSocket 的通信模式會退化成 HTTP 方式,客戶端通過 POST 方式推送消息到服務端,再通過 GET 長輪詢的方式從讀取服務端返回數據。客戶端初次請求服務端連接建立的握手流程:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/b7\/b7f2855975aa21eb91030197925db441.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"Client 發送 GET 請求嘗試建立連接;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"Server 返回相關連接數據,sid 爲本次連接產生的唯一 Socket ID,後續交互作爲憑證;"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"{\"sid\":\"xxx\",\"upgrades\":[\"websocket\"],\"pingInterval\":xxx,\"pingTimeout\":xxx}"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"Client 攜帶步驟 2 中的 sid 參數再次請求;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"Server 返回 40,表示請求接收成功;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"Client 發送 POST 請求確認後期降級通路情況;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"Server 返回 ok,此時第一階段握手流程完成;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"嘗試發起 WebSocket 連接,首先進行 2probe 和 3probe 的請求響應,確認通信通道暢通後,即可進行正常的 WebSocket 通信。"}]}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3.3 TLS 內存消耗優化"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"客戶端與服務端連接建立採用的 wss 協議,在 1.0 版本中 TLS 證書掛載在 Nginx 上,HTTPS 握手過程由 Nginx 完成,爲了降低 Nginx 的機器成本,在 2.0 版本中我們將證書掛載到服務上,通過分析服務內存,如下圖所示,TLS 握手過程中消耗的內存佔了總內存消耗的大概 30% 左右。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/a4\/a425ea15769e24313301af1757daf747.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這個部分的內存消耗無法避免,我們有兩個選擇:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"採用七層負載均衡,在七層負載上進行 TLS 證書掛載,將 TLS 握手過程移交給性能更好的工具完成;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"優化 Go 對 TLS 握手過程性能,在與業內大佬曹春暉(曹大)的交流中瞭解到,他最近在 Go 官方庫提交的 PR "},{"type":"link","attrs":{"href":"https:\/\/github.com\/golang\/go\/issues\/43563","title":"","type":null},"content":[{"type":"text","text":"https:\/\/github.com\/golang\/go\/issues\/43563"}]},{"type":"text","text":" ,以及相關的性能測試數據 "},{"type":"link","attrs":{"href":"https:\/\/github.com\/golang\/go\/pull\/48229","title":"","type":null},"content":[{"type":"text","text":"https:\/\/github.com\/golang\/go\/pull\/48229"}]},{"type":"text","text":" 。"}]}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3.4 Socket ID 設計"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對每次連接必須產生一個唯一碼,如果出現重複會導致串號,消息混亂推送的問題。選擇 SnowFlake 算法作爲唯一碼生成算法。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"物理機場景中,對副本所在物理機進行固定編號,即可保證每個副本上的服務產生的 Socket ID 是唯一值。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"K8S 場景中,這種方案不可行,於是採用註冊下發的方式返回編號,WS-Gateway 所有副本啓動後向數據庫寫入服務的啓動信息,獲取副本編號,以此作爲參數作爲 SnowFlake 算法的副本編號進行 Socket ID 生產,服務重啓會繼承之前已有的副本編號,有新版本下發時會根據自增 ID 下發新的副本編號。於此同時,Ws-Gateway 副本會向數據庫寫入心跳信息,以此作爲網關服務本身的健康檢查依據。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3.5 集羣會話管理方案:事件廣播"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"客戶端完成握手流程後,會話數據在當前網關節點內存存儲,部分可序列化數據存儲到 Redis,存儲結構說明如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"embedcomp","attrs":{"type":"table","data":{"content":"
說明
ws:user:clients:${uid}存儲用戶和 WebSocket 連接的關係,採用有序集合方式存儲
ws:guid:clients:${guid}存儲文件和 WebSocket 連接的關係,採用有序結合方式存儲
ws:client:${socket.id}存儲當前 WebSocket 連接下的全部用戶和文件關係數據,採用 Redis Hash 方式進行存儲,對應 key 爲 user 和 guid"}}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由客戶端觸發或組件服務觸發的消息推送,通過 Redis 存儲的數據結構,在 WS-API 服務查詢到返回消息體的目標客戶端的 Socket ID,再有 WS-Gateway 服務進行集羣消費,如果 Socket ID 不在當前節點,則需要進行節點與會話關係的查詢,找到客端戶 Socket ID 實際對應的 WS-Gateway 節點,通常有以下兩種方案:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"embedcomp","attrs":{"type":"table","data":{"content":"
優點缺點
事件廣播實現簡單消息廣播數量會隨着節點數量上升
註冊中心會話與節點映射關係清晰註冊中心強依賴,額外運維成本"}}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在確定使用事件廣播方式進行網關節點間的消息傳遞後,進一步選擇使用哪種具體的消息中間件,列舉了三種待選的方案:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"embedcomp","attrs":{"type":"table","data":{"content":"
特性RedisKafkaRocketMQ
開發語言CScalaJava
單機吞吐量10w+10w+10w+
可用性主從架構分佈式架構分佈式架構
特點功能簡單吞吐量、可用性極高功能豐富、定製化強,吞吐量、可用性高
功能特性數據 10K 以內性能優異,功能簡單,適用於簡單業務場景支持核心的 MQ 功能,不支持消息查詢或消息回溯等功能支持核心的 MQ 功能,擴展性強"}}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"於是對 Redis 和其他 MQ 中間件進行 100w 次的入隊和出隊操作,在測試過程中發現在數據小於 10K 時 Redis 性能表現十分優秀,進一步結合實際情況:廣播內容的數據量大小在 1K 左右,業務場景簡單固定,並且要兼容歷史業務邏輯,最後選擇了 Redis 進行消息廣播。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"後續還可以將 WS-API 與 WS-Gateway 兩兩互聯,使用 gRPC stream 雙向流通信節省內網流量。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3.6 心跳機制"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"會話在節點內存與 Redis 中存儲後,客戶端需要通過心跳上報持續更新會話時間戳,客戶端按照服務端下發的週期進行心跳上報,上報時間戳首先在內存進行更新,然後再通過另外的週期進行 Redis 同步,避免大量客戶端同時進行心跳上報對 Redis 產生壓力。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"客戶端建立 WebSocket 連接成功後,服務端下發心跳上報參數;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"客戶端依據以上參數進行心跳包傳輸,服務端收到心跳後會更新會話時間戳;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"客戶端其他上行數據都會觸發對應會話時間戳更新;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"服務端定時清理超時會話,執行主動關閉流程;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"通過 Redis 更新的時間戳數據進行 WebSocket 連接、用戶和文件之間的關係進行清理。會話數據內存以及 Redis 緩存清理邏輯:"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"go"},"content":[{"type":"text","text":"for {\n select {\n case int64(expireTime) {\n clients = append(clients, client)\n } else {\n dispatcher.clearRedisMapping(client.Id, client.Uid, lastTs, clearTimeout)\n }\n return true\n })\n for _, cli := range clients {\n cli.WsClose()\n }\n }\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在已有的兩級緩存刷新機制上,進一步通過動態心跳上報頻率的方式降低心跳上報產生的服務端性能壓力,默認場景中客戶端對服務端進行間隔 1s 的心跳上報,假設目前單機承載了 50w 的連接數,當前的 QPS 爲:"},{"type":"codeinline","content":[{"type":"text","text":"QPS1 = 500000\/1"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"從服務端性能優化的角度考慮,實現心跳正常情況下的動態間隔,每 x 次正常心跳上報,心跳間隔增加 a,增加上限爲 y,動態 QPS 最小值爲:"},{"type":"codeinline","content":[{"type":"text","text":"QPS2=500000\/y"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"極限情況下,心跳產生的 QPS 降低 y 倍。在單次心跳超時後服務端立刻將 a 值變爲 1s 進行重試。採用以上策略,在保證連接質量的同時,降低心跳對服務端產生的性能損耗。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3.7 自定義 Headers"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用 Kafka 自定義 Headers 的目的是避免網關層出現對消息體解碼而帶來的性能損耗,客戶端 WebSocket 連接建立成功後,會進行一系列的業務操作,我們選擇將 WS-Gateway 和 WS-API 之間的操作指令和必要的參數放到 Kafka 的 Headers 中,例如通過 X-XX-Operator 爲廣播,再讀取 X-XX-Guid 文件編號,對該文件內的所有用戶進行消息推送。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"embedcomp","attrs":{"type":"table","data":{"content":"
字段說明描述
X-IDWebSocket ID連接 ID
X-Uid用戶 ID用戶 ID
X-Guid文件 ID文件 ID
X-Inner網關內部操作指令用戶加入、用戶退出
X-Event網關事件Connect\/Message\/Disconnect
X-Locale語言類型設置語言類型設置
X-Operatorapi 層操作指令單播、廣播、網關內部操作
X-Auth-Type用戶鑑權類型SDKV2、主站、微信、移動端、桌面
X-Client-Version客戶端版本客戶端版本
X-Server-Version網關版本服務端版本
X-Push-Client-ID客戶端 ID客戶端 ID
X-Trace-ID鏈路 ID鏈路 ID"}}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 Kafka Headers 中寫入了 trace id 和 時間戳,可以追中某條消息的完整消費鏈路以及各階段的時間消耗。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/5d\/5d997025bf7e2c8f9393a531451c5000.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3.8 消息接收與發送"}]},{"type":"codeblock","attrs":{"lang":"go"},"content":[{"type":"text","text":"type Packet struct {\n ...\n}\n\ntype Connect struct {\n *websocket.Con\n send chan Packet\n}\n\nfunc NewConnect(conn net.Conn) *Connect {\n c := &Connect{\n send: make(chan Packet, N),\n }\n go c.reader()\n go c.writer()\n return c\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"客戶端與服務端的消息交互第一版的寫法類似以上寫法,對 Demo 進行壓測,發現每個 WebSocket 連接都會佔用 3 個 goroutine,每個 goroutine 都需要內存棧,單機承載連十分有限,主要受制於大量的內存佔用,而且大部分時間 c.writer() 是閒置狀態,於是考慮,是否只啓用 2 個 goroutine 來完成交互。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"go"},"content":[{"type":"text","text":"type Packet struct {\n ...\n}\n\ntype Connect struct {\n *websocket.Conn\n mux sync.RWMutex\n}\n\nfunc NewConnect(conn net.Conn) *Connect {\n c := &Connect{\n send: make(chan Packet, N),\n }\n go c.reader()\n return c\n}\n\nfunc (c *Connect) Write(data []byte) (err error) {\n c.mux.Lock()\n defer c.mux.Unlock()\n ...\n return nil\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"保留 c.reader() 的 goroutine,如果使用輪詢方式從緩衝區讀取數據,可能會產生讀取延遲或者鎖的問題,c.writer() 操作調整爲主動調用,不採用啓動 goroutine 持續監聽,降低內存消耗。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"調研了 gev 和 gnet 等基於事件驅動的輕量級高性能網絡庫,實測發現在大量連接場景下可能產生的消息延遲的問題,所以沒有在生產環境下使用。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3.9 核心對象緩存"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"確定數據接收與發送邏輯後,網關部分的核心對象爲 Connection 對象,圍繞 Connection 進行了 run、read、write、close 等函數的開發。使用 sync.pool 來緩存該對象,減輕 GC 壓力,創建連接時,通過對象資源池獲取 Connection 對象,生命週期結束之後,重置 Connection 對象後 Put 回資源池。在實際編碼中,建議封裝 GetConn()、PutConn() 函數,收斂數據初始化、對象重置等操作。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"go"},"content":[{"type":"text","text":"var ConnectionPool = sync.Pool{\n New: func() interface{} {\n return &Connection{}\n },\n}\n\nfunc GetConn() *Connection {\n cli := ConnectionPool.Get().(*Connection)\n return cli\n}\n\nfunc PutConn(cli *Connection) {\n cli.Reset()\n ConnectionPool.Put(cli) \/\/ 放回連接池\n}\n"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3.10 數據傳輸過程優化"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"消息流轉過程中,需要考慮消息體的傳輸效率優化,採用 MessagePack 對消息體進行序列化,壓縮消息體大小。調整 MTU 值避免出現分包情況,定義 a 爲探測包大小,通過如下指令,對目標服務 ip 進行 MTU 極限值探測。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"plain"},"content":[{"type":"text","text":" ping -s {a} {ip}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"a = 1400 時,實際傳輸包大小爲:1428。其中 28 由 8(ICMP 回顯請求和回顯應答報文格式)和 20(IP 首部)構成。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/93\/9310d96cf067ef77875a9ea476bb26ac.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果 a 設置過大會導致應答超時,在實際環境包大小超過該值時會出現分包的情況。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/68\/687f544943a06ed61c38ab9f0ee527bb.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在調試合適的 MTU 值的同時通過 MessagePack 對消息體進行序列號,進一步壓縮數據包的大小,並減小 CPU 的消耗。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3.11 基礎設施支持"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用 EGO 框架( "},{"type":"link","attrs":{"href":"https:\/\/github.com\/gotomicro\/ego","title":"","type":null},"content":[{"type":"text","text":"https:\/\/github.com\/gotomicro\/ego"}]},{"type":"text","text":" )進行服務開發:業務日誌打印,異步日誌輸出,動態日誌級別調整等功能,方便線上問題排查提升日誌打印效率;微服務監控體系,CPU、P99、內存、goroutine 等監控。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/91\/911100a06dd867253a6e46066836eb45.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"客戶端 Redis 監控:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/98\/982d6e9b8d5e1cc55173f7f265a1cc7f.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"客戶端 Kafka 監控:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/33\/3319e8cf0b1d5ae6e09e2dfce106a822.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"自定義監控大盤:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/f0\/f0d1da1e14813df192e68c3a60b3337c.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"4 性能壓測"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"4.1 壓測準備"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"選擇一臺配置爲 4 核 8G 的虛擬機,作爲服務機,目標承載 48w 連接;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"選擇八臺配置爲 4 核 8G 的虛擬機,作爲客戶機,每臺客戶機開放 6w 個端口。"}]}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"4.2 場景一"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"用戶上線,50w 在線用戶。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"embedcomp","attrs":{"type":"table","data":{"content":"
服務CPUMemory數量CPU%Mem%
WS-Gateway16 核32G1 臺22.38%70.59%"}}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"單個 WS-Gateway 每秒建立連接數峯值爲:1.6w 個\/s,每個用戶佔用內存:47K。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"4.3 場景二"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"測試時間 15 分鐘,在線用戶 50w,每 5s 推送一條所有用戶,用戶有回執。推送內容爲:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"42[\"message\",{\"type\":\"xx\",\"data\":{\"type\":\"xx\",\"clients\":[{\"id\":xx,\"name\":\"xx\",\"email\":\"[email protected]\",\"avatar\":\"ZgG5kEjCkT6mZla6.png\",\"created_at\":1623811084000,\"name_pinyin\":\"\",\"team_id\":13,\"team_role\":\"member\",\"merged_into\":0,\"team_time\":1623811084000,\"mobile\":\"+xxxx\",\"mobile_account\":\"\",\"status\":1,\"has_password\":true,\"team\":null,\"membership\":null,\"is_seat\":true,\"team_role_enum\":3,\"register_time\":1623811084000,\"alias\":\"\",\"type\":\"anoymous\"}],\"userCount\":1,\"from\":\"ws\"}}]"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"測試經過 5 分鐘後,服務異常重啓,重啓原因是內存使用量到超過限制。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/0f\/0f5b4eb46636b559b4224a6b737c8997.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/0f\/0f80f65771325021b7d5138d745538b5.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/63\/639334d4e20e9fa1f06656922e9f6ee1.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/3e\/3e6b930b4bbad1fd2cb67ad1b3afc221.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"分析內存超過限制的原因:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/f7\/f7ba370198c6d0e6c071ce5d78fa8af0.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"新增的廣播代碼用掉了 9.32% 的內存。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/dd\/ddbad46999761a8ca5e6199c24a32c2e.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接收用戶回執消息的部分消耗了 10.38% 的內存。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/e6\/e6c810dcfe947fd03127101a15c50dbc.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"進行測試規則調整,測試時間 15 分鐘,在線用戶 48w,每 5s 推送一條所有用戶,用戶有回執。推送內容爲:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"42[\"message\",{\"type\":\"xx\",\"data\":{\"type\":\"xx\",\"clients\":[{\"id\":xx,\"name\":\"xx\",\"email\":\"[email protected]\",\"avatar\":\"ZgG5kEjCkT6mZla6.png\",\"created_at\":1623811084000,\"name_pinyin\":\"\",\"team_id\":13,\"team_role\":\"member\",\"merged_into\":0,\"team_time\":1623811084000,\"mobile\":\"+xxxx\",\"mobile_account\":\"\",\"status\":1,\"has_password\":true,\"team\":null,\"membership\":null,\"is_seat\":true,\"team_role_enum\":3,\"register_time\":1623811084000,\"alias\":\"\",\"type\":\"anoymous\"}],\"userCount\":1,\"from\":\"ws\"}}]"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"embedcomp","attrs":{"type":"table","data":{"content":"
服務CPUMemory數量CPU%Mem%
WS-Gateway16 核32G1 臺44%91.75%"}}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"連接數建立峯值:1w 個\/s,接收數據峯值:9.6w 條\/s,發送數據峯值 9.6w 條\/s。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"4.4 場景三"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"測試時間 15 分鐘,在線用戶 50w,每 5s 推送一條所有用戶,用戶無需回執。推送內容爲:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"42[\"message\",{\"type\":\"xx\",\"data\":{\"type\":\"xx\",\"clients\":[{\"id\":xx,\"name\":\"xx\",\"email\":\"[email protected]\",\"avatar\":\"ZgG5kEjCkT6mZla6.png\",\"created_at\":1623811084000,\"name_pinyin\":\"\",\"team_id\":13,\"team_role\":\"member\",\"merged_into\":0,\"team_time\":1623811084000,\"mobile\":\"+xxxx\",\"mobile_account\":\"\",\"status\":1,\"has_password\":true,\"team\":null,\"membership\":null,\"is_seat\":true,\"team_role_enum\":3,\"register_time\":1623811084000,\"alias\":\"\",\"type\":\"anoymous\"}],\"userCount\":1,\"from\":\"ws\"}}]"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"embedcomp","attrs":{"type":"table","data":{"content":"
服務CPUMemory數量CPU%Mem%
WS-Gateway16 核32G1 臺30%93%"}}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"連接數建立峯值:1.1w 個\/s,發送數據峯值 10w 條\/s,出內存佔用過高之外,其他沒有異常情況。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/b5\/b5576712832a2376f3095b388deb8ba8.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/49\/49649dc6d04368729d209dac58a530f1.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/5a\/5a008bfb75211f6ba91801d708f49f7f.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/d3\/d3039dfd353aa751b67daa7602a96731.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"內存消耗極高,分析火焰圖,大部分消耗在定時 5s 進行廣播的操作上。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/03\/0327a9e82bfcaff3c7cc6a42902e9b7b.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"4.5 場景四"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"測試時間 15 分鐘,在線用戶 50w,每 5s 推送一條所有用戶,用戶有回執。每秒 4w 用戶上下線。推送內容爲:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"42[\"message\",{\"type\":\"xx\",\"data\":{\"type\":\"xx\",\"clients\":[{\"id\":xx,\"name\":\"xx\",\"email\":\"[email protected]\",\"avatar\":\"ZgG5kEjCkT6mZla6.png\",\"created_at\":1623811084000,\"name_pinyin\":\"\",\"team_id\":13,\"team_role\":\"member\",\"merged_into\":0,\"team_time\":1623811084000,\"mobile\":\"+xxxx\",\"mobile_account\":\"\",\"status\":1,\"has_password\":true,\"team\":null,\"membership\":null,\"is_seat\":true,\"team_role_enum\":3,\"register_time\":1623811084000,\"alias\":\"\",\"type\":\"anoymous\"}],\"userCount\":1,\"from\":\"ws\"}}]"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"embedcomp","attrs":{"type":"table","data":{"content":"
服務CPUMemory數量CPU%Mem%
WS-Gateway16 核32G1 臺46.96%65.6%"}}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"連接數建立峯值:18570 個\/s,接收數據峯值:329949 條\/s,發送數據峯值 393542 條\/s,未出現異常情況。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/0b\/0bfd7031a5bf0597be76e00526fe0ec5.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/1d\/1d7f754b09fcf5897e6b4a036f7bfa6c.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/9a\/9a799d51c2a1a72be85223932cfe8f46.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/d2\/d2788dab7a40d2cc3b8e406a6d5b23c7.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"4.6 壓測總結"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 16C 32G 內存的硬件條件下,單機 50w 連接數,進行以上包括用戶上下線、消息回執等四個場景的壓測,內存和 CPU 消耗都符合預期,並且在較長時間的壓測下,服務也很穩定。滿足目前量級下的資源節約要求,可在此基礎上繼續完善功能開發。"}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"5 總結"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"面臨日益增加的用戶量,網關服務的重構是勢在必行,本次重構主要是:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對網關服務與業務服務的解耦,移除對 Nginx 的依賴,讓整體架構更加清晰。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"從用戶建立連接到底層業務推送消息的整體流程分析,對其中這些流程進行了具體的優化。以下各個方面讓 2.0 版本的網關有了更少的資源消耗,更低的單位用戶內存損耗、更加完善的監控報警體系,讓網關服務本身更加可靠:"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可降級的握手流程;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Socket ID 生產;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"客戶端心跳處理過程的優化;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"自定義 Headers 避免了消息解碼,強化了鏈路追蹤與監控;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"消息的接收與發送代碼結構設計上的優化;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對象資源池的使用,使用緩存降低 GC 頻率;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"消息體的序列化壓縮;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接入服務觀測基礎設施,保證服務穩定性。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在保證網關服務性能過關的同時,更進一步的是收斂底層組件服務對網關業務調用的方式,從以前的 HTTP、Redis、Kafka 等方式,統一爲 gRPC 調用,保證了來源可查可控,爲後續業務接入打下了更好的基礎。"}]}]}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"6 Q&A"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"收錄了部分文章相關內容的討論問題:"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"6.1 SocketID 存在的價值"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"問題:按照我的理解socketID存在的價值是Kafka的消費者需要根據socketID找到對應的tcp鏈 接,既然你們已經有了自定義網關,那麼引入kafka的意義是什麼?消息的持久化?爲什麼不在網關層做負載均衡,讓節點直接跟客戶端通信。另外我猜測消費發送者需要根據socketId做hash然後發送到對應的partition,一旦初始partition過小,進行擴容時,客戶端和服務端都得進行重啓或則升級,不知道引入kafka的意義在哪裏,相反還極大的增加了架構的複雜度和維護成本,擴展性也沒那麼好,如果是http短鏈接還能理解。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"回答:圖中沒畫出SLB,是有負載均衡的。我們沒有采用socket id hash到對應partition,kafka的作用是在處理網關內部的不需要關心順序和推送消息的流轉,如果沒有kafka,那麼組件或者網關滾動更新,用戶重連的過程中,就可能丟消息;對於需要順序的消息,例如ping pong模式的是可以通過網關識別到header頭裏的cmd信息,找到對應後端,分發消息。"}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"6.2 Redis 進行消息廣播的作用"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"問題:廣播內容的數據量大小在 1K 左右,業務場景簡單固定,並且要兼容歷史業務邏輯,最後選擇了 Redis 進行消息廣播。 api與網關交互不是通過kafka嗎,這裏是什麼意思呢?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"回答:網關節點對kafka的消費是集羣模式。如果kafka,在k8s條件下,使用廣播模式比較麻煩。所以老的網關是用redis做pubsub的廣播,爲了兼容老的邏輯仍然採用redis做廣播。同時後續我們打算直接將api和ws做兩兩互聯,通過grpc stream做廣播,有更好的擴展性。"}]}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"7 技術鏈接"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"微服務框架: "},{"type":"link","attrs":{"href":"https:\/\/github.com\/gotomicro\/ego","title":"","type":null},"content":[{"type":"text","text":"https:\/\/github.com\/gotomicro\/ego"}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Kafka、Redis、MySQL 客戶端監控 SDK: "},{"type":"link","attrs":{"href":"https:\/\/github.com\/gotomicro\/ego-component","title":"","type":null},"content":[{"type":"text","text":"https:\/\/github.com\/gotomicro\/ego-component"}]}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"活動推薦:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"石墨專注於重塑辦公文檔領域,打造出國產企業級雲端 Office 辦公軟件。 助力企業效能充分釋放,打通數字化轉型最後一公里。 北京時間 12 月 1 日 14:00,石墨文檔全新 ToB 子品牌「石墨辦公」將重磅發佈: "},{"type":"link","attrs":{"href":"https:\/\/shimo.im\/conference-2021-12?from=qt-smbg","title":"","type":null},"content":[{"type":"text","text":"https:\/\/shimo.im\/conference-2021-12?from=qt-smbg"}]},{"type":"text","text":" ,預約參會有機會獲得神祕大禮,敬請參與!"}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章