Node.js Kafka 在 Windows 10 计算机上出现连接拒绝错误。

huangapple go评论84阅读模式
英文:

Nodejs Kafka error connection refused on windows 10 machine

问题

我在Windows 10机器上运行Kafka代理:

[2023-06-19 19:12:33,360] INFO Registered kafka:type=kafka.Log4jController MBean (kafka.utils.Log4jControllerRegistration$)
3.4.1 (Commit:8a516edc2755df89)

我使用bin\windows\可执行文件同时运行Zookeeper和Kafka服务器。
当我运行以下命令时:

kafka-console-producer.bat --topic test --bootstrap-server localhost:9092

kafka-console-consumer.bat --topic test --bootstrap-server localhost:9092 --from-beginning

都正常工作,我能够从生产者发送消息到消费者。

但是,当我尝试使用Node.js进行生产时,出现以下错误:

{"level":"WARN","timestamp":"2023-06-19T13:30:50.265Z","logger":"kafkajs","message":"KafkaJS v2.0.0 switched default partitioner. To retain the same partitioning behavior as in previous versions, create the producer with the option \"createPartitioner: Partitioners.LegacyPartitioner\". See the migration guide at https://kafka.js.org/docs/migration-guide-v2.0.0#producer-new-default-partitioner for details. Silence this warning by setting the environment variable \"KAFKAJS_NO_PARTITIONER_WARNING=1\"."}
{"level":"ERROR","timestamp":"2023-06-19T13:30:50.304Z","logger":"kafkajs","message":"[Connection] Connection error: connect ECONNREFUSED ::1:9092","broker":"localhost:9092","clientId":"my-app","stack":"Error: connect ECONNREFUSED ::1:9092\n at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16)"}

代码如下:

const { Kafka } = require('kafkajs')

const kafka = new Kafka({
  clientId: 'my-app',
  brokers: ['localhost:9092']
})

const producer = kafka.producer()

const run = async () => {
  // Producing
  await producer.connect()
  await producer.send({
    topic: 'test',
    messages: [
      { value: 'Hello KafkaJS user!' },
    ],
  })

  // Consuming
  await consumer.connect()
  await consumer.subscribe({ topic: 'test-topic', fromBeginning: true })

  await consumer.run({
    eachMessage: async ({ topic, partition, message }) => {
      console.log({
        partition,
        offset: message.offset,
        value: message.value.toString(),
      })
    },
  })
}

run().catch(console.error);

我尝试使用kafka-node和kafka npm包,但出现相同的错误。我已在server.properties文件中添加以下行:

listeners=PLAINTEXT://localhost:9092

但仍然没有成功。

我期望得到一个可以在高可扩展性的环境下使用的最佳Kafka Node.js包的解决方案。

我使用了这个链接:[https://www.npmjs.com/package/kafkajs]

英文:

I an running kafka broker on my windows 10 machine:

[2023-06-19 19:12:33,360] INFO Registered kafka:type=kafka.Log4jController MBean (kafka.utils.Log4jControllerRegistration$)
3.4.1 (Commit:8a516edc2755df89)

I am running zookeeper and kafka server both using bin\windows\ executables.
When I run

kafka-console-producer.bat --topic test --bootstrap-server localhost:9092 

And

kafka-console-consumer.bat --topic test --bootstrap-server localhost:9092 --from-beginning

both are working fine and I am able to send messages from producer to consumer.

But when I try to producer using nodejs it gives me below error.

{"level":"WARN","timestamp":"2023-06-19T13:30:50.265Z","logger":"kafkajs","message":"KafkaJS v2.0.0 switched default partitioner. To retain the same partitioning behavior as in previous versions, create the producer with the option \"createPartitioner: Partitioners.LegacyPartitioner\". See the migration guide at https://kafka.js.org/docs/migration-guide-v2.0.0#producer-new-default-partitioner for details. Silence this warning by setting the environment variable \"KAFKAJS_NO_PARTITIONER_WARNING=1\""}
{"level":"ERROR","timestamp":"2023-06-19T13:30:50.304Z","logger":"kafkajs","message":"[Connection] Connection error: connect ECONNREFUSED ::1:9092","broker":"localhost:9092","clientId":"my-app","stack":"Error: connect ECONNREFUSED ::1:9092\n    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16)"}

Code is:

const { Kafka } = require('kafkajs')

const kafka = new Kafka({
  clientId: 'my-app',
  brokers: ['localhost:9092']
})

const producer = kafka.producer()

const run = async () => {
  // Producing
  await producer.connect()
  await producer.send({
    topic: 'test',
    messages: [
      { value: 'Hello KafkaJS user!' },
    ],
  })

  // Consuming
  await consumer.connect()
  await consumer.subscribe({ topic: 'test-topic', fromBeginning: true })

  await consumer.run({
    eachMessage: async ({ topic, partition, message }) => {
      console.log({
        partition,
        offset: message.offset,
        value: message.value.toString(),
      })
    },
  })
}

run().catch(console.error);

I tried using kafka-node and kafka npm packages but getting same error.
I have added below lines in server.properties file
listeners=PLAINTEXT://localhost:9092 but still no success.

I am expectng a solution which will work with best kafka node package for high scalibility.

I am using this link: [https://www.npmjs.com/package/kafkajs]

答案1

得分: 1

我禁用了IPv6适配器,并将server.properties文件恢复到原始状态。我之前在Redis上也遇到过类似的问题,我认为这可能是同样的问题。

英文:

I disabled IPV6 adaptor and also reverted the server.properties file to its original. I had similar problem with redis I think this can be the same issue.

huangapple
  • 本文由 发表于 2023年6月19日 21:56:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76507332.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定