news 2026/9/12 17:40:52

MPRPC项目(第九天,新增服务以及controller实现)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
MPRPC项目(第九天,新增服务以及controller实现)

一、新增服务提供

两个都与用户登录没有什么区别

1、friend.proto

syntax = "proto3"; package fixbug; option cc_generic_services = true; message ResultCode{ int32 errcode = 1; bytes errmsg = 2; } message GetFriendListRequest{ uint32 userid = 1; } message GetFriendListResponse{ ResultCode result = 1; repeated bytes friends = 2; } service FriendServiceRpc{ rpc GetFriendList(GetFriendListRequest) returns(GetFriendListResponse); }

要新建一个proto文件。

2、服务端

#include<iostream> #include<string> #include"friend.pb.h" #include"mprpcapplication.h" #include"rpcprovider.h" #include<vector> class FriendService : public fixbug::FriendServiceRpc { public: std::vector<std::string> GetFriendList(uint32_t userid){ std::cout << "do GetFriendList service" << std::endl; std::vector<std::string> vec; vec.push_back("zhangsan"); vec.push_back("lisi"); vec.push_back("wangwu"); return vec; } void GetFriendList(::google::protobuf::RpcController* controller, const ::fixbug::GetFriendListRequest* request, ::fixbug::GetFriendListResponse* response, ::google::protobuf::Closure* done){ uint32_t userid = request->userid(); std::vector<std::string> friendlist = GetFriendList(userid); response->mutable_result()->set_errcode(0); response->mutable_result()->set_errmsg(""); //把好友列表序列化,写入到response的friends字段中,返回给客户端 //inline std::string* GetFriendListResponse::add_friends(); for(std::string &name : friendlist){ std::string *p = response->add_friends(); *p = name; } done->Run(); } }; int main(int argc, char **argv){ //调用框架的初始化操作 MprpcApplication::Init(argc,argv); //provider是一个rpc网络服务对象,把FriendService对象发布到rpc节点上 RpcProvider provider; provider.NotifyService(new FriendService()); //启动rpc服务发布节点 Run以后,进程进入阻塞状态,等待远程的rpc调用请求 provider.Run(); return 0; }

3、消费端

#include<iostream> #include "friend.pb.h" #include"mprpcapplication.h" int main(int argc,char **argv){ //整个程序启动后,想用mprpc框架调用rpc服务,一定要先调用框架的初始化函数(只初始化一次) MprpcApplication::Init(argc,argv); //演示远程调用发布的rpc方法 Login fixbug::FriendServiceRpc_Stub stub(new MprpcChannel()); fixbug::GetFriendListRequest request; request.set_userid(1); //RPC方法的响应 fixbug::GetFriendListResponse response; MprpcController controller; //发起rpc方法的调用 同步的rpc调用过程 MprpcChannel::callmethod stub.GetFriendList(&controller, &request, &response, nullptr); //一次rpc调用完整,读取调用结果 //0表示成功,非0表示错误 如404,not found if(controller.Failed()){ std::cout<<controller.ErrorText()<<std::endl; }else{ if(response.result().errcode() == 0){ std::cout<<"rpc GetFriendList response success! "<<std::endl; for(int i = 0; i < response.friends_size(); i++){ std::cout<<"name"<<i<<": "<<response.friends(i)<<std::endl; } }else{ std::cout<<"rpc GetFriendList response failed: "<<response.result().errcode()<<" msg: "<<response.result().errmsg()<<std::endl; } } return 0; }

这里新增了controller,让服务端能够获得错误信息,无论是出现在服务端还是消费端的。

二、controller实现

该对象是Protobuf RPC框架中用于错误报告和调用控制的核心组件,它使得上层应用能够获知RPC调用过程中发生的各种错误情况,是RPC调用结果反馈的重要机制。

类似于以下用法,在mprpcchannel.cc里修改错误输出,设置出错后,在消费端就能获得。

char errtxt[512] = {0}; sprintf(errtxt,"connect failed!errno : %d",errno); controller->SetFailed(errtxt);

1、mprpccontroller.h

#pragma once #include<google/protobuf/service.h> #include<string> class MprpcController:public google::protobuf::RpcController{ public: MprpcController(); void Reset(); bool Failed() const; std::string ErrorText() const; void SetFailed(const std::string& reason); //目前未实现具体的功能 void StartCancel(); bool IsCanceled() const; void NotifyOnCancel(google::protobuf::Closure* callback); private: bool m_failed;//RPC方法执行过程中的状态 std::string m_errText;//rpc方法执行过程中的错误信息 };

2、mprpccontroller.cc

#include "mprpccontroller.h" //初始化 MprpcController::MprpcController(){ m_failed = false; m_errText = ""; } //重置 void MprpcController::Reset(){ m_failed = false; m_errText = ""; } //判断是否成功 bool MprpcController::Failed() const{ return m_failed; } //返回错误信息 std::string MprpcController::ErrorText() const{ return m_errText; } //发生错误 void MprpcController::SetFailed(const std::string& reason){ m_failed = true; m_errText = reason; } //目前未实现具体的功能 void MprpcController::StartCancel(){} bool MprpcController::IsCanceled() const{return false;} void MprpcController::NotifyOnCancel(google::protobuf::Closure* callback){}

====

这里附上代码,这两次的都没添加

https://github.com/wky-2004/exp1/tree/master/MPRPC

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/12 3:52:03

工业以太网边缘设备中HAL_UART_RxCpltCallback集成指南

如何用HAL_UART_RxCpltCallback打造工业边缘设备的高效串口通信引擎&#xff1f;在工厂自动化现场&#xff0c;你是否遇到过这样的场景&#xff1a;PLC的数据还没收完&#xff0c;扫码枪又发来一串指令&#xff1b;Modbus报文刚解析一半&#xff0c;HMI界面却卡顿了&#xff1f…

作者头像 李华
网站建设 2026/9/12 12:15:29

CUDA安装后ldconfig未更新?手动添加库路径解决问题

CUDA安装后ldconfig未更新&#xff1f;手动添加库路径解决问题 在部署深度学习环境时&#xff0c;你是否遇到过这样的场景&#xff1a;明明已经安装了完整的CUDA Toolkit&#xff0c;NVIDIA驱动也正常工作&#xff0c;PyTorch或TensorFlow却始终无法启用GPU&#xff1f;运行 to…

作者头像 李华
网站建设 2026/9/12 15:36:20

Pyenv global设置默认Python版本影响Miniconda使用吗

Pyenv global设置默认Python版本影响Miniconda使用吗 在现代Python开发中&#xff0c;一个常见的困扰是&#xff1a;当我在系统中用 pyenv global 设定了默认的Python版本后&#xff0c;会不会“污染”或干扰我通过 Miniconda 创建的虚拟环境&#xff1f;特别是当我们使用像 Mi…

作者头像 李华
网站建设 2026/9/5 18:10:59

Linux crontab定时任务调用Miniconda环境执行PyTorch脚本

Linux crontab定时任务调用Miniconda环境执行PyTorch脚本 在AI工程实践中&#xff0c;一个常见的需求是让模型训练或推理脚本每天凌晨自动运行——比如推荐系统需要基于最新用户行为数据重新生成特征&#xff0c;或者监控系统要每小时对传感器数据做一次异常检测。理想情况下&a…

作者头像 李华
网站建设 2026/9/10 17:40:52

ST7735与MCU通过SPI连接的操作指南

从零点亮一块1.8寸TFT屏&#xff1a;ST7735 MCU的SPI实战全解析你有没有过这样的经历&#xff1f;手里的STM32或ESP32开发板一切正常&#xff0c;传感器数据也读得出来&#xff0c;可一到驱动那块小小的1.8英寸TFT屏时&#xff0c;屏幕却死活不亮——要么白屏、要么花屏、甚至…

作者头像 李华
网站建设 2026/9/11 13:56:06

circuit simulator核心要点:仿真精度与步长设置技巧

仿真精度的命门&#xff1a;如何拿捏电路仿真中的时间步长&#xff1f;你有没有遇到过这样的情况&#xff1f;辛辛苦苦搭好一个Buck电路&#xff0c;信心满满点下“运行”&#xff0c;结果波形看起来怪怪的——开关节点的振铃不见了&#xff0c;电感电流像是被“磨平”了&#…

作者头像 李华