0

I am trying to use docker to deploy my app, and I write the following docker-compose. I ran the docker-compose up -d, however, I get the following error:

ERROR [Connection] Connection error: connect ECONNREFUSED 127.0.0.1:29092 {\"timestamp\":\"2021-11-30T02:14:08.389Z\",\"logger\":\"kafkajs\",\"broker\":\"localhost:29092\",\"clientId\":\"nestjs-consumer-server\",\"stack\":\"Error: connect ECONNREFUSED 127.0.0.1:29092\\n    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16)\"}"}
notification-center-web-1  | {"level":"error","message":"ERROR [BrokerPool] Failed to connect to seed broker, trying another broker from the list: Connection error: connect ECONNREFUSED 127.0.0.1:29092 {\"timestamp\":\"2021-11-30T02:14:08.389Z\",\"logger\":\"kafkajs\",\"retryCount\":1,\"retryTime\":624}"}

I have tried different ports but still can not work. I am wondering how to connect my web app and the kafka so that I can send messages on a topic. localhost:8080 (kafka_ui) can work, and it shows that CLUSTERS are online.

Please help

Erika
  • 453
  • 8
  • 23

1 Answers1

1

You have to connect to the hostname "kafka" since that is your containers name. This is how networking works with docker-compose.
Localhost will refer to each containers own localhost in this case. Think about a container as a separate host regarding networking.

Since you're not using localhost you also need to change your port to 9092.

On a side-note: You would also be able to use your external IP (e.g. 192.168.0.123) port 29092 in case you have external services. This is because you exposed the localhost:29092 port using the "ports" keyword in docker-compose.

tldr: Change your connection string from localhost:29092 to kafka:9092

Mikael Kjær
  • 670
  • 3
  • 6
  • Hi Mikael, thank you for your reply. I change from localhost:29092 to kafka:9092, but I get the following error: ERROR Exiting Kafka due to fatal exception (kafka.Kafka$) java.lang.IllegalArgumentException: requirement failed: Each listener must have a different port, listeners: PLAINTEXT://0.0.0.0:9092,PLAINTEXT_HOST://0.0.0.0:9092 – Erika Nov 30 '21 at 17:53
  • Maybe the `KAFKA_LISTENER_SECURITY_PROTOCOL_MAP` is incorrect. I am not sure about that setting but it looks like it should be: `KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT_HOST` – Mikael Kjær Nov 30 '21 at 18:04
  • here is the error : ERROR Exiting Kafka due to fatal exception (kafka.Kafka$) org.apache.kafka.common.config.ConfigException: Invalid security protocol `PLAINTEXT_HOST` defined in listener.security.protocol.map – Erika Nov 30 '21 at 18:13
  • My above comment wasn't correct. You should revert it. Looks like the listeners isn't set correctly. Try to set `KAFKA_LISTENERS` to the same as `KAFKA_ADVERTISED_LISTENERS`. You can read https://rmoff.net/2018/08/02/kafka-listeners-explained/ for more information. – Mikael Kjær Nov 30 '21 at 18:23
  • I used the wrong hostname (localhost:29092), and I change it to kafka:29092 in my app. working fine. Thank you so much. – Erika Dec 02 '21 at 01:54