Apache Thrift 是 Facebook 實現的一種高效的、支持多種編程語言的遠程服務調用的框架。 安裝thrift 從官網下載 然後按照./configure,make,make install 方式進行安裝,其他系統可以參照官網http://thrift.apache.org/docs/ ...
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 <language> <Thrift filename>
- 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()