Home  >  Article  >  Java  >  Thrift framework quick start method

Thrift framework quick start method

一个新手
一个新手Original
2017-10-10 09:38:271994browse

Introduction to Thrift
1. What is thrift?
Thrift was early developed by Facebook’s internal team. It is mainly used to implement method calls between languages. It is a type of remote method call. Later, the open source was incorporated into Apache and became the Apache Thrift project.
Thrift allows defining data types and service interfaces in a simple definition file as an input file, and the compiler generates code to easily generate RPC client and server communication for seamless cross-programming languages.

2. What is RPC?
RPC (Remote Procedure Call Protocol), remote procedure call protocol.

Simply put, RPC is to call another machine (server) from one machine (client) by passing parameters. A function or method and get the returned result.
RPC will hide the underlying communication details (no need to directly handle Socket communication or Http communication) RPC is a request response model.
The client initiates a request and the server returns a response (similar to how Http works). RPC is used to call remote functions (or methods) just like calling local functions (or methods).

Thrift stack structure

Thrift data type
Basic type
bool: Boolean value, true or false, corresponding to Java's boolean
byte: 8-bit signed integer, corresponding to Java's byte
i16: 16-bit signed integer, corresponding to Java's short
i32: 32 bit signed integer, corresponding to Java's int
i64: 64-bit signed integer, corresponding to Java's long
double: 64-bit floating point number, corresponding to Java's double
string: unknown encoded text or binary string , corresponding to Java's String
structure type:
struct: defines a public object, similar to the structure definition in C language, and is a JavaBean
in Java Container type :
list: Corresponds to Java's ArrayList
set: Corresponds to Java's HashSet
map: Corresponds to Java's HashMap
Exception type :
exception: Corresponding to Java's Exception
Service type:
service:Corresponding service class

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


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

Hello .java content
(1) Asynchronous client class AsyncClient and asynchronous interface AsyncIface
(2) Synchronous client class Client and synchronous interface Iface. The Client class inherits from TServiceClient and implements the synchronous interface Iface; Iface is based on Generated by the interface functions defined in the thrift file; the Client class is used when developing Thrift client programs. The Client class is the client stub implementation of Iface. Iface is used when developing the Thrift server, and the Thrift server program needs Implement the interface Iface.
(3) Processor class, this class is mainly used when developing Thrift server programs. This class defines a map internally, which saves the mapping of all function names to function objects. Once Thrift receives a function call request, Find the function object of the function from the map according to the function name, and then execute it;
(4) Parameter class, define a parameter class for each interface function, for example: generate a parameter class for the interface helloInt: helloInt_args, Generally, the naming method of interface function parameter class is: interface function name_args;
(5) Return value class. Each interface function defines a return value class, for example: a return value class is generated for the interface helloInt. : helloInt_result. Generally, the naming method of the interface function return value class is: interface function name_result;

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

Client

# Iface

HelloServiceImpl

Calling process

During the Thrift calling process, the three main core classes of transport layer class, protocol layer class and processing class are mainly used between Thrift client and server. The three classes cooperate with each other to complete the entire calling process of rpc

(1) Pass the function name and parameters called by the client program to the protocol layer (TProtocol), and the protocol layer encapsulates the function name and parameters according to the protocol format , and then the encapsulated result is handed over to the lower transport layer. Note here: The protocol type used by the Thrift server program must be the same, otherwise the Thrift server program will not be able to parse data at its protocol layer;
(2) The transport layer (TTransport) processes the data passed by the protocol layer. Processing, for example, the implementation class TFramedTransport of the transport layer encapsulates the data into a frame, that is, "data length + data content", and then sends the processed data to the Thrift server through the network; you also need to pay attention here: you need to communicate with the Thrift server The implementation class of the transport layer used by the program is consistent, otherwise Thrift's transport layer cannot reversely process the data;
(3) The Thrift server receives the call request data transmitted over the network through the transport layer (TTransport), and then Reverse processing of the received data. For example, the implementation class TFramedTransport of the transport layer converts network data in the form of "data length + data content" into the form of only data content, and then delivers it to the protocol class (TProtocol) of the Thrift server. );
(4) The protocol class (TProtocol) of the Thrift server decapsulates the data processed by the transport layer according to the protocol, and hands the decapsulated data to the Processor class for processing;

( 5) The Processor class of the Thrift server finds the function object corresponding to the function name based on the result of the protocol layer (TProtocol) parsing;
(6) The Thrift server uses the passed parameters to call the found function object;
(7) The Thrift server delivers the execution result of the function object to the protocol layer;
(8) The protocol layer of the Thrift server encapsulates the execution result of the function;
(9) The transport layer of the Thrift server Process the encapsulated results of the protocol layer, such as encapsulating them into frames, and then send them to the Thrift client program;
(10) The transport layer of the Thrift client program reversely processes the received network results to obtain the actual protocol data ;
(11) The protocol layer of the Thrift client decapsulates the data according to the protocol format, then obtains the specific function execution result, and delivers it to the calling function;

Server

Client

Output result

##Protocol and transmission method

Thrift allows users to select the type of transmission communication protocol between the client and the server. The transmission protocol is generally divided into text and binary transmission protocols. In order to save bandwidth and improve transmission efficiency, Generally speaking, most binary-type transmission protocols are used, and sometimes text-based protocols are also used. This needs to be based on the actual needs of the project/product. Commonly used protocols include the following:

TBinaryProtocol: It is Thrift's default protocol, using binary encoding format for data transmission, basically sending raw data directly
TCompactProtocol: Compressed, dense data transmission protocol, based on Variable-length Quantity’s zigzag encoding format
TJSONProtocol: Data transmission using JSON (JavaScript Object Notation) data encoding protocol
TDebugProtocol: Often used for testing by coders, presented in text form for easy reading


Commonly used transport layers include the following: TSocket - using blocking I/O for transmission, which is the most common mode
TFramedTransport - using non-blocking mode, transmitting by block size , similar to NIO
TNonblockingTransport in Java - using non-blocking method for building asynchronous clients
TServerSocket: non-blocking socket, used on the server side, the socket types accecpt receives are all TSocket (that is, blocking type socket)

The above is the detailed content of Thrift framework quick start method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn