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 相关文章推荐
跟老齐学Python之让人欢喜让人忧的迭代
Oct 02 Python
Python中使用PIL库实现图片高斯模糊实例
Feb 08 Python
详解Python编程中time模块的使用
Nov 20 Python
插入排序_Python与PHP的实现版(推荐)
May 11 Python
python+matplotlib绘制饼图散点图实例代码
Jan 20 Python
pandas 转换成行列表进行读取与Nan处理的方法
Oct 30 Python
Python实现SQL注入检测插件实例代码
Feb 02 Python
基于OpenCV python3实现证件照换背景的方法
Mar 22 Python
python中for循环把字符串或者字典添加到列表的方法
Jul 20 Python
Python使用selenium + headless chrome获取网页内容的方法示例
Oct 16 Python
Python collections中的双向队列deque简单介绍详解
Nov 04 Python
python opencv常用图形绘制方法(线段、矩形、圆形、椭圆、文本)
Apr 12 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
苏联队长,苏联超人蝙蝠侠,这些登场的“山寨”英雄真的很严肃
2020/04/09 欧美动漫
在PWS上安装PHP4.0正式版
2006/10/09 PHP
Windows下IIS6/Apache2.2.4+MySQL5.2+PHP5.2.1安装配置方法
2007/05/03 PHP
PHP中使用register_shutdown_function函数截获fatal error示例
2015/04/21 PHP
根据key删除数组中指定的元素实现方法
2017/03/02 PHP
Laravel接收前端ajax传来的数据的实例代码
2017/07/20 PHP
用JavaScript显示随机图像或引用
2009/04/21 Javascript
JavaScript中获取元素索引的函数
2010/09/10 Javascript
js获取窗口相对于屏幕左边和上边的位置坐标
2014/05/15 Javascript
jQuery实现加入购物车飞入动画效果
2015/03/14 Javascript
js密码强度校验
2015/11/10 Javascript
Bootstrap被封装的弹层
2016/07/20 Javascript
JavaScript 详解预编译原理
2017/01/22 Javascript
js控制文本框禁止输入特殊字符详解
2017/04/07 Javascript
node.js支持多用户web终端实现及安全方案
2017/11/29 Javascript
Vue 组件封装 并使用 NPM 发布的教程
2018/09/30 Javascript
浅析微信扫码登录原理(小结)
2018/10/29 Javascript
Vue.js组件通信之自定义事件详解
2019/10/19 Javascript
Ant Design Pro 下实现文件下载的实现代码
2019/12/03 Javascript
JavaScript实现tab栏切换效果
2020/03/16 Javascript
浅析TypeScript 命名空间
2020/03/19 Javascript
微信小程序实现组件顶端固定或底端固定效果(不随滚动而滚动)
2020/04/09 Javascript
python定时检查某个进程是否已经关闭的方法
2015/05/20 Python
Python 利用内置set函数对字符串和列表进行去重的方法
2018/06/29 Python
对Tensorflow中的矩阵运算函数详解
2018/07/27 Python
详解python列表生成式和列表生成式器区别
2019/03/27 Python
Django上使用数据可视化利器Bokeh解析
2019/07/31 Python
Django values()和value_list()的使用
2020/03/31 Python
PyTorch中的C++扩展实现
2020/04/02 Python
俄罗斯韩国化妆品网上商店:Cosmasi.ru
2019/10/31 全球购物
英文版餐饮运营管理求职信
2013/11/06 职场文书
人事部专员岗位职责
2014/03/04 职场文书
民警群众路线教育实践活动对照检查材料
2014/10/04 职场文书
售后前台接待岗位职责
2015/04/03 职场文书
教你快速开启Apache SkyWalking的自监控
2021/04/25 Servers
教你如何用Python实现人脸识别(含源代码)
2021/06/23 Python