Apache Thrift Usage Summary

Source: Internet
Author: User

Use feeling

The previous understanding of thrift was somewhat inaccurate, and it was found that thrift was much simpler than imagined.

The thing thrift do is cross-language distributed RPC, which declares interface classes and methods by writing. Thrift files, client calls the defined method, and the server-side implementation defines the interface. While it is true that RPC requires a network request, it is not like the NIO network programming library such as Netty (which also focuses on many of the details of the data being transmitted, such as how the data is serialized, how it is structured in a byte array, how to parse the byte array at both ends, how to handle the event state in the handler, How to string multiple handler in sequence, thrift masked the transfer of data this thing, developers use when the use of pure RPC feel.


Basic use

Thrift use up almost no matter what threshold, can read this article HelloWorld, although a bit old, but after reading the basic use up no matter what obstacles.

The example given by the authorities is more comprehensive, and the overall list of things that can be declared in the. Thrift file is more complete.

Here's a look at two. Thrift Definitions:

Shared.thrift

/** * This Thrift file can is included by other Thrift files, want to share * these definitions. */namespace java com.baidu.mordor.sink.servicestruct sharedstruct {  1:i32 key  2:string Value}service Sharedservice {  sharedstruct getstruct (1:i32 Key)}

Tutorial.thrift

/** * The first thing to know about is types. The available types in Thrift is: * * 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) * Map<t1,t2> map from one type to another * list&lt ;t1> Ordered List of one type * set<t1> set of unique elements of one type * * Did you also notice that T Hrift supports C style comments? *///Just in case you were wondering ... yes. We Support Simple C comments too./** * Thrift files can reference other Thrift files to include common struct * and Servic E definitions. These is found using the current path, or by * searching relative to any paths specified with The-i compiler flag. * * Included objects is accessed using the name of the. Thrift file as a * prefix. i.e. Gkfx. Sharedobject */include "Shared.thrift"/** * Thrift files can namespace, package, or prefix their output in various * target languages. */namespace Java com.baidu.mordor.sink.service/** * Thrift lets you does typedefs to get pretty names for your types. Standard * C style here. */typedef i32 myinteger/** * Thrift also lets you define constants for use across languages. Complex * Types and structs are specified using JSON notation. */const i32 int32constant = 9853const map<string,string> mapconstant = {' Hello ': ' World ', ' Goodnight ': ' Moon '}/** * Y ou can define enums, which is just-bit integers. Values is optional * and start at 1 if not supplied, C style again. */enum operation {ADD = 1, SUBTRACT = 2, MULTIPLY = 3, DIVIDE = 4}/** * Structs is the basic complex data structures . They is comprised of fields * which each has an integer identifier, a type, a symbolic name, and an * optional default V Alue. * * fields can be declared "optional", which ensures they would not being included * in the serialized output if THey aren ' t set. Note that this requires some * manual management in some languages. */struct work {1:i32 NUM1 = 0, 2:i32 num2, 3:operation op, 4:optional string comment,}/** * Structs can also be E Xceptions, if they is nasty. */exception invalidoperation {1:i32 What, 2:string why}/** * Ahh, now onto the cool part, defining a service. Services just need a name * and can optionally inherit from another service using the extends keyword. */service Calculator extends Gkfx. sharedservice {void ping (), i32 Add (1:i32 num1, 2:i32 num2), i32 Calculate (1:i32 logid, 2:work W) throws (1:invalid Operation Ouch),/** * This method has a oneway modifier. That means the client is makes * a request and does not listen for any response at all.    Oneway methods * must be void. */oneway void zip ()}

Thrift uses IDL (interface Definition Language), declares interface classes and methods in. thrift files, declares struct structures, const, exception, and so on, and can include other. thrift files, which are similar to C. By writing IDL and generate code, it is easy to use the Rpc,client to implement interface classes and structure classes between different languages.
The code of the example above can be downloaded from the official, can be put to the local to see his use, very easy.


Thrift Important Components

The three important components of the Thrift API are: Protocal,transport,server.


Protocal defines how messages are serialized. The common is tbinaryprotocol,tjsonprotocol,tcompactprotocol.


Transport defines how messages communicate between the client and the server. Common is the tsocket,tframedtransport,tnonblockingtransport and so on.


The server receives the serialized message from the transport side, deserializes it back from the protocal, then invokes the user-implemented message handler (the interface implementation Class), and finally serializes the returned data back to the client. The common tserver is tsimpleserver,thshaserver,tthreadpoolserver,tnonblockingserver,tthreadedselectorserver. The following details the characteristics of each Server, and developers need to choose a set of server+ corresponding transport+ corresponding protocol appropriate to their scenario.


Tserver illustrates several different tserver implemented by thrift. For Java, the version number is based on 0.9.0:

Tsimpleserver at the sever end, there is only one single thread of I/O blocking, each time just accept and serve a client, suitable for testing use, not for online services.


Tnonblockingserver has changed the drawbacks of clogging in Tsimpleserver, using selector in NIO to implement non-clogging I/O, agreeing to multiple client connections and the client being able to select with select (). However, the processing of messages and select () is the same thread, when there are a large number of client connections, performance is not ideal.


Thshaserver (semi-synchronous semi-asynchronous server) based on the above, a single thread is used to process network I/O, and a worker thread pool handles messages. The advantage is that only the spare worker thread, the message can be processed in a timely and parallel manner, the throughput will be larger.


Tthreadedselectorserver, the difference with Thshaserver is that processing network I/O is also multithreaded, it maintains two thread pools, one is responsible for network I/O, and one is responsible for data processing. The advantage is that performance is better than thshaserver when network I/O is a bottleneck.


Tthreadpoolserver has a dedicated thread to receive connections, and when the connection is established, it takes a worker thread from the Threadpoolexecutor to take charge of the connection until the thread goes back to the thread pool after the connection is disconnected, and the pool size is available. In other words, the size of concurrency can be set according to the server, if you do not mind to open many threads, Tthreadpoolserver is a good choice.


Complete the full text:)


Apache Thrift Usage Summary

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.