1

I have this very simple code which is supposed just to transmit a simple message from producer to consumer using Kafka in local host which come from the official GitHub

const { Kafka } = require('kafkajs')

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

const producer = kafka.producer()
const consumer = kafka.consumer({ groupId: 'test-group' })

const run = async () => {
  // Producing
  await producer.connect()
  await producer.send({
    topic: 'test-topic',
    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)

When I execute this code using node index.js (which is this file) I get many of these two errors :

{"level":"ERROR","timestamp":"2021-03-29T12:01:00.633Z","logger":"kafkajs","message":"[Connection] Connection error: getaddrinfo ENOTFOUND kafka1","broker":"kafka1:9092","clientId":"my-app","stack":"Error: getaddrinfo ENOTFOUND kafka1\n    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:60:26)"}

{"level":"ERROR","timestamp":"2021-03-29T12:01:00.635Z","logger":"kafkajs","message":"[BrokerPool] Failed to connect to seed broker, trying another broker from the list: Connection error: getaddrinfo ENOTFOUND kafka1","retryCount":0,"retryTime":323}

So it looks like the first lines defining the broker on local port 9092 are not working I guess? But I don't know how to correct this. Could you help me please?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Kevin Heirich
  • 109
  • 2
  • 12
  • `kafka1:9092', 'kafka2:9092` are two remote servers, not local ports – OneCricketeer Mar 29 '21 at 13:38
  • So it looks like I'm trying to use a foo-server that doesn't exists ? I havn't made any configurations on my computer (i'm new on this), so my local server might be missing or not working ? I have installed zookeeper user apt but nothing more. – Kevin Heirich Mar 29 '21 at 13:59
  • The example given in the Readme is to show that the brokers argument is an array, not a string. You definitely need to install and run Kafka as well. And if doing locally, you need to give the appropriate localhost address in the code – OneCricketeer Mar 29 '21 at 14:08
  • Okay if you don't mind I find quite difficult to find an updated ressource on how to deploy a local kafka server ? I've seen many ways of doing using deprecated stuff on some dark dev blog ^^' Would you have one to refer to please to make my local tests ? Many thanks ! – Kevin Heirich Mar 29 '21 at 14:13
  • 1
    The official Kafka website shows exactly what you need – OneCricketeer Mar 29 '21 at 14:25
  • I must have missread then ! Many thanks to you ! – Kevin Heirich Mar 29 '21 at 14:33

1 Answers1

0

Thanks to OneCricketeer I've found that I havn't properly launch a local kafka server so the first lines could not work.

Many thanks to him !

Kevin Heirich
  • 109
  • 2
  • 12