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