简体   繁体   English

从 RabbitMQ / MQTT 使用 Spring 云 Stream

[英]Consuming from RabbitMQ / MQTT with Spring Cloud Stream

How do you consume using Spring Cloud Stream (that is using AMQP) messages sent with MQTT to RabbitMQ?您如何使用 Spring Cloud Stream(即使用 AMQP)消息使用 MQTT 发送到 RabbitMQ? With MQTT on Rabbit, all messages land on an exchange called "amq.topic".使用 Rabbit 上的 MQTT,所有消息都放在一个名为“amq.topic”的交换器上。

On the consumer side, with Spring Cloud Stream, a queue is created for each destination and an exchange of type "topic" with the name of the destination;在消费者方面,使用 Spring Cloud Stream,为每个目的地创建一个队列,并使用目的地名称进行“主题”类型的交换; the created queue is bound to the exchange.创建的队列绑定到交换器。

Now,现在,

  • I cannot manually bind my queue to the "amq.topic", because it is exclusive:我无法手动将队列绑定到“amq.topic”,因为它是独占的:

    cannot obtain exclusive access to locked queue '...' in vhost '/'.无法获得对 vhost '/' 中锁定队列 '...' 的独占访问权限。 It could be originally declared on another connection or the exclusive property value does not match that of the original declaration.它可能最初是在另一个连接上声明的,或者独占属性值与原始声明的值不匹配。

  • I cannot listen directly from the exchange "amq.topic", because, well, you have to listen to a queue...我不能直接从交换“amq.topic”中收听,因为,好吧,你必须听一个队列......

  • I created a "tmp" queue bound to "amq.topic" where messages are delivered, but I cannot use it as destination, because RMQ will create a new queue called "tmp.SOME_CLIENT_ID", bound to an exchange called "tmp", that has nothing to do with my "tmp" queue.我创建了一个绑定到传递消息的“amq.topic”的“tmp”队列,但我不能将其用作目标,因为 RMQ 将创建一个名为“tmp.SOME_CLIENT_ID”的新队列,绑定到一个名为“tmp”的交换,这与我的“tmp”队列无关。

Any idea would be welcome!任何想法都会受到欢迎!

See Using Existing Queues/Exchanges .请参阅使用现有队列/交换

By default, the binder will automatically provision a topic exchange with the name being derived from the value of the destination binding property <prefix><destination> .默认情况下,绑定器将自动提供一个主题交换,其名称源自目标绑定属性<prefix><destination>的值。 The destination defaults to the binding name, if not provided.如果未提供,则目标默认为绑定名称。 When binding a consumer, a queue will automatically be provisioned with the name <prefix><destination>.<group> (if a group binding property is specified), or an anonymous, auto-delete queue when there is no group.绑定消费者时,将自动配置名称为<prefix><destination>.<group>的队列(如果指定了组绑定属性),或者在没有组时使用匿名自动删除队列。 The queue will be bound to the exchange with the "match-all" wildcard routing key (#) for a non-partitioned binding or <destination>-<instanceIndex> for a partitioned binding.对于非分区绑定,队列将使用“match-all”通配符路由键 (#) 绑定到交换器,对于分区绑定,则使用<destination>-<instanceIndex> The prefix is an empty String by default.默认情况下,前缀为空字符串。 If an output binding is specified with requiredGroups, a queue/binding will be provisioned for each group.如果使用 requiredGroups 指定了 output 绑定,则将为每个组提供队列/绑定。

There are a number of rabbit-specific binding properties that allow you to modify this default behavior.有许多特定于 rabbit 的绑定属性允许您修改此默认行为。

If you have an existing exchange/queue that you wish to use, you can completely disable automatic provisioning as follows, assuming the exchange is named myExchange and the queue is named myQueue:如果您希望使用现有的交换/队列,则可以完全禁用自动配置,假设交换名为 myExchange,队列名为 myQueue:

spring.cloud.stream.bindings.<binding name>.destination=myExhange

spring.cloud.stream.bindings.<binding name>.group=myQueue

spring.cloud.stream.rabbit.bindings.<binding name>.consumer.bindQueue=false

spring.cloud.stream.rabbit.bindings.<binding name>.consumer.declareExchange=false

spring.cloud.stream.rabbit.bindings.<binding name>.consumer.queueNameGroupOnly=true

If you want the binder to provision the queue/exchange, but you want to do it using something other than the defaults discussed here, use the following properties.如果您希望活页夹提供队列/交换,但您想使用此处讨论的默认值以外的其他内容来执行此操作,请使用以下属性。 Refer to the property documentation above for more information.有关更多信息,请参阅上面的属性文档。

spring.cloud.stream.rabbit.bindings.<binding name>.consumer.bindingRoutingKey=myRoutingKey

spring.cloud.stream.rabbit.bindings.<binding name>.consumer.exchangeType=<type>

spring.cloud.stream.rabbit.bindings.<binding name>.producer.routingKeyExpression='myRoutingKey'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM