这一篇,我们来试着安装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
分享到:
相关推荐
Apache Thrift is an open source cross language serialization and RPC framework. With support for over 15 programming languages, Apache Thrift can play an important role in a range of distributed ...
Chapter 2: Installing and Running Apache Thrift Chapter 3: Running Your First Apache Thrift Service and Client Chapter 4: Understanding How Apache Thrift Works Chapter 5: Generating and Running Code ...
详细介绍了Apache Thrift在Ubuntu以及Windows下基于C++和Java语言的安装和运行。附有小例子,亲自测试通过。所述方法网上应该有教程,但大多零散不统一或者不完整,因此本人整理了一份,特来分享。
Learning Apache Thrift
第二部分-编程Apache Thrift 本部分深入研究Apache Thrift框架的每一层,详细检查传输,协议,类型,服务,服务器和Apache Thrift接口定义语言。 这些章节中的示例使用C ++,Java和Python作为演示语言。 C ++示例...
基于RPC技术的java开发,可用于分布式系统开发,高性能高可用。
本例改编自Apache Thrift教程: http://mikecvet.wordpress.com/2010/05/13/apache-thrift-tutorial-the-sequel/ http://chanian.com/2010/05/13/thrift-tutorial-a-php-client/ 原教程使用的是c++ server和...
NULL 博文链接:https://zhuwx.iteye.com/blog/2368356
NULL 博文链接:https://zhuwx.iteye.com/blog/2371657
Apache Thrift——可伸缩的跨语言服务开发框架
Apache Thrift Java实战源码,包含了客户端和服务端源码,客户端和服务端是分开的,如果需要放到一个工程,直接把Client.java文件复制到服务端运行即可。
NULL 博文链接:https://zhuwx.iteye.com/blog/2374326
FacebookThrift最初是紧跟Apache Thrift发行的,但现在朝着新的方向发展。 特别是,编译器是从头开始重写的,新的实现具有完全异步的Thrift服务器。 在了解有关这些改进的。 您还可以在原始的Facebook Code 了解...
使用Apache thrift通信框架编写的一个实例,以及提供开发中使用thrift所需的jar包
Thrift 在Windows下的安装与实例 vs2010 本文的不同之处在于,不借助Cygwin或者MinGW,只用VS2010,和Thrift官网下载的源文件,安装Thrift并使用。 本文可用于thrift-0.9.1.tar.gz,thrift-0.10.0.tar.gz版本安装
thrift0.12.0 Ubuntu 安装包及安装教程.tar.xz
要运行简单的套接字示例: 从 tutorial/rb 目录中的命令行,执行: ruby RubyServer.rb socket 从 tutorial/rb 目录中的第二个命令行,执行 RubyClient.rb socket 要运行 http 示例: 从 tutorial/rb 目录中的...
thrift入门学习教程
安装Thrift的官方文档地址: http://thrift.apache.org/docs/install/ 当我看到windows安装需要Cygwin或MinGW时,我就直接放弃在windows中配置的想法了,直接打开虚拟机用CentOS进行安装,使用一堆命令安装毕竟比...