Golang RPC's Thrift

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Thrift Introduction:

Thrift is a high-performance, open-source RPC framework that has been produced since Facebook and has contributed to Apache,thrift's upstream and downstream system of RPC, with its own serialized compilation tool, since Thrift uses binary serialization and is used with GRPC as long The connection establishes communication between the client and server, which is much faster than the traditional solution that uses short connections such as Xml,json,soap.
This article only introduces Golang's basic use of Thrift.

Installation

  • There are two scenarios for installing the Thrift Golang Library:

    1. Installed directly through the go Get command, the disadvantage is that most people may fail because of the irresistible network factor: $ go get git.apache.org/thrift.git/lib/go/thrift
    2. Install via Source:
      • Create multi-level catalogs in the SRC directory of the $GOPATH: Git.apache.org/thrift.git/lib/go
      • Download the source code for thrift 0.10.0 from GitHub, unzip it into the Thrift-0.10.0/lib/go directory, copy the thrift directory to the $GOPATH you just created/src/git.apache.org/thrift.gi Under the T/lib/go directory
      • Execute the Go install git.apache.org/thrift.git/lib/go/thrift in any directory to complete the installation of the Golang Thrift Library
  • Installing the Thrift IDL Compilation tool

    1. Installation under Windows platform:

      Direct download: Thrift complier Download Address, after the download is complete renamed: Thrift.exe and put it into the system environment variable can be used

    2. Linux Platform Installation:

      Download Thrift 0.10.0 Source from GitHub, unzip and enter: Thrift-0.10.0/compiler/cpp directory execute the following command after compiling, put it into the system environment variable can be used:
      $ mkdir Cmake-build
      $ CD Cmake-build
      $ cmake..
      $ make

  • Verify that the installation is successful:

    $ thrift-version If printed:Thrift version 0.10.0 indicates complier installation was successful

Practice:

Below we use Thrift to define an interface that implements the uppercase formatting of the incoming data.

    • Create Golang Project Thriftdemo project:

Paste_image.png
  1. The Client.go in the client directory implements the data that clients use to send data and print after receiving server-side processing
  2. The server.go in the server directory implements the service side to receive the data sent by the client, and then returns the data to the client when it is processed in uppercase.
  3. Thrift_file for storing thrift IDL files: *.thrift
    • Defining the Thrift RPC interface

Example.thrift:

namespace py examplestruct Data {    1: string text}service format_data {    Data do_format(1:Data data),}
    • Compiling the Thrift file

      Enter the Thrift_file directory to execute: $ thrift-out. --gen go example.thrift, the Golang package will be generated in Thrift_file's sibling directory: example, where Format_data-remote is the generated test code can be used without special attention


Paste_image.png
    • Implement server side:
 package mainimport ("Thriftdemo/example" "Strings" "git.apache.org/thrift.git/lib/go/t Hrift "" 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: = &formatdata impl{} 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 ()}  
    • Implement Client Side
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)}
    • performs validation:
      1. Start server first, then client
      2. Client Side Console If the result is: hello,world!, certificate RPC interface definition for Ming Thrift succeeded
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.