当前位置:网站首页>Distributed network communication framework: how to publish local services into RPC services
Distributed network communication framework: how to publish local services into RPC services
2022-07-26 09:58:00 【_ Soren】
List of articles
1. To write protobuf The configuration file determines the requirements
Want to publish local services as rpc service , The first point is to use protobuf, Generate corresponding Service Service .
Take login business as an example :
The bottom is rpc Service . That is, the contract type
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;
}
service UserServiceRpc
{
rpc Login(LoginRequest) returns(LoginResponse);
}
2. Inherit the corresponding class and rewrite the virtual method
Write the configuration file , Generate C++ The corresponding header file and source file , It will generate The classes specified in the above configuration file ( Here is the UserServiceRpc), There are corresponding methods .
class UserServiceRpc : public ::PROTOBUF_NAMESPACE_ID::Service {
protected:
// This class should be treated as an abstract interface.
inline UserServiceRpc() {
};
public:
virtual ~UserServiceRpc();
typedef UserServiceRpc_Stub Stub;
static const ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor* descriptor();
virtual void Login(::PROTOBUF_NAMESPACE_ID::RpcController* controller,
const ::fixbug::LoginRequest* request,
::fixbug::LoginResponse* response,
::google::protobuf::Closure* done);
// implements Service ----------------------------------------------
// Omit ...
When I write business locally , First, inherit this class , And you need to rewrite the Login Method
class UserService : public fixbug::UserServiceRpc // Use in rpc Service publisher
{
public:
bool Login(std::string name, std::string pwd)
{
std::cout << "doing local service: Login " << std::endl;
std::cout << "name: " << name << "pwd: " << name << std::endl;
return true;
}
// Override base class UserServiceRpc The virtual function , These methods are called directly by the framework
virtual void Login(::google::protobuf::RpcController* controller,
const ::fixbug::LoginRequest* request,
::fixbug::LoginResponse* response,
::google::protobuf::Closure* done)
{
}
};
3. Business implementation
The first thing to understand is the calling process :
As shown in the figure below ,caller The caller sends the request of this method to the network , from muduo Library to deal with , To callee Provider , After the provider is informed of this request , To find this method , This method is our business rewritten virtual method .

virtual void Login(::google::protobuf::RpcController* controller,
const ::fixbug::LoginRequest* request,
::fixbug::LoginResponse* response,
::google::protobuf::Closure* done);
So for such a Login() Method , We need to understand the help of these parameters :
First controller The pointer , Don't worry about his role here 
request: The framework reports the request parameters to the business LoginRequest, The application can obtain the corresponding data for local business

Then it is very convenient to request data :
std::string name = request->name();
std::string pwd = request->pwd();
Then you can do local business , Here we call the local Login() Method
// Do local business
bool login_result = Login(name, pwd);
Call this ::
Then write the response message to the parameter response in :
This LoginResponse And the above LoginRequest The request is also corresponding :

Why is it in mutable_result() Fill in , This is in the last article 【protobuf Use 】 There are mentioned .
// Write the response to ( Error code , Error message , Return value )
fixbug::ResultCode* code = response->mutable_result();
code->set_errcode(0);
code->set_errmsg("");
response->set_success(login_result);
Finally, execute the callback operation , Perform the serialization and network sending of response object data .
protobuf This parameter is provided for us done, The type is Closure

// Perform callback operations
done->Run();
Complete code :
#include <iostream>
#include <string>
#include "user.pb.h"
/* UserService It was originally a local service , Provide local methods within two processes ,Login and GetFriendLists */
class UserService : public fixbug::UserServiceRpc // Use in rpc Service publisher
{
public:
bool Login(std::string name, std::string pwd)
{
std::cout << "doing local service: Login " << std::endl;
std::cout << "name: " << name << "pwd: " << name << std::endl;
return true;
}
// Override base class UserServiceRpc The virtual function , These methods are called directly by the framework
// 1. caller ==> Login(LoginRequest) ==> muduo ==> callee
// 2. callee ==> Login(LoginRequest) ==> To rewrite this method
virtual void Login(::google::protobuf::RpcController* controller,
const ::fixbug::LoginRequest* request,
::fixbug::LoginResponse* response,
::google::protobuf::Closure* done)
{
// The framework reports the request parameters to the business LoginRequest, The application obtains the corresponding data to do local business
std::string name = request->name();
std::string pwd = request->pwd();
// Do local business
bool login_result = Login(name, pwd);
// Write the response to ( Error code , Error message , Return value )
fixbug::ResultCode* code = response->mutable_result();
code->set_errcode(0);
code->set_errmsg("");
response->set_success(login_result);
// Perform callback operations
done->Run();
}
};
end
边栏推荐
- QT handy notes (III) use qtcharts to draw a line chart in VS
- M-ary number STR to n-ary number
- JS one line code to obtain the maximum and minimum values of the array
- 万字详解“用知识图谱驱动企业业绩增长”
- Study notes of the fifth week of sophomore year
- Search module use case writing
- 2022 zhongkepan cloud - server internal information acquisition and analysis flag
- Wechat H5 payment on WAP, for non wechat browsers
- [MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)
- Matlab Simulink realizes fuzzy PID control of time-delay temperature control system of central air conditioning
猜你喜欢

Apple dominates, Samsung revives, and domestic mobile phones fail in the high-end market

服务发现原理分析与源码解读

Wechat applet learning notes 1

The diagram of user login verification process is well written!

分布式网络通信框架:本地服务怎么发布成RPC服务

Keeping alive to realize MySQL automatic failover

SSG framework Gatsby accesses the database and displays it on the page

Installation and use of cocoapods

Production of a-modal drag function in antui

图解用户登录验证流程,写得太好了!
随机推荐
Nodejs service background execution (forever)
QT handy notes (VI) -- update interface, screenshot, file dialog box
Explain automatic packing and unpacking?
JS 一行代码 获取数组最大值与最小值
Docker configuring MySQL Cluster
Getting started with SQL - combined tables
在Blazor 中自定义权限验证
Use of tabbarcontroller
编写一个在bash / shell 和 PowerShell中均可运行的脚本
Study notes of the second week of sophomore year
Phpexcel export Emoji symbol error
Opencv image processing
Sqoop【环境搭建 01】CentOS Linux release 7.5 安装配置 sqoop-1.4.7 解决警告并验证(附Sqoop1+Sqoop2最新版安装包+MySQL驱动包资源)
挡不住了,纯国产PC已就位,美国的软硬件体系垄断正式被破
JS one line code to obtain the maximum and minimum values of the array
I finished watching this video on my knees at station B
Mqtt x cli officially released: powerful and easy-to-use mqtt 5.0 command line tool
The combination of officially issued SSL certificate and self signed certificate realizes website two-way authentication
Solve the problem of storing cookies in IE7 & IE8
WARNING: [pool www] server reached pm. max_ children setting (5), consider raising it