本地服务怎么发布成rpc服务

目录

1.引入

2.user.proto

3.userservice.cc


1.引入

example文件夹作为我们框架项目的使用实例,在example文件夹下创建callee和caller两个文件夹

callee是RPC服务的提供者。在callee创建一个文件:userservice.cc

我们有没有这样一个框架,把这个UserService这个类生成的Login方法直接变成一个RPC远程方法?让在其他进程,其他机器都可以调用这个方法?

在这里我们使用protobuf进行参数和调用方法标识的序列化和反序列化。

2.user.proto

我们在example下再建一个文件:user.proto

cpp 复制代码
syntax="proto3";

package fixbug;

option cc_generic_services=true;

message ResultCode
{
    int32 errcode=1;
    bytes errmsg=2;
}

//函数的参数类型
message LoginRequest
{
    bytes name=1;
    bytes pwd=2;
}

//函数的返回值类型
message LoginResponse
{
    ResultCode result=1;
    bool success=2;
}

//RPC函数的描述
service UserServiceRpc
{
    rpc Login(LoginRequest) returns(LoginResponse);
}

保存退出,打开终端,进入到example下,执行
protoc user.proto --cpp_out=./

我们在CMakeLists.txt配置好相应的路径。

我们在example中再创建一个CMakeLists.txt

CMakeLists.txt配置如下:

cpp 复制代码
add_subdirectory(callee)
add_subdirectory(caller)

我们在callee和caller下各自再创建一个CMakeLists.txt

在callee的CMakeLists.txt中配置如下:

cpp 复制代码
set(SRC_LIST userservice.cc ../user.pb.cc)
add_executable(provider ${SRC_LIST})#可执行文件

3.userservice.cc

caller发送一个rpc请求,被rpc框架接受,rpc框架根据发送过来的请求,请求哪些方法,有哪些参数,然后匹配到Login方法,将这个请求上报过来,从请求中取到数据做本地业务(request),然后执行响应的响应(response),然后再执行一个回调(done)相当于把这个执行完的rpc方法的返回值塞给框架,由框架再给我们执行数据的序列化和反序列化。再通过框架的网络将响应返回回去(发送给caller),体现在Login的四个参数。

Closure是抽象类,需要重写Run方法。

userservice.cc代码:

cpp 复制代码
#include <iostream>
#include <string>
#include "user.pb.h"

/*
UserService原来是一个本地服务,提供了两个进程内的本地方法,Login和GetFriendLists
*/
class UserService:public fixbug::UserServiceRpc//使用在rpc服务发布端(rpc服务提供者)
{
public:
    bool Login(std::string name,std::string pwd)
    {
        std::cout<<"doing local service:Login"<<std::endl;
        std::cout<<"name:"<<name<<" pwd:"<<pwd<<std::endl;
        return true;
    }

    /*
    重写基类UserServiceRpc的虚函数  下面这些方法都是框架直接调用的
    1.caller  ===>  Login(LoginRequest) =>muduo =>  callee
    2.callee  ===>  Login(LoginRequest) =>交到下面重写的这个Login方法上了
    */
    void Login(::google::protobuf::RpcController *controller,
                               const ::fixbug::LoginRequest * request,
                               ::fixbug::LoginResponse * response,
                               ::google::protobuf::Closure *done)
    {
        //框架给业务上报了请求参数LoginRequest,应用获取相应数据做本地业务
        std::string name = request->name();
        std::string pwd = request->pwd();

        //做本地业务
        bool login_result=Login(name,pwd);

        //把响应写入  包括错误码、错误消息、返回值
        fixbug::ResultCode* code=response->mutable_result();
        code->set_errcode(0);
        code->set_errmsg("");
        response->set_success(login_result);

        //执行回调操作   执行响应对象数据的序列化和网络发送(都是由框架来完成的)
        done->Run();
    }
};

int main()
{
    return 0;
}
相关推荐
Mu.3878 小时前
计算机网络模型
网络·网络协议·计算机网络·安全·http·https
xixixi7777710 小时前
解析一下传输安全——“它是什么”,更是关于“它为何存在”、“如何实现”以及“面临何种挑战与未来”
网络·安全·通信
jerryinwuhan11 小时前
socket由浅入深
网络
xu_yule13 小时前
网络和Linux网络-3(套接字编程)TCP网络通信代码
linux·网络·tcp/ip
喜欢吃豆14 小时前
使用 OpenAI Responses API 构建生产级应用的终极指南—— 状态、流式、异步与文件处理
网络·人工智能·自然语言处理·大模型
xixixi7777715 小时前
解析一下存储安全——“它是什么”,更是关于“它为何存在”、“如何实现”以及“面临何种挑战与未来”
网络·安全·通信
运维有小邓@15 小时前
实时日志关联分析工具:智能检测潜在安全威胁
运维·网络·安全
j***576815 小时前
电脑可以连接wifi,但是连接后仍然显示没有网络
网络·电脑·php
brave and determined16 小时前
接口通讯学习(day04):RS-232与RS-485:通信接口全解析
网络·uart·通讯·emc·rs232·rs485·嵌入式设计
檀越剑指大厂16 小时前
在家也能远程调代码?WSL+cpolar 的实用技巧分享
网络