about u
Thrift is an open source, cross-language, RPC service framework. Thrift was originally developed by Facebook, and in 2007 Facebook submitted it to the Apache Foundation for open source. For Facebook at the time, the creation of thrift was designed to address the cross-platform nature of the large data traffic between systems in the Facebook system and the different language environments between the systems. So thrift can support a variety of programming languages, supported by the following languages:
Communication between many different languages thrift can be used as a binary high-performance communication middleware that supports data (object) serialization and multiple types of RPC services. Thrift is a specific implementation of the descriptive language of IDL (interface Definition Language), thrift is suitable for the program to the static data exchange, need to determine his data structure, he is completely static, when the data structure has changed, You must re-edit the IDL file, code generation, and then compile the loaded process, compared with other IDL tools can be regarded as the weakness of thrift, thrift for large-scale data exchange and storage of common tools, for large-scale systems in the inter-subsystem data transfer in relation to JSON and XML regardless of the performance , the transmission size has obvious advantages.
U Infrastructure
As shown in the overall architecture of the Thrift protocol stack, thrift is a client and server-side architecture (c/s), which is the user-implemented business logic code at the top level. The second layer is the code generated automatically by the thrift compiler, primarily for parsing, sending, and receiving structured data. Tserver primary task is to efficiently accept client requests and forward requests to processor processing. Processor is responsible for responding to client requests, including RPC request forwarding, call parameter resolution and user logic calls, return value writeback, and so on. The following sections from Tprotocol are the THIRFT transport protocol and the underlying I/O communication. Tprotocol is used for data type resolution, transforming structured data into a byte stream for ttransport transmission. Ttransport is a transport layer that is closely related to the underlying data transfer, and is responsible for receiving and sending the message body in a byte stream, without paying attention to what data type. The underlying IO is responsible for the actual data transfer, including sockets, files, and compressed data streams.
Protocol Layer Tprotocol:
On the transport protocol, which is generally divided into text (literal) and binary (binary) transport protocols, to save bandwidth, to provide transmission efficiency, typically using a binary type of transport protocol for the majority.
1>tbinaryprotocol– binary encoded format for data transfer.
2>tcompactprotocol– is a highly efficient encoding that compresses data using a protobuffer-like Variable-length Quantity (VLQ) encoding that can save transmission space and make the data more efficient to transmit. About VLQ Learn More (http://en.wikipedia.org/wiki/Variable-length_quantity)
The 3>tjsonprotocol– uses the JSON data encoding protocol to carry out the transmission.
4>tsimplejsonprotocol– This savings only provides JSON-only protocols for parsing through scripting languages
5>tdebugprotocol– in the development process to help developers debugging, in the form of text to display the convenience of reading.
Transport Layer Ttransport
1>tsocket-is the most common mode of transmission using blocking I/O.
The 2>tframedtransport-is transmitted in a non-blocking way, by the size of the block, similar to NiO in Java.
3>tfiletransport-, as the name implies, processes the files in a way that does not provide Java implementations, but is very simple to implement.
4>tmemorytransport-uses memory I/O, just like the Bytearrayoutputstream implementation in Java.
5>tzlibtransport-uses perform zlib compression and does not provide Java implementations.
The 6>tnonblockingtransport-uses non-blocking methods for building asynchronous clients.
Service-Side type:
1>tsimpleserver-the single-threaded server side uses standard blocking I/O.
2>tthreadpoolserver-multithreaded server side uses standard blocking I/O.
3>tnonblockingserver– uses non-blocking I/O on a multithreaded server side and implements the NIO channel in Java.
U data Type
Thrift scripts can define data types that include the following types:
- Basic type:
- BOOL: Boolean, True or False, Boolean corresponding to Java
- A byte:8-bit signed integer that corresponds to the byte of Java
- I16:16-bit signed integer that corresponds to the short of Java
- I32:32-bit signed integer that corresponds to the int of Java
- i64:64-bit signed integer that corresponds to a long in Java
- double:64 bit floating point number, corresponding to the double of Java
- String: Unknown encoded text or binary string, corresponding to Java string
- struct type:
- struct: Defines a common object, similar to a struct definition in C, which is a JavaBean in Java
- Container type:
- List: ArrayList for Java
- Set: corresponding to Java HashSet
- Map: HashMap for Java
- Exception type:
- Exception: Exception for Java
- Type of service: Service: Class of the corresponding services
U installation Use
It is cumbersome to install a lot of dependent packages before installing Windows. The following describes the source installation under Linux, direct wget or manually download the most tar.gz installation package to compile the installation:
TAR-XVF thrift-0.9.1.tar.gz CD thrift-0.9.1 ./configure Make Make install |
With the command thrift-version, thrift version 0.9.1 is displayed to indicate that the installation was successful.
We used Java, so we built the THRIFT related lib package through Ant, entered the $thrift_home/lib/java directory, the following problems occurred in the Ant compiler libthrift:
BuildFile:/letv/apps_install/thrift-0.9.1/lib/java/build.xml
Setup.init:
Mvn.ant.tasks.check:
Proxy
Mvn.ant.tasks.download: [Get] Getting:http://repo1.maven.org/maven2/org/apache/maven/maven-ant-tasks/2.1.3/maven-ant-tasks-2.1.3.jar [Get] To:/letv/apps_install/thrift-0.9.1/lib/java/build/tools/maven-ant-tasks-2.1.3.jar [Get] Not modified-so not downloaded
Mvn.init: Unable to obtain resource From/letv/apps_install/thrift-0.9.1/lib/java/build/tools/maven-ant-tasks-2.1.3.jar: Java.util.zip.ZipException:error in opening zip file [typedef] Unable to obtain resource From/letv/apps_install/thrift-0.9.1/lib/java/build/tools/maven-ant-tasks-2.1.3.jar: [typedef] JAVA.UTIL.ZIP.ZIPEXCEPTION:ERROR in opening zip file [typedef] at Java.util.zip.ZipFile.open (Native Method) [typedef] at java.util.zip.zipfile.<init> (zipfile.java:214) [typedef] at java.util.zip.zipfile.<init> (zipfile.java:144) [typedef] at java.util.jar.jarfile.<init> (jarfile.java:153) [typedef] at java.util.jar.jarfile.<init> (jarfile.java:117) [typedef] at Org.apache.tools.ant.AntClassLoader.getResourceURL (antclassloader.java:1014) [typedef] at Org.apache.tools.ant.antclassloader$resourceenumeration.findnextresource (antclassloader.java:150) [typedef] at org.apache.tools.ant.antclassloader$resourceenumeration.<init> (antclassloader.java:111) [typedef] at Org.apache.tools.ant.AntClassLoader.findResources (antclassloader.java:954) [typedef] at Org.apache.tools.ant.AntClassLoader.getNamedResources (antclassloader.java:923) [typedef] at Org.apache.tools.ant.loader.AntClassLoader5.getResources (antclassloader5.java:58) [typedef] at Org.apache.tools.ant.taskdefs.Definer.resourceToURLs (definer.java:360) [typedef] at Org.apache.tools.ant.taskdefs.Definer.execute (definer.java:246) [typedef] at Org.apache.tools.ant.UnknownElement.execute (unknownelement.java:292) [typedef] at Sun.reflect.GeneratedMethodAccessor4.invoke (Unknown Source) [typedef] at Sun.reflect.DelegatingMethodAccessorImpl.invoke (delegatingmethodaccessorimpl.java:43) [typedef] at Java.lang.reflect.Method.invoke (method.java:601) [typedef] at Org.apache.tools.ant.dispatch.DispatchUtils.execute (dispatchutils.java:106) [typedef] at Org.apache.tools.ant.Task.perform (task.java:348) [typedef] at Org.apache.tools.ant.Target.execute (target.java:435) [typedef] at Org.apache.tools.ant.Target.performTasks (target.java:456) [typedef] at Org.apache.tools.ant.Project.executeSortedTargets (project.java:1393) [typedef] at Org.apache.tools.ant.Project.executeTarget (project.java:1364) [typedef] at Org.apache.tools.ant.helper.DefaultExecutor.executeTargets (defaultexecutor.java:41) [typedef] at Org.apache.tools.ant.Project.executeTargets (project.java:1248) [typedef] at Org.apache.tools.ant.Main.runBuild (main.java:851) [typedef] at ORG.APACHE.TOOLS.ANT.MAIN.STARTANT (main.java:235) [typedef] at Org.apache.tools.ant.launch.Launcher.run (launcher.java:280) [typedef] at Org.apache.tools.ant.launch.Launcher.main (launcher.java:109) [typedef] Could not load definitions from resource Org/apache/maven/artifact/ant/antlib.xml. It could not being found.
BUILD FAILED /letv/apps_install/thrift-0.9.1/lib/java/build.xml:279:problem:failed to create task or type Antlib:o Rg.apache.maven.artifact.ant:remoteRepository Cause:the name is undefined. Action:check the spelling. Action:check that any custom tasks/types has been declared. Action:check that any <presetdef>/<macrodef> declarations has taken place. No types or tasks has been defined in this namespace yet
This appears to is an antlib declaration. Action:check The implementing library exists in one of: -/letv/apps_install/apache-ant-1.9.4/lib -/root/.ant/lib -a directory added on the command line with the-lib argument
Total Time:2 seconds |
Workaround: Ant installs in yum mode, default version 1.7.1, installs the latest version of 1.9.4, builds the build folder in the current directory after successful compilation, including Libthrift-0.9.1.jar and dependent Lib:
Commons-codec-1.6.jarcommons-lang3-3.1.jarcommons-logging-1.1.1.jarhttpclient-4.2.5.jarhttpcore-4.2.4.jarjunit-4.4.jarlog 4j-1.2.14.jarservlet-api-2.5.jarslf4j-api-1.5.8.jarslf4j-log4j12-1.5.8.jar
U Development process and code example
1, write the IDL file Helloservice.thrift, as follows:
/**
* Hello World Testing
*
*/
namespace Java Com.le.mms
Service HelloService {
I32 sayint (1:i32 param)
String saystring (1:string param)
BOOL Sayboolean (1:bool param)
void Sayvoid ()
}
2. Use the thrift compiler to generate the code for the desired language
Thrift--gen Java Helloservice.thrift
The Gen-java directory is generated under the current directory, and the Helloservice.java file is generated.
Thrift--gen py helloservice.thrift can generate Python-related code.
3. New Java Engineering or MAVEN engineering, Java Engineering introduces dependencies into Ant-generated Lib,maven engineering pom.xml.
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.9.1</version>
</dependency>
Automatically downloads the dependent lib.
4. Write the Helloserviceimpl.java business implementation class. The code that needs to implement the Helloservice.iface interface, generated automatically by thrift.
Package com.le.mms.thrift; Import org.apache.thrift.TException; public class Helloserviceimpl implements helloservice.iface{ @Override public int sayint (int param) throws Texception { SYSTEM.OUT.PRINTLN ("Say int:" + param); return param; } @Override public string saystring (string param) throws Texception { System.out.println ("Say string:" + param); return param; } @Override Public boolean Sayboolean (Boolean param) throws Texception { System.out.println ("Say Boolean:" + param); return param; } @Override public void Sayvoid () throws Texception { System.out.println ("Say void ..."); } } |
5. Write Thrift-java server side, listening port 9090
Package com.le.mms.thrift.server; Import Org.apache.thrift.protocol.TBinaryProtocol; Import Org.apache.thrift.protocol.TBinaryProtocol.Factory; Import Org.apache.thrift.server.TServer; Import Org.apache.thrift.server.TSimpleServer; Import Org.apache.thrift.transport.TServerSocket; Import org.apache.thrift.transport.TTransportException; Import Com.le.mms.thrift.HelloService; Import Com.le.mms.thrift.HelloServiceImpl; Import Com.le.mms.thrift.HelloService.Processor; /** * Start the Java server side of thrift * * @author David * */ public class Helloserviceserver { public static void Main (string[] args) { try { Setting Up server ports Tserversocket servertransport = new Tserversocket (9090); Set up a binary protocol factory Factory protocolfactory = new Tbinaryprotocol.factory (); Processor-related business implementation processor 1. Using single-threaded standard blocking I/O models Tserver.args Simpleargs = new Tserver.args (servertransport); Simpleargs.processor (processor); Simpleargs.protocolfactory (protocolfactory); Tserver Server = new Tsimpleserver (Simpleargs); 2. Using the thread pool service model Tthreadpoolserver.args Poolargs = new Tthreadpoolserver.args (servertransport); Poolargs.processor (processor); Poolargs.protocolfactory (protocolfactory); Tserver poolserver = new Tthreadpoolserver (Poolargs); Poolserver.serve (); SYSTEM.OUT.PRINTLN ("Open thrift server, listening port: 9090"); Server.serve (); } catch (Ttransportexception e) { E.printstacktrace (); } } } |
6. Write the Thrift-java client and make the request.
Package com.le.mms.thrift.client; 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; Import Com.le.mms.thrift.HelloService; /** * Call Thrift's Java Client * * @author David * */ public class Helloserviceclient { public static void Main (string[] args) { try { Set the service address of the call-port Ttransport transport = new Tsocket ("localhost", 9090); Using binary protocols Tprotocol protocol = new Tbinaryprotocol (transport); The interface used Helloservice.client Client = new helloservice.client (protocol); Open socket Transport.open (); Client.sayboolean (TRUE); Client.saystring ("Hello World"); Client.sayint (20141111); Client.sayvoid (); Transport.close (); } catch (Ttransportexception e) { E.printstacktrace (); } catch (Texception te) { Te.printstacktrace (); } } } |
After the server and client code is written, run the server to turn on listening. The client will then be able to communicate with the server side of the RPC call mode.
Reprinted from: http://www.blogjava.net/ldwblog/archive/2014/12/03/421011.html
Thrift RPC Framework Introduction