2

I'm using KafkaJS to produce a message on a Kafka topic. To do so, I've put the Kafka server in Docker using the wurstmeister image.

What I want to do: the Poll container produce a message to the Poll topic and consume messages from the responsePoll topic. But I have an error when trying to produce the message

Error: poll | {"level":"ERROR","timestamp":"2020-10-24T15:21:27.113Z","logger":"kafkajs","message":"[Connection] Connection error: connect ECONNREFUSED 127.0.0.1:9092","broker":"127.0.0.1:9092","clientId":"BlueOriginX","stack ":"Error: connect ECONNREFUSED 127.0.0.1:9092\n at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1145:16)"}

Here's the docker-compose.yml file :

version: '3.4'
services:
  poll:
    container_name: poll
    build: ./Services/Poll
    ports:
      - "4003:4003"
    networks:
      - blueorigin

  zookeeper:
    image: wurstmeister/zookeeper:latest
    ports:
      - "2181:2181"
    networks:
      - blueorigin
  kafka:
    image: wurstmeister/kafka:2.11-1.1.1
    ports:
      - "9092:9092"
    links:
      - zookeeper
    environment:
      KAFKA_ADVERTISED_HOST_NAME: 127.0.0.1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'true'
      KAFKA_DELETE_TOPIC_ENABLE: 'true'
      KAFKA_CREATE_TOPICS: "Poll:1:1,responsePoll:1:1"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    networks:
      - blueorigin

networks:
  blueorigin:

I imagine the error comes from my docker settings but I don't know :/

EDIT: Here's the code to produce a message:

const startPoll = async () =>{
    const producer = kafka.producer()

    await producer.connect()
    await producer.send({
        topic: 'Poll',
        messages: [
            { value: 'New Poll' },
        ],
    })

    await producer.disconnect()
}
Couldosh
  • 371
  • 3
  • 17

1 Answers1

2

I don't have your poll app so I replaced it with Confluent Rest Proxy, but this setup works. I can see topics here http://localhost:8086/topics.

version: '3.4'
services:
  restproxy:
    image: confluentinc/cp-kafka-rest:latest
    ports:
      - "8086:8086"
    environment:
      KAFKA_REST_HOST_NAME: restproxy
      KAFKA_REST_BOOTSTRAP_SERVERS: PLAINTEXT://kafka:9092
      KAFKA_REST_LISTENERS: http://0.0.0.0:8086

  zookeeper:
    image: wurstmeister/zookeeper:latest
    ports:
      - "2181:2181"

  kafka:
    image: wurstmeister/kafka:2.11-1.1.1
    ports:
      - "9092:9092"
    environment:
      KAFKA_ADVERTISED_HOST_NAME: kafka
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'true'
      KAFKA_DELETE_TOPIC_ENABLE: 'true'
      KAFKA_CREATE_TOPICS: "test:1:1"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

You don't need the links and network they are legacy. Also using localhost won't work when you try to connect from one service to another, that's probably why it's not working for you.

jokarls
  • 330
  • 2
  • 11