Apache Thrift 是 Facebook 实现的一种高效的、支持多种编程语言的远程服务调用的框架。
安装thrift 从官网下载 然后按照./configure,make,make install 方式进行安装,其他系统可以参照官网http://thrift.apache.org/docs/install/
定义thrift文件, 相当于通讯双方的接口定义,
bool Boolean, one byte * i8 (byte) Signed 8-bit integer * i16 Signed 16-bit integer * i32 Signed 32-bit integer * i64 Signed 64-bit integer * double 64-bit floating point value * string String * binary Blob (byte array) * map<t1,t2> Map from one type to another * list<t1> Ordered list of one type * set<t1> Set of unique elements of one type
用thrift生成关于thrift定义的代码,对应各种语言,我这主要是python,也可以其他的c++,java,命令是thrift --gen
server和client的编写,server要多考虑无阻塞,多进程,保证服务正常,调用thrift生成的类,对应定义好的数据类型。
def init_handler(): """ sub-process init handler """ signal.signal(signal.SIGINT, signal.SIG_DFL) signal.signal(signal.SIGTERM, signal.SIG_DFL) signal.signal(signal.SIGHUP, signal.SIG_DFL) signal.signal(signal.SIGUSR1, debug)def main(): """ The main entrance """ port = sys.argv[1] signal.signal(signal.SIGINT, exit_signal_handler) signal.signal(signal.SIGTERM, exit_signal_handler) signal.signal(signal.SIGHUP, exit_signal_handler) signal.signal(signal.SIGUSR1, debug) gc.set_threshold(20000, 10, 10) handler = MaitreyaHandler() processor = Maitreya.Processor(handler) transport = TSocket.TServerSocket(port=port) tfactory = TTransport.TBufferedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() # server = TProcessPoolServer(processor, transport, tfactory, pfactory) server = TGeventServer.TGeventProcessPoolServer(processor, transport, tfactory, pfactory) server.setNumWorkers(WORKER_NUM) server.setPostForkCallback(init_handler) print "server started..." server.serve()
Atas ialah kandungan terperinci 解析tcp交互thrift的使用实例. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!