繁体   English   中英

使用Spring Cloud Stream将RabbitMQ消费者绑定到现有队列

[英]Bind RabbitMQ consumer using Spring Cloud Stream to an existing queue

我已经使用RabbitMQ的网络的用户界面的话题交换TX并绑定到交换两个队列TX.Q1TX.Q2,每个路由密钥RK1RK2相应绑定创建的,产生的交流几条消息。

现在我想使用Spring Cloud Stream创建一个消费者,它只接收来自Q1的消息。 我尝试使用配置:

spring.cloud.stream.bindings.input.destination=TX
spring.cloud.stream.bindings.input.group=Q1

和消息消息的方法的注释@StreamListner(Sink.INPUT)

结果我可以看到消费者创建了一个具有相同名称TX.Q1的队列(或绑定),但新队列/ bind的Routing-Key是#。
如何通过Spring Cloud Stream配置消费者,消费者将使用来自预定义队列的消息(仅限于使用rk1路由的消息 )。

所以现在,Garry Russell建议的解决方案已经为我解决了这个问题。

我用这种方式使用了@RabbitListener而不是@StreamListenet
@RabbitListener(bindings = @QueueBinding(value = @Queue(value = "TX.Q1", durable = "true"), exchange = @Exchange(value = "TX", type = "topic", durable = "true"), key = "rk1")

结果,预定义队列TX.Q1与绑定密钥绑定: rk1到交换机TX

等待Spring Cloud Steream问题的更新。

我想我使用@StreamListener找到了解决方案,而没有使用解决方法。 一切都在配置中进行,而不是在代码中进行。

我使用的配置如下(它在.yml中,但您可以在.properties中轻松翻译它):

spring:
  cloud:
    stream:
      bindings:
        input:
          binder: <binder_name>
          destination: TX
          group: Q1
      binders:
        <binder_name>:
          type: rabbit
          environment:
            spring:
              rabbitmq:
                host: <host>
                port: <port>
                virtual-host: <vhost>
                username: <username>
                password: <password>
      rabbit:
        bindings:
          input:
            consumer:
              binding-routing-key: rk1
              exchange-name: TX
              queue-name-group-only: true
              bind-queue: true
              exchange-durable: true
              exchange-type: topic

使用这种方法,您不必编写特定的代码来让RabbitMQ使用者连接到您的集群,这应该可以解决您的问题。

希望这可以帮助。

Spring Cloud Stream在内部为使用者端点设置路由器密钥,使其成为目标名称( exchange名称)本身,或者在静态分区的情况下基于partition标头的路由。

我认为这个 github问题可能与您的情况有关。

鼓励在使用者下使用此属性以使兔子能够从现有队列中使用。 请注意,队列名称将仅从组属性中选取,而不是从目标中选取。

queueNameGroupOnly:true

例:

cloud:
stream:
  # rabbit setting: https://github.com/spring-cloud/spring-cloud-stream-binder-rabbit
  rabbit:
    bindings:
      input:
        consumer:
          acknowledgeMode: AUTO
          bindingRoutingKey: DECISION_PERSISTENCE_KEY
          declareExchange: false
          bindQueue: false
          queueNameGroupOnly: true
          consumerTagPrefix: dpa-rabbit-consumer
  bindings:
    input:
      binder: rabbit
      group: DECISION_PERSISTENCE_QUEUE
      content-type: application/json

暂无
暂无

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

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