【发布时间】:2022-08-17 08:22:39
【问题描述】:
我有一个简单的NodeJs 应用程序,它应该连接到 RabbitMq。
编码:
const amqp = require(\'amqplib/callback_api\');
const amqpUri = \"amqp://user:password@localhost:5672\"
if (amqpUri == null)
throw Error(\'Missing AMQP_URI environment variable.\');
amqp.connect(amqpUri, function(error0, connection) {
if (error0)
throw error0;
connection.createChannel(function(error1, channel) {
if (error1) {
throw error1;
}
const exchangeName = \'product.event\';
const queueName1 = \'create\';
const routingKey1 = \'create\';
const queueName2 = \'delete\';
const routingKey2 = \'delete\';
channel.assertExchange(exchangeName, \'topic\', {
durable: false,
});
// create
channel.assertQueue(queueName1, {
durable: false,
});
channel.bindQueue(queueName1, exchangeName, routingKey1);
channel.consume(queueName1, (msg) => consumeCreated(channel, msg));
// delete
channel.assertQueue(queueName2, {
durable: false,
});
channel.bindQueue(queueName2, exchangeName, routingKey2);
channel.consume(queueName2, (msg) => consumeDeleted(channel, msg));
});
});
然后使用以下命令运行 RabbitMq 图像:
docker run -d --hostname my-rabbit --name some-rabbit -p 5672:15672 -e RABBITMQ_DEFAULT_USER=user -e RABBITMQ_DEFAULT_PASS=password rabbitmq:3-management
- 我可以访问 rabbitmq 并使用凭据进行连接用户/密码在
http://localhost:5672。出于某种原因,我有错误:
/home/hamuto/CLO902-Group35/indexer/app.js:12 throw error0; ^ Error: Socket closed abruptly during opening handshake at Socket.endWhileOpening (/home/hamuto/CLO902-Group35/indexer/node_modules/amqplib/lib/connection.js:260:17) at Socket.emit (events.js:326:22) at endReadableNT (_stream_readable.js:1241:12) at processTicksAndRejections (internal/process/task_queues.js:84:21)