【问题标题】:Multiple @RabbitListeners sending reply to same queue when using sendAndReceive() in producer在生产者中使用 sendAndReceive() 时,多个@RabbitListeners 向同一队列发送回复
【发布时间】:2020-01-29 00:42:39
【问题描述】:

我正在使用带有 Spring AMQP 的 SpringBoot,并且我想在生产者中使用同步 sendAndReceive 方法来使用 RPC 模式。我的配置假设 1 次交换具有 2 个不同的绑定(1 个用于同一资源上的每个操作)。我想用 2 个不同的 routingKeys 发送 2 条消息,并在不同的回复队列上接收响应

据我所知,问题是,sendAndReceive 将在名为“.replies”的队列上等待回复,因此两个回复都将发送到products.replies 队列(至少这是我的理解)。

我的发布者配置:

    @Bean
    public DirectExchange productsExchange() {
        return new DirectExchange("products");
    }

    @Bean
    public OrderService orderService() {
        return new MqOrderService();
    }

    @Bean
    public RabbitTemplate rabbitTemplate(final ConnectionFactory connectionFactory) {
        final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(producerJackson2MessageConverter());
        return rabbitTemplate;
    }

    @Bean
    public Jackson2JsonMessageConverter producerJackson2MessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

和 2 个发件人:

...
final Message response = template.sendAndReceive(productsExchange.getName(), "products.get", message);
...

final Message response = template.sendAndReceive(productsExchange.getName(), "products.stock.update", message);

...

消费者配置:


    @Bean
    public Queue getProductQueue() {
        return new Queue("getProductBySku");
    }

    @Bean
    public Queue updateStockQueue() {
        return new Queue("updateProductStock");
    }

    @Bean
    public DirectExchange exchange() {
        return new DirectExchange("products");
    }

    @Bean
    public Binding getProductBinding(DirectExchange exchange) {
        return BindingBuilder.bind(getProductQueue())
                .to(exchange)
                .with("products.get");
    }

    @Bean
    public Binding modifyStockBinding(DirectExchange exchange) {
        return BindingBuilder.bind(updateStockQueue())
                .to(exchange)
                .with("products.stock.update");
    }

和带有以下签名的@RabbitListeners:

 @RabbitListener(queues = "getProductBySku")
    public Message getProduct(GetProductResource getProductResource) {...}

 @RabbitListener(queues = "updateProductStock")
    public Message updateStock(UpdateStockResource updateStockResource) {...}

我注意到第二个发送者收到 2 个响应,其中一个是无效类型(来自第一个接收者)。有什么方法可以让这些联系变得不同吗?还是对每个操作使用单独的交换是唯一合理的解决方案?

【问题讨论】:

    标签: spring-boot rabbitmq rpc spring-amqp


    【解决方案1】:

    据我所知,sendAndReceive 将在名为“.replies”的队列上等待回复

    你是从哪里得到这个想法的?

    根据您使用的版本,将为每个请求创建一个临时回复队列,或者使用 RabbitMQ 的“直接回复”机制,这再次意味着每个请求都在一个名为 @987654321 的专用伪队列上回复@。

    我看不出有任何方法可以让一个制作人得到另一个制作人的回复;即使您使用显式回复容器(通常不再需要),模板也会将回复与请求相关联。

    尝试启用 DEBUG 日志记录以查看是否提供任何提示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 2013-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多