简体   繁体   English

如何在Node.js中使ZeroMQ发布者冗余?

[英]How to make a ZeroMQ publisher redundant in Node.js?

Is there a common pattern that is used to make the publisher in the 0mq pub/sub redundant in node? 是否存在用于使节点0mq pub / sub中的发布者冗余的通用模式? The motivation is to be able to run multiple processes with publishers that could fail / be restarted periodically. 这样做的动机是能够与可能会失败/定期重新启动的发布者一起运行多个流程。

My initial thought is to create aa forwarder in the master and connect to it from the worker publishers: 我最初的想法是在母版中创建一个转发器,并从工作发布者处连接到它:

var cluster = require('cluster')
  , zmq = require('zmq')
  , endpointIn = 'ipc:///tmp/cluster_pub_sub'
  , endpointOut = 'tcp://127.0.0.1:7777';

if (cluster.isMaster) {
  for (var i = 0; i < 2; i++) cluster.fork();
  startPubSubForwarder();
} else {
  startPublisher();
}

function startPublisher() {
  var socket = zmq.socket('pub');
  socket.connect(endpointIn);
  setInterval(function () {
    socket.send('pid=' + process.pid);
  }, 1000);
}

function startPubSubForwarder() {
  var sIn = zmq.socket('sub')
    , sOut = zmq.socket('pub');

  // incoming
  sIn.subscribe('');
  sIn.bind(endpointIn, function (err) {
    if (err) throw err;
  });
  sIn.on('message', function (data) {
    sOut.send(data);
  });

  // outgoing
  sOut.bind(endpointOut, function (err) {
    if (err) throw err;
  });
}

Are there other / better ways of doing this? 还有其他/更好的方法吗?

If your concern is message durability, the I would think you'd be less concerned about having multiple publishers and more concerned about making sure that messages aren't lost when your publisher dies. 如果您关心的是消息的持久性,那么我想您会更不用担心拥有多个发布者,而更关注确保发布者去世时不会丢失消息。 You can simply restart the publisher immediately, and pick up where it left off. 您可以立即立即重新启动发布者,然后从停下来的地方领取。 You also need to know which messages have been successfully sent. 您还需要知道哪些消息已成功发送。

What this requires is 1) persistent storage and 2) and a means of acknowledging to the publisher that the message was received (and possibly that processing completed) on the receiver's end. 这需要1)持久存储和2)以及向发布者确认消息已在接收者端接收(并且可能已完成处理)的方法。 This setup should solve your reliability wants. 此设置应解决您的可靠性要求。

If you also want to accomplish high scale, then you need to augment the architecture a bit. 如果您还想实现高规模,则需要对体系结构进行一些扩充。 It's more straightforward for send/receive scenarios where the sender and receiver are 1:1, and a little more complex when you need to do a 1:N round-robin/load distribution scenario and that is probably what you need for scale. 对于发送方和接收方为1:1的发送/接收方案,它更简单,而当您需要进行1:N循环/负载分配方案时,情况可能会稍微复杂一些,这可能是扩展规模所需要的。

My input on accomplishing the scale-out scenario is to have a the following setup: 我对完成横向扩展方案的投入是要进行以下设置:

sender_process--(1:1)-->distributor--(1:N)-->receiver_process(es) sender_process-(1:1)->分发服务器-(1:N)-> receiver_process(es)

where the distributor acknowledges receipt of the message from sender and then fans out to the receiver processes. 分发者在其中确认收到了发件人的消息,然后将其扇形展开到接收者的进程。

You'll likely want to accomplish this by putting a queue in front of each of these processes. 您可能希望通过在每个流程的前面放置一个队列来实现此目的。 So, you don't send to the process. 因此,您不会发送到该过程。 You send to the queue that the process reads from. 您发送到该进程读取的队列。 Sender puts stuff on the distributor queue. 发件人将物料放入分发服务器队列。 The distributor puts stuff on the receiver's queues. 分发者将内容放入接收者的队列中。 At each point, each process attempts to process. 在每个点上,每个过程都会尝试进行处理。 If it fails past a number of max retries, it goes on an error queue. 如果经过多次最大重试后失败,它将进入错误队列。

We use rabbitmq / amqp to do all this. 我们使用rabbitmq / amqp来完成所有这些工作。 I've started open sourcing the bus we use to do the 1:1 and 1:N sending here: https://github.com/mateodelnorte/servicebus . 我已经开始开源总线,用于执行1:1和1:N的总线在这里发送: https : //github.com/mateodelnorte/servicebus I'll be adding a readme and more tests over the next few days. 在接下来的几天中,我将添加自述文件和更多测试。

From your example code, I think the XPUB/XSUB 0MQ pattern is your best fit. 从您的示例代码中,我认为XPUB / XSUB 0MQ模式是最合适的。 It is a more efficient way to achieve the same "startPubSubForwarder()" block, plus your subscriber side gets the benefit of being able to subscribe to certain patterns directly on the publishers backend side. 这是实现相同的“ startPubSubForwarder()”块的一种更有效的方法,此外,您的订阅方也可以直接在发布方的后端上订阅某些模式,从而获得了好处。 Here I left a link with an example of publishers/xpub-xsub (in a proxy fashion)/subscriptors: https://github.com/krakatoa/node_zmq_workshop/tree/master/03.3_news_proxy . 在这里,我留下了一个链接,其中包含发布者/ xpub-xsub(以代理方式)/下标者的示例: https : //github.com/krakatoa/node_zmq_workshop/tree/master/03.3_news_proxy It is NodeJS code (and it is mine, don't mind to ask details!). 它是NodeJS代码(这是我的,不要介意询问细节!)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM