Thrift之java例項
阿新 • • 發佈:2019-01-27
一、java例項
1.下載與安裝thrift工具
2.編寫Thrift檔案(定義介面,結構,異常等),儲存為test.thrift
namespace java com.zychen.thrift
struct User{
1:i64 id,
2:string name,
3:i32 age,
4:bool vip
}
service Test{
i32add(1:i32 a,2:i32 b)
User getById(1:i64 id)
}
3.生成介面程式碼
把thrift-0.9.3.exe和test.thrift檔案放在同一個目錄。
進入DOS命令執行:thrift-0.9.3.exe --gen java test.thrift
生成檔案gen-java/ com/zychen/thrift/Test.java
4、伺服器程式碼
服務Test實現類
package com.zychen.thrift;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.thrift.TException;
public class TestImpl implements Test.Iface {
public void Init(){
int n = 0;
for(n=0; n<10; n++){
User user = new User();
user.setId(n+1);
user.setName(String.format("name_%d", n+1));
user.setAge(18+n+1);
user.setVip(true);
listUser.add(user);
}
}
@Override
public int add(int a, int b) throws TException {
return a+b;
}
@Override
public User getById(long id) throws TException {
User userObj = null;
Iterator<User> iter = listUser.iterator();
while(iter.hasNext()){
userObj = iter.next();
if(userObj.getId() == id){
return userObj;
}
}
return null;
}
//插入使用者資訊
void InsertUser(User user){
listUser.add(user);
}
protected List<User> listUser = new ArrayList<User>();
}
//服務端程式碼
package com.zychen.test;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TBinaryProtocol.Factory;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.TNonblockingServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.server.TThreadPoolServer.Args;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;
import com.zychen.thrift.Test;
import com.zychen.thrift.Test.Processor;
import com.zychen.thrift.TestImpl;
public class Server {
//阻塞式IO
public void startOIOServer() {
try {
TServerSocket serverTransport = new TServerSocket(1234);
TestImpl testImpl = new TestImpl();
testImpl.Init();
Test.Processor process = new Processor(testImpl);
Factory portFactory = new TBinaryProtocol.Factory(true, true);
Args args = new Args(serverTransport);
args.processor(process);
args.protocolFactory(portFactory);
TServer server = new TThreadPoolServer(args);
System.out.println("init...");
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
}
}
//使用非阻塞式IO,服務端和客戶端需要指定TFramedTransport資料傳輸的方式。
public void startNIOServer() {
try {
TestImpl testImpl = new TestImpl();
testImpl.Init();
Test.Processor tprocessor = new Processor(testImpl);
TNonblockingServerSocket tnbServerTransport = new TNonblockingServerSocket(1234);
TNonblockingServer.Args tArgs = new TNonblockingServer.Args(tnbServerTransport);
tArgs.processor(tprocessor);
tArgs.transportFactory(new TFramedTransport.Factory());
tArgs.protocolFactory(new TCompactProtocol.Factory());
TServer server = new TNonblockingServer(tArgs);
System.out.println("init...");
server.serve();
}catch(TTransportException e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
Server server = new Server();
//server.startOIOServer();
server.startNIOServer();
}
}
依賴jar包
5、客戶端程式碼
package com.zychen.test;
import org.apache.thrift.TApplicationException;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import com.zychen.thrift.Test;
import com.zychen.thrift.User;
public class Client {
//阻塞io
public void startOIOClient() {
TTransport transport;
try {
transport = new TSocket("localhost", 1234);
TProtocol protocol = new TBinaryProtocol(transport);
Test.Client client = new Test.Client(protocol);
transport.open();
int nResult = client.add(100, 200);
String strTemp = String.format("client.add(100, 200) = %d", nResult);
System.out.println(strTemp);
User u1 = client.getById(1);
if(u1 != null){
System.out.println(u1.getName());
}
User u2 = client.getById(11);
if(u2 == null){
System.out.println("null");
}
u2 = client.getById(9);
transport.close();
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException e) {
if(e instanceof TApplicationException && ((TApplicationException) e).getType() == TApplicationException.MISSING_RESULT){
System.out.println("Theresult of Method function is NULL");
}else{
e.printStackTrace();
}
}
}
//非阻塞io
public void startNIOClient() {
TTransport transport;
try {
transport = new TFramedTransport(new TSocket("localhost", 1234,30000));
TProtocol protocol = new TCompactProtocol(transport);
Test.Client client = new Test.Client(protocol);
transport.open();
int nResult = client.add(100, 200);
String strTemp = String.format("client.add(100, 200) = %d", nResult);
System.out.println(strTemp);
User u1 = client.getById(1);
if(u1 != null){
System.out.println(u1.getName());
}
User u2 = client.getById(11);
if(u2 == null){
System.out.println("null");
}
u2 = client.getById(9);
transport.close();
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException e) {
if(e instanceof TApplicationException && ((TApplicationException) e).getType() == TApplicationException.MISSING_RESULT){
System.out.println("Theresult of Method function is NULL");
}else{
e.printStackTrace();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Client client = new Client();
//client.startOIOClient();
client.startNIOClient();
}
}
依賴jar包
二、Thrift-0.9.3 jar包編譯生成。
1、myeclipse新建一個java工程。
2、開啟thrift目錄,查詢到thrift-0.9.3\thrift-0.9.3\lib\java\src,將這個src目錄下的程式碼拷貝到工程目錄下。
3、引入httpClient、httpcore、javax.servlet、slf4j.api jar包。如圖:
4、編譯,匯出thrift-0.9.3jar包,匯出時選擇Exportall output folders for checked projects,否則scheme類無法匯出到jar包中。