【发布时间】:2020-04-24 06:45:46
【问题描述】:
他们说,根据“Defining Services”下的 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);
}
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();
}
}
}
};
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_;
};
MyRPCController.cpp - 基于this
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(); }
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;
};
到目前为止,我对我的示例有疑问:
- 我应该把 ZeroMQ 放在哪里?
- 似乎它应该进入 RPCChannel,因为在我看到的示例中(参见第三个代码块 here),它们传递了一个字符串,该字符串具有要绑定到的端口(即
MyRpcChannel channel("rpc:hostname:1234/myservice");)
- 似乎它应该进入 RPCChannel,因为在我看到的示例中(参见第三个代码块 here),它们传递了一个字符串,该字符串具有要绑定到的端口(即
- 我关心我的 RPCController 实现,它似乎太简单了。应该更多的去这里吗?
- 如何实现 RPCChannel,看起来和 SearchServiceImpl 很像。这些类中的 1 个虚函数具有非常相似的方法签名,只是它是通用的。
以下是我遇到的其他一些 Stack Overflow 问题,其中包含有关该主题的一些有用信息:
- Protobuf-Net: implementing server, rpc controller and rpc channel - 这是我找到 RPCController 实现示例的地方。
- Using Protocol Buffers for implementing RPC in ZeroMQ - 这个答案很有趣,因为在最佳答案中,似乎他们建议不要对 .proto 文件使用内置 RPC 格式的 Protobufs。
- 我可以/我应该使用现有的实现吗,例如RPCZ?
感谢您的帮助。我希望我提供了足够的信息,并且清楚我在寻找什么。如果有什么不清楚或缺乏信息,请告诉我。我很乐意相应地编辑问题。
【问题讨论】:
-
同样的问题,你最后做了什么?
标签: c++ protocol-buffers zeromq rpc