使用Java快速入門Thrift

Apache Thrift是一個facebook創建的RPC框架,如今是一個Apache的頂級項目。Thrift容許經過一個跨語言的定義文件的方式定義數據類型和服務接口,這個文件做爲RPC客戶端和服務器通訊的標準,你也能夠去看看Thrift的白皮書瞭解更多信息。java

根據Apache Thrift的官方站點的描述,Thrift是一個:shell

software framework, for scalable cross-language services development, combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml and Delphi and other languages.

安裝Thrift比較的煩,可是在Windows下官方編譯了一個thrift.exe,下載安裝就好了。

寫 Thrift定義文件(.thrift file)

若是你以前有接觸過這個東西的話,寫定義文件很是的簡單。但這裏能夠參考官方的教程快速開始。apache

示例定義文件(add.thrift)服務器

namespace java com.eviac.blog.samples.thrift.server  // defines the namespace   
  
typedef i32 int  //typedefs to get convenient names for your types  
  
service AdditionService {  // defines the service to add two numbers  
        int add(1:int n1, 2:int n2), //defines a method  
}

編譯Thrift定義文件

下面的命令編譯.thrift文件多線程

thrift --gen <language> <Thrift filename>

對於個人例子來說,命令是:框架

thrift --gen java add.thrift

在執行完代碼後,在gen-java目錄下你會發現構建RPC服務器和客戶端有用的源代碼。在個人例子中我將建立一個叫作AddtionService.java的java文件。less

寫一個 service handler

Service handler 類必須實現 AdditionService.Iface接口。
示例Service handler(AdditionServiceHandler.java)ide

package com.eviac.blog.samples.thrift.server;  
  
import org.apache.thrift.TException;  
  
public class AdditionServiceHandler implements AdditionService.Iface {  
  
 @Override  
 public int add(int n1, int n2) throws TException {  
  return n1 + n2;  
 }  
  
}

寫一個簡單的服務器

下面的示例代碼是一個簡單的Thrift服務器。能夠看到下面的代碼中有一段是註釋了的,能夠去掉註釋來啓用多線程服務器。
示例服務器(MyServer.java)ui

package com.eviac.blog.samples.thrift.server;  
  
import org.apache.thrift.transport.TServerSocket;  
import org.apache.thrift.transport.TServerTransport;  
import org.apache.thrift.server.TServer;  
import org.apache.thrift.server.TServer.Args;  
import org.apache.thrift.server.TSimpleServer;  
  
public class MyServer {  
  
 public static void StartsimpleServer(AdditionService.Processor<AdditionServiceHandler> processor) {  
  try {  
   TServerTransport serverTransport = new TServerSocket(9090);  
   TServer server = new TSimpleServer(  
     new Args(serverTransport).processor(processor));  
  
   // Use this for a multithreaded server  
   // TServer server = new TThreadPoolServer(new  
   // TThreadPoolServer.Args(serverTransport).processor(processor));  
  
   System.out.println("Starting the simple server...");  
   server.serve();  
  } catch (Exception e) {  
   e.printStackTrace();  
  }  
 }  
   
 public static void main(String[] args) {  
  StartsimpleServer(new AdditionService.Processor<AdditionServiceHandler>(new AdditionServiceHandler()));  
 }  
  
}

寫一個客戶端

下面的例子是一個使用Java寫的客戶端短使用AdditionService的服務。this

 

package com.eviac.blog.samples.thrift.client;  
  
import org.apache.thrift.TException;  
import org.apache.thrift.protocol.TBinaryProtocol;  
import org.apache.thrift.protocol.TProtocol;  
import org.apache.thrift.transport.TSocket;  
import org.apache.thrift.transport.TTransport;  
import org.apache.thrift.transport.TTransportException;  
  
public class AdditionClient {  
  
 public static void main(String[] args) {  
  
  try {  
   TTransport transport;  
  
   transport = new TSocket("localhost", 9090);  
   transport.open();  
  
   TProtocol protocol = new TBinaryProtocol(transport);  
   AdditionService.Client client = new AdditionService.Client(protocol);  
  
   System.out.println(client.add(100, 200));  
  
   transport.close();  
  } catch (TTransportException e) {  
   e.printStackTrace();  
  } catch (TException x) {  
   x.printStackTrace();  
  }  
 }  
  
}

運行服務端代碼(MyServer.java)將會看到下面的輸出。

Starting the simple server...

而後運行客戶端代碼(AdditionClient.java),將會看到以下輸出。

300
相關文章
相關標籤/搜索