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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)