Thrift installation method and simple example _linux

Source: Internet
Author: User
Tags generator git clone

This article is just a brief explanation of the thrift Open Source framework installation and simple use of examples, for detailed explanation, followed by the elaboration.

Thrift Brief Introduction

Thrift is a scalable, Cross-language service development framework developed by Fackbook that has been open source and added to the Apache project. The main function of thrift is to create RPC-based client and service-side service code through a custom interface definition Language (IDL). The creation of the service code is achieved by thrift the built-in code generator. Thrift of Cross-language Now, it can generate C + +, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C #, Cocoa, JavaScript, Node.js, Smalltalk, OCaml , Delphi and other language code, and they can be transparent communication between.

Installation of Thrift

The installation version is: Thrift v0.9.1

System version: Ubuntu 14.04 64-bit

Basic Installation Environment:

g++ 4.2
Boost 1.53.0
Libssl-dev

Thrift compiler is the code generator is written by C + +, so need g++ to compile installation, and thrift source code used in the Boost library related implementation, such as shared_ptr, so to install boost library beforehand.

SSL is used to securely include data during thrift communication, and if the library is installed, the Configure:error will appear when configure: "Error:libcrypto required."

Thrift provides, Tthreadsever, Tthreadpoolserver, tnonblockingserver four server frameworks, Tsimpleserver event handling in a single main thread blocking manner, Tthreadpoolserver provides services in a multi-threaded manner, tnonblockingserver work in a multi-threaded non-blocking manner. The use of the Tnonblockingserver service model requires the prior installation of the Libevent,libevent-dev Library, Libevent is the library of asynchronous event processing, which contains our commonly used asynchronous processing functions, such as Poll,select,epoll.

Installation steps:

 $./configure 
  $make 
  

The final part of the results of the Configure is as follows, where the result of build tnonblockingserver.: Yes is necessary for using an asynchronous server model.

 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 ...: N... o Building C # Library ...: no building Python Library ...: Yes Building Ruby Library ...: No, Bui, no, No. Lding Haskell Library ...: No building the Perl Library ...: No building PHP Library ...: No building Er, No., No.  
  The lang Library ...: no building go Library ...: no building D Library ...: no C + + library:-----No. Build Tzlibtransport ...: Yes build tnonblockingserver ... : Yes Build Tqtcpserver (Qt) ...: No python library:using python ...:/usr/bin/python..... Something is missing so you are present, please skim the output of configure to find the missing Compone  NT. Details are present in Config.log. 

The following bug appears when I make it on my computer,

AR:. libs/thrifttest_constants.o:no such file or directory

Do not know how makefile generated, resulting in the above compiled file path problem, the solution has the following two kinds:

METHOD1:  
solve the method directly from GitHub (git://git.apache.org/thrift.git) git clone source code, run first./bootstrap.sh, in accordance with the Configure installation.  
  
method2:  
You can copy the compiled TEST/CPP/*.O to Test/cpp/.libs, and then you can continue to compile the  
CP test/cpp/*.o test/cpp/.libs/  

A simple example of thrift

First, create a thrift syntax rule file named Server.thrift, which reads as follows:

struct message  
{  
  1:i32 seqid,  
  2:string content  
}  
  
service Serdemo  
{  
  void put (1: Message msg)  
}  

Execute execution below the shell:

Thrift-gen cpp Server.thrift

Use this statement to create a C + + service Framework, where the creation of a successful Gen-cpp folder will be generated in that directory, and then modify the SerDemo_server.skeleton.cpp in the directory to add the following code to the PUT function:

Class Serdemohandler:virtual public Serdemoif {public  
 :  
 Serdemohandler () {  
  //Your initialization goes  
 }  
  
 void put (const message& msg) {  
  //Your implementation goes here  
  printf ("Receive message:id:%d, content:%s\n ", Msg.seqid, Msg.content.c_str ());  
 }  

Then you can compile it : g++-o server *.cpp-lthrift

Above is the server framework code, the client framework has actually been created, but now need to add the client execution code, You can create client.cpp in this directory, and then enter the following content, which can be used as a Simpleserver client template, just to modify the part of the annotation.

-------------------------replace the. h file with the service name------------------------#include "SerDemo.h"//------------------- -----------------------------------------------------------#include <transport/TSocket.h> #include <   
Transport/tbuffertransports.h> #include <protocol/TBinaryProtocol.h> using namespace Apache::thrift;   
Using namespace Apache::thrift::p rotocol;   
   
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 ();  
  ----------------------------Our code is written here------------------------------message msg;  
  Msg.seqid = 1;    
  Msg.content = "client Message";  
  Client.put (msg); //--------------------------------------------------------------------------Transport->close ();   
return 0;   }

Then compile:g++-O-Client *[^n].cpp-lthriftcan also move serDemo_server.skeleton.cpp to other directories, using g++-O-Client *.cpp-lthrift command.

Then you can do it, start the server, start the Client,server execution as follows:

anonymalias@anonymalias-rev-1-0:~/code/thriftserdemo/gen-cpp$./server  
  

The above is a small set for everyone to bring thrift installation methods and simple examples of all the content, I hope that we support cloud-Habitat Community ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.