1. 程式人生 > >Thrift下Java客戶端與伺服器端的開發

Thrift下Java客戶端與伺服器端的開發

1建立Thrift檔案

Thrift檔案與程式語言無關,用於定義資料型別和服務介面,然後生成用來構建RPC客戶和伺服器所需的全部程式碼。

1.1編寫testJava.thrift

[html] view plain copy  print?
  1. #!/usr/local/bin/thrift --gen java  
  2. namespace java Test  
  3. service Something {  
  4.    i32 ping()  
  5. }  

1.2執行thrift編譯器,生成專案原始檔

在linux命令列下輸入:Thrift –gen javatestJava.thrift

生成的原始檔在./gen-java/目錄下。

2建立Java伺服器

進入./gen-java目錄,確保系統的CLASSPATH設定正確。libthrift.jar,,slf4j-api和slf4j-simple這幾個包都需要包含在CLASSPATH裡。

2.1編寫SomethingImpl.java

[html] view plain copy  print?
  1. package Test;  
  2. import org.apache.thrift.TException;  
  3. class SomethingImpl implements Something.Iface {  
  4.  public SomethingImpl() {}  
  5.  public int ping() throws TException {  
  6.     System.out.println( "Recieve ping from client..." );  
  7.     return 0;  
  8. }  
  9. }  

2.2建立Server.java

[html] view plain copy  print?
  1. package Test;  
  2. import java.io.IOException;  
  3. import org.apache.thrift.protocol.TBinaryProtocol;  
  4. import org.apache.thrift.protocol.TBinaryProtocol.Factory;  
  5. import org.apache.thrift.server.TServer;  
  6. import org.apache.thrift.server.TThreadPoolServer;  
  7. import org.apache.thrift.transport.TServerSocket;  
  8. import org.apache.thrift.transport.TTransportException;  
  9. public class Server {  
  10. private void start() {  
  11. try {  
  12.    TServerSocket serverTransport = new TServerSocket(7911);  
  13.    Something.Processor processor = new Something.Processor(newSomethingImpl());  
  14.    Factory protFactory = new TBinaryProtocol.Factory(true, true);  
  15.    TServer server = new TThreadPoolServer(processor, serverTransport,protFactory);  
  16.    System.out.println("Starting server on port 7911 ...");  
  17.    server.serve();  
  18.   } catch (TTransportException e) {  
  19.    e.printStackTrace();  
  20.   } catch (Exception e) {  
  21.    e.printStackTrace();  
  22. }  
  23. }  
  24. public static void main(String args[]){  
  25.    Server srv = new Server();  
  26.    srv.start();  
  27. }    
  28. }  

3建立Java客戶端

建立Client.java

[html] view plain copy  print?
  1. package Test;  
  2. import java.io.IOException;  
  3. import org.apache.thrift.*;  
  4. import org.apache.thrift.protocol.*;  
  5. import org.apache.thrift.transport.*;  
  6. public class Client {  
  7.      public static void main(String [] args) {  
  8.           try {  
  9.                     TTransport transport = new TSocket("localhost", 7911);  
  10.                     TProtocol protocol = new TBinaryProtocol(transport);  
  11.                     Something.Client client =new Something.Client(protocol);  
  12.                     transport.open();  
  13.                     System.out.println("Client calls ping()");  
  14.                     client.ping();  
  15.                     transport.close();  
  16.                } catch (TException x) {  
  17.                     x.printStackTrace();  
  18.                }    
  19.        }    
  20. }   

4編譯及執行

4.1編譯

在linux命令列下輸入:javac *.java,生成客戶端與伺服器端的class檔案。

4.2執行

首先啟動伺服器。退到gen-java目錄,輸入java Test/Server,螢幕顯示如下:

Starting server on port 7911 ...

然後啟動客戶端。在同一目錄下輸入java Test/Client,螢幕顯示如下:

Client calls ping()

這時伺服器端的輸出多了一行:

Recieve ping from client...