Thirft framework Quick Start and thirft Quick Start

Source: Internet
Author: User

Thirft framework Quick Start and thirft Quick Start

Thrift Introduction
1. What is thrift?
Thrift was initially developed by facebook's internal team and is mainly used for cross-language method calls. It is a remote method call. It was later incorporated into apache open source and became an apache thrift project.
Thrift allows you to define data types and service interfaces in a simple definition file as input files. The Compiler generates code to conveniently generate a seamless cross-programming language for RPC client and server communication.

2. What is RPC?
Remote Procedure Call Protocol (RPC.

 

To put it simply, RPC calls a function or method on another machine (server) and returns the result by passing parameters on one machine (client.
RPC hides the underlying communication details (Socket communication or Http Communication is not required). RPC is a request response model.
When a client initiates a request, the server returns a response (similar to the Http method). RPC calls a remote function (or method) like a local function (or method ).

Thrift stack structure

 

Thrift Data Type
Basic Type:
Bool: boolean value, true or false, corresponding to the Java boolean
Byte: an 8-bit signed integer corresponding to the Java byte
I16: A 16-bit signed integer corresponding to Java's short
I32: 32-bit signed integer, corresponding to Java int
I64: 64-bit signed integer, corresponding to the Java long
Double: 64-bit floating point number, corresponding to the double of Java
String: Unknown encoded text or binary String, corresponding to the Java string
Struct type:
Struct: defines public objects, similar to the struct definition in C language. In Java, It is a JavaBean
Container Type:
List: ArrayList corresponding to Java
Set: corresponding to Java HashSet
Map: corresponding to Java HashMap
Exception type:
Exception: Exception corresponding to Java
Service type:
Service: corresponding service class

Generate thrift java code
Http://thrift.apache.org/download

namespace java service.demoservice Hello{        string helloString(1:string para)        i32 helloInt(1:i32 para)        bool helloBoolean(1:bool para)        void helloVoid()        string helloNull()}

 

 

 

Hello. java content
(1) AsyncClient and AsyncIface
(2) Synchronization Client and synchronization interface Iface. The Client class inherits from TServiceClient and implements the synchronization interface Iface. Iface is generated based on the interface functions defined in the thrift file; the Client class is used when developing the Thrift Client program, and the Client class is the Client stub Implementation of Iface. Iface should be used when developing the Thrift server, the Thrift server program must implement the Iface interface.
(3) Processor class, which is mainly used when developing Thrift server programs. This class internally defines a map, which saves the ing between all function names and function objects, once Thrift receives a function call request, it finds the function object of the function based on the function name in the map and then executes it;
(4) parameter class: Define a parameter class for each interface function. For example, create a parameter class for the interface helloInt: helloInt_args. Generally, the interface function parameter class is named in the following way: interface function name_args;
(5) Return Value class. Each interface function defines a return value class. For example, it generates a return value class for the interface helloInt: helloInt_result. Generally, the interface function return value class is named as follows: interface function name_result;

The parameter class and return value class have read and write operations on data. In the parameter class, the called function names and parameters are encapsulated according to the protocol class. In the return value class, data will be read according to the protocol.

Client

Iface

 

HelloServiceImpl

 

Call Process

During the Thrift call process, the Thrift client and server mainly use three core classes: transport layer, protocol layer, and processing, these three classes work together to complete the entire rpc call process.

(1) Pass the function name and parameter called by the client program to the protocol layer (TProtocol). The protocol layer encapsulates the function name and parameter according to the protocol format, then the encapsulated result is sent to the lower transmission layer. Note: It must be the same as the protocol type used by the Thrift server program. Otherwise, the Thrift server program will not be able to parse data at its protocol layer;
(2) the transport layer (TTransport) processes the data transmitted from the protocol layer. For example, the transport layer implementation class TFramedTransport encapsulates the data into frames, that is, "Data Length + data content", and then the processed data is sent to the Thrift server over the network. Note the following: it must be consistent with the Implementation class of the transport layer used by the Thrift server program. Otherwise, the transport layer of Thrift cannot reverse process the data;
(3) The Thrift server receives the call request data transmitted over the network through the transport layer (TTransport), and then processes the received data in reverse order, for example, TFramedTransport, the implementation class of the transport layer, is to convert the network data in the form of "Data Length + data content" into the form of data content, and then deliver it to the Protocol Class (TProtocol) of the Thrift server );
(4) The Protocol Class (TProtocol) of the Thrift server unpacks the data processed by the transport layer according to the protocol, and submits the data after the unwrapped data to a Processor class for processing;

(5) The Processor class of the Thrift server finds the function object corresponding to the function name based on the parsing result of the protocol layer (TProtocol;
(6) The Thrift server uses the passed parameters to call the found function object;
(7) The Thrift server hand over the execution result of the function object to the protocol layer;
(8) The protocol layer on the Thrift server encapsulates the execution result of the function;
(9) the transport layer of the Thrift server processes the results encapsulated by the protocol layer, for example, encapsulated into frames, and then sent to the Thrift client program;
(10) the transport layer of the Thrift client processes the network results and obtains the actual protocol data;
(11) The protocol layer of the Thrift client unpacks the data according to the protocol format, obtains the specific function execution result, and delivers it to the called function;

Server

Client

Output result

 

 

Protocol and Transmission Mode

Thrift allows users to select the types of transmission communication protocols between the client and the server. The transmission protocols are generally divided into text and binary transmission protocols. To save bandwidth, improve transmission efficiency. Generally, most Binary-type transmission protocols are used, and sometimes text-based protocols are used, depending on the actual needs of the project/product. Common protocols include the following:
TBinaryProtocol: the default Thrift protocol. It transmits data in binary encoding format and basically directly sends original data.
TCompactProtocol: a compressed and intensive data transmission protocol. Based on Variable-length quantity, the zigzag encoding format
TJSONProtocol: data transmission using the JSON (JavaScript Object Notation) data encoding protocol
TDebugProtocol

There are several common transport layers:
TSocket-using blocking I/O for transmission is the most common mode
TFramedTransport-transmits data by block size in non-blocking mode, similar to NIO in Java
TNonblockingTransport -- uses a non-blocking method to build an asynchronous Client
TServerSocket: non-blocking socket, used on the server. The socket type from accecpt is TSocket (Block socket)

 

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.