python thrift 实现 单端口多服务的过程
作者:bug开发工程师. 发布时间:2022-04-28 21:46:00
Thrift 是一种接口描述语言和二进制通信协议。以前也没接触过,最近有个项目需要建立自动化测试,这个项目之间的微服务都是通过 Thrift 进行通信的,然后写自动化脚本之前研究了一下。
需要定义一个xxx.thrift的文件, 来生成各种语言的代码,生成之后我们的服务提供者和消费者,都需要把代码引入,服务端把代码实现,消费者直接使用API的存根,直接调用。
和 http 相比,同属于应用层,走 tcp 协议。Thrift 优势在于发送同样的数据,request包 和 response包 要比 http 小很多,在整体性能上要优于 http 。
前言
学习了两天thrift 一直想实现单端口多服务 但是苦于网上的 thrift 实在太少 而且大部分都是java实现的 最后 改了一个java的 实现了 单端口多服务
实现过程
1 创建 thrift 文件 添加两个服务 Transmit Hello_test
service Transmit {
string invoke(1:i32 cmd 2:string token 3:string data)
}
service Hello_test {
string hello(1: string name)
}
2 运行 thrift.exe -out gen-py --gen py test.thrift
生成对应接口 因为我的 服务端和 用户端 都是用 python写的 所以 只需要 生成python 接口即可
3 编写 server.py
# 服务类1 TransmitHandler
class TransmitHandler:
def __init__(self):
self.log = {}
def invoke(self, cmd, token, data):
cmd = cmd
token = token
data = data
if cmd == 1:
return data + 'and' + token
else:
return 'cmd不匹配'
# 服务类2 HelloHandler
class HelloHandler:
def hello(self, name):
return 'hello'+name
4 编写服务端运行代码 开启服务端
from test import Transmit
from test import Hello_test
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
# 导入
from thrift.TMultiplexedProcessor import TMultiplexedProcessor
from TransmitHandler_server import TransmitHandler
from Hello_server import HelloHandler
# open server
if __name__ == "__main__":
# 实现 单端口 多服务 的方法
transmit_handler = TransmitHandler()
transmit_processor = Transmit.Processor(transmit_handler)
hello_handler = HelloHandler()
hello_processor = Hello_test.Processor(hello_handler)
transport = TSocket.TServerSocket('127.0.0.1', 8000)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
# 多 processor
processor = TMultiplexedProcessor()
processor.registerProcessor('transmit', transmit_processor)
processor.registerProcessor('hello', hello_processor)
server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
print("Starting python server...")
server.serve()
值得注意的是 要想实现单端口 多服务 就必须得
引入processor = TMultiplexedProcessor()
用来注册两个服务类
processor.registerProcessor(‘name', procress对象)
name 属性将会在client 时用到
5运行 runserver.py
如果出现Starting python server… 则运行成功
6 编写client.py
from test import Transmit
from test import Hello_test
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.protocol.TMultiplexedProtocol import TMultiplexedProtocol
if __name__ == '__main__':
# 启动 服务
transport = TSocket.TSocket('127.0.0.1', 8000)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
# 注册两个protocol 如果想要实现单端口 多服务 就必须使用 TMultiplexedProtocol
transmit_protocol = TMultiplexedProtocol(protocol, 'transmit')
hello_protocol = TMultiplexedProtocol(protocol, 'hello')
# 注册两个客户端
transmit_client = Transmit.Client(transmit_protocol)
hello_client = Hello_test.Client(hello_protocol)
transport.open() # 打开链接
# 测试服务1
cmd = 1
token = '1111-2222-3333-4444'
data = "kong_ge"
msg = transmit_client.invoke(cmd, token, data)
print(msg)
# 测试服务2
name = '孔格'
msg2 = hello_client.hello(name)
print(msg2)
# 关闭
transport.close()
7运行client
观察结果 实现单端口多服务
总结
核心就是 TMultiplexedProcessor 类 和 TMultiplexedProtocol
但是网上关于 thrift python的实例 太少了 导致浪费了很长时间
通过这篇文章的学习很快的明白thrift 中的一些概念
来源:https://blog.csdn.net/kongge123456/article/details/106592289
猜你喜欢
- Python3 解释器Linux/Unix的系统上,一般默认的 python 版本为 2.x,我们可以将 python3.x 安装在 /us
- 工作中遇到一个很棘手的问题,交互设计师和视觉设计师在做出高保真原型后提交给前端开发工程师,最后得到的web产物从细节上和布局上都和高保真原型
- 最近在做项目的时候经常会用到定时任务,由于我的项目是使用Java来开发,用的是SpringBoot框架,因此要实现这个定时任务其实并不难。后
- 我想把本篇作为css基础教程的序曲,从今天开始翻译和整理我从设计网页以来所学到的css基础知识。本教程会分成N个部分,单独发表,其间可能会插
- 一、Python pass语句在实际开发中,有时候我们会先搭建起程序的整体逻辑结构,但是暂时不去实现某些细节,而是在这些地方加一些注释,方面
- python 里面与时间有关的模块主要是 time 和 datetime如果想获取系统当前时间戳:time.time(),是一个float型
- 概述Visdom:一个灵活的可视化工具,可用来对于 实时,富数据的 创建,组织和共享。支持Torch和Numpy还有pytorch。visd
- 1.如果客户端和服务器端的连接需要跨越并通过不可信任的网络,那么就需要使用SSH隧道来加密该连接的通信。2.用set password语句来
- 本文实例为大家分享了python实现飞机大战的具体代码,供大家参考,具体内容如下初学Python,写了一个简单的Python小游戏。师出bi
- 前言matplotlib.pyplot是一些命令行风格函数的集合,使matplotlib以类似于MATLAB的方式工作。每个pyplot函数
- 本文实例讲述了JS实现pasteHTML兼容ie,firefox,chrome的方法。分享给大家供大家参考,具体如下:<html>
- django rest framework使用django-filter注意事项:一定要在setting文件里面加载如下代码,而不是只安装包
- 作用域链(Scope Chain)JavaScript中的一种重要机制,JS中所有的标识符(Identifier)都是通过Scope Cha
- 前言你可能不需要经常处理分数,但当你需要时,Python的Fraction类会给你很大的帮助。本文将给大家详细介绍关于利用标准库fracti
- 桥接模式(Bridge Pattern)是什么桥接模式是一种结构型模式,它将抽象部分与实现部分分离开来,使它们可以独立地变化。在桥接模式中,
- 在软件开发的初始阶段,开发商们总是想把整个系统的最小的细节设计好了,然后再去单线程的编写代码。这样软件开发完成需要很长时间,但开发商们一直都
- 谢谢icedblog提供的代码, 从那个js中我参考了moveSelect的利用, 不过那个代码中有几个BUG, 比如按住鼠标不放会不见显示
- 最近用layer ui上传文件遇到了一个问题,我想在上传文件之前把data-id传入后台,layer文档找了一下也没有找到类似的说明,经过一
- 代码如下:<% '/* 函数名称:Zxj_ReplaceHtmlClearHtml '/
- 这里主要是解决multipart/form-data这种格式的文件上传,基本现在http协议上传文件基本上都是通过这种格式上传1 思路一般情