Spring boot 在producer中使用sendAndReceive()时,多个@RabbitListener将应答发送到同一队列

Spring boot 在producer中使用sendAndReceive()时,多个@RabbitListener将应答发送到同一队列,spring-boot,rabbitmq,rpc,spring-amqp,Spring Boot,Rabbitmq,Rpc,Spring Amqp,我将SpringBoot与SpringAMQP一起使用,我想在producer中使用使用同步发送和接收方法的RPC模式。我的配置假定1个exchange具有2个不同的绑定(1个用于同一资源上的每个操作)。我想用2个不同的路由键发送2条消息,并在不同的回复队列上接收响应 问题是,据我所知,sendAndReceive将在一个名为“.replies”的队列中等待答复,所以这两个答复都将发送到产品.replies队列(至少据我所知) 我的发布者配置: @Bean public Dire

我将SpringBoot与SpringAMQP一起使用,我想在producer中使用使用同步发送和接收方法的RPC模式。我的配置假定1个exchange具有2个不同的绑定(1个用于同一资源上的每个操作)。我想用2个不同的路由键发送2条消息,并在不同的回复队列上接收响应

问题是,据我所知,sendAndReceive将在一个名为“.replies”的队列中等待答复,所以这两个答复都将发送到
产品.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();
    }

    @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");
    }
以及两个发送者:

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

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

...
消费者配置:

    @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();
    }

    @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) {...}
我注意到第二个发送方接收到两个响应,其中一个是无效类型(来自第一个接收方)。有没有什么方法可以让这些联系变得清晰?还是对每个操作使用单独的交换是唯一合理的解决方案

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

你从哪里得到这个主意的

根据您使用的版本,将为每个请求创建一个临时回复队列,或者使用RabbitMQ的“直接回复”机制,这同样意味着在名为
amq.RabbitMQ.reply to
的专用伪队列上回复每个请求

我不认为一个制作人有任何办法得到另一个制作人的答复;即使使用显式回复容器(通常不再需要),模板也会将回复与请求关联起来

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

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

你从哪里得到这个主意的

根据您使用的版本,将为每个请求创建一个临时回复队列,或者使用RabbitMQ的“直接回复”机制,这同样意味着在名为
amq.RabbitMQ.reply to
的专用伪队列上回复每个请求

我不认为一个制作人有任何办法得到另一个制作人的答复;即使使用显式回复容器(通常不再需要),模板也会将回复与请求关联起来

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