Thrift 基本使用
2024年4月5日大约 1 分钟
记录 Thrift 的安装、使用。
参考:
安装
Thrfit 是 c++ 编写的,源码存放在 github 仓库上,一般需要源码编译安装。
安装脚本
File not found编译 IDL 文件
thrift -help
thrift -gen java user.thrift
thrift -gen cpp user.thrift
thrift -gen php user.thrift
thrift -gen js:node user.thrift
thrift -gen java -o target user.thrift例子:简单 C/S 通信
代码: https://github.com/LawssssCat/blog/tree/master/code/demo-thrift/
生成接口文件
File not found# tree thrift-api/
thrift-api/
├── pom.xml
└── src
└── main
└── java
└── com
└── example
└── thrift
├── User.java
└── UserService.java
9 directories, 3 files客户端/服务端代码 java/java
引入依赖
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.20.0</version>
</dependency>对于开发人员而言,使用原生的 Thrift 框架,需要关注以下四个核心内部接口类:
- Iface —— 服务端通过实现 UserService.Iface 接口,向客户端提供具体的同步业务逻辑。
- AsyncIface —— 服务端通过实现 UserService.Iface 接口,向客户端提供具体的异步业务逻辑。
- Client —— 客户端通过 UserService.Client 的实例对象,以同步的方式访问服务端提供的服务方法。
- AsyncClient —— 客户端通过 UserService.Client 的实例对象,以异步的方式访问服务端提供的服务方法。
::: tabs
@tab 服务端 - 处理层
File not found@tab 服务端 - 协议层/传输层
File not found@tab 客户端 - 协议层/传输层
File not found:::
客户端/服务端代码 python/java
服务端用上面的 java,下面写 python 客户端代码。
首先,下载 thrift 的 python 模块
pip install thrift客户端 - 传输层/协议层/处理层
File not found