初探可編程網關 Pipy

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有幸參加了 ","attrs":{}},{"type":"link","attrs":{"href":"https://flomesh.cn/","title":"","type":null},"content":[{"type":"text","text":"Flomesh","attrs":{}}]},{"type":"text","text":" 組織的workshop,瞭解了他們的 Pipy 網絡代理,以及圍繞 Pipy 構建起來的生態。Pipy 在生態中,不止是代理的角色,還是 Flomesh 服務網格​中的數據平面。","attrs":{}}]},{"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":"整理一下,做個記錄,順便瞄一下 Pipy 的部分源碼。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"介紹","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面是摘自 Github 上關於 Pipy 的介紹:","attrs":{}}]},{"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":"Pipy 是一個輕量級、高性能、高穩定、可編程的網絡代理。Pipy 核心框架使用 C++ 開發,網絡 IO 採用 ASIO 庫。 Pipy 的可執行文件僅有 5M 左右,運行期的內存佔用 10M 左右,因此 Pipy 非常適合做 Sidecar proxy。","attrs":{}}]}],"attrs":{}},{"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":"Pipy 內置了自研的 pjs 作爲腳本擴展,使得Pipy 可以用 JS 腳本根據特定需求快速定製邏輯與功能。","attrs":{}}]}],"attrs":{}},{"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":"Pipy 採用了模塊化、鏈式的處理架構,用順序執行的模塊來對網絡數據塊進行處理。這種簡單的架構使得 Pipy 底層簡單可靠,同時具備了動態編排流量的能力,兼顧了簡單和靈活。通過使用 REUSE_PORT 的機制(主流 Linux 和 BSD 版本都支持該功能),Pipy 可以以多進程模式運行,使得 Pipy 不僅適用於 Sidecar 模式,也適用於大規模的流量處理場景。 在實踐中,Pipy 獨立部署的時候用作“軟負載”,可以在低延遲的情況下,實現媲美硬件的負載均衡吞吐能力,同時具有靈活的擴展性。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ff/ff8cc5a06afbb1c4944628ba0a135c6a.jpeg","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":"Pipy 的核心是消息流處理器:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e4/e43786b5003adb6f0df5706729813c42.jpeg","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":"Pipy 流量處理的流程:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/03/03fcb7fceabb399a74fd8c3a360434ef.jpeg","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":3},"content":[{"type":"text","text":"核心概念","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"流(Stream):Pipy","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"管道(Pipeline)","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"模塊(Module)","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"會話(Session)","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上下文(Context)","attrs":{}}]}]}],"attrs":{}},{"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":"以下是個人淺見:","attrs":{}}]},{"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":"Pipy 使用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"pjs","attrs":{}}],"attrs":{}},{"type":"text","text":" 引擎將 JavaScript格式的配置,解析成其抽象的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Configuration","attrs":{}}],"attrs":{}},{"type":"text","text":" 對象。每個 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Configuration","attrs":{}}],"attrs":{}},{"type":"text","text":" 中包含了多個 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Pipeline","attrs":{}}],"attrs":{}},{"type":"text","text":",每個 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Configuration","attrs":{}}],"attrs":{}},{"type":"text","text":" 中又會用到多個 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Filter","attrs":{}}],"attrs":{}},{"type":"text","text":"。這些都屬於 Pipy 的","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"靜態","attrs":{}},{"type":"text","text":"配置部分。(後面會提到 Pipeline 的三種不同類型)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/8b/8b5b343ea0cc9509213baea71d053897.jpeg","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":"而屬於","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"運行時","attrs":{}},{"type":"text","text":"的就是流、會話和上下文了,在 Pipy 中,數據流是由對象(Pipy 的","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"抽象","attrs":{}},{"type":"text","text":")組成的。而這些對象抵達 Pipy,被抽象成不同的事件。而事件觸發不同的過濾器的執行。","attrs":{}}]},{"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":"我個人更喜歡將其核心理解爲:對數據流的事件處理引擎。","attrs":{}}]},{"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":"理解歸理解,實踐出真知。“大膽假設,小心求證!”","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"本地編譯","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"從編譯 Pipy 開始。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"環境準備","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"#安裝 nodejs\n$ nvm install lts/erbium \n#安裝 cmake\n$ brew install cmake\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"編譯 Pipy","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"從 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"https://github.com/flomesh-io/pipy.git","attrs":{}}],"attrs":{}},{"type":"text","text":" 克隆代碼。","attrs":{}}]},{"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":"Pipy 的編譯包括了兩個部分,GUI 和 Pipy 本體。","attrs":{}}]},{"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":"GUI 是 Pipy 提供的一個用於開發模式下進行配置的界面,首先編譯Pipy GUI。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"# pipy root folder\n$ cd gui\n$ npm install\n$ npm run build\n","attrs":{}}]},{"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":"接着編譯 Pipy 的本體","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"# pipy root folder\n$ mkdir build\n$ cd build\n$ cmake -DCMAKE_BUILD_TYPE=Release -DPIPY_GUI=ON ..\n$ make\n","attrs":{}}]},{"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":"完成後檢查根目錄下的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"bin","attrs":{}}],"attrs":{}},{"type":"text","text":" 目錄,可以看到 pipy 的可執行文件,大小隻有 11M。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f3/f34ba26311cbecb1383af29fad836aff.jpeg","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":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"$ bin/pipy --help\nUsage: pipy [options]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章