情况
我正在使用kafkajs写入一些动态生成的 kafka 主题。
我发现在注册制作人后立即写这些主题会经常导致错误:There is no leader for this topic-partition as we are in the middle of a leadership election
.
完整的错误是:
{"level":"ERROR","timestamp":"2020-08-24T17:48:40.201Z","logger":"kafkajs","message":"[Connection] Response Metadata(key: 3, version: 5)","broker":"localhost:9092","clientId":"tv-kitchen","error":"There is no leader for this topic-partition as we are in the middle of a leadership election","correlationId":1,"size":146}
编码
这是导致问题的代码:
import kafka from 'myConfiguredKafkaJs'
const run = async () => {
const producer = kafka.producer()
await producer.connect()
producer.send({
topic: 'myRandomTopicString',
messages: [{
value: 'yolo',
}],
})
}
run()
问题
两个问题:
- 在连接到生产者(或发送)时我应该做些什么特别的事情,以确保逻辑阻塞,直到生产者真正准备好将数据发送到 kafka 主题?
- 在向生产者发送数据以确保消息不被丢弃时,我应该做些什么特别的事情?