Apache Thrift - java開發詳解



1、添加依賴 jar

<dependency>
  <groupId>org.apache.thrift</groupId>
  <artifactId>libthrift</artifactId>
  <version>0.8.0</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.6.1</version>
</dependency>

2、編寫IDL文件 Hello.thrift

namespace java service.demo
service Hello {
    string helloString(1:string para)
    i32 helloInt(1:i32 para)
    bool helloBoolean(1:bool para)
    void helloVoid()
    string helloNull()
}


3、生成代碼

thrift -o <output directory> -gen java Hello.thrift
生成代碼縮略圖:



4、編寫實現類、實現Hello.Iface:

縮略圖:



5、編寫服務端,發佈(阻塞式IO + 多線程處理)服務。

  1. /** 
  2.      * 阻塞式、多線程處理 
  3.      *  
  4.      * @param args 
  5.      */  
  6.     @SuppressWarnings({ "unchecked""rawtypes" })  
  7.     public static void main(String[] args) {  
  8.         try {  
  9.             //設置傳輸通道,普通通道  
  10.             TServerTransport serverTransport = new TServerSocket(7911);  
  11.               
  12.             //使用高密度二進制協議  
  13.             TProtocolFactory proFactory = new TCompactProtocol.Factory();  
  14.               
  15.             //設置處理器HelloImpl  
  16.             TProcessor processor = new Hello.Processor(new HelloImpl());  
  17.               
  18.             //創建服務器  
  19.             TServer server = new TThreadPoolServer(  
  20.                     new Args(serverTransport)  
  21.                     .protocolFactory(proFactory)  
  22.                     .processor(processor)  
  23.                 );  
  24.               
  25.             System.out.println("Start server on port 7911...");  
  26.             server.serve();  
  27.         } catch (Exception e) {  
  28.             e.printStackTrace();  
  29.         }  
  30.     }  



6、編寫客戶端,調用(阻塞式IO + 多線程處理)服務:

  1. public static void main(String[] args) throws Exception {  
  2.         // 設置傳輸通道 - 普通IO流通道  
  3.         TTransport transport = new TSocket("localhost"7911);  
  4.         transport.open();  
  5.           
  6.         //使用高密度二進制協議  
  7.         TProtocol protocol = new TCompactProtocol(transport);  
  8.           
  9.         //創建Client  
  10.         Hello.Client client = new Hello.Client(protocol);  
  11.           
  12.         long start = System.currentTimeMillis();  
  13.         for(int i=0; i<10000; i++){  
  14.             client.helloBoolean(false);  
  15.             client.helloInt(111);  
  16.             client.helloNull();  
  17.             client.helloString("dongjian");  
  18.             client.helloVoid();  
  19.         }  
  20.         System.out.println("耗時:" + (System.currentTimeMillis() - start));  
  21.           
  22.         //關閉資源  
  23.         transport.close();  
  24.     }  



現在已完成整個開發過程,超級無敵簡單。

其中服務端使用的協議需要與客戶端保持一致

-------------------------------------------------------------------------------------------------------------------


上面展示了普通且常用的服務端和客戶端,下面請看非阻塞IO,即java中的NIO:


基於非阻塞IO(NIO)的服務端

  1. public static void main(String[] args) {  
  2.         try {  
  3.             //傳輸通道 - 非阻塞方式  
  4.             TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(7911);  
  5.               
  6.             //異步IO,需要使用TFramedTransport,它將分塊緩存讀取。  
  7.             TTransportFactory transportFactory = new TFramedTransport.Factory();  
  8.               
  9.             //使用高密度二進制協議  
  10.             TProtocolFactory proFactory = new TCompactProtocol.Factory();  
  11.               
  12.             //設置處理器 HelloImpl  
  13.             TProcessor processor = new Hello.Processor(new HelloImpl());  
  14.               
  15.             //創建服務器  
  16.             TServer server = new TThreadedSelectorServer(  
  17.                     new Args(serverTransport)  
  18.                     .protocolFactory(proFactory)  
  19.                     .transportFactory(transportFactory)  
  20.                     .processor(processor)  
  21.                 );  
  22.               
  23.             System.out.println("Start server on port 7911...");  
  24.             server.serve();  
  25.         } catch (Exception e) {  
  26.             e.printStackTrace();  
  27.         }  
  28.     }  



調用非阻塞IO(NIO)服務的客戶端

  1. public static void main(String[] args) throws Exception {  
  2.         //設置傳輸通道,對於非阻塞服務,需要使用TFramedTransport,它將數據分塊發送  
  3.         TTransport transport = new TFramedTransport(new TSocket("localhost"7911));  
  4.         transport.open();  
  5.           
  6.         //使用高密度二進制協議  
  7.         TProtocol protocol = new TCompactProtocol(transport);  
  8.           
  9.         //創建Client  
  10.         Hello.Client client = new Hello.Client(protocol);  
  11.           
  12.         long start = System.currentTimeMillis();  
  13.         for(int i=0; i<10000; i++){  
  14.             client.helloBoolean(false);  
  15.             client.helloInt(111);  
  16.             client.helloNull();  
  17.             client.helloString("360buy");  
  18.             client.helloVoid();  
  19.         }  
  20.         System.out.println("耗時:" + (System.currentTimeMillis() - start));  
  21.           
  22.         //關閉資源  
  23.         transport.close();  
  24.     }  



-----------------------------------------------------------------------------------------------------------------------------------

客戶端異步調用

  1. /** 調用[非阻塞IO]服務,異步 */  
  2.     public static void main(String[] args) {  
  3.         try {  
  4.             //異步調用管理器  
  5.             TAsyncClientManager clientManager = new TAsyncClientManager();  
  6.             //設置傳輸通道,調用非阻塞IO。  
  7.             final TNonblockingTransport transport = new TNonblockingSocket("localhost"7911);    
  8.             //設置協議  
  9.             TProtocolFactory protocol = new TCompactProtocol.Factory();    
  10.             //創建Client  
  11.             final Hello.AsyncClient client = new Hello.AsyncClient(protocol, clientManager, transport);  
  12.             // 調用服務   
  13.             System.out.println("開始:" + System.currentTimeMillis());  
  14.             client.helloBoolean(falsenew AsyncMethodCallback<Hello.AsyncClient.helloBoolean_call>() {  
  15.                 public void onError(Exception exception) {  
  16.                     System.out.println("錯誤1: " + System.currentTimeMillis());  
  17.                 }  
  18.                 public void onComplete(helloBoolean_call response) {  
  19.                     System.out.println("完成1: " + System.currentTimeMillis());  
  20.                     try {  
  21.                         client.helloBoolean(falsenew AsyncMethodCallback<Hello.AsyncClient.helloBoolean_call>() {  
  22.                             public void onError(Exception exception) {  
  23.                                 System.out.println("錯誤2: " + System.currentTimeMillis());  
  24.                             }  
  25.                               
  26.                             public void onComplete(helloBoolean_call response) {  
  27.                                 System.out.println("完成2: " + System.currentTimeMillis());  
  28.                                 transport.close();  
  29.                             }  
  30.                         });  
  31.                     } catch (TException e) {  
  32.                         e.printStackTrace();  
  33.                     }  
  34.                 }  
  35.             });  
  36.             System.out.println("結束:" + System.currentTimeMillis());  
  37.             Thread.sleep(5000);  
  38.         } catch (Exception e) {  
  39.             e.printStackTrace();  
  40.         }  
  41.     }  


-----------------------------------------------------------------------------------------------------------------------------------

使用SSL的服務端:



調用基於SSL服務端的客戶端:



發佈了0 篇原創文章 · 獲贊 4 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章