Thrift的安装和简单示例

文章出处:http://www.open-open.com/lib/view/open1400460273402.html


本文只是简单的讲解Thrift开源框架的安装和简单使用示例,对于详细的讲解,后面在进行阐述。

Thrift简述                                                                       

Thrift是一款由Fackbook开发的可伸缩、跨语言的服务开发框架,该框架已经开源并且加入的Apache项目。Thrift主要功能是:通过自定义的Interface Definition Language(IDL),可以创建基于RPC的客户端和服务端的服务代码。服务代码的生成是通过Thrift内置的代码生成器来实现的。Thrift 的跨语言性体现在,它可以生成C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml , Delphi等语言的代码,且它们之间可以进行透明的通信。

 

Thrift的安装                                                                   

安装版本为:Thrift v0.9.1

系统版本:Ubuntu 14.04 64位

基本安装环境:

  • g++ 4.2
  • boost 1.53.0
  • libssl-dev

Thrift的编译器即代码生成器是由C++编写的,所以需要g++来进行编译安装,且Thrift源码中用到了boost库中相关实现,例如shared_ptr,所以要事先安装boost库。

Thrift通信过程中使用ssl对数据进行安全包含,如果为安装该库,会在configure时出现configure: error: "Error: libcrypto required."

Thrift提供了,TThreadSever, TThreadPoolServer, TNonblockingServer四种服务器框架,TSimpleServer以单一主线程阻塞的方式进行事件处理,TThreadPoolServer以多线程阻塞的方式提供服务,TNonblockingServer以多线程非阻塞方式工作。 TNonblockingServer服务模型的使用需要事先安装libevent,libevent-dev库,libevent是异步事件处理的程序库,其包含我们常用的poll,select,epoll等异步处理函数。

安装步骤:

1 $./configure 
2 $make 
3 #sudo make install

configure的结果最后一部分如下,其中 Build TNonblockingServer .. : yes的结果对于使用异步的服务器模型是必须的。

1  anonymalias@anonymalias-Rev-1-0:~/download/thrift-0.9.1$./configure 
2  ...... 
3  thrift 0.9.1 
4   
5  Building C++ Library ......... : yes 
6  Building C (GLib) Library .... : no 
7  Building Java Library ........ : no 
8  Building C# Library .......... : no 
9  Building Python Library ...... : yes 
10 Building Ruby Library ........ : no 
11 Building Haskell Library ..... : no 
12 Building Perl Library ........ : no 
13 Building PHP Library ......... : no 
14 Building Erlang Library ...... : no 
15 Building Go Library .......... : no 
16 Building D Library ........... : no 
17   
18 C++ Library: 
19    Build TZlibTransport ...... : yes 
20    Build TNonblockingServer .. : yes 
21    Build TQTcpServer (Qt) .... : no 
22   
23 Python Library: 
24    Using Python .............. : /usr/bin/python 
25   
26 If something is missing that you think should be present, 
27 please skim the output of configure to find the missing 
28 component.  Details are present in config.log.

在本人电脑上make的时候会出现下面的bug,
ar: .libs/ThriftTest_constants.o: No such file or directory

不知道Makefile如何生成的,导致上面这个编译文件路径有问题,解决方法有下面两种:

1 method1: 
2 解决的方法时直接从Github(git://git.apache.org/thrift.git)上git clone 源码,先运行./bootstrap.sh,在按照configure安装。 
3    
4 method2: 
5 可以将已经编译的test/cpp/*.o复制到test/cpp/.libs后,继续编译就可以了 
6 cp test/cpp/*.o test/cpp/.libs/


Thrift的简单示例                                                            

首先创建Thrift的语法规则文件,命名为server.thrift,内容如下:

1  struct message 
2  { 
3      1:i32 seqId, 
4      2:string content 
5  } 
6   
7  service serDemo 
8  { 
9      void put(1:message msg) 
10 }

在shell下面执行执行:

1  thrift -gen cpp server.thrift
该语句用于创建c++服务框架,创建成功后会在该目录下生成gen-cpp文件夹,然后修改该目录下的serDemo_server.skeleton.cpp,在put函数中添加如下代码:


1  class serDemoHandler : virtual public serDemoIf { 
2   public: 
3    serDemoHandler() { 
4      // Your initialization goes here 
5    } 
6     
7    void put(const message& msg) { 
8      // Your implementation goes here 
9      printf("receive message: id: %d, content: %s\n", msg.seqId, msg.content.c_str()); 
10    }

然后进行编译就可以了 : g++ -o server *.cpp -lthrift

上面是server框架的代码,对于client的框架其实已经创建,但是现在需要添加client执行代码,可以在该目录下创建client.cpp,然后输入以下内容,下面的内容可以作为SimpleServer的client的模板,只需修改注释的部分。

1  // -------------------------替换成对应service名字的.h  文件------------------------ 
2  #include "SerDemo.h"   
3  //------------------------------------------------------------------------------ 
4  #include <transport/TSocket.h>   
5  #include <transport/TBufferTransports.h>   
6  #include <protocol/TBinaryProtocol.h>   
7     
8  using namespace apache::thrift;   
9  using namespace apache::thrift::protocol;   
10 using namespace apache::thrift::transport;   
11     
12 using boost::shared_ptr;   
13     
14 int main(int argc, char **argv) {   
15     boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));   
16     boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));   
17     boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));   
18     
19     transport->open();   
20     
21     // ----------------------------我们的代码写在这里------------------------------ 
22     message msg; 
23     msg.seqId = 1; 
24     msg.content = "client message";     
25     client.put(msg); 
26     //-------------------------------------------------------------------------- 
27    
28     transport->close();   
29      
30     return 0;   
31 }

然后进行编译: g++ -o client *[^n].cpp - lthrift,也可以将serDemo_server.skeleton.cpp移动到其它目录,使用 g++ -o client *.cpp - lthrift命令。

然后就可以执行了,启动server后,启动client,server执行如下:

1  anonymalias@anonymalias-Rev-1-0:~/code/thriftSerDemo/gen-cpp$ ./server  
2  receive message: id: 1, content: client message



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值