372-RpcChannel的调用过程

本文介绍了如何构建一个RPC调用方(caller)的流程,包括在caller中创建calluserservice.cc文件,定义自定义的MprpcChannel类继承自RpcChannel,并重写CallMethod方法,用于处理RPC方法调用的数据序列化和网络发送。此外,还展示了构建和编译项目的步骤。
摘要由CSDN通过智能技术生成

在这里插入图片描述

RPC调用方(caller)的调用(消费)过程

caller就是按照提供rpc服务的那一方(callee)提供的proto协议,发起调用
在这里插入图片描述

我们在caller下创建文件:calluserservice.cc
在这里插入图片描述
UserServiceRpc_Stub的构造函数必须要有RpcChannel参数
在这里插入图片描述
我们知道,里面那些方法最终都是调用channel的CallMethod方法。
在这里插入图片描述
这个RpcChannel是一个抽象类,new不了对象,我们得在框架上定义一个类从RpcChannel继承而来,然后重写CallMethod方法。
在这里插入图片描述

所以,我们现在要在框架上写代码,针对RPC的调用方。

我们在src的include下创建文件:mprpcchannel.h

在这里插入图片描述

#pragma once

#include <google/protobuf/service.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/message.h>

class MprpcChannel : public google::protobuf::RpcChannel
{
public:
    //所有通过stub代理对象调用的rpc方法,都走到这里了,统一做rpc方法调用的数据数据序列化和网络发送 
    void CallMethod(const google::protobuf::MethodDescriptor* method,
                          google::protobuf::RpcController* controller, 
                          const google::protobuf::Message* request,
                          google::protobuf::Message* response,
                          google::protobuf:: Closure* done);
};

在这里插入图片描述

我们更新一下example下的CmakeLists.txt

add_subdirectory(callee)
add_subdirectory(caller)

在这里插入图片描述
我们在example下的caller下创建CMakeLists.txt

set(SRC_LIST calluserservice.cc ../user.pb.cc)
add_executable(consumer ${SRC_LIST})
target_link_libraries(consumer mprpc protobuf)

在这里插入图片描述

我们在src下创建mprpcchannel.cc

#include "mprpcchannel.h"


//所有通过stub代理对象调用的rpc方法,都走到这里了,统一做rpc方法调用的数据数据序列化和网络发送 
void MprpcChannel::CallMethod(const google::protobuf::MethodDescriptor* method,
                                google::protobuf::RpcController* controller, 
                                const google::protobuf::Message* request,
                                google::protobuf::Message* response,
                                google::protobuf:: Closure* done)
{
}

我们再简单书写一下calluserservice.cc

在这里插入图片描述
更新一下src的CMakeLists.txt
在这里插入图片描述
编译成功
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林林林ZEYU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值