(Transfer) Installation and usage example of Thrift under Windows and Linux platforms

Reprinted from the installation and usage examples of Thrift under Windows and Linux platforms

Introduction to thrift

Apache Thrift is an efficient RPC (Remote Service Call) framework implemented by Facebook that supports multiple programming languages.

The main purpose of this article is to introduce the installation steps of Thrift under Windows and Linux platforms, and to implement a simple demo to demonstrate the use of Thrift. More Thrift principles will be introduced later.

thrift installation

Source code download: thrift official website , or thrift-github address , I downloaded thrift-0.9.3.tar.gz .

Install dependent libraries

  1. The compilation of boost
    boost is no longer introduced here. I used boost1.55 or boost1.49 to compile and pass;
  2. Libevent
    is compiled on demand. If you do not need an asynchronous server, you can not compile libevent, otherwise you can click here to download libevent-2.0.21-stable;
  3. openssl
    download openssl library for your system version, there are compiled binary files under windows, you can download it directly, 32-bit/62-bit system openssl ; Linux distributions generally come with ssl library;

Installation of thrift under Windows

I compiled it on Windows7 64bit, VS2010. It is not troublesome to compile under Windows. A brief introduction is as follows:

  1. Unzip the source code, go to the lib\cpp directory, open Thrift.sln, there are two projects libthrift and libthriftnb, of which the libthrift project is a conventional blocking server side (single-threaded server, one connected to a thread server, thread pool server), the libthriftnb project is a server in non-blocking mode, and the libevent library needs to be relied on only when compiling libthriftnb, otherwise the libevent library can not be compiled;
  2. Set dependent library header files and library files, which will not be introduced anymore;
  3. Compile, if it goes well, it will be OK, libthrift.lib and libthriftnb.lib (if compiled) will be generated in the lib\cpp\Debug directory;

Note: The release of thrift-0.9.3 can't be compiled under Windows, because the Thrift.cpp to be compiled in the vs project no longer exists, and it can be compiled smoothly by removing it from the project. Refer to thrift- pull-739 .

In addition, you can also compile the thrift file generation tool yourself, of course, you can also download it directly from the official website . Here are the compilation steps:

  1. Copy the compiler\cpp\src\windows\version.h.in file to the compiler\cpp\src\ directory and rename it to version.h;
  2. Go to the compiler\cpp directory, open compiler.sln, and compile

Installation of thrift under linux (Centos)

I compiled it on Centos6.4 64bit, g++ 4.4.7. The compilation is very simple. You can use the cmake or make tool to compile it. I won't introduce it here. Of course, some libraries are missing during the compilation process. You can follow it first. For more detailed steps, please see the reference article link of this article.

Development steps

  1. Write a .thrift file, which is IDL (Interface Description File);
  2. Use Thrift's IDL generation tool ( thrift-0.9.1.exe with the download link provided above under Windows , and /usr/local/bin/thrift program under Linux), and then generate the target language code as needed;
  3. The server-side program introduces the code generated in step 2 to implement the RPC business code;
  4. The client-side program introduces the code generated in step 2 to implement the RPC calling logic;
  5. Using the program generated in step 4, you can call the remote service implemented in step 3;

Getting started example

The following demonstrates a simple server-side and client-side programs.

Design thrift file (IDL)

Assuming that such a simple service is implemented, the client sends its own name through the hello interface and needs a reply from the server, such as hello.thrift:

service HelloService
{
    void hello(1: string name);
}

Generate source code via IDL tools

Execute the thrift command to generate the source file :

thrift --gen cpp hello.thrift                 # centos下
thrift-0.9.3.exe --gen cpp hello.thrift   # Windows下
thrift-0.9.3.exe --gen py hello.thrift    # Windows下python代码

The above command means to generate the source code of the C++ language, and then a gen-cpp directory will be generated, which contains several automatically generated source code files:

hello_constants.cpp
hello_constants.h
HelloService.cpp
HelloService.h
HelloService_server.skeleton.cpp
hello_types.cpp
hello_types.h

Implement the server-side program

HelloService_server.skeleton.cpp is the default server-side program entry. You can directly modify the file, or copy a copy and then modify it (I copied and renamed it to server.cpp) in order to add your own logic processing:

class HelloServiceHandler : virtual public HelloServiceIf {
 public:
  HelloServiceHandler() {
    // Your initialization goes here
  }

  void hello(const std::string& name) {
    // Your implementation goes here
    // 这里只简单打印出client传入的名称
    printf("hello, I got your name %s\n", name.c_str());
  } 
};

If it is under the linux platform, compile directly through g++:

g++ -o server hello_constants.cpp  HelloService.cpp hello_types.cpp  server.cpp -I/usr/local/include/thrift -L/usr/local/lib -lthrift

If it is on the Windows platform, create a new win32 console project through vs2010, copy all the files in the gen-cpp directory to the new project, and set the header file inclusion and lib library directory. For example, set the header file directory of libthrift.lib to thrift-0.9.3\lib\cpp\src\thrift, and the lib library directory to thrift-0.9.3\lib\cpp\Debug.

Implement the client-side program

Because there is no default client implementation, you need to create a new client.cpp file and add the implementation yourself:

#include <stdio.h>
#include <string>
#include "transport/TSocket.h"
#include "protocol/TBinaryProtocol.h"
#include "server/TSimpleServer.h"
#include "transport/TServerSocket.h"
#include "transport/TBufferTransports.h"
#include "hello_types.h"
#include "HelloService.h"
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using boost::shared_ptr;

int main(int argc, char** argv)
{
    shared_ptr<TTransport> socket(new TSocket("localhost", 9090));
    shared_ptr<TTransport> transport(new TBufferedTransport(socket));
    shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
    HelloServiceClient client(protocol);
    try
    {
        transport->open();
        client.hello("cpper.info");
        transport->close();
    }
    catch(TException& tx)
    {
        printf("ERROR:%s\n",tx.what());
    }
}

If it is under the linux platform, compile directly through g++:

g++ -o client client.cpp hello_constants.cpp  HelloService.cpp hello_types.cpp -I/usr/local/include/thrift -L/usr/local/lib -lthrift

If it is under the Windows platform, create a new win32 console project through vs2010, copy all the files in the gen-cpp directory (except HelloService_server.skeleton.cpp) to the new project, and add the above manually implemented client.cpp .

Through the above steps, a simple RPC server and client program has been implemented, which can be run and tested separately to see how it works.

Reference

Thrift official installation manual (translation)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326038570&siteId=291194637