多个队列的 RabbitMQ 配置

RabbitMQ configuration for multiple queues

显然,我在我的应用程序中配置了一个队列来接收用户的 post。下面是配置。

<!-- Creates a queue for consumers to retrieve messages -->
<rabbit:queue name="UserPostpublishQueue" durable="true">
</rabbit:queue>

<!-- queue for sending notifications to users -->
<rabbit:queue name="notificationQueue" durable="true"/>


<!-- Fanout exchange for a pubsub bound to UserPostpublishQueue -->
<fanout-exchange name="broadcastEvents" durable="true" xmlns="http://www.springframework.org/schema/rabbit">
    <bindings>
        <binding queue="UserPostpublishQueue"/>
    </bindings>
</fanout-exchange>

<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" retry-template="retryTemplate" 
exchange="broadcastEvents" channel-transacted="true"/>

在代码中,我刚刚自动装配了 AMQP 模板

@Autowired
    AmqpTemplate amqpTemplate;
    amqpTemplate.convertAndSend(post);

现在又要介绍一个通知队列了。我不知道该怎么做。我是否将队列与相同的扇出交换绑定,这样做时交换将用户的 post 推入通知队列。

或者我是否创建另一个扇出交换,然后将通知队列与该交换绑定,但是我如何使用 amqp 模板注册这个新的交换?

或者我是否为用户 post 和通知队列创建直接交换?

我不确定。任何帮助将不胜感激。

你应该从业务需求描述开始。

提及的所有绑定变体均有效。

  • 您真的可以向扇出交换器添加一个队列 - 并且相同的消息将被放置到所有队列。话题行为。

  • 您可以创建另一个交换并在那里绑定该队列。在这种情况下,直接交换就足够了。要将消息准确地发送到该队列(可能与 post 不同),您应该使用不同的 AmqpTemplate 方法:

    void convertAndSend(String exchange, String routingKey, Object message) throws AmqpException;
    

也许你最好去 RabbitMQ tutorials 研究所有可能的配置。

我们最近还在 Spring AMQP Samples 项目中为这些教程添加了 Spring AMQP 实现。