thrift -java

book.thrift

namespace java example
 
struct Book_Info {
 
1: i32 book_id,
 
2: string book_name,
 
3: string book_author,
 
4: double book_price,
 
5: string book_publisher,
 
}

test.thrift

namespace java net.johnc.thrift
include "book.thrift"
service Test{ 
 
 void ping(1: i32 length)
 
 list make(1: book.Book_Info book)
 
}

TestImpl.java

package com.free4lab.learning.thrift;

import java.util.ArrayList;
import java.util.List;

import org.apache.thrift.TException;

import com.free4lab.learning.protocol.Book_Info;

public class TestImpl implements com.free4lab.learning.protocol.Test.Iface{

	@Override
	public void ping(int length) throws TException {
		System.out.println("Calling ping, length = " + length);
	}

	@Override
	public List make(Book_Info book) throws TException {
		List r = new ArrayList();
		r.add("time");
		System.out.println("in server make.");
		return r;
	}

}


Server.java

package com.free4lab.learning.thrift;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TBinaryProtocol.Factory;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.server.TThreadPoolServer.Args;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;

import com.free4lab.learning.protocol.Test;
import com.free4lab.learning.protocol.Test.Processor;

public class Server {
	public void startServer() {
		try {
			TServerSocket serverTransport = new TServerSocket(1234);

			Test.Processor process = new Processor(new TestImpl());

			Factory portFactory = new TBinaryProtocol.Factory(true, true);

			Args args = new Args(serverTransport);
			args.processor(process);
			args.protocolFactory(portFactory);

			TServer server = new TThreadPoolServer(args);
			server.serve();
		} catch (TTransportException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Server server = new Server();
		server.startServer();
	}
}


Client.java

package com.free4lab.learning.thrift;

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.free4lab.learning.protocol.Book_Info;
import com.free4lab.learning.protocol.Test;

public class Client {
	public void startClient() {
		//TTransport transport;
		try {
			TSocket transport = new TSocket("localhost", 1234);
			transport.open();
			TProtocol protocol = new TBinaryProtocol(transport);
			Test.Client client = new Test.Client(protocol);
			client.ping(1234);
			Book_Info book = new Book_Info();
			book.book_name = "network";
			System.out.println(client.make(book).get(0));
			transport.close();
		} catch (TTransportException e) {
			e.printStackTrace();
		} catch (TException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Client client = new Client();
		client.startClient();
	}
}


注:我用的是windows下的 thrift 编译命令。

thrift-0.9.1-exe --gen java book.thrift

thrift-0.9.1-exe --gen java test.thrift

启动 Server 的main函数就好。

然后 Client 向 Server发请求。

代码结构如下(maven工程):

thrift -java_第1张图片

pom.xml


	4.0.0

	com.free4lab.learning
	protocol
	0.0.1-SNAPSHOT
	jar

	protocol
	http://maven.apache.org

	
		UTF-8
	

	
		
			junit
			junit
			3.8.1
			test
		

		
			org.apache.thrift
			libthrift
			0.9.1
		

		
		
			log4j
			log4j
			1.2.15
		

		
			org.slf4j
			slf4j-log4j12
			1.5.11
		

	

参考:

http://dongxicheng.org/search-engine/thrift-rpc/



你可能感兴趣的:(thrift)