Thrift Framework Introduction

Source: Internet
Author: User

1. Preface

Thrift is a cross-language service deployment framework that was originally developed by Facebook in 2007 and entered the Apache Open source project in 2008. Thrift defines RPC interfaces and data types through an intermediate language (IDL, Interface Definition Language), and then generates code in different languages via a compiler (currently supports C++,java, Python, PHP, Ruby, Erlang, Perl, Haskell, C #, Cocoa, Smalltalk, and OCaml), and the generated code is responsible for the implementation of the RPC protocol layer and the Transport layer.

2. Architecture

Thrift actually implements the C/S mode, which generates the interface definition file from the server side and client code (which can be in different languages) through the Code generation tool, enabling cross-language support for both the service side and the client. Users declare their services in the Thirft profile, which are compiled to generate code files for the appropriate language, and then the user implements the service (client invoke service, server-side service). Where Protocol (protocol layer, the definition of data transmission format, can be binary or XML, etc.) and transport (transport layer, the definition of data transmission, can be TCP/IP transmission, memory sharing or file sharing, etc.) is used as a runtime library. 3. Supported data transmission format, data transmission mode and service model
(1) Supported transfer formats
tbinaryprotocol– in binary format.
tcompactprotocol– compression format
Tjsonprotocol–json format
tsimplejsonprotocol– provides JSON-only write-only protocols, and the resulting files are easily parsed by scripting languages.
tdebugprotocol– use easy-to-read, readable text format for debug
(2) Supported data transmission mode
Tsocket-block-type Socker
tframedtransport– is used in a non-blocking service with frame as a unit of transmission.
The tfiletransport– is transmitted in file form.
The tmemorytransport– uses memory for I/O. A simple Bytearrayoutputstream is actually used internally when Java is implemented.
tzlibtransport– is compressed using zlib and is used in conjunction with other transport modes. There is currently no Java implementation.
(3) Supported service models
tsimpleserver– Simple single-threaded service model, often used for testing
tthreadpoolserver– Multi-threaded service model, using standard blocking IO.
tnonblockingserver– multithreaded service model with non-blocking IO (using Tframedtransport data transfer method) 4. Thrift Installation and Deployment 4.1Thrift Installation

Download the latest version to the official website, as of today (2012-06-11) The latest version is 0.8.0.

1. If you are building a project with MAVEN, add the following directly to the Pom.xml:

<dependency> <groupId>org.apache.thrift</groupId> <artifactid>libthrift</artifactid > <version>0.8.0</version></dependency>

2. If you compile the Lib package yourself, unzip the downloaded package to the X: Disk, and then run Ant for automatic compilation in the X:\thrift-0.8.0\lib\java directory, the X:\thrift-0.8.0\lib\java\build\ Directory to see the compiled Lib package: Libthrift-0.8.0.jar

The main process for deploying services with Thrift: Write service instructions to the. Thrift file, compile the. thrift file as needed, generate the appropriate language source code, and write the client and server-side code as needed. 4.2Thrift Deployment

Thrift-0.8.0.exe is a Windows compiler tool available on the website that uses this tool to generate relevant code:

Generate source Code command: To generate Java code: Thrift-0.8.0.exe-r-gen java./demo.thrift, the result code is stored in the Gen-java directory client side and the sever side code to invoke the compilation. Thrift generated Intermediate File. On the client side, the user-defined object of type calculatorclient (the service name declared by the user in the. thrift file is calculator, the main class in the generated intermediate code is calculatorclient), which encapsulates the various services , you can call directly (such as client.ping ()), and then thrift will call the server-side function with the same name through the encapsulated RPC.
On the server side, all the features in the services declared in the. thrift file need to be implemented in order to process requests from the client. Reference: http://dongxicheng.org/search-engine/thrift-framework-intro/

Thrift Framework Introduction

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.