Simple use of Apache thrift

Source: Internet
Author: User
Tags diff

Simple use of Apache thrift

----------------------

1. Introduction

Thrift is an open source project for Facebook, primarily a cross-language service development framework. It has a code generator to proactively generate the service Code framework for the IDL definition file it defines. Users only have to make two development before it, which is transparent to the underlying RPC communication. Now it supports languages such as C + +, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C #, Cocoa, Smalltalk, and OCaml.

2. Download and install

Be able to download its latest version number at http://incubator.apache.org/thrift/download/, now the latest version number is 0.5.0. In addition you can check out its SVN, such as the following:

SVN Co http://svn.apache.org/repos/asf/thrift/trunk Thrift

CD Thrift

As seen in its Jira, its 0.6 version number is also very fast coming out.

My laptop is Debian 6.0, assuming the same with Ubuntu's brother installation method

tar-zxvf thrift-0.5.0.tar.gzcd thrift-0.5.0./configuremake sudo make install

At this point the Thrift Code generator and some library files are generated.

You can use the following command to see the thrift version number information

thrift-version

3. A simple example

In the Thrift source folder has a folder called Tutorial, in which the execution of the thrift command to generate the corresponding service code:

$ thrift-r--gen cpp Tutorial.thrift//-R to the include file also generates the service code-gen is the language that generated the service code

After execution, you will see a gen-cpp folder in the current folder, which is the code generated by the Thrfit command.

At this point you CD to the Tutorial/cpp folder, perform make, generate the corresponding Cppserver and Cppclient program.

Then you can use the./cppserver to execute the server to listen to a specific port

You can then use the./cppclient to execute the client program to connect to the server and invoke its corresponding service. The default call will output such as the following information:

lemo@debian:~/workspace/facebook/thrift/thrift-0.5.0/tutorial/cpp$./cppserverstarting the server...ping () add (Calculate) (1,{4,1,0}) calculate (1,{2,15,10}) getstruct (1)

Let's say you have a message on your terminal, congratulations, it's done. Suppose you can't find the dynamic library when you execute cppserver, see if you've executed make install, assume it's done, and then execute sudo ldconfig. Then use LDD cppserver to see if it has found the corresponding dynamic library.

4. Sample Analysis

Analysis of 4.1 Thrift IDL

Here are two IDL files, such as the following:

Shared.thrift---------------* * This thrift file can is included by other thrift files, want to share * these Definit Ions. */namespace cpp sharednamespace java sharednamespace perl shared//here defines a struct, undefined method, corresponding to the generated code gen-cpp in Shared_ Types.h, there is a class called sharedstruct,//see there are two methods called Read and write, which is used to serialize and serialize the method./Yes, the I32 is the variable type defined in thrift IDL. corresponding to the int32_tstruct sharedstruct {1:i32 key 2:string value}//in the C + + language, a service defined here that is semantically similar to an interface defined in an object-oriented definition, the thrift compiler generates a set of The client and server-side methods that implement their interfaces//services are generally defined formats such as the following//service <name>//<returntype> <name> (<arguments>)//[ Throws (<exceptions>)]//...//}service Sharedservice {sharedstruct getstruct (1:i32 key)}tutorial.thrift------- ---------/** * Thrift files can reference other Thrift files to include common struct * and service 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 *////This IDL includes an IDL, which means that there is an inclusion "Shared.thrift" in the IDL object and the service is visible to it/** * Thrift files can namespace, package, or prefix their output in various * target languages. *//The namespace space of some languages is defined here namespace CPP tutorialnamespace java tutorialnamespace php tutorialnamespace perl tutorial/** * Thrift lets you does typedefs to get pretty names for your types. Standard * C style here. *///self-defined type typedef I32 myinteger/** * Thrift also lets you define constants for use across languages. Complex * Types and structs are specified using JSON notation. *//define some variables const I32 int32constant = 9853const map<string,string> mapconstant = {' Hello ': ' World ', ' Goodnight ': ' Moo N '}/** * You can define enums, which is just-bit integers. Values is optional * and start at 1 if not supplied, C style again. *//define enum type 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 value. * * fields can be declared "optional", which ensures they would not being included * in the serialized output if they aren ' t s Et. 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,//optional field type represented here Assuming the value of this field is not assigned, it will not be serialized out}/** * Structs can also be exceptions, if they is nasty. ///////This defines some exceptions exception invalidoperation {1:i32 What, 2:string why}/** * Ahh, then onto the cool part, defining a ser Vice. Services just need a name * and can optionally inherit from another service using the extends keyword. ////Here is the definition service, which inherits shared service Calculator extends shared. Sharedservice {/** * A method definition looks like C code. It has a return type, arguments, * and optionally a list of exceptions, which it may throw. Note that argument * lists and exception lisTS is specified using the exact same syntax as * field lists in struct or exception definitions. */void Ping (), i32 Add (1:i32 num1, 2:i32 num2), i32 Calculate (1:i32 logid, 2:work W) throws (1:invalidoperation Ouc h),/** * 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 ()}

4.2 Analysis of server-side and client code 4.2.1 C + + service side

The CppServer.cpp in the Tutorial/cpp folder is its service code, which is divided into two main parts,

part of the main method is used to do some initialization and service startup, the second part of the implementation of the interface defined in IDL

int main (int argc, char **argv) {//Defines RPC protocol factory, where binary protocol is used, you cannot use other protocols such as json,compact, etc. shared_ptr<tprotocolfactory > Protocolfactory (New Tbinaryprotocolfactory ());//This generates a user-implemented Calculatorhandler service and then helps set it up to a processor, It is mainly used for processing protocol input and output streams shared_ptr<calculatorhandler> handler (new Calculatorhandler ()); shared_ptr<tprocessor > Processor (new Calculatorprocessor (handler));//Generate a transport channel where the socket mode is used shared_ptr<tservertransport> Servertransport (New Tserversocket (9090));//Generate a transmission factory, which is mainly used to convert the above transport into a new application-layer transmission channel shared_ptr< Ttransportfactory> transportfactory (New Tbufferedtransportfactory ());//Generate a simple service end, which is a single-threaded server Tsimpleserver Server (processor, Servertransport, Transportfactory, Protoc olfactory);//You can also generate a multi-threaded server, which is to add a thread pool to it. However, it does not currently support process pooling, but it may be supported in the 0.7 version number. /** * Or could do one of these shared_ptr<threadmanager> Threadmanager = Threadmanager::newsimplethreadmanage R (Workercount); Shared_ptr<posixthreadfactory> threadfactory = shared_ptr<posixthreadfactory> (New posixthreadfactory ()); Threadmanager->threadfactory (threadfactory); Threadmanager->start (); Tthreadpoolserver Server (processor, Servertransport, Transportfactory, Protocolfactory, Threadmanager); Tthreadedserver Server (processor, Servertransport, Transportfactory, Protocolfactory); */printf ("Starting the server.../n"); Server.serve (); Start service printf ("done./n"); return 0;}

There are also some examples of the following:

//This part mainly implements the interface class, which is used to provide the corresponding service usage. Class Calculatorhandler:public Calculatorif {Public:calculatorhandler () {} void ping () {printf ("ping ()/n");} int32_ T Add (const int32_t n1, const int32_t n2) {...} int32_t calculate (const int32_t Logid, const work &work) {...} V OID Getstruct (sharedstruct &ret, const int32_t logid) {...} void Zip () {printf ("Zip ()/n");} protected:map<int32_t, sharedstruct> Log;};

4.2.2 C++client

int main (int argc, char** argv) {//Generate a socket to connect to the server shared_ptr<ttransport> socket ("localhost", 9090) ; Add buffer function to socket channel shared_ptr<ttransport> transport (new Tbufferedtransport (socket)); Generate the corresponding binary protocol, this should be consistent with the server, otherwise there will be incorrect protocol version number shared_ptr<tprotocol> protocol (new Tbinaryprotocol (transport)); Generate client's service object Calculatorclient Client (protocol); try {transport->open ();///Add-on service//Invoke pre-defined service interface client.ping (); printf ("Ping ()/n"); int32_t sum = Client.add (n); printf ("1+1=%d/n", sum); Work work; Work.op = operation::D ivide; WORK.NUM1 = 1; work.num2 = 0; try {int32_t quotient = client.calculate (1, work); printf ("Whoa?") We can divide by zero!/n "); } catch (InvalidOperation &io) {printf ("InvalidOperation:%s/n", Io.why.c_str ()); } work.op = Operation::subtract; WORK.NUM1 = 15; work.num2 = 10; int32_t diff = client.calculate (1, work); printf ("15-10=%d/n", diff); Note that C + + uses return by reference foR complex types to avoid//costly copy construction sharedstruct SS; Client.getstruct (SS, 1); printf ("Check log:%s/n", Ss.value.c_str ()); Close service Transport->close (); } catch (Texception &tx) {printf ("ERROR:%s/n", Tx.what ());}}

4.2.3 implementation of other code

There are examples of other code in the tutorial folder, such as Erl,java,python,perl,ruby.

5 References

1. http://incubator.apache.org/thrift/

2. Http://incubator.apache.org/thrift/static/thrift-20070401.pdf

Simple use of Apache thrift

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.