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:
- 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
- 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
- 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
- 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
- The Client.go in the client directory implements the data that clients use to send data and print after receiving server-side processing
- 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.
- 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
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 ()}
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:
- Start server first, then client
- Client Side Console If the result is: hello,world!, certificate RPC interface definition for Ming Thrift succeeded