RabbitMQ - 向队列中的特定消费者发送消息 [英] RabbitMQ - Send message to a particular consumer in a queue

查看:109
本文介绍了RabbitMQ - 向队列中的特定消费者发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是场景 - 有多个应用服务器.浏览器可以通过 websocket 连接到任何应用服务器.

This is the scenario - There are multiple app servers. Browser can connect via websocket to any app server.

应用服务器(消费者)都在监听一个特定的队列.一旦接收到 Web 套接字连接,特定的应用程序服务器就会将带有路由键 {userId} 的队列绑定到直接交换.

The app servers (consumers) are all listening on a particular queue. As soon as a web socket connection is received, the particular app server binds the queue with a routing key {userId} to a direct exchange.

我希望发送到带有路由密钥 {userId} 的直接交换的消息只能由发生绑定的特定应用服务器接收.

I want a message sent to the direct exchange with the routing key {userId} to be received by only the particular app server where the binding has occured.

在这种情况下,直接交换是正确的交换吗?还是应该使用其他类型的交换?

Is a direct exchange the right exchange to use in this case? Or should some other type of exchange be used?

当 websocket 进入时,我使用 spring-amqp 创建动态绑定

I'm using spring-amqp to create dynamic bindings when a websocket comes in

// create the RabbitMq queue and bind to it
String routingKey = MessageConstants.getRoutingKeyForUserRecommendationQueue(user);
Binding userRecommendationBinding = BindingBuilder.bind(userRecommendationsQueue).
    to(directExchange).with(routingKey);
amqpAdmin.declareBinding(userRecommendationBinding);

推荐答案

实际上你走对了路.

是的:使用适当绑定的直接交换应该可以拯救您.

And yes: Direct Exchange with an appropriate binding should save you.

在 RabbitMQ 教程中查看更多信息:http://www.rabbitmq.com/tutorials/tutorial-four-java.html

See more info in the RabbitMQ Tutorial: http://www.rabbitmq.com/tutorials/tutorial-four-java.html

还可以查看有关此问题的 Spring AMQP 示例:https://github.com/spring-projects/spring-amqp-samples/tree/master/rabbitmq-tutorials

Also take a look into Spring AMQP Samples on the matter: https://github.com/spring-projects/spring-amqp-samples/tree/master/rabbitmq-tutorials

更新

不幸的是,事实并非如此.消息似乎随机发送给任何消费者,而不仅仅是创建绑定的消费者.

Unfortunately that is not what is happening. The messages seem to go randomly to any consumer, and not just the consumer that created the binding.

米-米-米.这是可能的,因为我们只路由我的 key,但之后消息被放置到队列中,该队列可能在不同的机器上有多个消费者.

M-m-m. That's possible, because we route only my the key, but after that the message is placed to the queue, which may have several consumers on different machines.

在这种情况下是:动态绑定没有帮助.

In this case yes: the dynamic binding doesn't help.

您应该考虑创建一个独特的新队列(自动删除很好),并从中准确地绑定和侦听.SimpleMessageListenerContainer 支持在运行时使用 addQueues() 为新队列启动一个新的消费者.

You should consider to create an unique new queue (auto-deleted is fine) and bind and listen exactly from that. The SimpleMessageListenerContainer supports addQueues() at runtime to start a new consumer for a new queue.

我认为这对你有用.

您仍然不应该在生产者方面做任何事情:相同的 exhchangeroutingKey 逻辑.

You still shouldn't do anything on the producer side: the same exhchange and routingKey logic.

这篇关于RabbitMQ - 向队列中的特定消费者发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆