先下載 thrift.exe 與對應的 lib 包,版本號務必一致,否則編譯時會出現無法識別的錯誤。另外常見的 org.slf4j 缺包問題,只要把 slf4j-api.jar 下載回來加入專案即可。
namespace java com.nerd.thrift.service
/**
*
*/
service sayThriftService{
void say();
}
接著在命令列執行:
thrift-1.8.0.exe -gen java sayThriftService
完成後,磁碟上會出現 com.nerd.thrift.service 目錄與對應的 Java 程式碼,其中已包含 Iface、AsyncIface、Client、Processor 等一整套骨架(實際內容依版本而略有差異):
public class sayThriftService {
public interface Iface {
public void say() throws org.apache.thrift.TException;
}
public interface AsyncIface {
public void say(org.apache.thrift.async.AsyncMethodCallback<AsyncClient.say_call> resultHandler) throws org.apache.thrift.TException;
}
public static class Client extends org.apache.thrift.TServiceClient implements Iface {
public static class Factory implements org.apache.thrift.TServiceClientFactory<Client> {
public Factory() {}
public Client getClient(org.apache.thrift.protocol.TProtocol prot) {
return new Client(prot);
}
public Client getClient(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TProtocol oprot) {
return new Client(iprot, oprot);
}
}
// ...省略(具體看自己生成的代碼)
}
}
package com.nerd.clq;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
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 com.nerd.clq.thrift.sayThriftService;
import com.nerd.clq.thrift.sayThriftService.Iface;
public class Server implements sayThriftService.Iface{
private static TServer server;
@Override
public void say() throws TException {
System.out.println(System.currentTimeMillis());
}
public static void main(String[] args) throws TException {
Server server1 = new Server();
TServerSocket serverTransport = new TServerSocket(8080);
TProtocolFactory proFactory = new TBinaryProtocol.Factory();
sayThriftService.Processor<Iface> processor = new sayThriftService.Processor<Iface>(server1);
Args arg = new Args(serverTransport) {
}.protocolFactory(proFactory).processor(processor);
server = new TThreadPoolServer(arg);
// 啟動服務(先啟動這個類,然後啟動 client 類)
server.serve();
}
}
package com.nerd.clq;
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.nerd.clq.thrift.sayThriftService;
public class Client {
public static void main(String[] args) throws TException {
TTransport transport = new TSocket("localhost", 8080);
TProtocol protocol = new TBinaryProtocol(transport);
sayThriftService.Client client = new sayThriftService.Client(protocol);
transport.open();
client.say();
transport.close();
}
}
服務端:1. 建立 Handler;2. 依 Handler 建立 Processor;3. 建立 Transport;4. 選擇 Protocol;5. 用 Processor、Transport、Protocol 組出 Server;6. 啟動 Server。
客戶端:1. 建立 Transport;2. 選擇 Protocol;3. 用 Transport 與 Protocol 建立 Client;4. 呼叫 Client 的方法。
建立 Transport 時通常就是建立對應的 Socket。另外提醒:先啟動 Server 再啟動 Client,Client 執行完記得關閉 transport,否則連線會佔著 port 不放。