Apache Thrift以及在python下使用
2016-08-19
apache thrift官网:https://thrift.apache.org/
Ubuntu下安装
1.首先安装环境所需的编译器和C++库
sudo apt-get install automake bison flex g++ git libboost1.55-all-dev libevent-dev libssl-dev libtool make pkg-config
Ubuntu12下,还需要安装以下依赖
wget http://ftp.debian.org/debian/pool/main/a/automake-1.15/automake_1.15-3_all.deb
sudo dpkg -i automake_1.15-3_all.deb
wget http://sourceforge.net/projects/boost/files/boost/1.60.0/boost_1_60_0.tar.gz tar xvf boost_1_60_0.tar.gz
cd boost_1_60_0
./bootstrap.sh
sudo ./b2 install
- 安装python所需要的依赖包
apt-get install python-all python-all-dev python-all-dbg
参考:https://thrift.apache.org/docs/install/debian
- 下载thrift安装包
wget http://apache.fayea.com/thrift/0.9.3/thrift-0.9.3.tar.gz
tar zxvf thrift-0.9.3.tar.gz
cd thrift-0.9.3
- 编译安装
./configure
make
make install
参考:https://thrift.apache.org/docs/BuildingFromSource
生成thrift文件
安装好了thrift之后,根据thrift格式规范,生成对应语言的thrift文件。
参考:https://thrift.apache.org/docs/idl
https://thrift.apache.org/docs/types
编写一个简单的helloworld.thrift
service HelloWorld {
void ping(),
i32 sayHello(),
i32 sayMsg(1:string msg)
}
编译thrift文件
thrift -v --gen py hellowworld.thrift
其中py就代表编译成python格式
之后就能在当前编译路径下看到这份编译好的文件目录gen-py
gen-py文件目录内容:
drwxrwxr-x 3 test test 4096 Aug 19 15:22 ./
drwxrwxrwt 10 root root 393216 Aug 19 16:00 ../
drwxrwxr-x 2 test test 4096 Aug 19 15:49 hellowworld/
-rw-rw-r-- 1 test test 0 Aug 19 15:22 __init__.py
helloword文件夹中的内容就是刚才定义的内容:
drwxrwxr-x 2 test test 4096 Aug 19 15:49 ./
drwxrwxr-x 3 test test 4096 Aug 19 15:22 ../
-rw-rw-r-- 1 test test 244 Aug 19 15:47 constants.py
-rw-rw-r-- 1 test test 11352 Aug 19 15:47 HelloWorld.py
-rwxr-xr-x 1 test test 2256 Aug 19 15:47 HelloWorld-remote*
-rw-rw-r-- 1 test test 48 Aug 19 15:47 __init__.py
-rw-rw-r-- 1 test test 394 Aug 19 15:47 ttypes.py
python下测试
首先在python下安装thrift模块
sudo pip install thrift
编辑test_server.py
#!/usr/bin/env python
import socket
import sys
sys.path.append('./gen-py')
from helloworld import HelloWorld
from helloworld.ttypes import *
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
class HelloWorldHandler:
def ping(self):
return "pong"
def say(self, msg):
ret = "Received: " + msg
print ret
return ret
handler = HelloWorldHandler()
processor = HelloWorld.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!"
编辑test_client.py
#!/usr/bin/env python
import sys
sys.path.append('./gen-py')
from helloworld import HelloWorld
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
try:
transport = TSocket.TSocket('localhost', 9090)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = HelloWorld.Client(protocol)
transport.open()
print "client - ping"
print "server - " + client.ping()
print "client - say"
msg = client.say("Hello!")
print "server - " + msg
transport.close()
except Thrift.TException, ex:
print "%s" % (ex.message)
接下来测试即可。
git地址:https://github.com/apache/thrift
git中有源码还有各种语言下的测试版本。