Catalog: Overview Download configuration basic concept data type service-side encoding basic steps client-coded basic steps Data Transfer Protocol example demo (Java) thrift Generate code Implement Interface Iface Tsimpleserver service model Tthreadpools Erver Service Model Tnonblockingserver service Model Thshaserver Service model asynchronous Client [i], overview
Apache Thrift is an efficient framework for Facebook to implement remote service calls that support multiple programming languages. Thrift was developed by Facebook and was donated to the Apache Foundation in 2008 as an incubator project.
Thrift is a software framework for the development of extensible, cross-language services. It combines a powerful software stack and code generation engine,
Thrift is a driver-level interface that provides APIs for clients to implement in multiple languages.
Thrift is a code generation library that supports client languages including C + +, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C #, Cocoa, JavaScript, node. js, Smalltalk, an D OCaml. Its goal is to provide convenient RPC invocation mechanisms for a variety of popular languages, without the need to use expensive methods such as soap.
To use thrift, you use a language-neutral service definition file that describes the data type and the service interface. This file is used as an input to the engine, and the compiler generates code to generate the RPC client code base for each supported language. This statically generated design makes it very easy for developers to use, and because type validation occurs at compile time rather than run time, the code can run efficiently.
Thrift's design provides these features:
1, language-independent type
Because types are defined in a language-neutral manner using definition files, they can be analyzed in different languages. For example, the structure of C + + can exchange data with Python's dictionary types.
2. Universal Transmission Interface
Whether you're using a disk file, memory data, or a socket stream, you can use the same app code.
3. Protocol Independent
Thrift encodes and decodes data types and can be used across protocols.
4. Support version
Data types can include version information to support updates to the client API.
Website address: thrift.apache.org
Recommended articles worth reading: http://jnb.ociweb.com/jnb/jnbJun2009.html http://wiki.apache.org/thrift http://thrift.apache.org/ Static/files/thrift-20070401.pdf [ii], download configuration
1) Install thrift: Download EXE file to thrift website, then rename the file to Thrift.exe, copy it to the D:\EBOOK\thrift directory (or any directory), then you can use it in DOS environment.
D:\ebook\thrift>thrift-gen java D:\work\workspace\thriftworkspace\demo1\demoHello.thrift, output Java file output to the current directory by default D : \ebook\thrift\gen-java, you can also specify the output path using the-o parameter;
2) Download Dependent packages
2.1) Libthrift.jar, download address: http://repo1.maven.org/maven2/org/apache/thrift/libthrift/0.9.0/
2.2) Slf4j-api.jar
2.3) Slf4j-simple.jar
To the official website http://thrift.apache.org/download Download the latest version, as of today (2016-05-23) The latest version is 0.9.3
1. If you are building a project with MAVEN, add the following directly to the Pom.xml:
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactid>libthrift</ artifactid>
<version>0.8.0</version>
</dependency>
2. If you compile the Lib package yourself, unzip the downloaded package to the X: Disk, and then run Ant for automatic compilation in the X:\thrift-0.8.0\lib\java directory, the X:\thrift-0.8.0\lib\java\build\ Directory to see the compiled Lib package: Libthrift-0.8.0.jar
[III], Basic concepts
1. Data type base type: bool: Boolean, True or False, Boolean byte:8-bit signed integer corresponding to Java, byte i16:16-bit signed integer corresponding to JavaScript, Java short i32:32 bit Integer, corresponding to the Java i64:64 bit signed integer, corresponding to the Java long double:64 bit floating-point number, corresponding to the Java double String:utf-8 encoded string, corresponding to the Java string struct type: struct: Defines a common object, similar to a struct definition in C, which is a JavaBean container type in Java: List: ArrayList set for Java: HashSet map for Java: has for Java Hmap Exception Type: Exception: Exception service type for Java: Service: class for corresponding services
2. Server-side Coding basic steps: Implement service Processing interface Impl Create tprocessor create Tservertransport create Tprotocol create tserver start server
3. Client-side encoding basic steps: Create transport create Tprotocol method based on Ttransport and Tprotocol to create client call client
4. Data Transfer Protocol TBINARYPROTOCOL: binary format. TCOMPACTPROTOCOL: Compressed format tjsonprotocol:json format Tsimplejsonprotocol: Provides JSON write-only protocol, resulting files are easily parsed by scripting language
Tips: The client and server protocols are consistent [IV], example demo
1. Thrift Generate Code
Create the Thrift file: D:\work\workspace\thriftworkspace\demo1\demoHello.thrift, which reads as follows:
namespace Java Com.dxz.thrift.demo
service helloworldservice {
string SayHello (1:string username)
}
Thrift-0.8.0.exe is a Windows compiler tool available on the website that uses this tool to generate relevant code:
D:\ebook\thrift>thrift-0.9.3.exe-r-gen Java D:\work\workspace\thriftworkspace\demo1\demoHello.thrift
The resulting directory structure is as follows:
Copy the generated Helloworldservice.java file to your own test project, my project was built with Maven, so add the following in the Pom.xml:
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactid>libthrift</ artifactid>
<version>0.8.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
< Version>1.5.8</version>
</dependency>
If it's an ant-built project, add Libthrift-0.9.3.jar to the project
2. Implement Interface Iface
Java code: Helloworldimpl.java
Package Com.dxz.thrift.demo;
Import org.apache.thrift.TException;
public class Helloworldimpl implements Helloworldservice.iface {public
Helloworldimpl () {
}
@Override Public
string SayHello (string username) throws Texception {
return ' Hi, ' + username + ' Welcome to Thrift World ";
}
}
3.TSimpleServer Service Side
Simple single-threaded service model, typically used for testing.
Writing server code: Helloserverdemo.java
Package Com.dxz.thrift.demo;
Import Org.apache.thrift.TProcessor;
Import Org.apache.thrift.protocol.TBinaryProtocol;
Import Org.apache.thrift.protocol.TCompactProtocol;
Import Org.apache.thrift.protocol.TJSONProtocol;
Import Org.apache.thrift.protocol.TSimpleJSONProtocol;
Import Org.apache.thrift.server.TServer;
Import Org.apache.thrift.server.TSimpleServer;
Import Org.apache.thrift.transport.TServerSocket;
public class Helloserverdemo {public static final int server_port = 8090;
public void StartServer () {try {System.out.println ("HelloWorld tsimpleserver start ...");
Tprocessor tprocessor = new helloworldservice.processor