使用Java快速入門Thrift – JAVA編程語言程序開發技術文章

Apache Thrift是一個facebook簡歷的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)

1
namespace java com.eviac.blog.samples.thrift.server  // defines the namespace 
2
  
3
typedef i32 int  //typedefs to get convenient names for your types
4
  
5
service AdditionService {  // defines the service to add two numbers
6
        int add(1:int n1, 2:int n2), //defines a method
7
}
編譯Thrift定義文件
下面的命令編譯.thrift文件

1
thrift –gen <language> <Thrift filename>
對於我的例子來講,命令是:

1
thrift –gen java add.thrift
在執行完代碼後,在gen-java目錄下你會發現構建RPC服務器和客戶端有用的源代碼。在我的例子中我將創建一個叫做AddtionService.java的java文件。

寫一個 service handler
Service handler 類必須實現 AdditionService.Iface接口。
示例Service handler(AdditionServiceHandler.java)

01
package com.eviac.blog.samples.thrift.server;
02
  
03
import org.apache.thrift.TException;
04
  
05
public class AdditionServiceHandler implements AdditionService.Iface {
06
  
07
 @Override
08
 public int add(int n1, int n2) throws TException {
09
  return n1 + n2;
10
 }
11
  
12
}
寫一個簡單的服務器
下面的示例代碼是一個簡單的Thrift服務器。可以看到下面的代碼中有一段是註釋瞭的,可以去掉註釋來啟用多線程服務器。
示例服務器(MyServer.java)

01
package com.eviac.blog.samples.thrift.server;
02
  
03
import org.apache.thrift.transport.TServerSocket;
04
import org.apache.thrift.transport.TServerTransport;
05
import org.apache.thrift.server.TServer;
06
import org.apache.thrift.server.TServer.Args;
07
import org.apache.thrift.server.TSimpleServer;
08
  
09
public class MyServer {
10
  
11
 public static void StartsimpleServer(AdditionService.Processor<AdditionServiceHandler> processor) {
12
  try {
13
   TServerTransport serverTransport = new TServerSocket(9090);
14
   TServer server = new TSimpleServer(
15
     new Args(serverTransport).processor(processor));
16
  
17
   // Use this for a multithreaded server
18
   // TServer server = new TThreadPoolServer(new
19
   // TThreadPoolServer.Args(serverTransport).processor(processor));
20
  
21
   System.out.println("Starting the simple server…");
22
   server.serve();
23
  } catch (Exception e) {
24
   e.printStackTrace();
25
  }
26
 }
27
   
28
 public static void main(String[] args) {
29
  StartsimpleServer(new AdditionService.Processor<AdditionServiceHandler>(new AdditionServiceHandler()));
30
 }
31
  
32
}
寫一個客戶端
下面的例子是一個使用Java寫的客戶端短使用AdditionService的服務。

 

01
package com.eviac.blog.samples.thrift.client;
02
  
03
import org.apache.thrift.TException;
04
import org.apache.thrift.protocol.TBinaryProtocol;
05
import org.apache.thrift.protocol.TProtocol;
06
import org.apache.thrift.transport.TSocket;
07
import org.apache.thrift.transport.TTransport;
08
import org.apache.thrift.transport.TTransportException;
09
  
10
public class AdditionClient {
11
  
12
 public static void main(String[] args) {
13
  
14
  try {
15
   TTransport transport;
16
  
17
   transport = new TSocket("localhost", 9090);
18
   transport.open();
19
  
20
   TProtocol protocol = new TBinaryProtocol(transport);
21
   AdditionService.Client client = new AdditionService.Client(protocol);
22
  
23
   System.out.println(client.add(100, 200));
24
  
25
   transport.close();
26
  } catch (TTransportException e) {
27
   e.printStackTrace();
28
  } catch (TException x) {
29
   x.printStackTrace();
30
  }
31
 }
32
  
33
}
運行服務端代碼(MyServer.java)將會看到下面的輸出。

 
1
Starting the simple server…
然後運行客戶端代碼(AdditionClient.java),將會看到如下輸出。

1
300

作者:王振威

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *