
Thrift 簡介
Thrift 是什么
Thrift是一個軟件框架,用來進行可擴展且跨語言的服務的開發。它結合了功能強大的軟件堆棧和代碼生成引擎,以構建在 C++, Java, Go,Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml 這些編程語言間無縫結合的、高效的服務。
Thrift 的功能
Thrift允許定義一個簡單的定義文件中的數據類型和服務接口,以作為輸入文件,編譯器生成代碼用來方便地生成RPC客戶端和服務器通信的無縫跨編程語言。
環境安裝
基於MAC環境,打開終端,執行如下命令:
brew install thrift
查看是否安裝成功:
thrift -version
使用示例
Maven 依賴
在服務端和客戶端的 pom.xml 中添加 Thrift 依賴(版本和安裝時保持一致):
<dependency> <groupId>org.apache.thrift</groupId> <artifactId>libthrift</artifactId> <version>0.11.0</version> </dependency>
編寫服務端 Thrift 文件
hello.thrift
namespace java com.hans.thriftserver service Hello{ string helloString(1:string param) }
使用 Thrift 工具生成 java 代碼
thrift -r -gen java hello.thrift
修改 namespace java com.hans.thriftclient 后再次執行則生成客戶端 java 代碼
將代碼放到對應目錄
即 com.hans.thriftserver 子目錄 thrift 下
編寫服務實現類
package com.hans.thriftserver.thrift.impl; import com.hans.thriftserver.thrift.Hello; import org.apache.thrift.TException; public class HelloServiceImpl implements Hello.Iface { @Override public String helloString(String param) throws TException { return "hello: " + param; } }
編寫服務端
package com.hans.thriftserver;
import com.hans.thriftserver.thrift.Hello; import com.hans.thriftserver.thrift.impl.HelloServiceImpl; import org.apache.thrift.TProcessor; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.server.TServer; import org.apache.thrift.server.TSimpleServer; import org.apache.thrift.transport.TServerSocket; import org.apache.thrift.transport.TTransportException; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author huangxun */ @SpringBootApplication public class ThriftServerApplication { public static void main(String[] args) { SpringApplication.run(ThriftServerApplication.class, args); try { System.out.println("服務端開啟...."); TProcessor tprocessor = new Hello.Processor<Hello.Iface>(new HelloServiceImpl()); // 簡單的單線程服務模型 TServerSocket serverTransport = new TServerSocket(9898); TServer.Args tArgs = new TServer.Args(serverTransport); tArgs.processor(tprocessor); tArgs.protocolFactory(new TBinaryProtocol.Factory()); TServer server = new TSimpleServer(tArgs); server.serve(); } catch (TTransportException e) { e.printStackTrace(); } } }
編寫客戶端
package com.hans.thriftclient; import com.hans.thriftclient.thrift.Hello; 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; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author huangxun */ @SpringBootApplication public class ThriftClientApplication { public static void main(String[] args) { SpringApplication.run(ThriftClientApplication.class, args); System.out.println("客戶端啟動...."); TTransport transport = null; try { transport = new TSocket("localhost", 9898, 30000); // 協議要和服務端一致 TProtocol protocol = new TBinaryProtocol(transport); Hello.Client client = new Hello.Client(protocol); transport.open(); String result = client.helloString("hans"); System.out.println(result); } catch (TTransportException e) { e.printStackTrace(); } catch (TException e) { e.printStackTrace(); } finally { if (null != transport) { transport.close(); } } } }
啟動服務端
后台打印輸出:
服務端開啟....
執行客戶端
后台打印輸出:
客戶端啟動.... hello: hans