Vpa*_*ino 6 c++ rpc protocol-buffers zeromq
根据“定义服务”下的 Google Protocol Buffers 文档,他们说,
也可以在您自己的 RPC 实现中使用协议缓冲区。
据我了解,Protocol Buffers 本身并没有实现 RPC。相反,它们提供了一系列必须由用户实现的抽象接口(就是我!)。所以我想利用 ZeroMQ 实现这些抽象接口进行网络通信。
我正在尝试使用 ZeroMQ 创建一个 RPC 实现,因为我正在处理的项目已经实现了用于基本消息传递的 ZeroMQ(因此我不使用 gRPC,正如文档所建议的那样)。
在通读 proto 文档后,我发现我必须为自己的实现实现抽象接口RpcChannel和RpcController。
我已经构建了一个最小化的例子,说明我目前的 RPC 实现
.proto 文件:为简洁起见省略了 SearchRequest 和 SearchResponse 架构
service SearchService {
rpc Search (SearchRequest) returns (SearchResponse);
}
Run Code Online (Sandbox Code Playgroud)
SearchServiceImpl.h :
class SearchServiceImpl : public SearchService {
public:
void Search(google::protobuf::RpcController *controller,
const SearchRequest *request,
SearchResponse *response,
google::protobuf::Closure *done) override {
// Static function that processes the request and gets the result
SearchResponse res = GetSearchResult(request);
// Call the callback function
if (done != NULL) {
done->Run();
}
}
}
};
Run Code Online (Sandbox Code Playgroud)
MyRPCController.h :
class MyRPCController : public google::protobuf::RpcController {
public:
MyRPCController();
void Reset() override;
bool Failed() const override;
std::string ErrorText() const override;
void StartCancel() override;
void SetFailed(const std::string &reason) override;
bool IsCanceled() const override;
void NotifyOnCancel(google::protobuf::Closure *callback) override;
private:
bool failed_;
std::string message_;
};
Run Code Online (Sandbox Code Playgroud)
MyRPCController.cpp - 基于此
void MyRPCController::Reset() { failed_ = false; }
bool MyRPCController::Failed() const { return failed_; }
std::string MyRPCController::ErrorText() const { return message_; }
void MyRPCController::StartCancel() { }
void MyRPCController::SetFailed(const std::string &reason) {
failed_ = true;
message_ = reason;
}
bool MyRPCController::IsCanceled() const { return false; }
void MyRPCController::NotifyOnCancel(google::protobuf::Closure *callback) { }
MyRPCController::ChiRpcController() : RpcController() { Reset(); }
Run Code Online (Sandbox Code Playgroud)
MyRpcChannel.h :
class MyRPCChannel: public google::protobuf::RpcChannel {
public:
void CallMethod(const google::protobuf::MethodDescriptor *method, google::protobuf::RpcController *controller,
const google::protobuf::Message *request, google::protobuf::Message *response,
google::protobuf::Closure *done) override;
};
Run Code Online (Sandbox Code Playgroud)
到目前为止我对我的例子的问题:
MyRpcChannel channel("rpc:hostname:1234/myservice");)这是我遇到的其他一些 Stack Overflow 问题,其中包含有关该主题的一些有用信息:
感谢您的帮助。我希望我提供了足够的信息并且清楚我在寻找什么。如果有什么不清楚或缺乏信息,请告诉我。我很乐意相应地编辑问题。
ZeroMQ 和 gRPC 都提供对网络通信的支持,但方式不同。您必须选择 ZeroMQ 或 gRPC 进行网络通信。如果您选择 ZeroMQ,则可以使用交换二进制结构化数据的 ProtoBuffers 对消息进行编码。
要点是 ProtoBuffers 库允许对变体记录(类似于 C/C++ 联合)进行编码和解码,可以完全模拟具有交换 ProtoBuffers 消息功能的 RPC 服务提供的功能。
所以选项是:
Run Code Online (Sandbox Code Playgroud)union Request { byte msgType; MessageType1 msg1; MessageType2 msg2; MessageType3 msg3; } union Response { byte msgType; MessageType3 msg1; MessageType4 msg2; MessageType5 msg3; } send(Request request); receive(Response response);
Run Code Online (Sandbox Code Playgroud)service MyService { rpc function1(MessageType1) returns (Response); rpc function2(MessageType2) returns (Response); rpc function3(MessageType3) returns (Response); rpc functionN(MessageType3) returns (MessageType5); }
(这里可以使用很多组合)
Run Code Online (Sandbox Code Playgroud)service MyService { rpc function(Request) returns (Response); }
该选项可能取决于
对于第一个选项,与第二个选项相比,您必须做很多事情。您必须将发送的消息类型与预期接收的消息类型相匹配。
如果其他人将开发客户端,第二个选项将允许更容易/更快地理解所提供的服务的功能。
为了在 ZeroMQ 上开发 RPC 服务,我将定义这样的 .proto 文件,指定函数、参数(所有可能的输入和输出参数)和错误,如下所示:
enum Function
{
F1 = 0;
F2 = 1;
F3 = 2;
}
enum Error
{
E1 = 0;
E2 = 1;
E3 = 2;
}
message Request
{
required Function function = 1;
repeated Input data = 2;
}
message Response
{
required Function function = 1;
required Error error = 2;
repeated Output data = 3;
}
message Input
{
optional Input1 data1 = 1;
optional Input2 data2 = 2;
...
optional InputN dataN = n;
}
message Output
{
optional Output1 data1 = 1;
optional Output2 data2 = 2;
...
optional OutputN dataN = n;
}
message Message
{
repeated Request requests;
repeated Response responses;
}
Run Code Online (Sandbox Code Playgroud)
并且根据函数 ID,在运行时必须检查参数的数量和类型。
| 归档时间: |
|
| 查看次数: |
1001 次 |
| 最近记录: |