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、編寫實(shí)現(xiàn)類、實(shí)現(xiàn)Hello.Iface:

縮略圖:



5、編寫服務(wù)端,發(fā)布(阻塞式IO + 多線程處理)服務(wù)。

  1. /** 
  2.      * 阻塞式、多線程處理 
  3.      *  
  4.      * @param args 
  5.      */  
  6.     @SuppressWarnings({ "unchecked", "rawtypes" })  
  7.     public static void main(String[] args) {  
  8.         try {  
  9.             //設(shè)置傳輸通道,普通通道  
  10.             TServerTransport serverTransport = new TServerSocket(7911);  
  11.               
  12.             //使用高密度二進(jìn)制協(xié)議  
  13.             TProtocolFactory proFactory = new TCompactProtocol.Factory();  
  14.               
  15.             //設(shè)置處理器HelloImpl  
  16.             TProcessor processor = new Hello.Processor(new HelloImpl());  
  17.               
  18.             //創(chuàng)建服務(wù)器  
  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、編寫客戶端,調(diào)用(阻塞式IO + 多線程處理)服務(wù):

  1. public static void main(String[] args) throws Exception {  
  2.         // 設(shè)置傳輸通道 - 普通IO流通道  
  3.         TTransport transport = new TSocket("localhost", 7911);  
  4.         transport.open();  
  5.           
  6.         //使用高密度二進(jìn)制協(xié)議  
  7.         TProtocol protocol = new TCompactProtocol(transport);  
  8.           
  9.         //創(chuàng)建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("耗時(shí):" + (System.currentTimeMillis() - start));  
  21.           
  22.         //關(guān)閉資源  
  23.         transport.close();  
  24.     }  



現(xiàn)在已完成整個(gè)開發(fā)過程,超級無敵簡單。

其中服務(wù)端使用的協(xié)議需要與客戶端保持一致

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


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


基于非阻塞IO(NIO)的服務(wù)端

  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.             //使用高密度二進(jìn)制協(xié)議  
  10.             TProtocolFactory proFactory = new TCompactProtocol.Factory();  
  11.               
  12.             //設(shè)置處理器 HelloImpl  
  13.             TProcessor processor = new Hello.Processor(new HelloImpl());  
  14.               
  15.             //創(chuàng)建服務(wù)器  
  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.     }  



調(diào)用非阻塞IO(NIO)服務(wù)的客戶端

  1. public static void main(String[] args) throws Exception {  
  2.         //設(shè)置傳輸通道,對于非阻塞服務(wù),需要使用TFramedTransport,它將數(shù)據(jù)分塊發(fā)送  
  3.         TTransport transport = new TFramedTransport(new TSocket("localhost", 7911));  
  4.         transport.open();  
  5.           
  6.         //使用高密度二進(jìn)制協(xié)議  
  7.         TProtocol protocol = new TCompactProtocol(transport);  
  8.           
  9.         //創(chuàng)建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("耗時(shí):" + (System.currentTimeMillis() - start));  
  21.           
  22.         //關(guān)閉資源  
  23.         transport.close();  
  24.     }  



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

客戶端異步調(diào)用

  1. /** 調(diào)用[非阻塞IO]服務(wù),異步 */  
  2.     public static void main(String[] args) {  
  3.         try {  
  4.             //異步調(diào)用管理器  
  5.             TAsyncClientManager clientManager = new TAsyncClientManager();  
  6.             //設(shè)置傳輸通道,調(diào)用非阻塞IO。  
  7.             final TNonblockingTransport transport = new TNonblockingSocket("localhost", 7911);    
  8.             //設(shè)置協(xié)議  
  9.             TProtocolFactory protocol = new TCompactProtocol.Factory();    
  10.             //創(chuàng)建Client  
  11.             final Hello.AsyncClient client = new Hello.AsyncClient(protocol, clientManager, transport);  
  12.             // 調(diào)用服務(wù)   
  13.             System.out.println("開始:" + System.currentTimeMillis());  
  14.             client.helloBoolean(false, new AsyncMethodCallback<Hello.AsyncClient.helloBoolean_call>() {  
  15.                 public void onError(Exception exception) {  
  16.                     System.out.println("錯(cuò)誤1: " + System.currentTimeMillis());  
  17.                 }  
  18.                 public void onComplete(helloBoolean_call response) {  
  19.                     System.out.println("完成1: " + System.currentTimeMillis());  
  20.                     try {  
  21.                         client.helloBoolean(false, new AsyncMethodCallback<Hello.AsyncClient.helloBoolean_call>() {  
  22.                             public void onError(Exception exception) {  
  23.                                 System.out.println("錯(cuò)誤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("結(jié)束:" + System.currentTimeMillis());  
  37.             Thread.sleep(5000);  
  38.         } catch (Exception e) {  
  39.             e.printStackTrace();  
  40.         }  
  41.     }  


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

使用SSL的服務(wù)端:



調(diào)用基于SSL服務(wù)端的客戶端: