Kafka 消费者监听两个事件/主题

问题描述 投票:0回答:2

有什么办法可以消费两个主题并等待这两个事件继续消费?

node.js apache-kafka kafka-consumer-api
2个回答
0
投票

有什么办法可以同时使用两个主题吗?

您可以在Kafka消费者中消费多个主题。如果您使用

KafkaJS
,那么这里是 示例,下面是一个片段

// Subscribe can be called several times
await consumer.connect()

await consumer.subscribe({ topic: 'topic-B' })
await consumer.subscribe({ topic: 'topic-C' })

// Alternatively, you can subscribe to multiple topics at once using a RegExp:
await consumer.connect()

await consumer.subscribe({ topic: /topic-(eu|us)-.*/i })

请注意,消费者不会匹配订阅后创建的主题


0
投票

我就是这样用的

await consumer.subscribe({
      topic: /^topic-/,
      fromBeginning: true, // Set to false if you want to start consuming from the latest offset
    });

这意味着像这样的模式订阅所有主题作为主题-B,主题-C,

© www.soinside.com 2019 - 2024. All rights reserved.