One: What is RPC
RPC Popular to understand is the remote call function, relative to the local call, only need to call the function in the keynote function can be, the code is as follows:
1 voidFuninti)2 {3cout <<"function Call"<<Endl;4cout <<"args:"<< I <<Endl;5 return;6 }7 8 intMain ()9 {TenFun5); One return 0; A}
In the above code, the main () function calls the local function fun () on line 10th, which is as simple as the local call. If you want to call a function remotely, then you need to network communication, network communication design to network programming, network programming has a famous classic book: "UNIX Network Programming", referred to as UNP, this book is basically the system Layer network programmer must read books, but read this book people know that The details of the network are many, but also more complex, if each project needs to write these low-level implementation, it will greatly delay the project development progress, and many of the upper-level developers do not understand these details. The solution is to build wheels, encapsulate these bottom-level details in a library, and block the bottom layer so that other developers can simply use it directly.
II: Installation of Thrift
Thrift is one of the RPC libraries mentioned earlier. , it is cross-lingual and is C/s mode.
Three: Using thrift for C + + and go communication
C + + Service side:
//This autogenerated skeleton file illustrates how to build a server.//You should copy it to another filename to avoid overwriting it.//system#include <time.h>#include<unistd.h>//Lib#include <thrift/protocol/TBinaryProtocol.h>#include<thrift/server/TSimpleServer.h>#include<thrift/transport/TServerSocket.h>#include<thrift/transport/TBufferTransports.h>using namespaceApache::thrift;using namespaceapache::thrift::p rotocol;using namespaceApache::thrift::transport;using namespaceApache::thrift::server;usingboost::shared_ptr;//Project#include"/home/archer/develop/thrift/gen-cpp/timeserve.h"using namespaceUtilityoftime;classTimeservehandler:Virtual PublicTimeserveif { Public: Timeservehandler () {//Your initialization goes here} int32_t Getcurrtenttime () {//Your implementation goes hereprintf"getcurrtenttime\n"); Sleep (1); returnTime (nullptr); }};intMain () {intPort =9090; shared_ptr<timeServeHandler> Handler (NewTimeservehandler ()); shared_ptr<TProcessor> Processor (NewTimeserveprocessor (handler)); shared_ptr<TServerTransport> Servertransport (NewTserversocket (port)); shared_ptr<TTransportFactory> Transportfactory (Newtbufferedtransportfactory ()); shared_ptr<TProtocolFactory> Protocolfactory (Newtbinaryprotocolfactory ()); Tsimpleserver Server (processor, Servertransport, Transportfactory, protocolfactory); Server.serve (); return 0;}
C + + client:
//system#include <iostream>//Lib#include <thrift/protocol/TBinaryProtocol.h>#include<thrift/transport/TSocket.h>#include<thrift/transport/TTransportUtils.h>using namespaceApache::thrift;using namespaceapache::thrift::p rotocol;using namespaceApache::thrift::transport;usingboost::shared_ptr;//Project#include"timeServe.h"using namespaceUtilityoftime;intMain () {//Get SocketShared_ptr<ttransport> socket (NewTsocket ("127.0.0.1",9090)); //Choose TransportShared_ptr<ttransport> Transport (NewTbufferedtransport (socket)); //SerializeShared_ptr<tprotocol> Protocol (NewTbinaryprotocol (transport)); Timeserveclient Client (protocol); //Open ConnectTransport->open (); Auto TimeNow=Client.getcurrtenttime (); Std::cout<< TimeNow <<Std::endl; Transport-Close (); return 0;}
Golang Client:
Package Mainimport ("FMT" "Git.apache.org/thrift.git/lib/go/thrift" "OS" "Timeserve") func main () {//Get socketsocket, ERR: = Thrift. Newtsocket ("127.0.0.1:9090")//Choose transporttransport: = Thrift. Newtbufferedtransport (socket, 8192)//serializeprotocolfactory: = Thrift. Newtbinaryprotocolfactorydefault () Client: = Timeserve. Newtimeserveclientfactory (transport, protocolfactory)//Open Connecttransport. Open () defer socket. Close () Timeresult, err: = client. Getcurrtenttime () if err! = Nil {fmt. Println (Err. Error ()) OS. Exit (2)}fmt. Println (Timeresult)}
Using thrift RPC for C + + and go communication