Thrift交流(一)簡單的Thrift

轉載自 CSDN Arjick


Thrift是一個可伸縮的跨語言的服務開發框架,是facebook開發的一個跨語言通信平臺。爲各種語言提供快捷的rpc服務。現階段已經支持C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk, and OCaml等語言。在近來的工作中,重新學習Thrift通信的內容,和大家做個簡單的交流。

Thrift示意圖



1)安裝Thrift環境

Thrift的環境的安裝並不複雜,我們現在以最簡單的windows環境做個案例。

首先在官方網站下載最新的thrift exe文件,http://thrift.apache.org/download/

然後把exe文件改名爲thrift.exe,放在了windows的目錄下,如圖所示:


接着配置thrift環境變量:

測試Thrift運行環境:


2)編寫Thrift文件

thrift文件如下

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. namespace java thrift  // defines the namespace    
  2.      
  3. typedef i32 int  //typedefs to get convenient names for your types  
  4.   
  5. service ThriftService {    
  6.     int add(1:int a,2:int b),  
  7. }  

通過命令行執行Thrift文件,並生成


3)建立Thrift工程

首先建立maven工程,把如下內容加到pom.xml

[html] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. <dependency>  
  2.             <groupId>org.apache.thrift</groupId>  
  3.             <artifactId>libthrift</artifactId>  
  4.             <version>0.9.0</version>  
  5.         </dependency>  
  6.         <dependency>  
  7.             <groupId>org.slf4j</groupId>  
  8.             <artifactId>slf4j-api</artifactId>  
  9.             <version>1.7.5</version>  
  10.         </dependency>  
  11.         <dependency>  
  12.             <groupId>org.slf4j</groupId>  
  13.             <artifactId>slf4j-simple</artifactId>  
  14.             <version>1.7.5</version>  
  15.         </dependency>  
  16.   
  17.         <dependency>  
  18.             <groupId>org.slf4j</groupId>  
  19.             <artifactId>slf4j-log4j12</artifactId>  
  20.             <version>1.7.5</version>  
  21.         </dependency>  
  22.         <dependency>  
  23.             <groupId>log4j</groupId>  
  24.             <artifactId>log4j</artifactId>  
  25.             <version>1.2.17</version>  
  26.         </dependency>  


[html] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1.   
編寫ThriftService的實現類:

代碼如下:

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. package thrift;  
  2.   
  3. import org.apache.thrift.TException;  
  4.   
  5. public class ThriftServiceImpl implements Iface {  
  6.   
  7.     @Override  
  8.     public int add(int a, int b) throws TException {  
  9.         return a + b;  
  10.     }  
  11.   
  12. }  

4)編寫服務器及測試

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. package com.duowan.yy.thriftTest;  
  2.   
  3. import org.apache.thrift.TProcessor;  
  4. import org.apache.thrift.protocol.TBinaryProtocol;  
  5. import org.apache.thrift.protocol.TBinaryProtocol.Factory;  
  6. import org.apache.thrift.server.TServer;  
  7. import org.apache.thrift.server.TSimpleServer;  
  8. import org.apache.thrift.transport.TServerSocket;  
  9. import org.apache.thrift.transport.TTransportException;  
  10.   
  11. import thrift.ThriftService;  
  12. import thrift.ThriftServiceImpl;  
  13.   
  14. public class ThriftServer {  
  15.     public static void main(String[] args) {  
  16.         try {  
  17.             TServerSocket serverTransport = new TServerSocket(7911);  
  18.   
  19.             Factory proFactory = new TBinaryProtocol.Factory();  
  20.   
  21.             TProcessor processor = new ThriftService.Processor<ThriftService.Iface>(  
  22.                     new ThriftServiceImpl());  
  23.             TServer.Args tArgs = new TServer.Args(serverTransport);  
  24.             tArgs.processor(processor);  
  25.             tArgs.protocolFactory(proFactory);  
  26.   
  27.             TServer server = new TSimpleServer(tArgs);  
  28.             System.out.println("Start server on port 7911....");  
  29.             server.serve();  
  30.         } catch (TTransportException e) {  
  31.             e.printStackTrace();  
  32.         }  
  33.     }  
  34. }  

測試啓動:



編寫測試類:

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. package com.duowan.yy.thriftTest;  
  2.   
  3. import org.apache.thrift.TException;  
  4. import org.apache.thrift.protocol.TBinaryProtocol;  
  5. import org.apache.thrift.protocol.TProtocol;  
  6. import org.apache.thrift.transport.TSocket;  
  7. import org.apache.thrift.transport.TTransport;  
  8. import org.apache.thrift.transport.TTransportException;  
  9.   
  10. import thrift.ThriftService;  
  11.   
  12.   
  13. public class Test {  
  14.   
  15.     /** 
  16.      * @param args 
  17.      */  
  18.     public static void main(String[] args) {  
  19.         try {  
  20.             TTransport transport = new TSocket("localhost"7911);  
  21.             transport.open();  
  22.             TProtocol protocol = new TBinaryProtocol(transport);  
  23.             ThriftService.Client client = new ThriftService.Client(protocol);  
  24.             System.out.println(client.add(775));  
  25.             transport.close();  
  26.   
  27.         } catch (TTransportException e) {  
  28.             // TODO Auto-generated catch block  
  29.             e.printStackTrace();  
  30.         } catch (TException e) {  
  31.             // TODO Auto-generated catch block  
  32.             e.printStackTrace();  
  33.         }  
  34.     }  
  35.   
  36. }  

  效果如下:



簡單的Thrift之旅已經完成了,希望大家玩的開心。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章