Thrift安装与使用

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值