簡體   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