作者:l沈佩玉iVf | 来源:互联网 | 2017-05-14 02:44
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 Map from one type to another
* list Ordered list of one type
* set 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()
以上就是解析tcp交互thrift的使用实例的详细内容,更多请关注 第一PHP社区 其它相关文章!