I. Installation process 1. Install dependent libraries
]# yum install boost-devel-static libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev ant
2. Installing Thrift
Download thrift-0.9.3.tar.gz First, unzip and enter the thrift-0.9.3 directory
//需要支持的语言用--with, 不需要支持的语言用--without, 像ruby等语言最好去掉,否则可能会有一些兼容问题]# ./configure --with-cpp --with-boost --with-python --without-csharp --with-java --without-erlang --without-perl --without-php --without-php_extension --without-ruby --without-haskell
//安装Thrift的时候遇到,如下错误#./configure --prefix=/usr/local/thrifttrhift configure: error: "Error: libcrypto required."//解决办法://安装 openssl openssl-devel (centOS)#yum -y install openssl openssl-devel# ./configure --prefix=/usr/local/thrift
Second, the Thrift,python version of the single version 1. Install dependent libraries
]# pip install thrift==0.9.3
2. Writing the schema file
//创建schema目录,创建一个schema文件RecSys.thrift[[email protected] schema]# cat RecSys.thrift service RecSys{ string rec_data(1:string data)}
3. Using thrift to generate Python files
]# thrift --gen python RecSys.thrift//产生gen-py目录
4. Developing Python code
//client code: #coding =utf=8import syssys.path.append ('.. /schema/gen-py ') from thrift import thriftfrom thrift.transport import tsocketfrom thrift.transport Import Ttransportfrom thrift.protocol Import tbinaryprotocolfrom recsys import recsystry: # set Port transport = Tsocket.tsocke T (' localhost ', port=9090) # Set Transport Layer transport = Ttransport.tbufferedtransport (transport) # Set Transport protocol protocol = TBi Naryprotocol.tbinaryprotocol (transport) client = recsys.client (protocol) Transport.open () rst = Client.rec_data (" is OK!!! ") Print "Receive return Data:", rst transport.close () except Thrift.texception, ex:print "%s"% (ex.message)
pre>
// server 代码#coding=utf=8import syssys.path.append(‘../schema/gen-py‘)from thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocolfrom thrift.server import TServerfrom RecSys import RecSysfrom RecSys.ttypes import *class RecSysHandler(RecSys.Iface): def rec_data(self, a): print "Receive: %s" %(a) return "I‘m OK !!!"if __name__ == "__main__": # 实例化handler handler = RecSysHandler() # 设置processor processor = RecSys.Processor(handler) # 设置端口 transport = TSocket.TServerSocket(‘localhost‘, port=9090) # 设置传输层 tfactory = TTransport.TBufferedTransportFactory() # 设置传输协议 pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TThreadedServer(processor, transport, tfactory, pfactory) print ‘Starting the server...‘ server.serve() print ‘done.‘
Thrift installation and debugging of Python and C + + versions