thrift 的基本介绍与使用

8413 2025-12-23 22:19:57

1. thrift 介绍thrift 是一个 apache 公司开源的一款 RPC 框架,让不同语言构建的服务可以做到远程调用无缝对接。

thrift 服务分为服务提供方(server 端)和服务请求方(client 端)

通过 idl 文件做到 server 与 client 的解耦。

代码语言:javascript复制service DemoService {

string say();

}上面就是一个最简单的 idl 文件,他表示服务名为:DemoService 的服务中提供了一个名为 say 的方法,这个方法无参数传入,返回 string 类型。

thrift idl 的详细介绍见本文第四部分。

2. 使用方式1 – thrift -gen2.1. 安装 thrift 环境1. 安装 thrift 生成工具。

windows 下载 thrift.exe – http://archive.apache.org/dist/thrift/0.9.1/thrift-0.9.1.execentos 执行 yum install thrift通过源码编译安装 – https://github.com/apache/thrift2. 安装 thrift python 包。

执行:

pip install thrift

2.2. 生成代码文件执行:

thrift -r -gen py demoservice.thrift

demoservice 就是存储我们上述所说的 DemoService 的文件,如果要生成其他语言的代码,则将 py 换成对应的目标语言,如 java、cpp、php 等。

2.3. server 代码代码语言:javascript复制# coding: utf-8

"""

thrift_client.py

"""

from thrift.transport import TSocket

from thrift.transport import TTransport

from thrift.protocol import TBinaryProtocol

from thrift.server import TServer

from genthrift.service import DemoService

class DemoServiceHandler:

def say(self):

return "hello world"

handler = DemoServiceHandler()

processor = DemoService.Processor(handler)

transport = TSocket.TServerSocket(port=9090)

tfactory = TTransport.TBufferedTransportFactory()

pfactory = TBinaryProtocol.TBinaryProtocolFactory()

server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)

print("Starting thrift server in python...")

server.serve()

print("done!")上面的代码实现了 DemoService 的 server 端,返回 hello world 字符串,核心代码是 DemoServiceHandler 的实现。

执行脚本,打印出了:

Starting thrift server in python…

2.4. client 代码代码语言:javascript复制from thrift import Thrift

from thrift.protocol import TBinaryProtocol

from thrift.transport import TSocket, TTransport

from genthrift.service import DemoService

if __name__ == '__main__':

try:

transport = TSocket.TSocket(port=9090)

transport = TTransport.TBufferedTransport(transport)

protocol = TBinaryProtocol.TBinaryProtocol(transport)

client = DemoService.Client(protocol)

transport.open()

str = "client - say: " + client.say()

print(str)

transport.close()

except Thrift.TException as ex:

print("%s" % (ex.message))代码实现了对 server 的调用,执行脚本,打印出了:

client - say: hello world。

3. 使用方式2 – thriftpy2thriftpy2 是饿了么开源的 thrift 协议纯 python 实现,具有与原生 thrift 完全相同的特性,但编写和调用方法更为简单,且不需要生成额外的代码文件。

3.1. 环境搭建执行:

pip install thriftpy2

3.2. server 端代码代码语言:javascript复制import thriftpy2

demo_thrift = thriftpy2.load("demoservice.thrift", module_name="demo_thrift")

from thriftpy2.rpc import make_server

class DemoServiceHandler(object):

def say(self):

return "hello world"

server = make_server(demo_thrift.DemoService, DemoServiceHandler(), '0.0.0.0', 9090)

server.serve()

3.3 client 端代码

import thriftpy2

demo_thrift = thriftpy2.load("demoservice.thrift", module_name="demo_thrift")

from thriftpy2.rpc import make_client

client = make_client(demo_thrift.DemoService, '127.0.0.1', 9090)

print(client.say())执行 client 端代码,打印出了:

hello world。

4. idl 介绍4.1. 基本类型bool: 布尔值 (true or false), one bytebyte: 有符号字节i16: 16位有符号整型i32: 32位有符号整型i64: 64位有符号整型double: 64位浮点型string: 包括文本类型和二进制字符串void: 方法无返回值,可以定义返回类型为 void4.2. 容器类型list: 元素类型为t1的有序表,容许元素重复set:元素类型为t1的无序表,不容许元素重复map: 键类型为t1,值类型为t2的kv对,键不容许重复4.3. 枚举类型枚举可以指定每个元素的值,也可以不指定,默认从 0 开始递增。

代码语言:javascript复制enum TweetType {

TWEET,

RETWEET = 2,

DM = 0xa,

REPLY

}4.4. 结构体thrift 要求每个域都必须有一个唯一的正整数标识符,结构体可以包含其它结构体,可以指定默认值。

代码语言:javascript复制struct Location {

1: required double latitude = 1.0;

2: required double longitude;

}4.5. 常量代码语言:javascript复制const i32 INT_CONST = 1234;4.6. 其他namespace – 用于指定不同语言生成的目录结构

namespace cpp project

namespace java com.example.project

namespace php projectinclude – 用于引用其他 idl 文件

include "tweet.thrift"

暗黑破坏神3要塞之心在哪
国产PLC 三大巨头区别在哪,PLC编程学一种够用了吗?