使用Java快速入門RPC框架 - Thrift
Apache Thrift是一個非死book簡歷的RPC框架,現在是一個Apache的頂級項目。Thrift允許通過一個跨語言的定義文件的方式定義數據類型和服務接口,這個文件作為RPC客戶端和服務器通信的標準,你也可以去看看Thrift的白皮書了解更多信息。
根據Apache Thrift的官方站點的描述,Thrift是一個:
software framework, for scalable cross-language services development, combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml and Delphi and other languages.

安裝Thrift比較的煩,但是在Windows下官方編譯了一個thrift.exe,下載安裝就行了。
寫 Thrift定義文件(.thrift file)
如果你之前有接觸過這個東西的話,寫定義文件非常的簡單。但這里可以參考官方的教程快速開始。
示例定義文件(add.thrift)
namespace java com.eviac.blog.samples.thrift.server // defines the namespace typedef i32 int //typedefs to get convenient names for your types service AdditionService { // defines the service to add two numbers int add(1:int n1, 2:int n2), //defines a method }
編譯Thrift定義文件
下面的命令編譯.thrift文件
thrift --gen
對于我的例子來講,命令是:
thrift --gen java add.thrift
在執行完代碼后,在gen-java目錄下你會發現構建RPC服務器和客戶端有用的源代碼。在我的例子中我將創建一個叫做AddtionService.java的java文件。
寫一個 service handler
Service handler 類必須實現 AdditionService.Iface接口。
示例Service handler(AdditionServiceHandler.java)
package com.eviac.blog.samples.thrift.server; import org.apache.thrift.TException; public class AdditionServiceHandler implements AdditionService.Iface { @Override public int add(int n1, int n2) throws TException { return n1 + n2; } }
寫一個簡單的服務器
下面的示例代碼是一個簡單的Thrift服務器。可以看到下面的代碼中有一段是注釋了的,可以去掉注釋來啟用多線程服務器。
示例服務器(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.Processorprocessor) { 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 (new AdditionServiceHandler())); } }
寫一個客戶端
下面的例子是一個使用Java寫的客戶端短使用AdditionService的服務。
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
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!