开发步骤说明
【任务1】调通单机版的thrift-python版本
安装thrift
下载源码包
wget http://apache.fayea.com/thrift/0.9.3/thrift-0.9.3.tar.gz
安装thrift [进入thrift目录]
- 运行命令:
yum install openssl-devel.x86_64
[可选择是否安装] - 运行命令:
yum install boost-devel.x86_64
[必须安装] - 运行命令:
./configure --with-cpp --with-boost --with-python --without-csharp --with-java --without-erlang --without-perl --with-php --without-php_extension --without-ruby --without-haskell --without-go
- 运行命令:
yum install boost-devel-static
- 运行命令:
make
,make isntall
- 运行命令:
安装thrift对python的支持模块
pip install thrift==0.9.3
创建thrift模块文件并编译
- 创建RecSys.thrift文件 [创建到任意目录]
service RecSys {
string rec_data(1:string data)
}
- 命令:
thrift --gen py RecSys.thrift
,此时在当前目录中会产生gen-py
的文件夹,其中有当前的模块名字
开发python版的client和server
- 创建 server.py 文件测试
import sys
sys.path.append('../schema/gen-py/')
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from RecSys import RecSys
from thrift.server import TServer
from 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=9900)
# 设置传输层
tfactory = TTransport.TBufferedTransportFactory()
# 设置传输协议
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
handler = RecSysHandler()
server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
print 'Starting the server...'
server.serve()
print 'done'
- 创建 client.py 文件测试
#coding=utf-8
import sys
sys.path.append('../schema/gen-py/')
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from RecSys import RecSys
if __name__ == "__main__":
# 设置端口
transport = TSocket.TSocket('localhost', port=9900)
# 设置传输层
transport = TTransport.TBufferedTransport(transport)
# 设置传输协议
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = RecSys.Client(protocol)
transport.open()
rs = client.rec_data("are you ok ??")
print "receive return data: ", rs
transport.close()