With RabbitMq, subscriptions to multiple queues are achieved over a single connection via Channels. This allows a single socket to be used to process multiple subscriptions. What is the analog strategy for minimizing socket connections for multiple message types in Zero MQ?
问问题
1399 次
1 回答
3
我想说 PUB/SUB 消息信封是 ZeroMQ 中解决此问题的首选方法(请参阅Pub-Sub Message Envelopes)。
在发送消息之前,发布者应该在信封中发送其主题:
s_sendmore (publisher, "A");
s_send (publisher, <Serialized MessageA>);
.....
s_sendmore (publisher, "B");
s_send (publisher, <Serialized MessageB>);
Suscriber 有 3 个选项来处理多个主题:
从信封中读取消息类型,根据消息类型使用反序列化器(图中的客户端 1)。
char *topic = s_recv (subscriber); char *body = s_recv (subscriber); //If topic == "A", the body contains serialized MessageA, otherwise MessageB
使用 仅订阅一个主题
zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE, ...);
。在这种情况下ZeroMQ会进行PUB端的消息过滤(从3.x开始),客户端可以确定只会接收MessageA类型的消息(图中客户端2)char *topic = s_recv (subscriber); char *body = s_recv (subscriber); //Ignore the topic, the body contains serialized MessageA
订阅所有消息并在客户端进程中创建一个代理,该代理在
INPROC
套接字上发布消息。单个订阅者应该使用zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE, ...);
订阅单一类型的消息。(见客户 3)。void *frontend = zsocket_new (context, ZMQ_SUB); zsocket_connect (frontend, "tcp://<....>"); void *backend = zsocket_new (context, ZMQ_PUB); zsocket_bind (backend, "inproc://all_messages"); // Start the proxy zmq_proxy (frontend, backend, NULL); ... //On another thread void *sub1 = zsocket_new (context, ZMQ_SUB); zsocket_connect (sub1, "inproc://all_messages"); zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE, "A", 1); ... void *sub2 = zsocket_new (context, ZMQ_SUB); zsocket_connect (sub2, "inproc://all_messages"); zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE, "B", 1);
您还可以找到用于各种技巧的有用 XPUB/XSUB 套接字。参见例如 最后值缓存
于 2013-07-05T11:59:55.657 回答