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<string> 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<String> make(Book_Info book) throws TException {
		List<String> r = new ArrayList<String>();
		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工程):


pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.free4lab.learning</groupId>
	<artifactId>protocol</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>protocol</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.apache.thrift</groupId>
			<artifactId>libthrift</artifactId>
			<version>0.9.1</version>
		</dependency>

		<!--log4j -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.15</version>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.5.11</version>
		</dependency>

	</dependencies>
</project>

參考:

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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章