I'm working on a node js and kafka app and i put both of the services in docker compose.
The problem is my nodejs app connects to kafka broker but when i produce messages or try to consume i get this error:
connect ECONNREFUSED 127.0.0.1:9092","broker":"127.0.0.1:9092","clientId":"myapp","stack":"Error: connect ECONNREFUSED 127.0.0.1:9092\
I'm not sure but from what i did read i guess the problem is KAFKA_CFG_ADVERTISED_LISTENERS
docker-compose.yml
version: '3.8'
services:
zookeeper:
image: 'bitnami/zookeeper:latest'
container_name: zookeeper
ports:
- '2181:2181'
environment:
- ALLOW_ANONYMOUS_LOGIN=yes
kafka:
image: 'bitnami/kafka:latest'
container_name: kafka
ports:
- '9092:9092'
environment:
- KAFKA_BROKER_ID=1
- KAFKA_CFG_LISTENERS=PLAINTEXT://:9092
/*
// I THINK THE PROBLEM IS HERE
- KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://127.0.0.1:9092
*/
- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
- ALLOW_PLAINTEXT_LISTENER=yes
depends_on:
- zookeeper
emailing-service:
build:
context: ./emailing-service
dockerfile: Dockerfile
image: emailing-services-image
ports:
- "6000:6000"
container_name: emailing-service-container
volumes:
- ./emailing-service/:/app:ro
My node app
import { Kafka, Partitioners } from "kafkajs";
const kafka = new Kafka({
clientId: 'myapp',
brokers: ['kafka:9092']
})
const producer = kafka.producer({ createPartitioner: Partitioners.LegacyPartitioner })
const consumer = kafka.consumer({ groupId: 'test-group' })
const run = async () => {
await producer.connect()
console.log('Connecting');
await producer.send({
topic: 'test-topic',
messages: [
{ value: 'Hello KafkaJS user!' },
],
})
await consumer.connect()
await consumer.subscribe({ topic: 'test-topic', fromBeginning: true })
console.log('CONSUMER subscribed')
await consumer.run({
eachMessage: async ({ topic, partition, message }) => {
console.log({
topic,
partition,
offset: message.offset,
value: message.value?.toString(),
})
},
})
}
run().catch(console.error)