1. Introduction to thrift
Baidu and Google it yourself.
Second, thrift installation
(1) Install some necessary libraries
sudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libboost-system-dev libboost-filesystem-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-devFor other systems, such as centos, mac, and windows, see the following official website tutorials for installation requirements:
http://thrift.apache.org/docs/install/
(2) Download
http://thrift.apache.org/download The version I downloaded is 0.9.1
(3) Decompression and installation
After decompression, enter the root directory and enter:
./configure && makeIf an ERROR is output at the end, saying "libcrypto required", it may be a package that needs to be installed libssl-dev
sudo apt-get install libssl-dev
If you enter thrift -version, the prompt says that thrift-compiler is not installed, how to install it.
sudo apt-get install thrift-compilerAfter installing the compiler, you can start writing thrift files.
Three, thrift basic syntax and architecture
1. Thrift infrastructure
Thrift is a server-side and client-side architecture system. From my personal point of view, Thrift is something similar to XML-RPC+Java-to-IDL+Serialization Tools=Thrift. Thrift has its own internally defined transmission protocol specification ( TProtocol) and transmission data standards (TTransports), through the IDL script, the data structure (struct) of the transmission data and the business logic (service) of the transmission data are quickly constructed according to different operating environments. The corresponding code, and through its own internal serialization The mechanism simplifies and compresses the transmitted data to increase the cost of data interaction in high-concurrency and large-scale systems. The following figure depicts the overall architecture of Thrift, which is divided into 6 parts:
(1). Your business logic implementation (You Code)
(2). The Service corresponding to the client and the server
(3). Calculation results of performing read and write operations
(4).TProtocol
(5).TTransports
(6). Low-level I/O communication
The first three parts in the figure are 1. The code you generate through the Thrift script file, 2. The brown box in the figure is the
code of the client and processor you build based on the generated code, 3. The red part in the figure is the calculation result generated by the 2 terminal
. The following three parts from TProtocol are Thrift's transmission system and transmission protocol and underlying I/O communication.
Thrift also provides blocking, non-blocking, single-threaded, multi-threaded modes to run on the server, and can also work with
servers/containers. Can be seamlessly integrated with existing JEE servers/Web containers.
2. Data type
A. Base Type: basic type
(1) bool: Boolean type (true or false)
(2) byte: 8-bit signed integer
(3) i16: 16-bit signed integer
(4) i32: 32-bit signed integer
(5) i64: 64-bit signed integer
(6) double: 64-bit floating point number
(7) string: text string, encoded in UTF-8
B. Container: container: such as list, set, map
C. Struct: structure type
As an example below:
struct People {
1:string name;
2:string sex;
}
struct is very similar to the structure in C.
D. Exception: exception type
E. Service: defines the interface and a series of methods of the object
Such as the following example:
service DemoService {
string getUserName(1:i32 id);
i32 add(1:i32 num1, 2:i32 num2);
}
A service corresponds to a class, which contains some methods.
For example, the getUserName method returns a string based on an id, and the add method sums num1 and num2.
3. Agreement
Thrift can transmit selected communication protocols between kehudaun and the server. The protocols are generally divided into text and binary transmission protocols.
* TBinaryProtocol - binary encoding format for data transfer.
* TCompactProtocol - This protocol is very efficient and uses Variable-Length Quantity (VLQ) encoding to compress data.
* TJSONProtocol - use JSON's data encoding protocol for data transfer.
* TSimpleJSONProtocol - This save only provides JSON write-only protocol, which is suitable for parsing through scripting language
* TDebugProtocol - Helps developers to debug in the process of development, and it is displayed in the form of text for easy reading.
4. Transport layer
* TSocket - uses blocking I/O for transmission, and is the most common mode.
* TFramedTransport- uses a non-blocking method, according to the size of the block, to transmit, similar to NIO in Java.
* TFileTransport- As the name implies, it is transmitted in the way of files. Although this method does not provide Java implementation, it is very simple to implement.
* TMemoryTransport - uses memory I/O, just like the ByteArrayOutputStream implementation in Java.
* TZlibTransport - Use to perform zlib compression, does not provide a Java implementation.
5. Server type
* TSimpleServer - single-threaded server side using standard blocking I/O.
* TThreadPoolServer - Multithreaded server side using standard blocking I/O.
* TNonblockingServer - The multi-threaded server side uses non-blocking I/O and implements the NIO channel in Java.
Four, thrift simple example
1. Create a thrift file
vim demo.thrift
java namespace com.demo service DemoService{ //Get username from id string getNameById(1:i32 id) }namespace is the corresponding package in java, where namespace java com.demo, the second keyword indicates the language used
A service will generate a corresponding class file
2. Compile and create code
thrift -r --gen java demo.thrift
Then a gen-java folder is generated, and the structure of the folder is viewed through tree gen-java:
3. Create eclipse project
(1) Create a java project, and then add the necessary jar package generated by compiling thrift before. Here, since we are using the java language, go to
Add jar packages under /thrift-0.9.1/lib/java/build/lib and /thrift-0.9.1/lib/java/build/.
(2) Copy the previously generated DemoService.java into the project
(3) Create a server
Create the implementation class of server: ServerImpl.java
package com.server; import org.apache.thrift.TException; import com.demo. *; public class ServerImpl implements DemoService.Iface { @Override public String getNameById(int id) throws TException { System.out.println("Your username is wgc"); return "wgc"; } }
Create Server startup class: Server.java
package com.server; import org.apache.thrift.TProcessor; import org.apache.thrift.protocol.TBinaryProtocol; 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.demo.DemoService; public class Server { public static final int SERVER_PORT = 8090; public void startServer() throws TTransportException{ System.out.println("Server start ...."); TProcessor tprocessor = new DemoService.Processor<DemoService.Iface>(new ServerImpl()); TServerSocket serverTransport = new TServerSocket(SERVER_PORT); TServer.Args tArgs = new TServer.Args(serverTransport); tArgs.processor(tprocessor); tArgs.protocolFactory(new TBinaryProtocol.Factory()); TServer server = new TSimpleServer(tArgs); server.serve(); } public static void main(String[] args) throws TTransportException { Server server = new Server(); server.startServer(); } }
(4) Create a client
Create Client.java:
package com.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 com.demo.DemoService; public class Client { public static final String SERVER_IP = "localhost"; public static final int SERVER_PORT = 8090; public static final int TIMEOUT = 30000; public void startClient( ) throws TException { TTransport transport = null; transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT); // The protocol must be consistent with the server TProtocol protocol = new TBinaryProtocol(transport); // TProtocol protocol = new TCompactProtocol(transport); // TProtocol protocol = new TJSONProtocol(transport); DemoService.Client client = new DemoService.Client(protocol); transport.open(); String result = client.getNameById(1); } public static void main(String[] args) throws TException { Client client = new Client(); client.startClient(); } }
(5) Test procedure
Start the server first, then start the client, it will output in the terminal:
Server start ....
Your username is wgc
At this point, a simple client and server program is implemented.
(6) Project directory structure
references:
(2)http://www.dataguru.cn/forum.php?mod=viewthread&tid=219377