当前位置:   首页安装配置安装框架:Thrift的安装方法和简单实例

安装框架:Thrift的安装方法和简单实例

发布日期:2022-04-20 15:59 | 文章来源:源码之家

本文只是简单的讲解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 

以上就是小编为大家带来的Thrift的安装方法和简单实例全部内容了,希望大家多多支持本站~

联系我们
关于使用场景和技术架构的更多咨询,请联系我们的销售和技术支持团队。
Yingsoo Host

在线
客服

在线客服:7*24小时在线

客服
热线

400-630-3752
7*24小时客服服务热线

关注
微信

关注官方微信
顶部