欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Thrift的安裝方法和簡(jiǎn)單實(shí)例

 更新時(shí)間:2016年12月20日 09:52:57   投稿:jingxian  
下面小編就為大家?guī)?lái)一篇Thrift的安裝方法和簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

本文只是簡(jiǎn)單的講解Thrift開(kāi)源框架的安裝和簡(jiǎn)單使用示例,對(duì)于詳細(xì)的講解,后面在進(jìn)行闡述。

Thrift簡(jiǎn)述  

Thrift是一款由Fackbook開(kāi)發(fā)的可伸縮、跨語(yǔ)言的服務(wù)開(kāi)發(fā)框架,該框架已經(jīng)開(kāi)源并且加入的Apache項(xiàng)目。Thrift主要功能是:通過(guò)自定義的Interface Definition Language(IDL),可以創(chuàng)建基于RPC的客戶端和服務(wù)端的服務(wù)代碼。服務(wù)代碼的生成是通過(guò)Thrift內(nèi)置的代碼生成器來(lái)實(shí)現(xiàn)的。Thrift 的跨語(yǔ)言性體現(xiàn)在,它可以生成C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml , Delphi等語(yǔ)言的代碼,且它們之間可以進(jìn)行透明的通信。

Thrift的安裝 

安裝版本為:Thrift v0.9.1

系統(tǒng)版本:Ubuntu 14.04 64位

基本安裝環(huán)境:

g++ 4.2
boost 1.53.0
libssl-dev

Thrift的編譯器即代碼生成器是由C++編寫(xiě)的,所以需要g++來(lái)進(jìn)行編譯安裝,且Thrift源碼中用到了boost庫(kù)中相關(guān)實(shí)現(xiàn),例如shared_ptr,所以要事先安裝boost庫(kù)。

Thrift通信過(guò)程中使用ssl對(duì)數(shù)據(jù)進(jìn)行安全包含,如果為安裝該庫(kù),會(huì)在configure時(shí)出現(xiàn)configure: error: "Error: libcrypto required."

Thrift提供了,TThreadSever, TThreadPoolServer, TNonblockingServer四種服務(wù)器框架,TSimpleServer以單一主線程阻塞的方式進(jìn)行事件處理,TThreadPoolServer以多線程阻塞的方式提供服務(wù),TNonblockingServer以多線程非阻塞方式工作。 TNonblockingServer服務(wù)模型的使用需要事先安裝libevent,libevent-dev庫(kù),libevent是異步事件處理的程序庫(kù),其包含我們常用的poll,select,epoll等異步處理函數(shù)。

安裝步驟:

 $./configure 
  $make 
  #sudo make install 

configure的結(jié)果最后一部分如下,其中Build TNonblockingServer .. : yes的結(jié)果對(duì)于使用異步的服務(wù)器模型是必須的。

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的時(shí)候會(huì)出現(xiàn)下面的bug,

ar: .libs/ThriftTest_constants.o: No such file or directory

不知道Makefile如何生成的,導(dǎo)致上面這個(gè)編譯文件路徑有問(wèn)題,解決方法有下面兩種:

method1:  
解決的方法時(shí)直接從Github(git://git.apache.org/thrift.git)上git clone 源碼,先運(yùn)行./bootstrap.sh,在按照configure安裝。  
  
method2:  
可以將已經(jīng)編譯的test/cpp/*.o復(fù)制到test/cpp/.libs后,繼續(xù)編譯就可以了  
cp test/cpp/*.o test/cpp/.libs/  

Thrift的簡(jiǎn)單示例

首先創(chuàng)建Thrift的語(yǔ)法規(guī)則文件,命名為server.thrift,內(nèi)容如下:

struct message  
{  
  1:i32 seqId,  
  2:string content  
}  
  
service serDemo  
{  
  void put(1:message msg)  
}  

在shell下面執(zhí)行執(zhí)行:
 
thrift -gen cpp server.thrift 

該語(yǔ)句用于創(chuàng)建c++服務(wù)框架,創(chuàng)建成功后會(huì)在該目錄下生成gen-cpp文件夾,然后修改該目錄下的serDemo_server.skeleton.cpp,在put函數(shù)中添加如下代碼:

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());  
 }  

然后進(jìn)行編譯就可以了:g++ -o server *.cpp -lthrift

上面是server框架的代碼,對(duì)于client的框架其實(shí)已經(jīng)創(chuàng)建,但是現(xiàn)在需要添加client執(zhí)行代碼,可以在該目錄下創(chuàng)建client.cpp,然后輸入以下內(nèi)容,下面的內(nèi)容可以作為SimpleServer的client的模板,只需修改注釋的部分。

// -------------------------替換成對(duì)應(yīng)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();   
   
  // ----------------------------我們的代碼寫(xiě)在這里------------------------------  
  message msg;  
  msg.seqId = 1;  
  msg.content = "client message";    
  client.put(msg);  
  //--------------------------------------------------------------------------  
  
  transport->close();   
   
  return 0;   
}  

然后進(jìn)行編譯:g++ -o client *[^n].cpp - lthrift,也可以將serDemo_server.skeleton.cpp移動(dòng)到其它目錄,使用g++ -o client *.cpp - lthrift命令。

然后就可以執(zhí)行了,啟動(dòng)server后,啟動(dòng)client,server執(zhí)行如下:

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

以上就是小編為大家?guī)?lái)的Thrift的安裝方法和簡(jiǎn)單實(shí)例全部?jī)?nèi)容了,希望大家多多支持腳本之家~

相關(guān)文章

最新評(píng)論