当前位置:网站首页>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
边栏推荐
- Leetcode 504. 七进制数
- Sqoop【付诸实践 02】Sqoop1最新版 全库导入 + 数据过滤 + 字段类型支持 说明及举例代码(query参数及字段类型强制转换)
- 服务发现原理分析与源码解读
- Use of tabbarcontroller
- Force deduction DFS
- Logical architecture of MySQL
- Spolicy request case
- (2) Hand eye calibration of face scanner and manipulator (eye out of hand: nine point calibration)
- Nodejs service background execution (forever)
- Why does new public chain Aptos meet market expectations?
猜你喜欢
在Blazor 中自定义权限验证
一种分布式深度学习编程新范式:Global Tensor
The diagram of user login verification process is well written!
【荧光字效果】
Wechat applet learning notes 1
Principle analysis and source code interpretation of service discovery
Unstoppable, pure domestic PCs have been in place, and the monopoly of the U.S. software and hardware system has been officially broken
Xiaobai makes a wave of deep copy and shallow copy
苹果独占鳌头,三星大举复兴,国产手机在高端市场颗粒无收
Leetcode 504. 七进制数
随机推荐
Solve proxyerror: CONDA cannot proceed due to an error in your proxy configuration
Development to testing: a six-year road to automation starting from 0
Gauss elimination
反射机制的原理是什么?
Sqoop【环境搭建 01】CentOS Linux release 7.5 安装配置 sqoop-1.4.7 解决警告并验证(附Sqoop1+Sqoop2最新版安装包+MySQL驱动包资源)
Rocky basic exercise -shell script 2
Fiddler download and installation
Study notes of the first week of sophomore year
网易云UI模仿--&gt;侧边栏
Due to fierce competition in the new market, China Mobile was forced to launch a restrictive ultra-low price 5g package
在同一conda环境下先装Pytroch后装TensorFlow
Interview shock 68: why does TCP need three handshakes?
Flutter event distribution
2021年山东省中职组“网络空间安全”B模块windows渗透(解析)
MFC handy notes
MQTT X CLI 正式发布:强大易用的 MQTT 5.0 命令行工具
MySQL function
Uni app learning summary
JS table auto cycle scrolling, mouse move in pause
Vs2019 configuring opencv