在哪里设置交换和队列的绑定(生产者与消费者)?
Where to set up of the binding of exchange and queue (producer vs. consumer)?
所有 official RabbitMQ examples set up the queues and bindings in the consumer. The Publish/Subscribe tutorial 声明
The messages will be lost if no queue is bound to the exchange yet, but that's
okay for us; if no consumer is listening yet we can safely discard the message.
这对我来说绝对不行,因为我在 RabbitMQ 上实现了一个作业工作者队列,重要的是当消费者还没有 运行 时不要丢失任何消息。因此,我正在考虑在 producer 中建立交换 <-> 队列路由。这些示例是否有相反的原因?
顺便说一句,每次我连接到 RabbitMQ 服务器时都进行基本 exchange/queue/routing 设置还是仅一次(曾经)基本配置 RabbitMQ 实例被认为是最佳实践?我目前发布消息的方法目前看起来有点像这样:
const getChannel = () =>
ampq.connect() // The real implementation caches the connection
.then(conn => conn.createChannel())
.then(channel => channel.assertExchange(...)
.then(() => channel.assertQueue(...)) // Assert and bind for all queues
.then(() => channel.bindQueue(...)) // Assert and bind for all queues
);
const publish = (task, payload) =>
getChannel().then(channel =>
channel.publish(exchange, task, payload)
);
是的,您可以在发布者中声明队列和交换器。有很多 RabbitMQ 用例,用户将 RabbitMQ 用作工作队列。如果队列和交换器尚不存在,它们将被声明和创建。 (RabbitMQ 不允许您使用不同的参数重新定义现有队列,并且 return 任何试图这样做的程序都会出错。)
我建议您在运行时定义它们,在应用程序实例启动时(或需要 queue/exchange 时)进行基本的 exchange/queue/routing 设置。为确保 RabbitMQ 永远不会丢失您的队列,您需要将其声明为持久队列。请记住,需要两件事来确保消息不会丢失:我们需要将队列和消息都标记为持久的。 (https://www.cloudamqp.com/blog/2017-03-14-how-to-persist-messages-during-RabbitMQ-broker-restart.html)
所有 official RabbitMQ examples set up the queues and bindings in the consumer. The Publish/Subscribe tutorial 声明
The messages will be lost if no queue is bound to the exchange yet, but that's okay for us; if no consumer is listening yet we can safely discard the message.
这对我来说绝对不行,因为我在 RabbitMQ 上实现了一个作业工作者队列,重要的是当消费者还没有 运行 时不要丢失任何消息。因此,我正在考虑在 producer 中建立交换 <-> 队列路由。这些示例是否有相反的原因?
顺便说一句,每次我连接到 RabbitMQ 服务器时都进行基本 exchange/queue/routing 设置还是仅一次(曾经)基本配置 RabbitMQ 实例被认为是最佳实践?我目前发布消息的方法目前看起来有点像这样:
const getChannel = () =>
ampq.connect() // The real implementation caches the connection
.then(conn => conn.createChannel())
.then(channel => channel.assertExchange(...)
.then(() => channel.assertQueue(...)) // Assert and bind for all queues
.then(() => channel.bindQueue(...)) // Assert and bind for all queues
);
const publish = (task, payload) =>
getChannel().then(channel =>
channel.publish(exchange, task, payload)
);
是的,您可以在发布者中声明队列和交换器。有很多 RabbitMQ 用例,用户将 RabbitMQ 用作工作队列。如果队列和交换器尚不存在,它们将被声明和创建。 (RabbitMQ 不允许您使用不同的参数重新定义现有队列,并且 return 任何试图这样做的程序都会出错。)
我建议您在运行时定义它们,在应用程序实例启动时(或需要 queue/exchange 时)进行基本的 exchange/queue/routing 设置。为确保 RabbitMQ 永远不会丢失您的队列,您需要将其声明为持久队列。请记住,需要两件事来确保消息不会丢失:我们需要将队列和消息都标记为持久的。 (https://www.cloudamqp.com/blog/2017-03-14-how-to-persist-messages-during-RabbitMQ-broker-restart.html)