RabbitMQ——有选择地从队列中检索消息 [英] RabbitMQ-- selectively retrieving messages from a queue

查看:73
本文介绍了RabbitMQ——有选择地从队列中检索消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 RabbitMQ 的新手,想知道我正在考虑的这个问题的好方法.我想创建一个订阅队列并且只拉取满足特定条件的消息的服务;例如,如果邮件中包含特定主题标头.

I'm new to RabbitMQ and was wondering of a good approach to this problem I'm mulling over. I want to create a service that subscribes to a queue and only pulls messages that meet a specific criteria; for instance, if a specific subject header is in the message.

我仍在学习 RabbitMQ,并正在寻找有关如何解决此问题的提示.我的问题包括:消费者如何仅从队列中提取特定消息?生产者如何在消息中设置主题标头(如果这是正确的术语?)

I'm still learning about RabbitMQ, and was looking for tips on how to approach this. My questions include: how can the consumer pull only specific messages from the queue? How can the producer set a subject header in the message (if that's even the right term?)

推荐答案

RabbitMQ 非常适合这种情况.您有多种选择可以做您想做的事.我建议阅读文档以获得更好的理解.我建议您使用主题或直接交流.话题更灵活.事情是这样的.

RabbitMQ is perfect for this situation. You have a number of options to do what you want. I suggest reading the documentation to get a better understanding. I would suggest that you use a topic or direct exchange. Topic is more flexible. It goes like this.

生产者代码连接到 RabbitMQ Broker 并使用特定名称创建和交换.

Producer code connects to the RabbitMQ Broker and creates and Exchange with a specific name.

生产者发布到交换.发布的每条消息都将使用路由密钥发布.

Producer publishes to exchange. Each message published will be published with a routing key.

消费者连接到 RabbitMQ 代理.

Consumer connects to RabbitMQ broker.

消费者创建队列

Consumer 将Queue 绑定到exchange,与producer 中定义的exchange 相同.该绑定还包括此特定消费者所需的每条消息的路由键.

Consumer binds Queue to the exchange, the same exchange defined in the producer. The binding also includes the routing keys for each message require for this particular consumer.

假设您要发布日志消息.路由键可能类似于log.info"、log.warn"、log.error".生产者发布的每条消息都会附加相关的路由密钥.然后,您将有一个消费者发送所有错误消息并通过电子邮件发送,另一个消费者将所有错误消息写入文件.因此,电子邮件发送者将使用路由键log.error"定义从其队列到交换器的绑定.这样,虽然交换器接收到所有消息,但为电子邮件发送者定义的队列将只包含错误消息.文件记录器将定义一个新的单独队列绑定到同一个交换并设置不同的路由键.您可以为三个不同的路由键需要做三个单独的绑定,或者只使用通配符log.*"从以 log 开头的交换请求所有消息.

Lets say you were publishing log messages. The routing key might be something like "log.info", "log.warn", "log.error". Each message published by the producer will have the relevant routing key attached. You will then have a consumer that sends and email for all the error messages and another one that writes all the error messages to a file. So the emailer will define the binding from its queue to the exchange with the routing key "log.error". This way though the exchange receives all messages, the queue defined for the emailer will only contain the error messages. The filelogger will define a new separate queue bound to the same exchange and set up a different routing key. You could do three separate bindings for the three different routing keys require or just use the wildcard "log.*" to request all messages from the exchange starting with log.

这是一个简单的例子,展示了如何实现你想做的事情.

This is a simple example that shows how you can achieve what you want to do.

查看这里的代码示例,特别是第 5 号教程.

look here for code examples specifically number tutorial number 5.

这篇关于RabbitMQ——有选择地从队列中检索消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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