Thrift-java learning summary

The original text comes from [Tingyun Technology Blog]: http://blog.tingyun.com/web/article/detail/1083

What is Thrift? When to use thrift

Thrift originated from the well-known facebook. In 2007, facebook submitted the Apache foundation to use Thrift as an open source project. For facebook at that time, thrift was created to solve the transmission and communication of large amounts of data between various systems in the facebook system and the communication between systems. Different locales require cross-platform features. So thrift can support a variety of programming languages, such as: C++, C#, Cocoa, Erlang, Haskell, Java, Ocami, Perl, PHP, Python, Ruby, Smalltalk. Communication between many different languages ​​thrift can be used as a binary high-level High-performance communication middleware that supports data (object) serialization and various types of RPC services. Thrift is a specific implementation of IDL (interface definition language) descriptive language. Thrift is suitable for program-to-program static data exchange. It needs to determine its data structure first. It is completely static. When the data structure changes, It is necessary to re-edit the IDL file, code generation, re-compile and load process. Compared with other IDL tools, Thrift can be regarded as a weak point of Thrift. Thrift is suitable for building a general tool for large-scale data exchange and storage. For subsystems in large-scale systems Compared with JSON and XML, inter-data transmission has obvious advantages in terms of performance and transmission size.

Thrift infrastructure

Thrift is a server-side and client-side architecture system, which is socket transmission. Thrift has its own internally defined transmission protocol specification (TProtocol) and transmission data standard (TTransports). Through IDL script, the data structure (struct) and transmission data of transmission data are The business logic (service) quickly builds the corresponding code according to different operating environments, and simplifies and compresses the transmitted data through its own internal serialization mechanism to improve the cost of data interaction in high-concurrency and large-scale systems. The following figure depicts The overall architecture of Thrift is divided into 6 parts: 1. Your business logic implementation (You Code) 2. The Service corresponding to the client and the server 3. Calculation results of performing read and write operations 4. TProtocol 5. TTransports 6. Bottom layer I/O communication

Data Types for Thrift Scripts

 * Base Types: base types

bool        Boolean, one byte

byte        Signed byte

i16         Signed 16-bit integer

i32         Signed 32-bit integer

i64         Signed 64-bit integer

double      64-bit floating point value

string      String

binary      Blob (byte array)

 * Struct: structure type

 * Container: container type, namely List, Set, Map

map<t1,t2> Map from one type to another

list<t1>    Ordered list of one type

set<t1>     Set of unique elements of one type

 * Exception: exception type

 * Service: defines the interface of the object, and a series of methods

protocol

  Thrift可以让你选择客户端与服务端之间传输通信协议的类别,在传输协议上总体上划分为文本(text)和二进制(binary)传输协议, 为节约带宽,提供传输效率,一般情况下使用二进制类型的传输协议为多数,但有时会还是会使用基于文本类型的协议,这需要根据项目/产品中的实际需求:

    * TBinaryProtocol – 二进制编码格式进行数据传输。

    * TCompactProtocol – 这种协议非常有效的,使用Variable-Length Quantity (VLQ) 编码对数据进行压缩。

    * TJSONProtocol – 使用JSON的数据编码协议进行数据传输。

    * TSimpleJSONProtocol – 这种节约只提供JSON只写的协议,适用于通过脚本语言解析

    * TDebugProtocol – 在开发的过程中帮助开发人员调试用的,以文本的形式展现方便阅读。

传输层

    * TSocket- 使用堵塞式I/O进行传输,也是最常见的模式。

    * TFramedTransport- 使用非阻塞方式,按块的大小,进行传输,类似于Java中的NIO。

    * TFileTransport- 顾名思义按照文件的方式进程传输,虽然这种方式不提供Java的实现,但是实现起来非常简单。

    * TMemoryTransport- 使用内存I/O,就好比Java中的ByteArrayOutputStream实现。

    * TZlibTransport- 使用执行zlib压缩,不提供Java的实现。

服务端类型

    * TSimpleServer -  单线程服务器端使用标准的堵塞式I/O。

    * TThreadPoolServer -  多线程服务器端使用标准的堵塞式I/O。

    * TNonblockingServer – 多线程服务器端使用非堵塞式I/O,并且实现了Java中的NIO通道。

Thrift构建步骤总结(参考实例1)

1 、下载thrift.exe

到thrift官网下载,这里我用的是0.9.3版本

http://thrift.apache.org/

2、将exe放到C:\Windows下(重命名为thrift.exe)

3、C:\Windows创建.thrift文件,编写ThriftServer.thrift代码

4、在目录下执行thrift.exe -r -gen java ./ThriftServer.thrift;执行成功后看到在该目录下生成了gen-java文件夹,在该文件夹中生成了IThriftServer.java;

5、将IThriftServer.java拷贝到server端,进行后续编写即可。

6、如果client和server端是跨应用,那么需要server端先编译打包成jar,将这个jar添加到client端的依赖中。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327040185&siteId=291194637