1. Brief description
This article mainly introduces thrift Multi-language, cross-language code examples. Thrift support for multiple languages is very good, define a thrift interface file, through thrift IDL compiler (code generation engine) to generate the code of each language, the respective language of the code into the respective language of the project, write good server and client programs, communication problems immediately resolved.
2. Simple architecture Diagram
Example Thrift interface File, Test8.thrift:
Service Testservice { string test (1:i32 num,2:string name)}
for code generation methods, file definition methods, see the Thrift Series article:
Quick Start: http://blog.csdn.net/hrn1216/article/details/51274934
Type definition:http://blog.csdn.net/hrn1216/article/details/51306395
3.python Code
After Python's underlying code is generated, it needs to be added to the project. Please make sure you have thrift module in your environment, if not please download the installation. : https://pypi.python.org/pypi/thrift/0.9.3. Installation method is very simple, after extracting the downloaded package, the command line into the thrift-0.9.3 directory, using the command: Python setup.py install can complete the installation of the module.
The following is the package structure of the Python project:
The following is the Python service-side code:
#-*-Coding:utf-8-*-from thrift.transport import tsocketfrom thrift.transport import Ttransportfrom Thrift.protocol Import tbinaryprotocolfrom thrift.server import tserver# introduce from TEST8 import testservice# based on the actual package structure Test8.thrift specific implementation class Testservicehandler: def __init__ (self): Self.log = {} def test (self,num,name): return name + str (num) If __name__ = = ' __main__ ': handler = Testservicehandler () processor = Testservice.processor (handler) transport = Tsocket.tserversocket (host= ' 127.0.0.1 ', port=9090) tfactory = Ttransport.tbufferedtransportfactory () pfactory = tbinaryprotocol.tbinaryprotocolfactory () Server = Tserver.tsimpleserver (processor, transport, tfactory, pfactory) print ' Python server:ready to start ' Server.serve ()
4.java Code
To add the generated Testservice.java to the Java project, here is the code for the Java client:
Package Test8;import Org.apache.thrift.texception;import Org.apache.thrift.protocol.tbinaryprotocol;import Org.apache.thrift.protocol.tprotocol;import Org.apache.thrift.transport.tsocket;import Org.apache.thrift.transport.ttransport;import Org.apache.thrift.transport.ttransportexception;public class Client {public static void main (string[] args) {//config service-side request Information ttransport transport = new Tsocket ("127.0.0.1", 9090); try {transport . open (); Tprotocol protocol = new Tbinaryprotocol (transport); Testservice.client Client = new testservice.client (protocol);//Interface Call String rs = Client.test (123, "Test");// Print Call result System.out.println ("Java Client:" + RS); Transport.close ();} catch (Ttransportexception e) {e.printstacktrace ();} catch (Texception e) {e.printstacktrace ()}}}
5.demo Operation
Run the Python server first, and if it works, it will print out: Python server:ready to start
Run the Java client again, and if it works, it will print out: Java client:test123
The contents of this article are as follows:
"1" http://thrift.apache.org/
Attention:
If you find a clear error in this document,
Or if you find that you have referenced someone else's information in this document without a description, please contact me for corrections.
Please make a note when reproducing or using this document.
Please contact the author if necessary, otherwise the legal liability will be investigated.
Note:
If you find the document with any error,
Or If you find any illegal citations, please contact me correct.
Reprint or use of this document,please explain for striking.
Please Contact the author if necessary, or they would pursue the corresponding legal responsibility.
Thrift Series-Multi-language instances for Java and Python