Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用amqplib向队列发送消息的奇怪低性能?_Node.js_Rabbitmq_Node Amqp - Fatal编程技术网

Node.js 使用amqplib向队列发送消息的奇怪低性能?

Node.js 使用amqplib向队列发送消息的奇怪低性能?,node.js,rabbitmq,node-amqp,Node.js,Rabbitmq,Node Amqp,我正在尝试使用amqp.node测试Rabbitmq延迟 这是消费者: import amqp from 'amqplib'; amqp.connect('amqp://localhost').then((connection) => { connection.createChannel().then((channel) => { const queue = 'test1'; channel.assertQueue(queue, { durable: false

我正在尝试使用amqp.node测试Rabbitmq延迟

这是消费者:

import amqp from 'amqplib';

amqp.connect('amqp://localhost').then((connection) => {
  connection.createChannel().then((channel) => {
    const queue = 'test1';
    channel.assertQueue(queue, { durable: false }).then(()=>{
      var consumedMessages = 0;
      channel.consume(queue, (msg) => {
        const timestamp = new Date().getTime();
        consumedMessages++;
        if (msg != null) console.log(`consumed ${consumedMessages}: ${timestamp - Number(msg.content.toString())}`);
      }, { noAck: true });
    });
  }).catch((error) => { throw error; });
}).catch((error) => { throw error; });
这是制作人:

import amqp from 'amqplib';

amqp.connect('amqp://localhost').then((connection) => {
  connection.createChannel().then((channel) => {
    const queue = 'test1';
    const totalMessages = 3;

    channel.assertQueue(queue, { durable: false }).then(()=>{    
      for(var n = 0; n < totalMessages; n++){
        channel.sendToQueue(queue, Buffer.from(new Date().getTime().toString()));
      }
      setTimeout(()=>{
        connection.close();
        process.exit(0);
      }, 1000);
    });
  }).catch((error) => { throw error; });
}).catch((error) => { throw error; });
每个生产者的第一条消息都有较低的延迟,但其他生产者没有


有什么可以改进的吗?

试试amqp.connect'amqp://localhost“,{noDelay:true},可能需要rabbitmq服务器配置。详细信息位于。

Tx@menya,如果我把它放在producer中,这个问题就解决了。
for ((i=1;i<=3;i+=1)); do node dist/consumer.js & done
for ((i=1;i<=3;i+=1)); do node dist/producer.js & done
consumed 1: 8
consumed 1: 6
consumed 1: 10
consumed 2: 42
consumed 2: 43
consumed 3: 42
consumed 2: 42
consumed 3: 42
consumed 3: 42