使用 KafkaJS 创建主题时出错

问题描述

我正在使用 KafkaJS 创建主题

 const kafka = new Kafka({

  connectionTimeout: 10_000,authenticationTimeout: 10_000,brokers: [`HOST_NAME:9092`],clientId: 'example-producer',ssl: {
    servername: 'HOST_NAME',rejectUnauthorized: false
  },sasl: {
    mechanism: 'plain',username: '*******',password: '******'
  }
})

const admin = kafka.admin();
await admin.connect()

const topic = 'test-topics';
const run = async () => {
  await admin.connect()
  await admin.createtopics({
    topics: [{ topic }],waitForleaders: true,})
  await admin.createPartitions({
    topicPartitions: [{ topic: topic,count: 1}],})
}
run().catch(e => kafka.logger().error(`[Kafka-config] ${e.message}`,{ stack: e.stack }));

出现以下错误

message":"[Connection] Response Createtopics(key: 19,version: 3)","broker":"HOST_NAME:9092","clientId":"example-producer","error": "请求参数不满足配置的策略","correlationId":3,"size":58}

但是当我尝试列出集群中存在的主题时,我得到了成功的响应 因为主题已经存在。

解决方法

添加 topics: [{ topic **,replicationFactor: 3**}] 解决了问题