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 thrift 实现 单端口多服务的过程的更多相关文章

  1. Thrift笔记(六)--单端口 多服务

    多个服务,使用监听一个端口.先上一个demo Test.thrift namespace java com.gxf.thrift enum RequestType { SAY_HELLO, //问好 ...

  2. Apache Thrift - 可伸缩的跨语言服务开发框架

    To put it simply, Apache Thrift is a binary communication protocol 原文地址:http://www.ibm.com/developer ...

  3. 【转】Apache Thrift - 可伸缩的跨语言服务开发框架

    Apache Thrift - 可伸缩的跨语言服务开发框架 Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架.本文将从 Java 开发人员角度详 ...

  4. Python Thrift 简单示例

    本文基于Thrift-0.10,使用Python实现服务器端,使用Java实现客户端,演示了Thrift RPC调用示例.Java客户端提供两个字符串参数,Python服务器端计算这两个字符串的相似度 ...

  5. python thrift使用实例

    前言 Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架.本文将从 Python开发人员角度简单介绍 Apache Thrift 的架构.开发和使 ...

  6. WebRTC网关服务器单端口方案实现

    标准WebRTC连接建立流程 这里描述的是Trickle ICE过程,并且省略了通话发起与接受的信令部分.流程如下: 1) WebRTC A通过Signal Server转发SDP OFFER到Web ...

  7. 端口与服务-ftp服务

    端口与服务-ftp服务 1概述 1.1.从先知和乌云上爬取端口历史漏洞报告,总结报告 1.2.全面总结,出具一个表格之类的汇总表 2.ftp # -*- coding: utf-8 -*- impor ...

  8. python thrift demo

    简介Thrift最初由Facebook研发,主要用于各个服务之间的RPC通信,支持跨语言,常用的语言比如C++, Java, Python, PHP, Ruby, Erlang, Perl, Hask ...

  9. python 里面的单下划线与双下划线的区别

    python 里面的单下划线与双下划线的区别 Python 用下划线作为变量前缀和后缀指定特殊变量. _xxx 不能用'from moduleimport *'导入 __xxx__ 系统定义名字 __ ...

随机推荐

  1. F查询与Q查询、事务及其它

    一.F查询和Q查询 1.1 F查询 在上面所有的例子中,我们构造的过滤器都是将字段值与某个我们自己设定的常量做比较.如果是对两个字段的值做比较,那这时候就要用到F查询了. Django提供F()来做这 ...

  2. 关于对Entity Framework 3.1的理解与总结

    Entity Framework Core 是一个ORM,所谓ORM也是ef的一个框架之一吧,简单的说就是把C#一个类,映射到数据库的一个表,把类里面的属性映射到表中的字段.然后Entity Fram ...

  3. Redis 的基本数据类型 和 基础应用场景

    Redis 的基础应用场景 获取中奖用户ID,随机弹出之后集合中就不存在了[set] 存储活动中中奖的用户ID,保证同一个用户不会中奖两次[set] 存储粉丝列表,value 为粉丝的用户ID,sco ...

  4. C# 特性篇 Attributes

    特性[Required] (必修的) /// <summary> /// 操作人EmpID /// </summary> [Required] public string Op ...

  5. FreeMarkerz在List中取任意一条数据的某一个值

    首先你要知道要取的数据的下标 <#list itemsList as item> <#if item_index==1> <#if "${item.value} ...

  6. vim常用指令参考

    (完)

  7. 【python爬虫实战】使用Selenium webdriver采集山东招考数据

    目录 1.目标 2.Selenium webdriver说明 2.1 为什么使用webdriver 2.2 webdriver支持浏览器 2.3 配置与使用说明 3.采集 3.1 分析网站 3.2 遍 ...

  8. web网页多语言的实现方案_前端实现多语言切换

    实现的效果 需要在web中实现多语言的切换,当用户语言切换完成后下次重新打开网页,也是上次设置的语言进行显示. 资源网站搜索大全https://55wd.com 实现步骤 1.在用户点击切换语言后,把 ...

  9. cf1216E2 Numerical Sequence (hard version) 二分查找、思维题

    题目描述 The only difference between the easy and the hard versions is the maximum value of k. You are g ...

  10. CSS让一个图片显示在另一个图片上面

    思路,在两个图片的父元素上设置  position:relative  , 然后给小图片设置 position:absolute ,位置通过top,bottom,left,right来修改,然后用  ...