一个 Queue 能否将消息传递给多个 Consumer In fanout exchange?

Can one Queue Deliver messages to multiple Consumer In fanout exchange?

我想知道我们是否需要创建三个不同的队列来将消息传递给三个不同的消费者?像这里一样,我创建了 3 个队列并将其绑定到扇出交换器,然后我在发布者 class 中传递了来自 POSTMAN 的产品模型列表,并为此创建了 3 个消费者。

这里是配置Class

@Configuration
public class MessagingConfig {
    public static final String QUEUE1 = "queue_one";
    public static final String QUEUE2 = "queue_two";
    public static final String QUEUE3 = "queue_three";
    public static final String EXCHANGE = "fanout-exchange";

    @Bean
    public Queue queue1() {
        return new Queue(QUEUE1);
    }

    @Bean
    public Queue queue2() {
        return new Queue(QUEUE2);
    }

    @Bean
    public Queue queue3() {
        return new Queue(QUEUE3);
    }

    @Bean
    public FanoutExchange exchange() {
        return new FanoutExchange(EXCHANGE);
    }

    @Bean
    public org.springframework.amqp.core.Binding binding1(Queue queue1, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(queue1).to(fanoutExchange);
    }

    @Bean
    public org.springframework.amqp.core.Binding binding2(Queue queue2, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(queue2).to(fanoutExchange);
    }

    @Bean
    public org.springframework.amqp.core.Binding binding3(Queue queue3, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(queue3).to(fanoutExchange);
    }

    @Bean
    public MessageConverter converter() {
        return new Jackson2JsonMessageConverter();
    }

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

这里是发布者Class

@RestController
@RequestMapping("/check")
public class ProductPublisher {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostMapping("/inventory")
    public String checkInventory(@RequestBody List<Product> product) {
        for (Product product1 : product) {
            if (product1.getQuantity() < 10) {
                ProductInventory productInventory = new ProductInventory(product1, "Low Quantity",
                        "This Product is low on Quantity in Inventory");
                rabbitTemplate.convertAndSend(MessagingConfig.EXCHANGE, "", productInventory);
                break;
            }
        }
        return "Success !!";
    }
}

没有

在 amqp 中,如果您需要 相同的 消息被多个消费者接收,则需要多个 queues。

一个 queue 可以被许多消费者共享,但是每个消费者将以 round-robin 的方式获得其共享的消息(对分布式工作人员有用)。

https://www.rabbitmq.com/getstarted.html 上的教程应该阐明这些概念,尤其是标题为“一次向多个消费者发送消息”的 # 3。