Installation method and simple example of Thrift

  • 2020-05-15 03:17:07
  • OfStack

This article is just a brief introduction to the installation and simple use of the Thrift open source framework.

Thrift briefly

Thrift is a scalable, cross-language service development framework developed by Fackbook, which is open source and part of the Apache project. The main function of Thrift is: through the custom Interface Definition Language(IDL), you can create the client and server side service code based on RPC. The generation of the service code is done through Thrift's built-in code generator. Thrift is trans-lingual in that it can generate codes for C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node. js, Smalltalk, OCaml, Delphi and other languages, and communicate transparently between them.

The installation of Thrift

The installed 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

The compiler or code generator of Thrift is written by C++, so it needs g++ to compile and install, and the Thrift source code USES the relevant implementation of boost library, such as shared_ptr, so you need to install boost library in advance.

configure: error: "Error: libcrypto required."

Thrift provides four kinds of server frameworks: TThreadSever, TThreadPoolServer, TNonblockingServer, TSimpleServer, TThreadPoolServer, TThreadPoolServer, TNonblockingServer, TNonblockingServer, TThreadSever, TThreadPoolServer, TNonblockingServer. The use of the TNonblockingServer service model requires the prior installation of the libevent, libevent-dev libraries. libevent is a library for asynchronous event handling, which contains the common poll, select, epoll and other asynchronous processing functions.

Installation steps:


 $./configure 
  $make 
  #sudo make install 

The last part of the configure results is as follows, where Build TNonblockingServer.. : the results of yes are necessary to use the 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 ........ : 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.  

When make appears on my computer, the following bug will appear.

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

I don't know how Makefile was generated, so there is a problem with the above compiled file path. There are two ways to solve this problem:


method1 :   
 The solution comes directly from Github(git://git.apache.org/thrift.git) on git clone  Source code, run first ./bootstrap.sh , in accordance with configure The installation.   
  
method2 :   
 Can be compiled test/cpp/*.o Copied to the test/cpp/.libs After that, you can continue to compile   
cp test/cpp/*.o test/cpp/.libs/  

A simple example of Thrift

First, create the Thrift grammar rules file, named server.thrift, which reads as follows:


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

Execute under shell:

thrift -gen cpp server.thrift

This statement is used to create c++ service framework. After successful creation, the gen-cpp folder will be generated in this directory, and then the serDemo_server.skeleton.cpp will be modified, and the following code will be added in the put function:


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

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

The above is the code of server framework. The framework of client has already been created, but now you need to add the client execution code. You can create client.cpp in this directory, and then enter the following.


// ------------------------- Substitute for the corresponding service The name of the .h  file ------------------------  
#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();   
   
  // ---------------------------- 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-lthrift, serDemo_server.skeleton.cpp can also be moved to other directories using the g++ -o client *. cpp-lthrift command.

Then you can execute it. After starting server, start client. server performs the following:


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

Related articles: