根据 Google Protocol Buffers 文档中 "定义服务" 部分的说明,他们说:
我正试图创建一个使用 ZeroMQ 的 RPC 实现,因为我正在处理的项目已经使用 ZeroMQ 进行基本消息传递(这也是为什么我没有使用文档推荐的 gRPC 的原因)。
经过仔细阅读 Proto 文档后,我发现我需要为自己的实现实现抽象接口RpcChannel 和 RpcController。
下面是我目前在 RPC 实现方面所处位置的最小化示例。 .proto 文件: 出于简洁起见省略了 SearchRequest 和 SearchResponse 模式。
据我理解,Protocol Buffers 没有原生实现 RPC。相反,它们提供了一系列必须由用户(也就是我!)实现的抽象接口。因此,我想利用 ZeroMQ 实现这些抽象接口以进行网络通信。也可以使用您自己的 RPC 实现与协议缓冲区一起使用。
我正试图创建一个使用 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 - Based off of 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中,因为在我看到的示例中(请参见此处的第3个代码块),它们传递一个字符串,其中包含绑定端口的端口(例如:
MyRpcChannel channel("rpc:hostname:1234/myservice");
)。
- 看起来应该放到RPCChannel中,因为在我看到的示例中(请参见此处的第3个代码块),它们传递一个字符串,其中包含绑定端口的端口(例如:
- 我对我的RPCController实现感到担忧,它似乎过于简单了。这里应该再加一些东西吗?
- 如何实现RPCChannel,它与SearchServiceImpl非常相似。这些类中的1个虚函数具有非常相似的方法签名,只是它是通用的。
以下是我遇到的一些其他Stack Overflow问题,它们提供了一些有关此主题的有用信息:
- Protobuf-Net:实现服务器、rpc控制器和rpc通道 - 这是我找到RPCController实现示例的地方。
- 使用Protocol Buffers实现ZeroMQ RPC - 这个答案很有趣,因为在顶部答案中,它似乎建议不使用Protobuf中的RPC格式化.proto文件。
- 我是否应该使用现有的实现,例如RPCZ?
感谢您的帮助。我希望提供了足够的信息,并且对我所寻求的内容清晰明确。如果有什么不清楚或缺少信息,请告诉我。我很乐意编辑问题。