Thrift的安装和简单示例

本文只是简单的讲解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等异步处理函数。

安装步骤:

    $./configure  
    $make  
    #sudo make install  
configure的结果最后一部分如下,其中 Build TNonblockingServer .. : yes 的结果对于使用异步的服务器模型是必须的。
    anonymalias@anonymalias-Rev-1-0:~/download/thrift-0.9.1$./configure  
    ......  
    thrift 0.9.1  
      
    Building C++ Library ......... : yes  
    Building C (GLib) Library .... : no  
    Building Java Library ........ : no  
    Building C# Library .......... : no  
    Building Python Library ...... : yes  
    Building Ruby Library ........ : no  
    Building Haskell Library ..... : no  
    Building Perl Library ........ : no  
    Building PHP Library ......... : no  
    Building Erlang Library ...... : no  
    Building Go Library .......... : no  
    Building D Library ........... : no  
      
    C++ Library:  
       Build TZlibTransport ...... : yes  
       Build TNonblockingServer .. : yes  
       Build TQTcpServer (Qt) .... : no  
      
    Python Library:  
       Using Python .............. : /usr/bin/python  
      
    If something is missing that you think should be present,  
    please skim the output of configure to find the missing  
    component.  Details are present in config.log.  
在本人电脑上make的时候会出现下面的bug,
ar: .libs/ThriftTest_constants.o: No such file or directory

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

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

Thrift的简单示例                                                             

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

    struct message  
    {  
        1:i32 seqId,  
        2:string content  
    }  
      
    service serDemo  
    {  
        void put(1:message msg)  
    }  
在shell下面执行执行:
 
thrift -gen cpp server.thrift  
该语句用于创建c++服务框架,创建成功后会在该目录下生成gen-cpp文件夹,然后修改该目录下的serDemo_server.skeleton.cpp,在put函数中添加如下代码:
    class serDemoHandler : virtual public serDemoIf {  
     public:  
      serDemoHandler() {  
        // Your initialization goes here  
      }  
      
      void put(const message& msg) {  
        // Your implementation goes here  
        printf("receive message: id: %d, content: %s\n", msg.seqId, msg.content.c_str());  
      }  
然后进行编译就可以了 : g++ -o server *.cpp -lthrift

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

    // -------------------------替换成对应service名字的.h  文件------------------------  
    #include "SerDemo.h"    
    //------------------------------------------------------------------------------  
    #include <transport/TSocket.h>    
    #include <transport/TBufferTransports.h>    
    #include <protocol/TBinaryProtocol.h>    
        
    using namespace apache::thrift;    
    using namespace apache::thrift::protocol;    
    using namespace apache::thrift::transport;    
        
    using boost::shared_ptr;    
        
    int main(int argc, char **argv) {    
        boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));    
        boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));    
        boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));    
        
        transport->open();    
        
        // ----------------------------我们的代码写在这里------------------------------  
        message msg;  
        msg.seqId = 1;  
        msg.content = "client message";      
        client.put(msg);  
        //--------------------------------------------------------------------------  
      
        transport->close();    
        
        return 0;    
    }  
然后进行编译: g++ -o client *[^n].cpp - lthrift ,也可以将serDemo_server.skeleton.cpp移动到其它目录,使用 g++ -o client *.cpp - lthrift 命令。

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

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

来自:http://blog.csdn.net/anonymalias/article/details/26154405
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值