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

 更新时间:2020年06月08日 10:44:56   作者:bug开发工程师.  
这篇文章主要介绍了python thrift 实现 单端口多服务的过程,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

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使用MD5加密算法对字符串进行加密操作示例

    Python使用MD5加密算法对字符串进行加密操作示例

    这篇文章主要介绍了Python使用MD5加密算法对字符串进行加密操作,结合实例形式分析了Python实现md5加密相关操作技巧,需要的朋友可以参考下
    2018-03-03
  • 150行Python代码实现带界面的数独游戏

    150行Python代码实现带界面的数独游戏

    这篇文章主要介绍了150行Python代码实现带界面的数独游戏,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • Python操作Sql Server 2008数据库的方法详解

    Python操作Sql Server 2008数据库的方法详解

    这篇文章主要介绍了Python操作Sql Server 2008数据库的方法,结合实例形式分析了Python使用pyodbc库操作Sql Server 2008数据库的连接、执行sql语句、关闭连接等相关操作技巧与注意事项,需要的朋友可以参考下
    2018-05-05
  • python3 selenium自动化 下拉框定位的例子

    python3 selenium自动化 下拉框定位的例子

    今天小编就为大家分享一篇python3 selenium自动化 下拉框定位的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08
  • 解决python明明pip安装成功却找不到包的问题

    解决python明明pip安装成功却找不到包的问题

    今天小编就为大家分享一篇解决python明明pip安装成功却找不到包的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08
  • Python练习之操作SQLite数据库

    Python练习之操作SQLite数据库

    这篇文章主要介绍了Python练习之操作SQLite数据库,主要通过三个问题如何创建SQLite数据库?如何向SQLite表中插入数据?如何查询SQLite表中的数据?展开文章主题详情,需要的朋友可以参考一下
    2022-06-06
  • Python基本数据类型及内置方法

    Python基本数据类型及内置方法

    这篇文章主要介绍了Python基本数据类型及内置方法,​ 数据类型是用来记录事物状态的,而事物的状态是不断变化的,下文围绕主题展开相关内容需要的小伙伴可以参考一下
    2022-04-04
  • python写的一个文本编辑器

    python写的一个文本编辑器

    这篇文章主要介绍了python写的一个文本编辑器,大家参考使用吧
    2014-01-01
  • python 读取目录下csv文件并绘制曲线v111的方法

    python 读取目录下csv文件并绘制曲线v111的方法

    今天小编就为大家分享一篇python 读取目录下csv文件并绘制曲线v111的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • python判断无向图环是否存在的示例

    python判断无向图环是否存在的示例

    今天小编就为大家分享一篇python判断无向图环是否存在的示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11

最新评论