`
ipjmc
  • 浏览: 702888 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

阅读更多
    这一篇,我们来试着安装Thrift,并运行Thrift自带的例子,以对Thrift有个感性的认识。

一、下载

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

    首先,在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


     如果没有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

二、源码的编译与安装

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


    如果编译有错误的话,请参照http://thrift.apache.org/docs/BuildingFromSource/

三、运行示例程序

    Thrift支持多种语言之间通信,一个作为服务器,另一个作为客户端,在示例程序中我们可以选择Python和Php,用Python作为服务器,Php作为客户端。下载的源码中自带示例程序,源码目录下面有个tutorial目录,里面有README,可以查看说明。使用方法如下:


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


    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。当然也有其他解决办法,解决方法看这里

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


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


    修改下面两行代码

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

    修改成功后运行Php客户端./php PhpClient.php
    如果出现找不到类的话,需要注意PHP命名空间的使用。


四、使用C++服务器

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

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

       安装C++ lib的方法是:


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


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

    下一步是更新Makefile文件, 可以参考我的Makefile(Wiki上也有更详细的说明),

    我的Makefile文件如下:


#
# 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

0
4
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics