Thrift Quick Start

Source: Internet
Author: User

Thrift Simple Example

2017-01-19 16:47:57

The first two examples are a simple way to feel the communication between the thrift (RPC) server and the client ...

RPC Learning----Thrift Quick Start and Java simple example

Thrift Introduction and Java instance Demo

What is Thrift?

Thrift from the famous Facebook hand, in 2007, Facebook submitted the Apache Foundation to Thrift as an open source project for the then Facebook to create Thrift to solve the Facebook system The cross-platform features are required for the communication of large data volumes between systems and the different language environments between systems. 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-performance communication middleware that supports data (object) serialization and multiple types of RPC services. Thrift applies to program static data exchange, need to determine his data structure, he is completely static, when the data structure changes, you must re-edit the IDL file, code generation, and then compile the loaded process, compared with other IDL tools can be regarded as the weakness of Thrift, Thrift is suitable for large-scale data exchange and storage of common tools, for large systems of internal data transmission in relation to JSON and XML in terms of performance, transmission size has obvious advantages.

Thrift is a specific implementation of the descriptive language of IDL (interface Definition Language), and we can trace the topic of IDL back to CORBA 1999-2001 (Common Object Request Broker Architecture/Common Object Request Broker architecture), we don't seem to forget these keywords in IDL: module, interface, String, long, and int, I remember IDL using module to create namespaces, and accurately mapped to the Java package, these features are almost identical to those of the current thrift, so thrift's design ideas and concepts are by no means anything new idea from Mars, to see the concept that CORBA has been in the prevailing era, the various section, we'll go back and compare it with thrift:

Thrift Infrastructure

Thrift is a server and client architecture architecture, from my personal senses to see Thrift is a similar xml-rpc+java-to-idl+serialization tools=thrift East, Thrift has its own internal Define the Transport protocol specification (TPROTOCOL) and transport Data Standard (Ttransports), through the IDL script on the data structure (struct) and the traffic data transfer business logic (service) according to the different operating environment to quickly build the corresponding code, and through its own internal serialization mechanism to simplify and compress the transmitted data to improve the cost of high concurrency, large system data interaction, depicting the overall architecture of Thrift, divided into 6 parts:

    1. 你的业务逻辑实现(Your Code)
    2. 客户端和服务端对应的 Service
    3. 执行读写操作的计算结果
    4. TProtocol
    5. TTransports
    6. 底层 I/O 通信

The first 3 parts of the figure are 1. The code that you generated from the Thrift script file, 2. The brown box in the figure is the code for the client and the processor that you build from the generated code, 3. The red part of the figure is the result of the 2-terminal calculation. The following 3 sections from the Tprotocol are the Thrift transport system and the transport Protocol as well as the underlying I/O communication, Thrift and provides plug-and-play, non-blocking, single threaded, multi-threaded mode running on the server, and can be run with the server/container, and the existing JEE server can be/web Seamless integration of containers.

Thrift Basic data types
  • bool:布尔值,true 或 false,对应 Java 的 boolean
  • byte:8 位有符号整数,对应 Java 的 byte
  • i16:16 位有符号整数,对应 Java 的 short
  • i32:32 位有符号整数,对应 Java 的 int
  • i64:64 位有符号整数,对应 Java 的 long
  • double:64 位浮点数,对应 Java 的 double
  • string:utf-8编码的字符串,对应 Java 的 String
  • struct:定义公共的对象,类似于 C 语言中的结构体定义,在 Java 中是一个 JavaBean
  • list:对应 Java 的 ArrayList
  • set:对应 Java 的 HashSet
  • map:对应 Java 的 HashMap
  • exception:对应 Java 的 Exception
  • service:对应服务的类
Thrift Data Transfer Protocol

Thrift allows you to select the type of communication protocol between the client and the server, which is generally divided into text and binary (binary) transport protocols on the transport protocol, to conserve bandwidth, provide transmission efficiency, and generally use a binary type of transport protocol for the majority, However, text-based protocols are sometimes used, depending on the actual requirements in the project/product.

    • TBinaryProtocol–二进制编码格式进行数据传输
    • TCompactProtocol–这种协议非常有效的,使用 Variable-Length Quantity(VLQ) 编码对数据进行压缩
    • TJSONProtocol–使用 JSON 的数据编码协议进行数据传输
    • TSimpleJSONProtocol–这种节约只提供 JSON 只写的协议,适用于通过脚本语言解析
    • TDebugProtocol–在开发的过程中帮助开发人员调试用的,以文本的形式展现方便阅读
Thrift Transport Layer
    • TSocket-使用堵塞式 I/O 进行传输,也是最常见的模式
    • TFramedTransport-使用非阻塞方式,按块的大小,进行传输,类似于 Java 中的 NIO
    • TFileTransport-顾名思义按照文件的方式进程传输,虽然这种方式不提供 Java 的实现,但是实现起来非常简单
    • TMemoryTransport-使用内存 I/O,就好比 Java 中的 ByteArrayOutputStream实现
    • TZlibTransport- 使用执行 zlib 压缩,不提供 Java 的实现
Thrift Service-side type
    • TSimpleServer-单线程服务器端使用标准的堵塞式 I/O
    • TThreadPoolServer-多线程服务器端使用标准的堵塞式 I/O
    • TNonblockingServer–多线程服务器端使用非堵塞式 I/O,并且实现了 Java 中的 NIO 通道
Comparison of Thrift and other transmission modes

XML is much larger than JSON, but the XML tradition is not complicated. JSON is small, novel, but not perfect. Thrift volume is very small, the use of more trouble, than the former two light, but for 1. High concurrency, 2. Large data transmission, 3. Multi-lingual environment, it is worthwhile to meet 2 points of use Thrift.

Service-side encoding basic steps:
    1. 实现服务处理接口impl
    2. 创建TProcessor
    3. 创建TServerTransport
    4. 创建TProtocol
    5. 创建TServer
    6. 启动Server
Client-side encoding basic steps:
    1. 创建TTransport
    2. 创建TProtocol
    3. 基于TTransport和TProtocol创建 Client
    4. 调用Client的相应方法

Tips: The client and server protocols are consistent

Java service-side code and explanations
1 ImportOrg.apache.thrift.protocol.TCompactProtocol;2 ImportOrg.apache.thrift.server.TServer;3 ImportOrg.apache.thrift.server.TSimpleServer;4 ImportOrg.apache.thrift.transport.TServerSocket;5 ImportOrg.apache.thrift.transport.TServerTransport;6 7  Public classHelloserverdemo {8     9     //Define the service port numberTen      Public Static Final intServer_port = 8090; One      A      Public Static voidMain (string[] args) { -Helloserverdemo Service =NewHelloserverdemo (); - service.startserver (); the     } -      -      Public voidStartServer () { -         Try{ +System.out.println ("HelloWorld tsimpleserver start ..."); -              +         /*Thrift Processor Business logic processing layer A Create a Processor,helloworldimpl implementation class as a parameter into the processor constructor method at processor main things to do: - 1. Encapsulate the method defined in IDL and eventually expose a unified interface for Thrift server invocation - 2. Encapsulating protocol and transport layers, including processing details for input and output streams, serialization deserialization*/             -Helloworldservice.processorNewHelloworldservice.processorNewHelloworldimpl ()); -              -             //Create Tservertransport inTservertransport Servertransport =NewTserversocket (server_port); -Tserver.args Targs =NewTserver.args (servertransport); to targs.processor (tprocessor); +             //Create Tprotocol -Targs.protocolfactory (Newtcompactprotocol.factory ()); the             //Create Tserver *Tserver Server =NewTsimpleserver (Targs); $             //Start TserverPanax Notoginseng Server.serve (); -}Catch(Exception e) { theSystem.out.println ("Server start error!"); + e.printstacktrace (); A         } the     } +}
View CodeJava Client code implementation and interpretation
1 Importorg.apache.thrift.TException;2 ImportOrg.apache.thrift.protocol.TCompactProtocol;3 ImportOrg.apache.thrift.protocol.TProtocol;4 ImportOrg.apache.thrift.transport.TSocket;5 ImportOrg.apache.thrift.transport.TTransport;6 Importorg.apache.thrift.transport.TTransportException;7 8 /**9  * @authorJxwchTen * @date 2017.01.17 One  * A  */ -  Public classHelloclientdemo { -      the     //Defining socket Service Parameters -      Public Static FinalString server_ip = "localhost"; -      Public Static Final intServer_port = 8090; -      Public Static Final intTIMEOUT = 30000; +      -  +      Public Static voidMain (string[] args) { AHelloclientdemo client =NewHelloclientdemo (); atClient.startclient ("Jxwch"); -     } -      -      Public voidstartclient (String userName) { -          -          inTtransport transport =NULL; -         Try{ to             //Create Ttransport +Transport =NewTsocket (server_ip, Server_port, TIMEOUT); -             //Create Tprotocol theTprotocol protocol =NewTcompactprotocol (transport); *             //Create client $Helloworldservice.client Client =Newhelloworldservice.client (protocol);Panax Notoginseng             //Start Ttransport - Transport.open (); the              +             //calling the encapsulated method in the client AString result =Client.sayhello (userName); theSystem.out.println ("Thrift Client Result:" +result); +}Catch(ttransportexception e) { - e.printstacktrace (); $}Catch(texception e) { $ e.printstacktrace (); -}finally{ -             if(NULL!=transport) { the transport.close (); -             }Wuyi         } the     } -}
View CodeThriftdemo Sample Download

Sample source Download

Thrift Quick Start

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.