Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of thrift

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

The main content of this article is to explain "what is the principle of thrift". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the principle of thrift"?

Brief introduction

Thrift is a rpc framework (remote procedure call) developed by Facebook and has been added to the Apache project.

Thrift is a RPC implementation using binary coding protocol and TCP/IP transport protocol, while XML-RPC/JSON-RPC/SOAP and WSDL protocol stack uses text protocol, and WSDL implementation WebService uses HTTP as the transport protocol. For network data transmission, the performance of TCP/IP protocol is higher than that of HTTP protocol, not only because HTTP protocol is an application layer protocol, and HTTP protocol transmits content in addition to the application data itself. There are also response status codes, Header information, and so on.

RPC introduction

It can be shown in a picture:

Thrift workflow

Design the services required

Write a Thrift IDL service description file according to the designed service

According to the written Thrift IDL service description file, use the code generation tool provided by Thrift to generate the code for the server and the client.

Realize the writing of server-side business logic and client-side calling code at the same time.

Run the server and the client

Internal operation of Thrift

The network stack of Thrift runtime consists of four parts: Transport, Protocol, Processor and Server. As shown in the following figure:

Transport

Transport: provides a way to read and write data over the network

In Thrift source code, it is used by packaging socket into various Transport

Java implementation method:

A, TSocket and TIOStreamTransport are based on blocking Imax O model.

B, TnonblockingTransort, TNonblockingSocket based on non-blocking model

C, TMemoryInputTransport, TMemoryBuffer encapsulation based on byte array as input and output stream

D, TFastFramedTransport is a memory read and write cache with higher memory utilization. It uses auto-growing byte [] (new only if the length is not enough), instead of creating a new byte [] every time, which improves the memory utilization.

Protocol

Transport: provides a concrete implementation of serialization / deserialization of network transmission data

Java implementation method:

A, TbinaryProtocol: binary format transfer protocol

B, TCompactProtocol: compressed binary format transfer protocol

C, TJSONProtocol:JSON format transfer protocol

D, TSimpleJSONProtocol: simple JSON format data transfer protocol

E, TDebugProtocol: text transfer protocol used in debugging

Protocol is the upper layer of transport. Transport is responsible for data transmission, but in order to make the program know what the transmitted data is, we have to rely on the component protocol to parse the data and parse it into the corresponding structural code for the program to call directly.

Processor

Thrift automatically generates Processor by using the written Thrift IDL description file, which reads data from the Protocol responsible for input, passes it to the processor, and sends the result to the Protocol responsible for output.

Processor: after passing through the transport and protocol layers, the program can already obtain the corresponding data structure, but the data structure needs to be used in order to be valuable. In Thrift, it is called by processor.

Server

Server combines Transport, Protocol and Processor to run the service and wait for the request from the caller at the specified port

Java implementation method:

A, TnonblockingServer: based on the multi-threaded non-blocking Istroke O model, it is suitable for high concurrency environments with large number of connections.

B, TthreadPoolServer: based on the multi-thread blocking I / O model, it consumes more system resources than TNonblockingServer.

C, ThsHaServer: semi-synchronous and asynchronous servers

D, TsimpleServer: a single-threaded blocking Ithreading O model is implemented, which is mainly used for testing and is not recommended in production environments.

Client

A client-side Client for synchronous calls, and a client-side AsyncClient for asynchronous calls

Thrift data type

Thrift implementation method

IDL descriptive language: when the data structure changes, you must re-edit the IDL file and generate the corresponding code

Annotation-based approach

Service @ ThriftService Annotation service Class @ ThriftMethod Annotation methods in this Class

Struct @ ThriftStruct annotations the model class @ ThriftField annotations the getter method in this class

Enum @ ThriftEnumValue Annotation enum

At this point, I believe you have a deeper understanding of "what is the principle of thrift". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report