栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python调用rpc代码维护方案探讨

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python调用rpc代码维护方案探讨

user重构,我们准备增加一个代码仓库rpc_client,维护python调用rpc服务的代码。

⁣1,现状

1.1,微服务架构

我们现在有多个python旧服务项目(user_server/course_server/cms_server_v2等等),以后会有多个golang的rpc服务(passport_i/user_i/business_i等)。 

1.2,封装rpc

如上图每一个旧服务的rpc调用,都需要封装python代码如下,供旧服务调用:

# 封装的rpc调用方式from hd_lib.rpc_client.etcd_rpc_call import parse_rpc_respfrom user_i.client import get_user_clientfrom user_i.user_py import user_pb2def get_profile(pid):  # 一个rpc调用方法    cli_req = user_pb2.GetUserProfileReq(pid=pid)    return parse_rpc_resp(get_user_client().GetUserProfile)(cli_req)

1.3,存在的问题

  • 原生的rpc方法(通过proto生成的python代码)并不友好,每个rpc方法都需要初始化client,都需要组装req,都需要解析resp。
  • 每个python服务都需要实现一次rpc方法。

1.4,解决方案

  • 把rpc方法都放到一个地方封装好,并按照子模块进行聚类。
  • 把封装好的rpc方法都维护在一个仓库,供上层的python服务引用调用。

理论上,我们基于每个微服务对应的xxx_i,对每个rpc方法封装即可。

但是我们准备增加一个新仓库rpc_client里面包括所有的xxx_i,并对每个xxx_i维护rpc调用代码。

        可以共用client初始化,以及resp解析的相关代码。

        每个上层的python服务只需要通过一个(而不是多个)子模块引用所有的rpc能力。

 

2,rpc_client仓库维护方法

2.1,仓库地址

git@xxxx/rpc_client.git

2.2,目录结构与注释

|__passport_i // git submodule add git@git.hundun.cn:golang-servers/passport_i.git

|__user_i // git submodule add git@git.hundun.cn:golang-servers/user_i.git

|__user_rpc

        |__ __init__.py

        |__ profile.py // 封装profile子模块的rpc调用方法

        |__ permission.py // 封装permission.py子模块的rpc调用方法

        |__ ... // 封装其他子模块的rpc调用方法

|__passport_rpc

        |__ __init__.py

        |__ login.py // 封装登录子模块的rpc调用方法

        |__ ...

|__ __init__.py

|__helper

        |__ __init__.py

        |__ etcd_rpc_call.py // etcd服务发现与负载均衡,以及rpc通用解析处理方法parse_rpc_resp

2.3,新增rpc服务

  1. 在rpc_client项目下,执行:git submodule add git@git.hundun.cn:golang-servers/xxx_i.git
  2. 增加xxx_rpc目录,对每一个子模块创建py文件,封装rpc调用方法如下
# 封装rpc调用,主要对req和resp进行处理from rpc_client.common.parse_resp import parse_rpc_respfrom rpc_client.user_i.user_py import user_pb2from rpc_client.user_rpc.client import get_user_clientclass Profile(object):    def __init__(self, pid=0, user_id=""):        self.client = get_user_client()        self.pid = pid        self.user_id = user_id    @parse_rpc_resp    def get_profile(self):        cli_req = user_pb2.GetUserProfileReq(            pid=self.pid,            user_id=self.user_id,        )        return self.client.GetUserProfile(cli_req)

3,旧服务调用rpc的使用方法

3.1,rpc调用⁣

  1. 在旧服务项目下增加submodule,执行:git submodule add git@git.hundun.cn:v2-online-server/rpc_client.git
  2. ⁣在需要调用rpc的地方,增加如下代码
from rpc_client.user_rpc.profile import Profileclass oneClass(object):    ...       def oneFunc(self, pid, *args, **kwargs):         profile = Profile(pid).get_profile()   # 这里是一次rpc调用

3.2,子模块更新

当rpc_client子模块需要更新时,在旧服务项目主目录下,执行:

git submodule foreach --recursive git submodule update

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/268469.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号