我在 RabbitMQ 服务器上运行 node.js amqp-node 客户端。我以两种不同的方式设置了一个简单的发布/消费测试——每种方式都会产生不同的延迟,我不知道为什么。延迟是从消息发送到传递的时间测量的。
第一种方法使用两个独立的进程 - 一个用于发布应用程序 - 一个用于消费应用程序。它产生大约 4 毫秒的延迟。第二种方法只使用一个应用程序来生产和消费。这会产生大约 40 毫秒的延迟。谁能向我解释这种行为,为什么?感谢您的时间。
方法 #1 生产者:
var amqp = require('amqp'),
connection = amqp.createConnection({host:'localhost'}),
testExchange = {};
connection.on('ready', function(){
testExchange = connection.exchange('testExchange', {type:'topic', autoDelete:true});
testMessage();
});
function testMessage(){
console.log('message sent');
testExchange.publish('test.message', { msg:'testMessage', time: new Date().getTime() });
setTimeout(testMessage, 500);
}
方法 #1 消费者
var amqp = require('amqp'),
connection = amqp.createConnection({host:'localhost'}),
connection.on('ready', function(){
var testExchange = connection.exchange('testExchange', {type:'topic', autoDelete:true});
var testQ = connection.queue('testQ', function(queue){
queue.bind('testExchange', 'test.#');
queue.subscribe( function(message){
console.log('message received');
var now = new Date().getTime();
console.log(now-message.time);
});
});
});
方法#2 生产者和消费者
var amqp = require('amqp'),
connection = amqp.createConnection({host:'localhost'}),
testExchange = {};
connection.on('ready', function(){
testExchange = connection.exchange('testExchange', {type:'topic', autoDelete:true});
var testQ = connection.queue('testQ', function(queue){
queue.bind('testExchange', 'test.#');
queue.subscribe( function(message){
console.log('message received');
var now = new Date().getTime();
console.log(now-message.time);
});
testMessage();
});
});
function testMessage(){
console.log('message sent');
testExchange.publish('test.message', { msg:'testMessage', time: new Date().getTime() });
setTimeout(testMessage, 500);
}