天天看點

golang 實踐配合 php 進行 web 開發,go語言實踐配合php開發.pdf

Golang 實踐 配合 PHP 進行 Web 開發 2012 09 目錄 Web 應用的典型技術架構 PHP 的謎思 毫無新意的 Golang 實踐出真知 Q curl mutil xxx pctrl xxx event xxx 毫無新意的 Golang 現代的 通用程式設計語言 編譯為本地機器碼 靜态類型 簡單的文法 類型推導 簡明的類型系統 垃圾收集 内建并行支援 毫無新意的 Golang goroutine channel 毫無新意的 Golang 實踐出真知 場景A 在某系統中 一個 PHP 頁面的操作除完成本 地資料儲存外 例如 MySQL 還需要向某後 台服務送出資料 該服務由于壓力巨大 響應 較慢 延遲較大 100ms 實踐出真知 實踐出真知 func main w worker New worker Unlimited defer w Close w AddServer 127 0 0 1 4730 w AddFunc ToUpper ToUpper worker Immediately w AddFunc ToUpperTimeOut5 ToUpper 5 w Work func ToUpper job worker Job byte error data byte strings ToUpper string job Data return data nil 實踐出真知 實踐出真知 場景B 某個頁面需要多次調用不同的服務生成 30 次調用 各個服務之間資料的依賴關系較 弱 調用相對獨立 但由于調用服務較多 雖 然每個服務的響應都很快 但是仍然導緻 PHP 生成頁面很慢 實踐出真知 CombineRPC 實踐出真知 核心方法 同步 RPC 調用函數 func Call rpcreq RPCRequest RPCResponse 分解打包的 RPC 并發起異步請求 func AsyncCall requests RPCRequests timeout int64 RPCResponses 實踐出真知 AsyncCall 主要邏輯 初始化 l len requests rpcchan make chan RPCResponse l rslt make RPCResponses l 發起異步請求 for id r range requests RPC goroutine 擷取結果 for i 0 i l i r rpcchan rslt r Id r 實踐出真知 RPC goroutine go func id string req RPCRequest 設定逾時 省略代碼 構造一個異步調用後處理結果的 channel c make chan RPCResponse 1 利用 goroutine 進行異步 RPC 調用 go func c Call req 逾時判斷 channel select id r 實踐出真知 Channel select 利用 channel 的 select 實作逾時 select case rpcresp c rpcresp Id id rpcchan rpcresp case time After time Duration to rpcchan err nil log Fatal err if err php Exec foobar php err nil log Fatal err 實踐出真知 Cgo 總是能帶來驚喜 func php PHP Eval script string err error if err C php eval script C CString script err nil return errors New C GoString err return 實踐出真知 Gearman Go https bitbucket org mikespook gearman go CombineRPC https bitbucket org mikespook combinerpc GoEmPHP https bitbucket org mikespook goemphp 歡迎大家加入我們 Q A THANK YOU Also thanks go team 邢興 39健康網 mikespook mikespook

展開閱讀全文