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
make
make 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 hello
struct User {
1: string firstname
2: 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.thrift
thrift -r --gen py hello.thrift
server.py
#!/usr/bin/env python
import sys
sys.path.append('./gen-py')
from hello import UserManager
from hello.ttypes import *
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
class UserManagerHandler:
def __init__(self):
pass
def 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;
}