Thrift是一個軟件框架,用來進行可擴展且跨語言的服務的開發。它結合了功能強大的軟件堆棧和代碼生成引擎,以構建在 C++, Java, Go,Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml 這些編程語言間無縫結合的、高效的服務。java
Thrift容許定義一個簡單的定義文件中的數據類型和服務接口,以做爲輸入文件,編譯器生成代碼用來方便地生成RPC客戶端和服務器通訊的無縫跨編程語言。git
基於MAC環境,打開終端,執行以下命令:github
brew install thrift
查看是否安裝成功:spring
thrift -version
在服務端和客戶端的 pom.xml 中添加 Thrift 依賴(版本和安裝時保持一致):apache
<dependency> <groupId>org.apache.thrift</groupId> <artifactId>libthrift</artifactId> <version>0.11.0</version> </dependency>
hello.thrift編程
namespace java com.hans.thriftserver service Hello{ string helloString(1:string param) }
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(); } } } }
後臺打印輸出:編程語言
服務端開啓....
後臺打印輸出:ide
客戶端啓動.... hello: hans