一个队列可以在Fanout Exchange中向多个消费者传递消息吗?
我想知道我们是否需要创建三个不同的队列来将消息传递给三个不同的消费者?像这里一样,我创建了 3 个队列并将其绑定到扇出交换,然后在发布者类中,我从 POSTMAN 传递产品模型列表,并为此创建 3 个消费者。
这里是配置类
@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;
}
}
这里是发布商类
@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 !!";
}
}
I want to know do we need to create three different queues to deliver message to three different consumer? Like here I have created 3 queues and bind it to the fanout exchange and then I in publisher class I am passing list of Product Model from POSTMAN and I am creating 3 consumer for that.
Here is the Config 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;
}
}
Here is the Publisher 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 !!";
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
否。
在AMQP中,如果您需要多个消费者接收的相同消息,则需要多个队列。
许多消费者可以共享一个队列,但是每个消费者都会以圆形旋转方式获得大量消息(对分布式工人有用)。
No.
In amqp, if you need the same message to be received by multiple consumers, you'd need need multiple queues.
A queue could be shared by many consumers but then each consumer would get its share of messages in a round-robin fashion (useful for distributed workers).
The tutorials at https://www.rabbitmq.com/getstarted.html should clarify these concepts, especially the # 3, titled "Sending messages to many consumers at once".