Apache Thrift 学习第二篇(安装&试用)

本文详细介绍Thrift在Ubuntu下的安装过程及配置方法,并通过Python和PHP示例演示跨语言RPC服务实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[size=medium] 这一篇,我们来试着安装Thrift,并运行Thrift自带的例子,以对Thrift有个感性的认识。[/size]

[size=large][b]一、下载[/b][/size]

[size=medium] 从官网下载最新版:[url]http://thrift.apache.org/[/url],在写这篇博客的时候,最新版稳定版是0.8.0。官网给的有安装教程以及系统要求,可参照[url]http://thrift.apache.org/docs/install/[/url],上面有各种系统下的编译安装方法。由于我的系统是ubuntu,所以就把ubuntu下thrift的编译安装教程放到这里。

首先,在ubuntu上,系统需要需要有如下组件:[/size]

sudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev


[size=medium] 如果没有JDK的话,还需要安装JDK。JDK的安装就不再细说了。

然后,如果Thrift支持下面的语言的的话还需要安装一些额外的包:

Ruby

ruby-full ruby-dev librspec-ruby rake rubygems libdaemons-ruby libgemplugin-ruby mongrel

Python

python-dev python-twisted

Php

php5-dev php5-cli

C_glib

libglib2.0-dev

Erlang

erlang-base erlang-eunit erlang-dev

Haskell

ghc6 cabal-install libghc6-binary-dev libghc6-network-dev libghc6-http-dev
[/size]
[size=large][b]二、源码的编译与安装 [/b][/size]

   #需要指定Boost库的位置,在我的机器上是/usr/include
./configure --with-boost=/usr/include
make -j 4 #4核编译
make install


[size=medium] 如果编译有错误的话,请参照[url]http://thrift.apache.org/docs/BuildingFromSource/[/url]
[/size]
[size=large][b]三、运行示例程序[/b][/size]
[size=medium]
Thrift支持多种语言之间通信,一个作为服务器,另一个作为客户端,在示例程序中我们可以选择Python和Php,用Python作为服务器,Php作为客户端。下载的源码中自带示例程序,源码目录下面有个tutorial目录,里面有README,可以查看说明。使用方法如下:[/size]

    cd tutorial
#我们用自带的例子生成Python和Php代码,下面两行代码执行后会创建目录gen-py和gen-php,里面有生成的代码。
thrift -r --gen py tutorial.thrift
thrift -r --gen php:namespace tutorial.thrift


[size=medium]tutorial里面已经有了现成的服务器代码,tutorial/py/PythonServer.py。但是在Thrift 0.8.0里面,这个代码是有问题的,需要修改

第84行,

transport = TSocket.TServerSocket(9090)

修改为transport = TSocket.TServerSocket(port = 9090)

否则,运行的时候会报错,TypeError: getaddrinfo() argument 1 must be string or None。而且我们在运行的时候,最好要先进入目录tutorial/py,然后以./PythonServer.py方式运行,否则由于PYTHONPATH的问题,还会有错误ImportError: No module named tutorial。当然也有其他解决办法,解决方法[url=http://www.thrift.pl/Thrift-tutorial-running-tutorial.html]看这里[/url]。

在运行PHP客户端之前,我们还有一些其他工作要做,建议这样配置:[/size]

    cd tutorial/php
cp -r ../../lib/php/src/ ./
cp -r ../gen-php ./src/packages/


[size=medium]修改下面两行代码

$GLOBALS['THRIFT_ROOT'] = './src';
$GEN_DIR = $GLOBALS['THRIFT_ROOT'].'/packages';

修改成功后运行Php客户端./php PhpClient.php
如果出现找不到类的话,需要注意PHP[url=http://php.net/manual/zh/language.namespaces.basics.php]命名空间[/url]的使用。
[/size]

[size=large][b]四、使用C++服务器[/b][/size]

[size=medium] 使用C++做服务器需要安装C++ lib,修改Makefile文件,参照wiki

[url]http://wiki.apache.org/thrift/ThriftUsageC%2B%2B[/url]

安装C++ lib的方法是:[/size]

cd thrift-0.8.0/lib/cpp/
make
sudo make install
sudo /sbin/ldconfig -v


[size=medium] 上面的第4步是更新系统lib库的,让系统库包含/usr/local/lib,否则运行C++ Server的时候,找不到libthrift-0.8.0.so。参考链接:[url]http://blog.csdn.net/shendl/article/details/5746062[/url]

下一步是更新Makefile文件, 可以参考我的Makefile([url=http://wiki.apache.org/thrift/ThriftUsageC%2B%2B]Wiki[/url]上也有更详细的说明),

我的Makefile文件如下:[/size]

#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

BOOST_DIR := /usr/include
THRIFT_DIR := /usr/local/include/thrift
LIB_DIR := /usr/local/lib

GEN_SRC := ./gen-cpp/SharedService.cpp ./gen-cpp/shared_types.cpp ./gen-cpp/tutorial_types.cpp ./gen-cpp/Calculator.cpp
GEN_OBJ := $(patsubst %.cpp, %.o, $(GEN_SRC))

INC = -I$(THRIFT_DIR) -I$(BOOST_DIR)
.PHONY: all clean

all: server client

%.o: %.cpp
$(CXX) -Wall -DHAVE_INTTYPES_H -DHAVE_NETINET_IN_H $(INC) -c $< -o $@

server: CppServer.o $(GEN_OBJ)
$(CXX) $^ -o $@ -L$(LIB_DIR) -lthrift

client: CppClient.o $(GEN_OBJ)
$(CXX) $^ -o $@ -L$(LIB_DIR) -lthrift

clean:
$(RM) *o server client
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值