golang 網路架構之 thrift

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

thrift 最初是 facebook 開發使用的 rpc 通訊架構,後來貢獻給了 apache 基金會,出來得比較早,幾乎支援所有的後端語言,使用非常廣泛,是不可不知的一個網路架構

和 grpc 一樣,需要先定義通訊協定,然後實現自己商務邏輯,下面還是通過一個簡單樣本(之前的echo程式)說明 thrift 的用法,下面樣本使用的完整代碼在下列地址:
實現檔案:https://github.com/hatlonely/...
協議檔案:https://github.com/hatlonely/...

簡單 echo 服務

擷取 thrift

go get git.apache.org/thrift.git/lib/go

定義協議檔案

namespace go echostruct EchoReq {    1: string msg;}struct EchoRes {    1: string msg;}service Echo {    EchoRes echo(1: EchoReq req);}

執行 thrift -r --gen go echo.thrift 命令會產生 gen-go 檔案夾,這個過程其實是將上面的協議翻譯成 golang 代碼

這個命令依賴於 thrift 工具,可以通過下面命令擷取

Mac

brew install thrift

Linux

wget http://www-us.apache.org/dist/thrift/0.11.0/thrift-0.11.0.tar.gztar -xzvf thrift-0.11.0.tar.gzcd thrift-0.11.0./configuremake -j8[sudo] make install 

實現服務端

type EchoServerImp struct {}func (e *EchoServerImp) Echo(ctx context.Context, req *echo.EchoReq) (*echo.EchoRes, error) {    fmt.Printf("message from client: %v\n", req.GetMsg())    res := &echo.EchoRes{        Msg: req.GetMsg(),    }    return res, nil}func main() {    transport, err := thrift.NewTServerSocket(":3000")    if err != nil {        panic(err)    }    processor := echo.NewEchoProcessor(&EchoServerImp{})    server := thrift.NewTSimpleServer4(        processor,        transport,        thrift.NewTBufferedTransportFactory(8192),        thrift.NewTCompactProtocolFactory(),    )    if err := server.Serve(); err != nil {        panic(err)    }}

這個過程和 grpc 類似,不同的地方在於,thrift 支援更多的伺服器類型,支援不同的協議打包方式,方便使用者選擇,這裡的 compact 協議是一種壓縮的協議,使用比較多

實現用戶端

func main() {    var transport thrift.TTransport    var err error    transport, err = thrift.NewTSocket("localhost:3000")    if err != nil {        fmt.Errorf("NewTSocket failed. err: [%v]\n", err)        return    }    transport, err = thrift.NewTBufferedTransportFactory(8192).GetTransport(transport)    if err != nil {        fmt.Errorf("NewTransport failed. err: [%v]\n", err)        return    }    defer transport.Close()    if err := transport.Open(); err != nil {        fmt.Errorf("Transport.Open failed. err: [%v]\n", err)        return    }    protocolFactory := thrift.NewTCompactProtocolFactory()    iprot := protocolFactory.GetProtocol(transport)    oprot := protocolFactory.GetProtocol(transport)    client := echo.NewEchoClient(thrift.NewTStandardClient(iprot, oprot))    var res *echo.EchoRes    res, err = client.Echo(context.Background(), &echo.EchoReq{        Msg: strings.Join(os.Args[1:], " "),    })    if err != nil {        fmt.Errorf("client echo failed. err: [%v]", err)        return    }    fmt.Printf("message from server: %v", res.GetMsg())}

這個 client 相對複雜一些,需要和 server 端設定一致的打包方式,如果不一致會出現通訊失敗,這一點需要特別注意一下

參考連結

  • thrift go 官網: http://thrift.apache.org/tuto...
  • thrift github: https://github.com/apache/thr...
  • thrift go tutorial: https://github.com/apache/thr...
轉載請註明出處
本文連結:http://hatlonely.github.io/20...

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.