- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
根据“Defining Services”下的 Google Protocol Buffers 文档”他们说,
it's also possible to use protocol buffers with your own RPC implementation.
据我了解,Protocol Buffers 本身并不实现 RPC。相反,它们提供了一系列必须由用户(那就是我!)实现的抽象接口(interface)。所以我想利用 ZeroMQ 来实现这些抽象接口(interface)来进行网络通信。
我正在尝试使用 ZeroMQ 创建一个 RPC 实现,因为我正在开发的项目已经实现了 ZeroMQ 来进行基本消息传递(因此我不使用 gRPC,正如文档所建议的那样) 。
彻底阅读了proto文档后,我发现我必须实现抽象接口(interface)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;
};
到目前为止,我对示例的疑问:
MyRpcChannel Channel("rpc :主机名:1234/myservice");
)以下是我遇到的其他一些 Stack Overflow 问题,其中包含有关该主题的一些有用信息:
感谢您的帮助。我希望我提供了足够的信息并且清楚我在寻找什么。如果有不清楚或缺乏信息,请告诉我。我很乐意相应地编辑问题。
最佳答案
ZeroMQ 和 gRPC 都提供对网络通信的支持,但方式不同。您必须选择 ZeroMQ 或 gRPC 进行网络通信。如果您选择 ZeroMQ,则可以使用交换二进制结构化数据的 ProtoBuffers 对消息进行编码。
要点是 ProtoBuffers 库允许对变体记录(类似于 C/C++ union )进行编码和解码,可以完全模拟具有交换 ProtoBuffers 消息功能的 RPC 服务提供的功能。
所以选项是:
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);
service MyService
{
rpc function1(MessageType1) returns (Response);
rpc function2(MessageType2) returns (Response);
rpc function3(MessageType3) returns (Response);
rpc functionN(MessageType3) returns (MessageType5);
}
(这里可以使用很多组合)
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;
}
并且根据函数 ID,在运行时必须检查参数的数量和类型。
关于c++ - 我如何利用 ZeroMQ 编写自己的 Protocol Buffers RPC 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59616929/
你能举一些zeromq的例子吗? 最佳答案 假设您想要某种公告板。您希望通过订阅公告板来只允许某些人看到它。 这可以使用 ZeroMQ 的发布者/订阅者模型来完成。 现在,假设您需要发送一些异步消息。
因此,正如我在上一篇文章中所问的那样,我希望能够使用不同语言编写的程序或函数在它们之间进行通信。 我最近遇到了 zeromq,我试图弄清楚这是否可以帮助我,因为它提供了某种套接字。例如,zeromq
与通过 POLLIN 多路复用多个套接字有何不同? while True: socks = dict(poller.poll()) if socks.get(control_recei
我正在设计一个与 ZeroMQ 对话的服务器应用程序。无需深入细节,服务器将存储和服务(来自查询请求)(eventid, eventstring)元组。 我的问题涉及有线协议(protocol)的设计
我有一个服务器(在 Amazon 上运行)和一个连接到它的客户端。建立连接后,客户端和服务器专门相互通信并发送消息。 例如 1. Client -> Server 2. Client -> Serve
我正在开发一个新的客户端-服务器应用程序 (.Net),并且到目前为止一直在使用 WCF,它非常适合应用程序的请求-响应方法。然而,我被要求用基于套接字的解决方案替换它,部分是为了支持非 .Net 客
我正在尝试做一个发布/订阅架构,其中多个发布者和多个订阅者存在于同一总线上。根据我在互联网上阅读的内容,只有一个套接字应该调用 bind(),而所有其他套接字(无论是 pub 还是 sub)都应该调用
使用zeromq,发送者发送10条消息后,发送者崩溃。 场景1:接收方正在一条一条地处理消息,花费了一些明显的时间成本,在这种情况下它还会收到 10 条消息吗? 场景 2:另一种情况是,当接收器崩溃时
我有一个 ZeroMQ 套接字,它正在从不同机器上的多个进程接收数据。在不改变数据内容的情况下,有没有办法识别数据的来源呢?具体来说,我想要发送者的 IP 地址(如果它来自 TCP 连接)。 最佳答案
有人知道在哪里可以找到有关 ZeroMQ 延迟与 29 West LBM 等竞争对手的性能详细信息吗? 看起来便宜得多,但我找不到任何指标来决定哪个更合适。 最佳答案 ZeroMQ 和 29West
有没有办法在不使用转发器概念的情况下使用 zeroMQ 库进行消息广播? 最佳答案 是的,一个 PUB 套接字将广播到所有连接的 SUB 套接字。只有当您想要桥接不同的网络时才需要转发器(代理),例如
几天前我才开始使用zeromq。我的目标是设计一个具有多个代理(代理网络)的发布订阅系统。我已经阅读了 zeromq 指南的相关部分,并为简单的发布子系统编写了代码。如果有人可以帮助我解决以下问题:
我需要编写一个订单管理器,将客户(股票、外汇等)订单发送到适当的交易所。客户想要发送订单,但对 FIX 或其他专有协议(protocol)一无所知,只知道发送订单的内部(规范化)格式。我有应用程序(服
我正在尝试从示例 wuclient/wuserver 在 zeromq 上实现一个惰性订阅者。 客户端比服务器慢得多,因此它必须只获取服务器最后发送的消息。 到目前为止,我发现这样做的唯一方法是连接/
我是 ZeroMQ 的新手并试图找出设计问题。我的情况是我有一个或多个客户端向单个服务器发送请求。服务器将处理请求,做一些事情,并向客户端发送回复。有两个条件: 回复必须发送到发送请求的客户端。 如果
如 docs 中所述在 3.x 版本的 zeromq 中,PUB/SUB 场景中的消息正在被过滤 出版商侧(而不是在订阅者方面,这是微不足道的)。 对我来说,这听起来像是发布者必须持有所有连接的套接字
引自 ZeroMQ 指南 However, with a little extra work, this humble pattern becomes a good basis for real wo
假设我有一个带有 ZeroMQ 接口(interface)的节点(进程、线程等),比方说一个 REP 套接字。这意味着我有一个无限主循环,它在 zmq_recv 或 zmq_poll 函数中休眠。 现
我想以某种方式比较 grpc 与 Zeromq 及其模式的功能:并且我想创建一些比较(功能集) - 不知何故 - 0mq 是“更好”的套接字 - 但无论如何 - 如果我应用 0mq模式 - 我认为我得
我正在试验 ZeroMQ。我发现在 ZeroMQ 中非常有趣,connect 或 bind 先发生并不重要。我试着查看 ZeroMQ 的源代码,但它太大了,找不到任何东西。 代码如下。 # clien
我是一名优秀的程序员,十分优秀!