python thrift 实现 单端口多服务的过程


Posted in Python onJune 08, 2020

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 中的一些概念

到此这篇关于python thrift 实现 单端口多服务的过程的文章就介绍到这了,更多相关python thrift单端口多服务内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
django1.8使用表单上传文件的实现方法
Nov 04 Python
利用python实现简单的循环购物车功能示例代码
Jul 05 Python
pytorch 调整某一维度数据顺序的方法
Dec 08 Python
举例讲解Python常用模块
Mar 08 Python
解决django 新增加用户信息出现错误的问题
Jul 28 Python
PIL对上传到Django的图片进行处理并保存的实例
Aug 07 Python
Python GUI学习之登录系统界面篇
Aug 21 Python
Python 解决OPEN读文件报错 ,路径以及r的问题
Dec 19 Python
Python使用多进程运行含有任意个参数的函数
May 02 Python
python中get和post有什么区别
Jun 19 Python
Python使用文件操作实现一个XX信息管理系统的示例
Jul 02 Python
Python模拟登录requests.Session应用详解
Nov 17 Python
Python astype(np.float)函数使用方法解析
Jun 08 #Python
python opencv 实现读取、显示、写入图像的方法
Jun 08 #Python
python:删除离群值操作(每一行为一类数据)
Jun 08 #Python
pyecharts在数据可视化中的应用详解
Jun 08 #Python
python numpy实现rolling滚动案例
Jun 08 #Python
Python如何向SQLServer存储二进制图片
Jun 08 #Python
python求numpy中array按列非零元素的平均值案例
Jun 08 #Python
You might like
eaglephp使用微信api接口开发微信框架
2014/01/09 PHP
php中数字、字符与对象判断函数用法实例
2014/11/26 PHP
PHP中new static() 和 new self() 的区别介绍
2015/01/09 PHP
Zend Framework开发入门经典教程
2016/03/23 PHP
php使用FFmpeg接口获取视频的播放时长、码率、缩略图以及创建时间
2016/11/07 PHP
关于javascript 回调函数中变量作用域的讨论
2009/09/11 Javascript
将list转换为json失败的原因
2013/12/17 Javascript
禁止iframe页面的所有js脚本如alert及弹出窗口等
2014/09/03 Javascript
TypeScript具有的几个不同特质
2015/04/07 Javascript
使用jspdf生成pdf报表
2015/07/03 Javascript
jquery遍历table的tr获取td的值实现方法
2016/05/19 Javascript
jQuery回调方法使用示例
2017/06/26 jQuery
详解vue中使用express+fetch获取本地json文件
2017/10/10 Javascript
jQuery实现的粘性滚动导航栏效果实例【附源码下载】
2017/10/19 jQuery
NW.js 简介与使用方法
2018/02/01 Javascript
基于vue1和vue2获取dom元素的方法
2018/03/17 Javascript
vue中render函数的使用详解
2018/10/12 Javascript
vue自定义js图片碎片轮播图切换效果的实现代码
2019/04/28 Javascript
Element-ui中元素滚动时el-option超出元素区域的问题
2019/05/30 Javascript
微信小程序连接服务器展示MQTT数据信息的实现
2020/07/14 Javascript
[49:08]OpTic vs Serenity 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
Python构造自定义方法来美化字典结构输出的示例
2016/06/16 Python
详解Python并发编程之创建多线程的几种方法
2019/08/23 Python
Python上下文管理器全实例详解
2019/11/12 Python
Python的对象传递与Copy函数使用详解
2019/12/26 Python
使用 PyTorch 实现 MLP 并在 MNIST 数据集上验证方式
2020/01/08 Python
使用Tensorflow将自己的数据分割成batch训练实例
2020/01/20 Python
Python 日期时间datetime 加一天,减一天,加减一小时一分钟,加减一年
2020/04/16 Python
Python Celery异步任务队列使用方法解析
2020/08/10 Python
用Python自动清理系统垃圾的实现
2021/01/18 Python
英国团购网站:Groupon英国
2017/11/28 全球购物
八一建军节活动方案
2014/02/10 职场文书
经贸韩语专业大学生职业规划
2014/02/14 职场文书
侵犯商业秘密的律师函
2015/05/27 职场文书
gtx1650怎么样 gtx1650显卡相当于什么级别
2022/04/08 数码科技
vue 给数组添加新对象并赋值
2022/04/20 Vue.js