Golang RPC 之 Thrift

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

Thrift 簡介:

Thrift 是一款高效能、開源的 RPC 架構,產自 Facebook 後貢獻給了 Apache,Thrift 囊括了整個 RPC 的上下遊體系,內建序列化編譯工具,因為 Thrift 採用的是二進位序列化,並且與 gRPC 一樣使用的都是長串連建立 client 與 server 之間的通訊,相比於比傳統的使用XML,JSON,SOAP等短串連的解決方案效能要快得多。
本篇只介紹 Golang 關於 Thrift 的基礎使用。

安裝

  • 安裝 Thrift 的 Golang 庫有兩種方案:

    1. 直接通過 go get 命令安裝,缺點是因為不可抗拒的網路因素大部分人可能會失敗:$ go get git.apache.org/thrift.git/lib/go/thrift
    2. 通過源碼安裝:
      • 在 $GOPATH 的 src 目錄下建立多層級目錄:git.apache.org/thrift.git/lib/go
      • 從 github 上下載 thrift 0.10.0 的源碼 ,解壓後進入 thrift-0.10.0/lib/go 目錄下,將 thrift 目錄 copy 到剛建立的 $GOPATH/src/git.apache.org/thrift.git/lib/go 目錄下
      • 在任意目錄下執行 $ go install git.apache.org/thrift.git/lib/go/thrift 就完成了 golang 的 thrift 庫的安裝
  • 安裝 Thrift 的 IDL 編譯工具

    1. windows 平台下安裝:

      直接下載:thrift complier 下載地址,下載完成後改名為:thrift.exe 並將其放入到系統內容變數下即可使用

    2. Linux 平台下安裝:

      從 github 上下載 thrift 0.10.0 的源碼,解壓後進入:thrift-0.10.0/compiler/cpp 目錄執行如下命令完成編譯後,將其放入到系統內容變數下即可使用:
      $ mkdir cmake-build
      $ cd cmake-build
      $ cmake ..
      $ make

  • 驗證是否安裝成功:

    $ thrift -version,如果列印出來:Thrift version 0.10.0 表明 complier 安裝成功

實踐:

下面我們使用 Thrift 定義一個介面,該介面實現對傳入的資料進行大寫的格式化處理。

  • 建立 golang 項目 ThriftDemo 工程:

Paste_Image.png
  1. client目錄下的 client.go 實現了用戶端用於發送資料並列印接收到 server 端處理後的資料
  2. server 目錄下的 server.go 實現了服務端用於接收用戶端發送的資料,並對資料進行大寫處理後返回給用戶端
  3. thrift_file 用於存放 thrift 的 IDL 檔案: *.thrift
  • 定義 Thrift RPC 介面

example.thrift:

namespace py examplestruct Data {    1: string text}service format_data {    Data do_format(1:Data data),}
  • 編譯 thrift 檔案

    進入 thrift_file 目錄執行:$ thrift -out .. --gen go example.thrift,就會在 thrift_file 的同級目錄下產生 golang 的包:example,其中 format_data-remote 是產生的測試代碼可以不用特別關注


Paste_Image.png
  • 實現 server 端:
package mainimport (    "ThriftDemo/example"    "strings"    "git.apache.org/thrift.git/lib/go/thrift"    "fmt"    "log")type FormatDataImpl struct {}func (fdi *FormatDataImpl) DoFormat(data *example.Data) (r *example.Data, err error){    var rData example.Data    rData.Text = strings.ToUpper(data.Text)    return &rData, nil}const (    HOST = "localhost"    PORT = "8080")func main() {    handler := &FormatDataImpl{}    processor := example.NewFormatDataProcessor(handler)    serverTransport, err := thrift.NewTServerSocket(HOST + ":" + PORT)    if err != nil {        log.Fatalln("Error:", err)    }    transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())    protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()    server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)    fmt.Println("Running at:", HOST + ":" + PORT)    server.Serve()}
  • 實現 client 端
package mainimport (    "git.apache.org/thrift.git/lib/go/thrift"    "net"    "fmt"    "ThriftDemo/example"    "log")const (    HOST = "localhost"    PORT = "8080")func main()  {    tSocket, err := thrift.NewTSocket(net.JoinHostPort(HOST, PORT))    if err != nil {        log.Fatalln("tSocket error:", err)    }    transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())    transport := transportFactory.GetTransport(tSocket)    protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()    client := example.NewFormatDataClientFactory(transport, protocolFactory)    if err := transport.Open(); err != nil {        log.Fatalln("Error opening:", HOST + ":" + PORT)    }    defer transport.Close()    data := example.Data{Text:"hello,world!"}    d, err := client.DoFormat(&data)    fmt.Println(d.Text)}
  • 執行驗證結果:
    1. 先啟動 server,之後再執行 client
    2. client 側控制台如果列印的結果為: HELLO,WORLD! ,證明 Thrift 的 RPC 介面定義成功

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.