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 接口即可
# 服务类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
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 时用到
如果出现Starting python server… 则运行成功
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()
核心就是 TMultiplexedProcessor 类 和 TMultiplexedProtocol 但是网上关于 thrift python的实例 太少了 导致浪费了很长时间 通过这篇文章的学习很快的明白thrift 中的一些概念
到此这篇关于python thrift 实现 单端口多服务的过程的文章就介绍到这了,更多相关python thrift单端口多服务内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
- Thrift笔记(六)--单端口 多服务
多个服务,使用监听一个端口.先上一个demo Test.thrift namespace java com.gxf.thrift enum RequestType { SAY_HELLO, //问好 ...
- Apache Thrift - 可伸缩的跨语言服务开发框架
To put it simply, Apache Thrift is a binary communication protocol 原文地址:http://www.ibm.com/developer ...
- 【转】Apache Thrift - 可伸缩的跨语言服务开发框架
Apache Thrift - 可伸缩的跨语言服务开发框架 Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架.本文将从 Java 开发人员角度详 ...
- Python Thrift 简单示例
本文基于Thrift-0.10,使用Python实现服务器端,使用Java实现客户端,演示了Thrift RPC调用示例.Java客户端提供两个字符串参数,Python服务器端计算这两个字符串的相似度 ...
- python thrift使用实例
前言 Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架.本文将从 Python开发人员角度简单介绍 Apache Thrift 的架构.开发和使 ...
- WebRTC网关服务器单端口方案实现
标准WebRTC连接建立流程 这里描述的是Trickle ICE过程,并且省略了通话发起与接受的信令部分.流程如下: 1) WebRTC A通过Signal Server转发SDP OFFER到Web ...
- 端口与服务-ftp服务
端口与服务-ftp服务 1概述 1.1.从先知和乌云上爬取端口历史漏洞报告,总结报告 1.2.全面总结,出具一个表格之类的汇总表 2.ftp # -*- coding: utf-8 -*- impor ...
- python thrift demo
简介Thrift最初由Facebook研发,主要用于各个服务之间的RPC通信,支持跨语言,常用的语言比如C++, Java, Python, PHP, Ruby, Erlang, Perl, Hask ...
- python 里面的单下划线与双下划线的区别
python 里面的单下划线与双下划线的区别 Python 用下划线作为变量前缀和后缀指定特殊变量. _xxx 不能用'from moduleimport *'导入 __xxx__ 系统定义名字 __ ...
随机推荐
- mysql错误详解(1819):ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
O(∩_∩)O哈哈~ 在学习 Mysql 的时候又遇到了新问题了 o(╥﹏╥)o 当我在准备为用户授权的时候: grant all privileges on *.* to 'root'@'%' id ...
- docker创建tomcat容器
配置阿里云镜像地址:先在阿里云搜索:容器镜像服务 --> 最下面找到 容器加速服务 docker配置 etc目录下 创建docker文件夹 mkdir --->docker --- ...
- MBA都需要学习哪些课程
MBA课程内容详解: 1.核心课程: 管理经济学.营销管理.战略管理.组织行为学.会计学.公司财务管理.人力资源管理与开发.管理与沟通.经济法.国际贸易 2.学位课程: 商务英语(一.二).管理伦理学 ...
- 编译运行Zookeeper源码
GitHub地址: https://github.com/apache/zookeeper 最新版本的 zookeeper 已经使用了 maven 进行管理了.不再需要安装 Ant 下载完成之后.使用 ...
- paramiko报错 Garbage packet received
前情概要 今天想要写一个多进程的python脚本上传代码至服务器,于是在本地用虚拟机测试了一下,可总是报错,具体报错信息如下 Traceback (most recent call last): Fi ...
- 洛谷 P3243 【[HNOI2015]菜肴制作】
先吐槽一下这个难度吧,评的有点高了,但是希望别降,毕竟这是我能做出来的不多的紫题了(狗头). 大家上来的第一反应应该都是啊,模板题,然后兴高采烈的打了拓补排序的板子,然后搞个小根堆,按照字典序输出就可 ...
- Laytpl 1.2
https://jeesite.gitee.io/front/laytpl/index.html
- 化繁就简,如何利用Spring AOP快速实现系统日志
1.引言 有关Spring AOP的概念就不细讲了,网上这样的文章一大堆,要讲我也不会比别人讲得更好,所以就不啰嗦了. 为什么要用Spring AOP呢?少写代码.专注自身业务逻辑实现(关注本身的业务 ...
- Android 伤敌一千自损八百之萤石摄像头集成(一)
最近忙着修改萤石摄像头C3型号开头的设备添加 本来不是很复杂的事情. , 现在我感觉我入魔了 总感觉这是个小人 螺丝口是眼睛 插入SD卡的事鼻子嘴 接信号的事手 怎么看怎么像愤怒的小人 总结,先看一下 ...
- 在执行 pip install 时遇到错误:python setup.py egg_info ...
最近重新安装win10 64位专业版, 正好遇到python3.8发布,试了一下.结果jupyter都安装不了...心碎. ERROR: Command errored out with exit s ...