1. 程式人生 > >thrift 安裝測試示例

thrift 安裝測試示例

2012-05-31

http://abloz.com

date:2012.5.31

官方:http://thrift.apache.org/ 安裝文件:http://thrift.apache.org/docs/install/ 下載地址:http://labs.renren.com/apache-mirror/thrift/0.8.0/thrift-0.8.0.tar.gz

[[email protected] ~]$ wget http://labs.renren.com/apache-mirror/thrift/0.8.0/thrift-0.8.0.tar.gz

[[email protected] ~]$ tar zxvf thrift-0.8.0.tar.gz
[
[email protected]
~]$ cd thrift-0.8.0 [[email protected] thrift-0.8.0]$ ./configure ... thrift 0.8.0 Building code generators ..... : Building C++ Library ......... : yes Building C (GLib) Library .... : no Building Java Library ........ : no Building C# Library .......... : no Building Python Library ...... : yes Building Ruby Library ........ : no Building Haskell Library ..... : no Building Perl Library ........ : no Building PHP Library ......... : yes Building Erlang Library ...... : no Building Go Library .......... : no Building TZlibTransport ...... : yes Building TNonblockingServer .. : no Using Python ................. : /usr/local/bin/python Using php-config ............. : /usr/bin/php-config [
[email protected]
thrift-0.8.0]$ make

centos 可能需要安裝其他環境: [[email protected] thrift-0.8.0]$ sudo yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel ubuntu 可能需要安裝的其他環境: sudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev

[[email protected] thrift-0.8.0]$ sudo make install

[[email protected] ~]$ mkdir test [[email protected] ~]$ cd test

教程地址: http://svn.apache.org/repos/asf/thrift/trunk/tutorial/ 下載檔案:

http://svn.apache.org/repos/asf/thrift/trunk/tutorial/tutorial.thrift
http://svn.apache.org/repos/asf/thrift/trunk/tutorial/shared.thrift
[[email protected] test]$ wget http://svn.apache.org/repos/asf/thrift/trunk/tutorial/tutorial.thrift

[[email protected] test]$ wget http://svn.apache.org/repos/asf/thrift/trunk/tutorial/shared.thrift

[[email protected] test]$ thrift -r --gen py tutorial.thrift
[FAILURE:/home/zhouhh/test/shared.thrift:26] No generator named 'd' could be found!

會編譯失敗,需將D語言的namespace註釋掉:

[[email protected] test]$ vi tutorial.thrift
#namespace d tutorial
[[email protected] test]$ vi shared.thrift +26
#namespace d share

[[email protected] test]$ thrift -r --gen py tutorial.thrift
[[email protected] test]$ ls
gen-py shared.thrift tutorial.thrift

下載python測試程式

[[email protected] py]$ wget http://svn.apache.org/repos/asf/thrift/trunk/tutorial/py/PythonClient.py
[[email protected] py]$ wget http://svn.apache.org/repos/asf/thrift/trunk/tutorial/py/PythonServer.py

[[email protected] py]$ python PythonServer.py
Traceback (most recent call last):
File "PythonServer.py", line 25, in <module>
from tutorial import Calculator
File "../gen-py/tutorial/Calculator.py", line 9, in <module>
from thrift.Thrift import TType, TMessageType, TException
ImportError: No module named thrift.Thrift

或者直接到原始碼裡面也自帶教程:

[[email protected] thrift-0.8.0]$ cd tutorial/
[[email protected] tutorial]$ ls
cpp csharp erl go hs java js perl php py py.twisted rb README shared.thrift tutorial.thrift
[[email protected] tutorial]$ cd py
[[email protected] py]$ ls
PythonClient.py PythonServer.py
[[email protected] tutorial]$ thrift -r --gen py tutorial.thrift

[[email protected] py]$ pwd
/home/zhouhh/thrift-0.8.0/lib/py
[[email protected] py]$ python setup.py --help
[[email protected] py]$ python setup.py build
[[email protected] py]$ sudo python setup.py install

[[email protected] py]$ pwd
/home/zhouhh/thrift-0.8.0/tutorial/py

[[email protected] py]$ python PythonServer.py
Starting the server...
Traceback (most recent call last):
File "PythonServer.py", line 95, in <module>
server.serve()
File "build/bdist.linux-x86_64/egg/thrift/server/TServer.py", line 74, in serve
File "build/bdist.linux-x86_64/egg/thrift/transport/TSocket.py", line 136, in listen
File "build/bdist.linux-x86_64/egg/thrift/transport/TSocket.py", line 31, in _resolveAddr
TypeError: getaddrinfo() argument 1 must be string or None

[[email protected] py]$ vi PythonServer.py

第84行, transport = TSocket.TServerSocket(9090) 改為: transport = TSocket.TServerSocket(port = 9090)

參考我另一篇文件,如何處理此錯誤: http://abloz.com/2012/05/31/thrift-typeerror-getaddrinfo-argument-1-must-be-the-string-or-none.html

[[email protected] py]$ python PythonServer.py
Starting the server...
Traceback (most recent call last):
File "PythonServer.py", line 95, in <module>
server.serve()
File "build/bdist.linux-x86_64/egg/thrift/server/TServer.py", line 74, in serve
File "build/bdist.linux-x86_64/egg/thrift/transport/TSocket.py", line 156, in listen
File "/usr/local/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 98] Address already in use
[[email protected] py]$ sudo netstat -antp | grep 9090
tcp 0 0 0.0.0.0:9090 0.0.0.0:* LISTEN 11369/java

[[email protected] py]$ python PythonServer.py
Starting the server...
Traceback (most recent call last):
File "PythonServer.py", line 95, in <module>
server.serve()
File "build/bdist.linux-x86_64/egg/thrift/server/TServer.py", line 74, in serve
File "build/bdist.linux-x86_64/egg/thrift/transport/TSocket.py", line 156, in listen
File "/usr/local/lib/python2.7/socket.py", line 226, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 98] Address already in use

[[email protected] py]$ vi PythonClient.py +36

修改 transport = TSocket.TServerSocket(9090) 為 transport = TSocket.TServerSocket(‘localhost’,19090) 或 transport = TSocket.TServerSocket(port=9090)

[[email protected] py]$ python PythonServer.py Starting the server…

[[email protected] py]$ python PythonClient.py ping() 1+1=2 InvalidOperation: InvalidOperation(what=4, why=’Cannot divide by 0’) 15-10=5 Check log: 5

server列印:

[[email protected] py]$ python PythonServer.py
Starting the server...
ping()
add(1,1)
calculate(1, Work(comment=None, num1=1, num2=0, op=4))
calculate(1, Work(comment=None, num1=15, num2=10, op=2))
getStruct(1)

如非註明轉載, 均為原創. 本站遵循知識共享CC協議,轉載請註明來源