当前位置: 首页 > news >正文

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

http://www.rkmt.cn/news/184385.html

相关文章:

  • PCB过孔与电流对照一览表快速理解手册
  • Android16 默认关闭touch声音
  • 基于STM32的LED阵列扫描控制实战案例
  • C# 高效编程:Any () 与 Count () 正确选择
  • GitHub Projects项目管理:跟踪Miniconda-Python3.11开发进度
  • PyTorch Lightning集成:在Miniconda-Python3.11中简化训练代码
  • 将PyTorch训练脚本打包进Miniconda-Python3.11镜像发布到GitHub
  • SSH multiplexing复用连接:加快Miniconda-Python3.11频繁登录场景
  • Markdown写技术博客推荐:记录Miniconda配置PyTorch全过程
  • 校园健康驿站管理系统信息管理系统源码-SpringBoot后端+Vue前端+MySQL【可直接运行】
  • 范式跃迁:2025,一位技术人在大模型浪潮中的破局与深耕
  • 前后端分离校园竞赛管理系统系统|SpringBoot+Vue+MyBatis+MySQL完整源码+部署教程
  • 大厂数据结构面试题合集
  • Markdown mermaid流程图:在Miniconda-Python3.11中绘制AI架构
  • 第十四章 群体遗传与进化
  • CANoe环境下UDS诊断会话控制:完整示例
  • 一张图讲清楚国自然逻辑结构
  • Conda install常见错误:解决Miniconda-Python3.11中的Solving Environment问题
  • Pyenv与Miniconda对比:哪个更适合管理Python3.11用于大模型训练
  • 第十二章 遗传与发育
  • Windows平台Keil5汉化包兼容性深度剖析
  • CSDN首页发布文章【分布鲁棒】数据驱动的多离散场景电热综合能源系统分布鲁棒优化算法研究(Matlab代码实现)46 / 1002020 年 9 月 22 号中国公布了碳中和目标,可见的
  • Keil5烧录STM32时的复位电路设计操作指南
  • 【USTC-Shaohua Kevin Zhou组-arXiv25】U-Bench:通过100种变体基准测试全面理解U-Net
  • 清华源HTTPS证书过期?临时禁用SSL验证以更新Miniconda-Python3.11
  • Jupyter Lab安装教程:比Notebook更强大的Miniconda-Python3.11 IDE
  • CSP-J 2025
  • 大模型领域负载均衡技术
  • codefoeces EDU186 D[组合数学] E[贪心]
  • CF GYM106049 G [构造][数论]