Thrift Communication Framework
0 Introduction
Thrift is a software communication framework for the development of extensible and cross-lingual services, originally developed by Facebook in 2007 and entered the Apache Open source project in 2008. It combines powerful software stacks and code generation engines to build on C, C + +, Python, PHP, Ruby, Erlang, Perl, Haskell, C #, Cocoa, C++script, node. js, Smalltalk, and OCaml and so on, a seamless and efficient service between programming languages. Thrift allows you to define a simple definition of the data type in the file and the service interface as input files, the compiler generates code to easily generate RPC client and server communication seamlessly across programming languages.
1 Installation of thrift (windows/c++)
Thrift need to use several Open source libraries:
1) Boost (http://www.boost.org/)
Boost Library is a thoroughly tempered, portable, open source C + + library, as the backup of C + + standard library, is a C + + standardization process of the engine, is the "quasi" standard library, most of the use of the Boost library only need to include a header file, a few need to link the library. Some of the more famous libraries are regex regular expression library, thread portable C + + multiline libraries, pool memory pool management library, Smart_ptr smart pointer Library, etc.
2) Libevent (http://libevent.org/)
Libevent is a lightweight, open source high-performance network library written in C that features: event-driven, high-performance, lightweight, network-focused, cross-platform, and more.
First compile the boost and libevent to generate the corresponding LIB file, after the compiled file automatically stored location:
. \boost_1_61_0\stage\ (Simple compilation of the Boost library under Windows)
. \libevent\libevent-2.0.21-stable\ (under Windows compile and use libevent)
3) Download, install Libthrift (https://thrift.apache.org)
Download Thrift Compressed Package (v0.9.3), extract into \thrift-0.9.3\lib\cpp, use VS2010 and above to open Thrift.sln, there are LIBTHRIFT,LIBTHRIFTNB two projects. LIBTHRIFTNB Engineering is a non-blocking (non-blocking) mode server, and nonblocking mode requires a dependency on the Libevent library. Check the required include path and Lib path, and build Libthrift.lib and Libthriftnb.lib in \thrift-0.9.1\lib\cpp\debug (Release) after compilation is complete.
2 Thrift Foundation
( 1 ) data type
Basic Type :
BOOL: Boolean value, True or FALSE, corresponding to C + + bool
Byte:8-bit signed integer corresponding to C + + byte
I16:16-bit signed integer corresponding to the C + + short
I32:32-bit signed integer corresponding to C + + int
A i64:64-bit signed integer that corresponds to the C + + long
double:64 bit floating point number, corresponding to C + + double
STRING:UTF-8 encoded string, corresponding to C + + string
struct type :
struct: Defines a common object, similar to a struct definition in C
Container type :
List: List for C + +
Set: corresponding to C + + set
Map: Map corresponding to C + +
Exception type :
Exception: corresponding to C + + exception
Service Type :
Service: The class of the corresponding services
( 2 ) Communication Support
Supported data Transfer Protocol (transmission format)
Tbinaryprotocol: binary format.
Tcompactprotocol: Compression format
Tjsonprotocol:json format
Tsimplejsonprotocol: Provides JSON write-only protocol, resulting files are easily parsed by scripting language
Tips: The client and server protocols are consistent
Supported methods of data transfer
Tsocket-block-type Socker
tframedtransport– is used in a non-blocking service with frame as a unit of transmission.
The tfiletransport– is transmitted in file form.
The tmemorytransport– uses the memory for I/O. Java implementations use a simple bytearrayoutputstream.
tzlibtransport– is compressed using zlib and is used in conjunction with other transport modes. There is currently no Java implementation.
Supported service Models
tsimpleserver– Simple single-threaded service model, often used for testing teaching use
tthreadpoolserver– Multi-threaded service model, using standard blocking IO.
tnonblockingserver– multithreaded service model with non-blocking IO (using Tframedtransport data transfer method)
31 examples
(1), write IDL file
Write Calculate.idl as follows:
1 namespace CPP Calculate 2 Service Calculator 3 {4 i32 Add (12: i32 add_num2),5 i32 Subtract ( 1 2 : i32 sub_num2) 6 }
(2), compile the build framework file (c + +)
Thrift consists of two parts: the compiler (in the compiler directory, written in C + +) and the server (in the Lib directory), where the role of the compiler is to compile the user-defined thrift files to generate the corresponding language code, and the server is already implemented in advance, RPC server that can be used directly by users (and, of course, users can easily write their own server).
Open cmd in the same path as the IDL file, enter:thrift-0.9.3.exe--gen cpp calculate.idl
The result code is stored in the Gen-cpp directory, where the file description:
Definitions of some constants in the Calculate_constants:idl file
Data types defined in the Calculate_types:idl file
Services defined in the Calculator:idl file
Calculator_server.skeleton: Give a server-side code framework
(3), writing server servers
Copy the Calculator_server.skeleton.cpp content and add the code at the beginning of the main function:
1 // high version no longer needed 2 int 9090;
(4), writing client clients
1#include"Calculator.h"2#include <thrift/transport/TSocket.h>3#include <thrift/transport/TBufferTransports.h>4#include <thrift/protocol/TBinaryProtocol.h>5#include <thrift/server/TSimpleServer.h>6#include <thrift/transport/TServerSocket.h>7#include <thrift/transport/TBufferTransports.h>8 9 using namespaceApache::thrift;Ten using namespaceapache::thrift::p rotocol; One using namespaceApache::thrift::transport; A usingboost::shared_ptr; - using namespace:: Calculate; - intMainintargcChar**argv) the { -Shared_ptr<tsocket> socket (NewTsocket ("127.0.0.1",9090)); -Shared_ptr<ttransport> Transport (NewTbufferedtransport (socket)); -Shared_ptr<tprotocol> Protocol (NewTbinaryprotocol (transport)); + calculatorclient Client (protocol); - Try + { ATransport->open (); at } - Catch(ttransportexception) - { -Transport->close (); - } - in intRes_add = Client.add (1,2); -printf"Client.add (%d,%d) =%d\n",1,2, Res_add); to intRes_subtract = Client.subtract (3,2); +printf"Client.subtract (%d,%d) =%d\n",3,2, res_subtract); -Transport->close (); the * GetChar (); $ return 0;Panax Notoginseng}
Note : Both client and server must contain the necessary header files and the static library files required for the link.
Include Catalog: Project configuration page, Additional include Directories
Library Directory: Project configuration page, Linker, General-and Additional Library directories, and join Lib on input, Additional dependencies
In addition, the Include Path and library path for OpenSSL are required, which can be specified after installation.
As can be seen from the above example, the most important component of thrift is the compiler (written in C + +), which generates network communication-related code for the user, which greatly reduces the user's coding effort.
Thrift uses the C/S model, does not support two-way communication: The client can only remotely invoke the server-side RPC interface, but the client side does not have RPC for server-side calls, which means that the client side can actively communicate with the server side, However, the server side cannot actively communicate with the client side and can only respond passively to client requests. This RPC pattern has flaws in some applications, such as: Some applications, in most cases, the client side will be active to the server side of the request or send data to the server side, and in a few cases, the server side also need to actively send some commands to the client, Tells you to do certain things. In order to solve this problem, we usually use double client/server, both client and server. This program needs to establish two communication channels between the two sides of the communication, open two ports, which is more cumbersome, and very not elegant. However, it is still a common set of programmes currently in use.
Related information
L Apache Thrift Installation and development under Windows
L Dong's Blog: THIRFT Framework Introduction
L Dong's Blog: Thrift User Guide
L Dong's blog: Using thrift RPC to write programs
L Dong's blog: A brief talk on thrift internal realization principle
L thrift:scalable cross-language Services implementation
Ps:
Csdn question: Is there anyone under windows that has successfully run a thrift program with VS2010?
CSDN Download: Solve the problem in thrift0.9.3 CPP Lib compile time
Need examples of source code can give me a message ~
C + + library (Thrift)