根據官方網站 Apache Thrift的說法,該軟件框架用于可擴展的跨語言服務開發,它結合了軟件堆棧和代碼生成引擎,以構建可在C ++,Java,Python,PHP,Ruby, Erlang,Perl,Haskell,C#,Cocoa,JavaScript,Node.js,Smalltalk,OCaml和Delphi等語言。 圖片由維基百科提供

在Windows中安裝Apache Thrift
安裝節儉可能是一個令人厭煩的過程。 但是對于Windows,編譯器可以作為預構建的exe使用。 下載thrift.exe并將其添加到您的環境變量中。
編寫Thrift定義文件(.thrift文件)
一旦習慣了,編寫Thrift定義文件就變得非常容易。 我發現本教程非常有用。
定義文件示例(add.thrift)
namespace java com.eviac.blog.samples.thrift.server // defines the namespace typedef i32 int //typedefs to get convenient names for your typesservice AdditionService { // defines the service to add two numbersint add(1:int n1, 2:int n2), //defines a method
}
編譯Thrift定義文件
要編譯.thrift文件,請使用以下命令。
thrift --gen <language> <Thrift filename>
在我的示例中,命令是
thrift --gen java add.thrift
執行完該命令后,您將在gen-java目錄中找到對構建RPC客戶端和服務器有用的源代碼。 在我的示例中,它將創建一個名為AdditionService.java的Java代碼。
編寫服務處理程序
服務處理程序類是實現AdditionService.Iface接口所必需的。
示例服務處理程序(AdditionServiceHandler.java)
package com.eviac.blog.samples.thrift.server;import org.apache.thrift.TException;public class AdditionServiceHandler implements AdditionService.Iface {@Overridepublic int add(int n1, int n2) throws TException {return n1 + n2;}}
編寫一個簡單的服務器
以下是啟動簡單的節儉服務器的示例代碼。 要啟用多線程服務器,請取消注釋示例代碼的注釋部分。
示例服務器(MyServer.java)
package com.eviac.blog.samples.thrift.server;import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TServer.Args;
import org.apache.thrift.server.TSimpleServer;public class MyServer {public static void StartsimpleServer(AdditionService.Processor<AdditionServiceHandler> processor) {try {TServerTransport serverTransport = new TServerSocket(9090);TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));// Use this for a multithreaded server// TServer server = new TThreadPoolServer(new// TThreadPoolServer.Args(serverTransport).processor(processor));System.out.println("Starting the simple server...");server.serve();} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) {StartsimpleServer(new AdditionService.Processor<AdditionServiceHandler>(new AdditionServiceHandler()));}}
寫客戶
以下是使用AdditionService提供的服務的示例Java客戶端代碼。
客戶端代碼示例(AdditionClient.java)
package com.eviac.blog.samples.thrift.client;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;public class AdditionClient {public static void main(String[] args) {try {TTransport transport;transport = new TSocket("localhost", 9090);transport.open();TProtocol protocol = new TBinaryProtocol(transport);AdditionService.Client client = new AdditionService.Client(protocol);System.out.println(client.add(100, 200));transport.close();} catch (TTransportException e) {e.printStackTrace();} catch (TException x) {x.printStackTrace();}}}
運行服務器代碼(MyServer.java)。 它應該輸出以下內容,并將監聽請求。
Starting the simple server...
然后運行客戶端代碼(AdditionClient.java)。 它應該輸出以下內容。
300
參考: EVIAC博客上的JCG合作伙伴 Pavithra Siriwardena的Apache Thrift with Java快速入門 。
翻譯自: https://www.javacodegeeks.com/2012/07/apache-thrift-with-java-quickstart.html