![](/img/trans.png)
[英]How to send messages to one of the multiple topics based on condition in Spring Cloud Stream Kafka application
[英]Spring cloud stream and consume multiple kafka topics
在一些@StreamListener
使用多個主題的嘗試中,我遇到了spring-boot-stream的問題。
根據spring-cloud-stream docs:Accordidng to docs:
目的地
綁定中間件上的通道的目標目標(例如,RabbitMQ交換或Kafka主題)。 如果通道綁定為使用者,則可以綁定到多個目標,並且可以將目標名稱指定為逗號分隔的String值 。 如果未設置,則使用通道名稱。 無法覆蓋此>屬性的默認值。
但是,在我使用下一個配置后:
spring:
cloud:
stream:
bindings:
testchannel:
group: test
destination: platform.metrics, platform.sleuth
現在我有下一個錯誤:
Caused by: java.lang.IllegalArgumentException: Topic name can only have ASCII alphanumerics, '.', '_' and '-'
at org.springframework.cloud.stream.binder.kafka.utils.KafkaTopicUtils.validateTopicName(KafkaTopicUtils.java:39) ~[spring-cloud-stream-binder-kafka-core-1.2.1.RELEASE.jar:1.2.1.RELEASE]
at org.springframework.cloud.stream.binder.kafka.provisioning.KafkaTopicProvisioner.provisionProducerDestination(KafkaTopicProvisioner.java:107) ~[spring-cloud-stream-binder-kafka-core-1.2.1.RELEASE.jar:1.2.1.RELEASE]
at org.springframework.cloud.stream.binder.kafka.provisioning.KafkaTopicProvisioner.provisionProducerDestination(KafkaTopicProvisioner.java:60) ~[spring-cloud-stream-binder-kafka-core-1.2.1.RELEASE.jar:1.2.1.RELEASE]
at org.springframework.cloud.stream.binder.AbstractMessageChannelBinder.doBindProducer(AbstractMessageChannelBinder.java:110) ~[spring-cloud-stream-1.2.2.RELEASE.jar:1.2.2.RELEASE]
... 20 common frames omitted
如何將多個主題綁定到一個@StreamListner
或從主題列表生成動態streamListeners?
您只需要替換逗號和下一個目標值之間的空格,它將如下所示:
spring:
cloud:
stream:
bindings:
testchannel:
group: test
destination: platform.metrics,platform.sleuth
在這里,我寫了更多關於如何向動態目的地發送消息以及如何從動態目的地接收消息的細節。
https://stackoverflow.com/a/56446574/4587961
正如Varun Miglani所說,主題用逗號分隔,沒有空格。
spring:
cloud:
stream:
default:
consumer:
concurrency: 2
partitioned: true
bindings:
# inputs
input:
group: application_name_group
destination: topic-1,topic-2
content-type: application/json;charset=UTF-8
要動態發送消息,請使用BinderAwareChannelResolver和dynamicDestinations屬性。
spring:
cloud:
stream:
dynamicDestinations: output.topic.1,output.topic2,output.topic.3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.