Four steps to complete cross-platform calling-Thrift Development and Application

Source: Internet
Author: User

1. Install Thrift

First, you need to have a Linux platform. You just need to install it, and you don't need much.

 

 

ThriftThis can be downloaded at http://www.thri?rpc.org.

Specifically in the http://www.thrift-rpc.org /? P = thrift. Git; A = mongolog; H = refs/MISC/instant. Generally, just click the first snapshot. This is the latest one. The version was updated several hours ago.

 

 

Next, install thrift. Decompress the directory to thrift and run the following command:

# Chmod + x * // set the execution permission

#./Bootstrap. Sh

#./Configure

# Make

# Make install

 

Ii. Interface confirmation

The method call or data exchange between the platform and the platform depends on a set of structures and methods. You need to define this method for subsequent development. For example, I now need to call Java through C. If the Java server has been developed, it is very like the adapter mode.

 

Now you can write a simple interface:

 

Service thrifttest {   Void work ();   I32 test (1: String S );}

Service thrifttest

{

 Void work ();

 I32 test (1: String S );

}

(Note: For more complex interfaces, see the *. Thrift file under the thrift \ test directory in the downloaded package .)

 

Generate nowCode:

#/Home/xieping/thrift/Compiler/CPP/thrift-gen Java test. Thrift

#/Home/xieping/thrift/Compiler/CPP/thrift-gen CSHARP test. Thrift

 

Java and CSHARP applications are generated respectively, under the Gen-Java and gen-CSHARP directories.

 

Iii. Development Server

 

Copy the thrifttest class in Java to the project. Add the package. Define the service class thriftserver and implement the thrifttest. iface interface. (The project must reference the project in thrift \ Lib \ Java \ SRC. Also need to reference slf4j-log4j12-1.5.11.jar, slf4j-api-1.5.11.jar, log4j-1.3alpha-8.jar three packages .)

 

Import org. Apache. Thrift. texception;

 

Public class thriftserver implements thrifttest. iface {

 

@ Override

Public int test (string s) throws texception {

// Todo auto-generated method stub

Return integer. parseint (s );

}

 

@ Override

Public void work () throws texception {

// Todo auto-generated method stub

System. Out. println ("CCC ");

}

 

}

 

Write in the main method:

 

Try {

Thriftserver T = new thriftserver ();

Thrifttest. processor P = new thrifttest. processor (t );

Tservertransport servertransport = new tserversocket (9090 );

Tserver Server = new tthreadpoolserver (p, servertransport );

 

// Use this for a multithreaded Server

// Server = new tthreadpoolserver (processor, servertransport );

 

System. Out. println ("starting the server ...");

Server. Serve ();

 

} Catch (exception X ){

X. printstacktrace ();

}

System. Out. println ("done .");

 

 

 

There are several server types here. I used tthreadpoolserver to run and the server is OK.

 

Iv. Client call

The client copies the classes in gen-CSHARP to the project. Reference the project in thrift \ Lib \ CSHARP \ SRC.

 

Then write the code:

Static void main (string [] ARGs ){

TTransport transport = new tsocket ("localhost", 9090 );

Tprotocol protocol = new tbinaryprotocol (transport );

Thrifttest. Client client = new thrifttest. Client (Protocol );

Transport. open ();

Int val = client. Test ("1213 ");

Client. Work ();

Transport. Close ();

Console. writeline (VAL );

}

Run and the result is displayed.

 

The thrift cross-platform call is completed in four steps. Java and C # are better. php calls Java are more widely used.

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.