sli*_*fty 9 node.js apache-kafka kafkajs
我正在使用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}
Run Code Online (Sandbox Code Playgroud)
这是导致问题的代码:
{"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}
Run Code Online (Sandbox Code Playgroud)
两个问题:
KafkajscreateTopics通过管理客户端提供了一种方法,该方法有一个可选waitForLeaders标志:
admin.createTopics({
waitForLeaders: true,
topics: [
{ topic: 'myRandomTopicString123' },
],
}
Run Code Online (Sandbox Code Playgroud)
使用这个可以解决这个问题。
import kafka from 'myConfiguredKafkaJs'
const run = async () => {
const producer = kafka.producer()
const admin = kafka.admin()
await admin.connect()
await producer.connect()
await admin.createTopics({
waitForLeaders: true,
topics: [
{ topic: 'myRandomTopicString123' },
],
})
producer.send({
topic: 'myRandomTopicString',
messages: [{
value: 'yolo',
}],
})
}
run()
Run Code Online (Sandbox Code Playgroud)
不幸的是,如果该主题已经存在,这将导致不同的错误,但这是一个单独的问题,我怀疑该错误比破坏更具信息性。
{"level":"ERROR","timestamp":"2020-08-24T18:19:48.465Z","logger":"kafkajs","message":"[Connection] Response CreateTopics(key: 19, version: 2)","broker":"localhost:9092","clientId":"tv-kitchen","error":"Topic with this name already exists","correlationId":2,"size":86}
Run Code Online (Sandbox Code Playgroud)
编辑:上述设置确实需要正确配置您的 Kafka 实例。领导选举有可能永远无法解决,在这种情况下,KafkaJS 仍然会抱怨领导选举!
根据我的经验,这是由于 kafka 代理在没有从 Zookeeper 注销的情况下被停止的情况,这意味着 Zookeeper 正在等待不再存在的响应。
| 归档时间: |
|
| 查看次数: |
10377 次 |
| 最近记录: |