大家好,我是安果!
RPC,全程为 Remote Procedure Call,是一种进程间的通信方式,它采用「 服务端 / 客户机 」模式,是一种请求响应模型
其中,服务端负责提供服务程序、响应请求做具体的实现逻辑,客户机负责请求调用
主流的 RPC 框架包含:
阿里的 Dubbo
Facebook 的 Thrift
Google 的 gRpc
新浪微博的 Motan
Golang 生态的 rpcx
其中,gRpc 和 Thrift 是跨语言的 RPC 服务框架,并且 Thrift 相比性能更高
本篇文章以 Thrift 为例,聊聊 Python 中使用 RPC 的流程
Thrift 是一种接口描述语言和二进制通讯协议,它可以被用来定义和创建跨语言的服务,使得不同语言的客户端、服务器之间能进行高效透明的通信
thriftpy2 是在 Thrift 的基础上进行二次封装,使用它编写 RPC 更加方便快捷
项目地址:https://github.com/Thriftpy/thriftpy2
首先,我们在虚拟环境下安装依赖包
# 安装依赖
pip3 install thriftpy2
然后,如果是 Windows,建议在 Pycharm 中安装 thrift 插件
PS:该插件可以方便我们编写 Thrift 通讯文件
下载地址:https://plugins.jetbrains.com/plugin/7331-thrift-support
首先,根据需求编写 Thrift 通讯文件
比如,该文件定义 2 个方法
无参函数 ping
登录 login
包含两个参数:username、password
# foo.thrift
service PingPong{
string ping(),
string login(
1: string username,
2: string password
)
}
然后,编写服务端代码
根据 Thrift 通讯文件中定义的方法,编写具体的实现逻辑
https://blog.csdn.net/qq634506153/article/details/117749013
https://blog.csdn.net/qq634506153/article/details/117749081
https://blog.csdn.net/qq634506153/article/details/117749203
https://blog.csdn.net/qq634506153/article/details/117749793
https://blog.csdn.net/qq634506153/article/details/117749846
https://blog.csdn.net/qq634506153/article/details/117750043
https://blog.csdn.net/qq634506153/article/details/117750126
https://tieba.baidu.com/p/7397213334
https://tieba.baidu.com/p/7397401990
https://zhuanlan.zhihu.com/p/379642485
https://www.meipian.cn/3natn4pq?share_depth=1
https://blog.csdn.net/yunkeyi/article/details/117788010
https://blog.51cto.com/weixincrm/2891728
https://xueqiu.com/4824316523/182415573
https://tieba.baidu.com/p/7397734435
https://weibo.com/ttarticle/p/show?id=2309404646600764162327
https://wenwen.sogou.com/question/q931575847113395456.htm
https://tieba.baidu.com/p/7399310225
https://www.meipian.cn/3ne3kn1t?share_depth=1
https://blog.51cto.com/weixincrm/2896856
https://www.im286.net/thread-24263689-1.html
https://blog.csdn.net/yunkeyi/article/details/117820490
https://xueqiu.com/4824316523/182557333
https://zhuanlan.zhihu.com/p/379945380
https://weibo.com/ttarticle/p/show?id=2309404646941614277006
https://itbbs.pconline.com.cn/soft/54638694.html
https://iask.sina.com.cn/b/si74NmLq7m.html?kindof=IQ
https://www.xiaohongshu.com/discovery/item/60c327c7000000000102ebd2
https://zhidao.baidu.com/question/440674863586474444.html
https://itbbs.pconline.com.cn/soft/54639604.html
https://itbbs.pconline.com.cn/soft/54639587.html
https://www.meipian.cn/3np4kr2p?share_depth=1
https://zhuanlan.zhihu.com/p/381130797
https://xueqiu.com/4824316523/183186606
https://www.im286.net/thread-24264507-1.html
创建一个服务对象,指定绑定的 ip 地址及端口号,开启服务并监听消息
# rcp_server.py
import thriftpy2
from thriftpy2.rpc import make_server
# 读取通信配置文件
pingpong_thrift = thriftpy2.load("foo.thrift", module_name="pingpong_thrift")
class Dispatcher(object):
"""根据通信配置文件定义的方法,重写实现方法"""
def ping(self):
"""
Ping一下
:return:
"""
return "pong"
def login(self, username, password):
"""
登录
:param username: 用户名
:param password: 密码
:return:
"""
print('获取客户端传过来的参数,用户名:',username,",密码:",password)
return '登录成功!'
# 创建服务,指定本地ip地址及监听端口号
server = make_server(pingpong_thrift.PingPong, Dispatcher(), '192.168.40.217', 9000)
# 开启服务并监听
server.serve()
接着,编写客户端代码
这里,根据服务端提供的 ip 地址、端口号,创建客户端连接对象,调用通信文件中定义好的方法
PS:如果客户端在远程执行,需要将 Thrift 通讯文件放置到同级目录下执行
# rcp_client.py
import thriftpy2
from thriftpy2.rpc import make_client
# 读取通信配置文件
pingpong_thrift = thriftpy2.load("foo.thrift", module_name="pingpong_thrift")
# 创建客户端
client = make_client(pingpong_thrift.PingPong, '192.168.40.217', 9000)
# 调用通信文件中定义好的方法(实际调用服务端的方法)
print(client.ping())
print(client.login('root', 'pwd'))
最后,分别运行服务端和客户端的代码
使用 WireShark 进行抓包分析,能发现服务端和客户端通讯的方式及数据传输过程
WireShark 的使用可以参考这篇文章:
企业项目中,常用的 HTTP 的特点是简单、开发方便,上手简单、是主流的数据传输协议
相比 HTTP 或 H2,RPC 的主要优势体现在安全高、性能消耗低、传输效率高、服务治理方便上,所以我们可以根据实际项目需求选择合理的数据通信方式
如果你觉得文章还不错,请大家 点赞、分享、留言 下,因为这将是我持续输出更多优质文章的最强动力!
分享 Python 自动化及爬虫、数据分析实战干货,欢迎关注。