This is a creation in Article, where the information may have evolved or changed.
Thrift was originally the RPC communication framework developed by Facebook and later contributed to the Apache Foundation, coming out earlier, almost all back-end languages, using a very broad, well-known network framework
Like GRPC, you need to define a communication protocol and then implement your own business logic, and here's a simple example (the previous ECHO program) that illustrates the use of thrift, and the following example uses the full code at the following address:
Implementation file: https://github.com/hatlonely/...
Protocol file: https://github.com/hatlonely/...
Simple echo Service
Get Thrift
go get git.apache.org/thrift.git/lib/go
Defining protocol Files
namespace go echostruct EchoReq { 1: string msg;}struct EchoRes { 1: string msg;}service Echo { EchoRes echo(1: EchoReq req);}
Executing a thrift -r --gen go echo.thrift
command generates a gen-go
folder, which in fact translates the above protocol into a Golang code
This command relies on the Thrift tool, which can be obtained with the following command
Mac
brew install thrift
Linux
Implement the service side
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) }}
This process and Grpc similar, the difference is that the thrift support more server types, support different protocol packaging, user-friendly choice, here the compact protocol is a compressed protocol, using more
Implementing the Client
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 (tr Ansport) 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 ())}
This client is relatively complex and needs to be packaged in a consistent manner with the server side, which requires special attention if there is a communication failure.
Reference links
- Thrift Go official website: Http://thrift.apache.org/tuto ...
- Thrift Github:https://github.com/apache/thr ...
- Thrift Go tutorial:https://github.com/apache/thr ...
Reprint please indicate the source
This article link: http://hatlonely.github.io/20 ...