前言apache thrift 是 facebook 实现的一种高效的、支持多种编程语言的远程服务调用的框架。本文将从 python开发人员角度简单介绍 apache thrift 的架构、开发和使用。
thrift简介thrift network stacktransporttransport网络读写(socket,http等)抽象,用于和其他thrift组件解耦。
transport的接口包括:open, close, read, write, flush, isopen, readall。
server端需要servertransport(对监听socket的一种抽象),用于接收客户端连接,接口包括:listen, accept, close。
python中transport的实现包括:tsocket, thttpserver, tsslsocket, ttwisted, tzlibtransport,都是对某种协议或框架的实现。还有两个装饰器,用于为已有的transport添加功能,tbufferedtransport(增加缓冲)和tframedtransport(添加帧)。
在创建server时,传入的时tranport的工厂,这些factory包括:ttransportfactorybase(没有任何修饰,直接返回),tbufferedtransportfactory(返回带缓冲的transport)和tframedtransportfactory(返回帧定位的transport)。
protocolprotocol用于对数据格式抽象,在rpc调用时序列化请求和响应。
tprotocol的实现包括:tjsonprotocol,tsimplejsonprotocol,tbinaryprotocol,tbinarypotocolaccelerated,tcompactprotocol。
processorprocessor对stream读写抽象,最终会调用用户编写的handler已响应对应的service。
具体的processor有compiler生成,用户需要实现service的实现类。
serverserver创建transport,输入、输出的protocol,以及响应service的handler,监听到client的请求然后委托给processor处理。
tserver是基类,构造函数的参数包括:
1) processor, servertransport
2) processor, servertransport, transportfactory, protocolfactory
3) processor, servertransport, inputtransportfactory, outputtransportfactory, inputprotocolfactory, outputprotocolfactory
tserver内部实际上需要3)所列的参数,1)和2)会导致对应的参数使用默认值。
tserver的子类包括:tsimpleserver, tthreadedserver, tthreadpoolserver, tforkingserver, thttpserver, tnonblockingserver, tprocesspoolserver
tserver的serve方法用于开始服务,接收client的请求。
code generatedconstants.py: 包含声明的所有常量
ttypes.py: 声明的struct,实现了具体的序列化和反序列化
service_name.py: 对应service的描述文件,包含了:
iface: service接口定义
client: client的rpc调用桩
用法thrift的用法实际上很简单,定义好idl,然后实现service对应的handler(方法名、参数列表与接口定义一致接口),最后就是选择各个组件。需要选择的包括:transport(一般都是socket,只是十分需要选择buffed和framed装饰器factory),protocol,server。
示例idl文件/*
thrift接口定义文件
*/service helloservice { string say(1:string msg)
}
在编辑好定义文件后, 运行如下命令,生成thrift文件。可把hello目录移到当前目录下,便于后面调用。
thrift -r -gen py hello.thrift
server# coding: utf-8"""
thrift_client.py
"""import socketimport sysfrom hello import helloservicefrom hello.ttypes import *from thrift.transport import tsocketfrom thrift.transport import ttransportfrom thrift.protocol import tbinaryprotocolfrom thrift.server import tserverclass helloservicehandler:
def say(self, msg):
ret = "received: " + msg print ret return ret
handler = helloservicehandler()
processor = helloservice.processor(handler)
transport = tsocket.tserversocket("localhost", 9090)
tfactory = ttransport.tbufferedtransportfactory()
pfactory = tbinaryprotocol.tbinaryprotocolfactory()
server = tserver.tsimpleserver(processor, transport, tfactory, pfactory)print "starting thrift server in python..."server.serve()print "done!"
client# coding: utf-8"""
thrift_client.py
"""import sysfrom hello import helloservicefrom thrift import thriftfrom thrift.transport import tsocketfrom thrift.transport import ttransportfrom thrift.protocol import tbinaryprotocoltry:
transport = tsocket.tsocket('localhost', 9090)
transport = ttransport.tbufferedtransport(transport)
protocol = tbinaryprotocol.tbinaryprotocol(transport)
client = helloservice.client(protocol)
transport.open() print "client - say"
msg = client.say("hello!") print "server - " + msg
transport.close()except thrift.texception, ex: print "%s" % (ex.message)
运行结果$ ptyhon thrift_client.pyclient - say
server - received: hello!
$ python thrift_server.pystarting thrift server in python...received: hello!
小结本文只是一个简单的示例,在实际项目中,一般会基于zookeeper来注册和管理服务的thrift状态,并对server和client进一步封装,便于在项目各个模块中调用。
以上就是详解python中thrift的示例代码分享的详细内容。
