question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

can't connect to broker

See original GitHub issue

i just copied the code from readme.md

and change the broker address to localhost:9092

but why the server address like this ?

broker":“f43be2dafb3b:9092”,

btw i use this one to start kafka https://hub.docker.com/r/bitnami/kafka

{"level":"ERROR","timestamp":"2019-08-23T07:15:04.982Z","logger":"kafkajs","message":"[Connection] Connection error: getaddrinfo ENOTFOUND f43be2dafb3b f43be2dafb3b:9092","broker":"f43be2dafb3b:9092","clientId":"my-app","stack":"Error: getaddrinfo ENOTFOUND f43be2dafb3b f43be2dafb3b:9092\n    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:57:26)"}
{"level":"ERROR","timestamp":"2019-08-23T07:15:04.984Z","logger":"kafkajs","message":"[BrokerPool] Failed to connect to broker, reconnecting","retryCount":0,"retryTime":314}
{"level":"ERROR","timestamp":"2019-08-23T07:15:05.304Z","logger":"kafkajs","message":"[Connection] Connection error: getaddrinfo ENOTFOUND f43be2dafb3b f43be2dafb3b:9092","broker":"f43be2dafb3b:9092","clientId":"my-app","stack":"Error: getaddrinfo ENOTFOUND f43be2dafb3b f43be2dafb3b:9092\n    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:57:26)"}
{"level":"ERROR","timestamp":"2019-08-23T07:15:05.304Z","logger":"kafkajs","message":"[BrokerPool] Failed to connect to broker, reconnecting","retryCount":1,"retryTime":604}
{"level":"ERROR","timestamp":"2019-08-23T07:15:05.911Z","logger":"kafkajs","message":"[Connection] Connection error: getaddrinfo ENOTFOUND f43be2dafb3b f43be2dafb3b:9092","broker":"f43be2dafb3b:9092","clientId":"my-app","stack":"Error: getaddrinfo ENOTFOUND f43be2dafb3b f43be2dafb3b:9092\n    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:57:26)"}
{"level":"ERROR","timestamp":"2019-08-23T07:15:05.911Z","logger":"kafkajs","message":"[BrokerPool] Failed to connect to broker, reconnecting","retryCount":2,"retryTime":1138}
{"level":"ERROR","timestamp":"2019-08-23T07:15:07.055Z","logger":"kafkajs","message":"[Connection] Connection error: getaddrinfo ENOTFOUND f43be2dafb3b f43be2dafb3b:9092","broker":"f43be2dafb3b:9092","clientId":"my-app","stack":"Error: getaddrinfo ENOTFOUND f43be2dafb3b f43be2dafb3b:9092\n    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:57:26)"}
{"level":"ERROR","timestamp":"2019-08-23T07:15:07.055Z","logger":"kafkajs","message":"[BrokerPool] Failed to connect to broker, reconnecting","retryCount":3,"retryTime":2218}
{"level":"ERROR","timestamp":"2019-08-23T07:15:09.278Z","logger":"kafkajs","message":"[Connection] Connection error: getaddrinfo ENOTFOUND f43be2dafb3b f43be2dafb3b:9092","broker":"f43be2dafb3b:9092","clientId":"my-app","stack":"Error: getaddrinfo ENOTFOUND f43be2dafb3b f43be2dafb3b:9092\n    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:57:26)"}
{"level":"ERROR","timestamp":"2019-08-23T07:15:09.278Z","logger":"kafkajs","message":"[BrokerPool] Failed to connect to broker, reconnecting","retryCount":4,"retryTime":4694}
{"level":"ERROR","timestamp":"2019-08-23T07:15:13.975Z","logger":"kafkajs","message":"[Connection] Connection error: getaddrinfo ENOTFOUND f43be2dafb3b f43be2dafb3b:9092","broker":"f43be2dafb3b:9092","clientId":"my-app","stack":"Error: getaddrinfo ENOTFOUND f43be2dafb3b f43be2dafb3b:9092\n    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:57:26)"}
{"level":"ERROR","timestamp":"2019-08-23T07:15:13.975Z","logger":"kafkajs","message":"[BrokerPool] Failed to connect to broker, reconnecting","retryCount":5,"retryTime":10562}
{"level":"ERROR","timestamp":"2019-08-23T07:15:13.977Z","logger":"kafkajs","message":"[Producer] Connection error: getaddrinfo ENOTFOUND f43be2dafb3b f43be2dafb3b:9092","retryCount":0,"retryTime":340}
version: '2'

services:
  zookeeper:
    image: 'bitnami/zookeeper:3'
    ports:
      - '2181:2181'
    volumes:
      - 'zookeeper_data:/bitnami'
    environment:
      - ALLOW_ANONYMOUS_LOGIN=yes
  kafka:
    image: 'bitnami/kafka:2'
    ports:
      - '9092:9092'
    volumes:
      - 'kafka_data:/bitnami'
    environment:
      - KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
      - ALLOW_PLAINTEXT_LISTENER=yes
    depends_on:
      - zookeeper

volumes:
  zookeeper_data:
    driver: local
  kafka_data:
    driver: local
const { Kafka } = require('kafkajs')

const kafka = new Kafka({
  clientId: 'my-app',
  brokers: 'localhost: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)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
tgbvcommented, Feb 26, 2022

If it helps anyone, I received the same error and I managed to solve it by providing the hostname to kafka service in docker-compose.yml:

kafka:
  image: 'bitnami/kafka:2'
  hostname: localhost
  ports:
    - '9092:9092'
  volumes:
    - 'kafka_data:/bitnami'
  environment:
    - KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
    - ALLOW_PLAINTEXT_LISTENER=yes
  depends_on:
    - zookeeper
4reactions
tulioscommented, Aug 23, 2019

Hi @crapthings, your setup is advertising the docker hostnames. Kafka has a configuration for that, so it can correctly pick up the host IP. Try setting the KAFKA_ADVERTISED_HOST_NAME to the host IP.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why Can't I Connect to Kafka? | Troubleshoot Connectivity
When a client wants to send or receive a message from Apache Kafka ®, there are two types of connection that must succeed:...
Read more >
How to handle "Could not connect to broker"? #298 - GitHub
I have a somewhat related issue. I need to connect to brokers dynamically, not statically. Having the broker defined in config is a...
Read more >
Can't connect to Mosquitto broker with local IP address
I have a Mosquitto broker running on my Windows machine. I can connect to this on the same machine using an MQTT client...
Read more >
Can't connect to mqtt broker - Stack Overflow
First you have to make sure that you can connect to the Raspberry Pi. You can try using libraries other than Paho or...
Read more >
[MQTT Web Client] Can't connect to broker | OutSystems
The most common reason for a connection refused with and MQTT client is that it cannot reach the server itself. Where is the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found