thrift簡介:
thrift是一個軟件框架,用來進行可擴展且跨語言的服務的開發。它結合了功能強大的軟件堆棧和代碼生成引擎,以構建在 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml 這些編程語言間無縫結合的、高效的服務。
thrift允許定義一個簡單的定義文件中的數據類型和服務接口,以作爲輸入文件,編譯器生成代碼用來方便地生成RPC客戶端和服務器通信的無縫跨編程語言。
windows 環境下thrift 的安裝:
1. 到thrift官網下載相應的exe文件
2. 將其放到某個目錄下,重命名爲thrift.exe
3. 將該目錄添加到path環境變量中
4. 在命令行下運行命令 thrift -version,如果出現"Thrift version xx.xx.xx",說明安裝成功。
thrift的基本用法:
thrift --gen language Thrift filename
其中language是要生成的代碼的編程語言,Thrift filename爲依據的thrift文件。
thrift的Hello World程序:
0. 依賴的jar包:
libthrift-0.9.1.jar
log4j.jar
slf4j-api.jar
slf4j-log4j12.jar
1. 生成一個HelloWorld.thrift文件,內容如下:
namespace java com.zhang.thrift.server //定義命名空間
service HelloWorldService { //定義服務類
string helloWorld() //定義服務的方法
}
其中的“java” 爲要生成的代碼語言(我不明白爲什麼一定要有這個東西,thrift命令的參數有了生成的代碼語言)。
在該thrift文件所在的目錄下運行命令 thrift --gen java HelloWorld.thrift
就會在該目錄下生成一個gen-java的文件夾,其中有一個類com.zhang.thrift.server.HelloWorldService
2. 新建一個類實現com.zhang.thrift.server.HelloWorldService.Iface接口
public class HelloWorldServiceImpl implements HelloWorldService.Iface{ @Override public String helloWorld() throws TException { return "Hello World!"; } }
3. 創建一個類,作爲服務器端
package com.zhang.thrift.server.java; import org.apache.thrift.server.TServer; import org.apache.thrift.server.TServer.Args; import org.apache.thrift.server.TSimpleServer; import org.apache.thrift.transport.TSSLTransportFactory; import org.apache.thrift.transport.TSSLTransportFactory.TSSLTransportParameters; import org.apache.thrift.transport.TServerSocket; import org.apache.thrift.transport.TServerTransport; import com.zhang.thrift.server.HelloWorldService; import com.zhang.thrift.server.impl.HelloWorldServiceImpl; public class JavaServer { public static HelloWorldServiceImpl helloWorldServiceImpl; public static HelloWorldService.Processor processor; public static void main(String[] args) { try { helloWorldServiceImpl = new HelloWorldServiceImpl(); processor = new HelloWorldService.Processor(helloWorldServiceImpl); Runnable simple = new Runnable() { public void run() { simple(processor); } }; // Runnable secure = new Runnable() { // public void run() { // secure(processor); // } // }; new Thread(simple).start(); // new Thread(secure).start(); } catch (Exception x) { x.printStackTrace(); } } public static void simple(HelloWorldService.Processor processor) { try { TServerTransport serverTransport = new TServerSocket(8080); 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(); } } }
4. 創建一個類,作爲客戶端,對服務進行調用測試。
package com.zhang.thrift.server.java; 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 com.zhang.thrift.server.HelloWorldService; public class JavaSimpleClient { public static void main(String[] args) { try { TTransport transport; transport = new TSocket("localhost", 8080); transport.open(); TProtocol protocol = new TBinaryProtocol(transport); HelloWorldService.Client client = new HelloWorldService.Client(protocol); perform(client); transport.close(); } catch (TException x) { x.printStackTrace(); } } private static void perform(HelloWorldService.Client client) throws TException { String result = client.helloWorld(); System.out.println(result); } }