节点js工作线程和rabbitmq用户是一样的吗?

这是为了获得更多关于如何将rabbitmq排队和节点js主/工作线程相结合的知识。

Node.js主工作线程不同于rabbitmq排队,因为rabbitmq提供了将任务存储到队列中的function,以便在工作人员空闲时由工作进程使用它们。 将这两者结合起来将会有非常具体的用例,而且通常是不需要的。

这两者联合实现需要几件事情,主要包括node-amqp客户端和集群 。 群集是为主工作线程提供api的节点的默认function。 如果没有rabbitmq,你通常会使用一个主进程分配任务,即将任务发送给所有工作进程,工作线程监听接收任务。

现在既然你想使用rabbitmq,你必须首先subscribe一个交换来监听所有的任务,当你接收到这个任务的时候,把它传递给你的工作进程。 下面是提供解释的要点的小片段。

 connection.on('ready', function() { connection.exchange('exchange-name', function(exchange) { _exchange = exchange; connection.queue('queue-name', function(queue) { _queue = queue; // Bind to the exchange queue.bind('exchange-name', 'routing-key'); // Subscribe to the queue queue .subscribe(function(message) { // When receiving the message call the worker thread to complete the task console.log('Got message', message); queue.shift(false, false); }) .addCallback(function(res) { // Hold on to the consumer tag so we can unsubscribe later _consumerTag = res.consumerTag; }) ; }); }); }); 

主和工作者之间的消息交换:直接发送消息给主工作者需要将成功消息放入队列。 主人将听取该队列以接收确认和成功消息。