Thrift安装与使用

来源:互联网 发布:更改windows桌面路径 编辑:程序博客网 时间:2024/04/26 04:12

centos   jdk6  php5.3以上  python2.6   thrift0.9.0

让thrift支持java php python,所以要安装java php python(linux系统默认自带python)
java: 安装JDK 和 ant (thrift安装时会检查 java javac ant等命令,ant命令要编译java的开发库)

安装thrift

yum install automake libtool flex bison pkgconfig gcc-c++ \            boost-devel libevent-devel zlib-devel python-devel./configure --with-java --with-php --with-python

makemake install
安装客户端开发库,在thrift的lib目录下,可以通过查看README来进行安装

Java Library: /usr/local/lib/libthrift-0.9.0.jar
Protocol Extension: /usr/local/lib/libthrift-0.9.0.so

PHP Script Library: /usr/lib/php/
Protocol Extension: /usr/lib64/php/modules/thrift_protocol.so
Protocol Extension Configuration: /etc/php.d/thrift_protocol.ini

Python Script Library: /usr/lib64/python2.6/site-packages/thrift
Protocol Extension: /usr/lib64/python2.6/site-packages/thrift/protocol/fastbinary.so

示例:python作为server端,php作为客户端

namespace php hellostruct User {1: string firstname2: string lastname}exception UserException {1: i32 error_code,2: string error_msg}service UserManager {void ping(),string get_user(1:string firstname,2:string lastname) throws(1:UserException e),oneway void clear_list()}
thrift -r --gen php hello.thriftthrift -r --gen py hello.thrift
server.py

#!/usr/bin/env pythonimport syssys.path.append('./gen-py')from hello import UserManagerfrom hello.ttypes import *from thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocolfrom thrift.server import TServerclass UserManagerHandler:def __init__(self):passdef ping(self):print 'Welcome To Thrift...'def get_user(self, firstname, lastname):if firstname == '':raise UserException(1, 'firstname is empty')if lastname == '':raise UserException(2, 'lastname is empty')return lastname+' '+firstname+'!'if __name__ == '__main__':handler = UserManagerHandler()processor = UserManager.Processor(handler)transport = TSocket.TServerSocket(port=9090)tfactory = TTransport.TBufferedTransportFactory()pfactory = TBinaryProtocol.TBinaryProtocolFactory()server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)print 'Starting the server...'server.serve()
client.py
#!/usr/bin/env php<?php$GLOBALS['THRIFT_ROOT'] = '/usr/lib/php';require_once $GLOBALS['THRIFT_ROOT'].'/Thrift/Transport/TTransport.php';require_once $GLOBALS['THRIFT_ROOT'].'/Thrift/Transport/TSocket.php';require_once $GLOBALS['THRIFT_ROOT'].'/Thrift/Protocol/TProtocol.php';require_once $GLOBALS['THRIFT_ROOT'].'/Thrift/Protocol/TBinaryProtocol.php';require_once $GLOBALS['THRIFT_ROOT'].'/Thrift/Transport/TBufferedTransport.php';require_once $GLOBALS['THRIFT_ROOT'].'/Thrift/Type/TMessageType.php';require_once $GLOBALS['THRIFT_ROOT'].'/Thrift/Factory/TStringFuncFactory.php';require_once $GLOBALS['THRIFT_ROOT'].'/Thrift/StringFunc/TStringFunc.php';require_once $GLOBALS['THRIFT_ROOT'].'/Thrift/StringFunc/Core.php';require_once $GLOBALS['THRIFT_ROOT'].'/Thrift/Type/TType.php';require_once $GLOBALS['THRIFT_ROOT'].'/Thrift/Exception/TException.php';require_once $GLOBALS['THRIFT_ROOT'].'/Thrift/Exception/TTransportException.php';require_once $GLOBALS['THRIFT_ROOT'].'/Thrift/Exception/TProtocolException.php';require_once './gen-php/hello/UserManager.php';require_once './gen-php/hello/Types.php';use Thrift\Protocol\TBinaryProtocol as TBinaryProtocol;use Thrift\Transport\TSocket as TSocket;use Thrift\Transport\TSocketPool as TSocketPool;use Thrift\Transport\TFramedTransport as TFramedTransport;use Thrift\Transport\TBufferedTransport as TBufferedTransport;use hello\UserManagerClient as UserManagerClient;try {$socket = new TSocket('localhost', 9090);$transport = new TBufferedTransport($socket, 1024, 1024);$protocol = new TBinaryProtocol($transport);$client = new UserManagerClient($protocol);$transport->open();$client->ping();printf("%s\n", $client->get_user('World', 'Hello'));$transport->close();} catch (UserException $e) {echo $e->error_msg;}

原创粉丝点击