Thrift uses

 

 Thrift is apache's open source rpc framework that supports multiple language bindings. Compared with protobuf, it provides one-stop solutions such as server and serialization.

 

1. Download

 http://thrift.apache.org/download

 

2. Writing IDL

namespace java org.sun.service

service SimpleThriftService
{
	string getStr( 1:string src, 2:string dst );
	
	i32 getInt( 1:i32 val );
	
	map<i32,i32> getMap( 1:string name );
}

 

The IDL is used to generate the description file of each language binding, and the corresponding *.proto file in pb. Thrift's IDL supports rich data types, including struct/map.

 

Thrift Types:

Base Types:

bool: A boolean value (true or false)

byte: An 8-bit signed integer

i16: A 16-bit signed integer

i32: A 32-bit signed integer

i64: A 64-bit signed integer

double: A 64-bit floating point number

string: A text string encoded using UTF-8 encoding

Structs

Containers:

list, set, map 

 

3. Generate Java Binding

thrift --gen java test_service.thrift

 Current directory generation: ./gen-java/org/sun/service/SimpleThriftService.java

 

4. Write the implementation class

// Implement the Iface interface of the Binding class
public class ThriftServiceImpl implements SimpleThriftService.Iface {

	public String getStr(String src, String dst) throws TException {
		System.out.println( "Input: " + src + "\t" + dst );
		String strResult = String.format( "%s-%s", src, dst );
		return strResult;
	}

	public int getInt(int val) throws TException {
		int sum = 10*val;
		return sum;
	}

	public Map<Integer, Integer> getMap(String name) throws TException {
		System.out.println( "Name: " + name );
		
		Map<Integer,Integer> pMap = new HashMap<Integer,Integer>();
		pMap.put( 1, 1 );
		return pMap;
	}
}

 

5. Start the server

Thrift provides external working modes: TSimpleServer, TNonblockingServer, TThreadPoolServer, TThreadedSelectorServer, etc.

 

Thrift supports communication protocol formats: TCompactProtocol, TBinaryProtocol, TJSONProtocol, etc.

/**
 * Thrift Server
 */
private static void main( String[] args) throws TTransportException {
	ThriftServiceImpl m_ServImpl = new ThriftServiceImpl(); // Implementation class

	TProcessor tProcessor = new SimpleThriftService.Processor<SimpleThriftService.Iface>( m_ServImpl );
	TNonblockingServerSocket nioSocket = new TNonblockingServerSocket( 12345 );  

	// Setting parameters
	TNonblockingServer.Args tnbArgs = new TNonblockingServer.Args(nioSocket);  
	tnbArgs.processor(tProcessor); // set the processor
	tnbArgs.transportFactory(new TFramedTransport.Factory()); // transmit in chunks
	tnbArgs.protocolFactory(new TBinaryProtocol.Factory()); // binary serialization protocol

	// start the TCP service
	m_Server = new TNonblockingServer( tnbArgs );		

	// loop to serve
	m_Server.serve();
}

 

6. Client test

/**
 * Thrift Client
 */
public static void main(String[] args) {
	// create tcp client
	TTransport m_Socket = new TFramedTransport( new TSocket( "127.0.0.1", 12345, 2000 ) );
	
	// set binary protocol
	TProtocol protocol = new TBinaryProtocol( m_Socket );
	SimpleThriftService.Client client = new SimpleThriftService.Client(protocol);
	
	try {
		m_Socket.open();
		
		// call remote function
		String result = client.getStr( "hello", "world" );
		System.out.println( "Result: " + result );
		m_Socket.close();
	} catch (TException e) {
		e.printStackTrace ();
	}
}

 

7. Packet capture analysis

 

 

 Wireshark can actually recognize the thrift protocol. The thrift protocol includes header (18 bytes) + Data. Obviously, thrift does not compress the fields, and it occupies network bandwidth compared to pb.

 

 

Reference article 

Thrift official website

In-depth understanding of Thrift

 

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326315014&siteId=291194637