通信框架淺析--google protobuf vs facebook thirft

protobuf 請參考下面這篇文章

http://blog.csdn.net/hguisu/article/details/20721109


WINDOWS配置THRIFT開發環境

 

  1)安裝thrift:到thrift官網下載exe文件,然後將文件重命名爲thrift.exe,拷貝到c:\windows目錄下(或者任何目錄下),然後就可以在dos環境下使用了

c:\windows>thrift -gen java D:\mywork\javaProject\thriftTest\test.thrift ,輸出的java文件默認輸出到當前目錄下c:\windows,也可以使用-o參數指定輸出路徑

  2)下載相關依賴包

  2.1)libthrift.jar ,下載地址:http://repo1.maven.org/maven2/org/apache/thrift/libthrift/0.9.0/

  2.2)slf4j-api.jar

  2.3)slf4j-simple.jar

  3)編寫thrift 接口文件

  1. namespace cpp zam.thrift.test  

  2. namespace py thriftTest  

  3. namespace java com.zam.thrift.test  

  4. namespace php thriftTest  

  5.   

  6. service Hello {    

  7.     string helloString(1:string word)    

  8. }  

    4)編寫接口實現代碼

  1. package com.zam.server;  

  2. import org.apache.thrift.TException;  

  3. import com.zam.thrift.test.Hello.Iface;  

  4. public class HelloImpl implements Iface{  

  5.   private static int count = 0;  

  6.   @Override  

  7.   public String helloString(String word) throws TException {  

  8.     // TODO Auto-generated method stub  

  9.     count += 1;  

  10.     System.out.println("get " + word + " " +count);     return "hello " + word + " " + count;  

  11.     }  

  12. }  

    5)編寫server代碼

  1. package com.zam.server;  

  2. import org.apache.thrift.protocol.TBinaryProtocol;    

  3. import org.apache.thrift.protocol.TBinaryProtocol.Factory;  

  4. import org.apache.thrift.server.TServer;    

  5. import org.apache.thrift.server.TThreadPoolServer;    

  6. import org.apache.thrift.server.TThreadPoolServer.Args;   

  7. import org.apache.thrift.transport.TServerSocket;    

  8. import org.apache.thrift.transport.TTransportException;   

  9. import com.zam.thrift.test.Hello;  

  10. import com.zam.thrift.test.Hello.Processor;  

  11. public class Server {  

  12.   public void startServer() {    

  13.    try {    

  14.     System.out.println("thrift server open port 1234");

  15.     TServerSocket serverTransport = new TServerSocket(1234);  

  16.     Hello.Processor process = new Processor(new HelloImpl()); 

  17.     Factory portFactory = new TBinaryProtocol.Factory(true, true);    

  18.     Args args = new Args(serverTransport);    

  19.     args.processor(process);    

  20.     args.protocolFactory(portFactory);    

  21.     TServer server = new TThreadPoolServer(args);   

  22.     server.serve();    

  23.       } 

  24.         catch (TTransportException e) {    

  25.             e.printStackTrace();    

  26.         }    

  27.     }    

  28.         

  29.     public static void main(String[] args) {    

  30.         System.out.println("thrift server init");  

  31.         Server server = new Server();    

  32.         System.out.println("thrift server start"); 

  33.         server.startServer();    

  34.         System.out.println("thrift server end");  

  35.     }    

  36. }  

    6)編寫client 代碼

  1. package com.zam.server;  

  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 com.zam.thrift.test.Hello;  

  11. public class Client {  

  12.     public void startClient() {    

  13.         TTransport transport;    

  14.         try {    

  15.             System.out.println("thrift client connext server at 1234 port ");  

  16.             transport = new TSocket("localhost", 1234);    

  17.             TProtocol protocol = new TBinaryProtocol(transport);    

  18.             Hello.Client client = new Hello.Client(protocol);    

  19.             transport.open();    

  20.             System.out.println(client.helloString("panguso"));    

  21.             transport.close();    

  22.             System.out.println("thrift client close connextion");  

  23.         } catch (TTransportException e) {    

  24.             e.printStackTrace();    

  25.         } catch (TException e) {    

  26.             e.printStackTrace();    

  27.         }    

  28.     }    

  29.     

  30.     public static void main(String[] args) {    

  31.         System.out.println("thrift client init ");  

  32.         Client client = new Client();    

  33.         System.out.println("thrift client start ");  

  34.         client.startClient();    

  35.         System.out.println("thrift client end ");  

  36.     }    

  37. }  

 8)運行server和client代碼

      8.1)啓動server端

  1. thrift server init  

  2. thrift server start  

  3. thrift server open port 1234  

      8.2)啓動client端

  1. thrift client init   

  2. thrift client start   

  3. thrift client connext server at 1234 port   

  4. hello panguso 1  

  5. thrift client close connextion  

  6. thrift client end 


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