Kafka-docker: can't produce messages

2019-06-09 14:40发布

问题:

I set up kafka in a docker container using this project https://github.com/wurstmeister/kafka-docker. I can successfully create and list topics on it, but once I try to produce a message with either

bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test

or

bin/kafka-console-producer.sh --broker-list 0.0.0.0:9092 --topic test

I get the following error:

ERROR Error when sending message to topic test with key: null, value: 4 bytes with error: (org.apache.kafka.clients.producer.internals.ErrorLoggingCallback) org.apache.kafka.common.errors.TimeoutException: Expiring 1 record(s) for test-0: 1536 ms has passed since batch creation plus linger time

My docker-compose.yml lookes like this:

version: '2'
services:
  zookeeper:
    image: wurstmeister/zookeeper
    ports:
      - "2181:2181"
  kafka:
    build: .
    ports:
      - "9092:9092"
    environment:
      KAFKA_ADVERTISED_HOST_NAME: docker.for.mac.localhost
      KAFKA_ADVERTISED_PORT: 9092
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

I tried different options for the KAFKA_ADVERTISED_HOST_NAME property, including docker.for.mac.localhost which is supposed to be resolved as the actual docker host ip, and 0.0.0.0, but the result is the same.

回答1:

You can try with this docker-compose, it's working for me:

version: '2'
services:
  zookeeper:
    image: wurstmeister/zookeeper
    stdin_open: true
    tty: true
    ports:
    - 2181:2181/tcp
  kafka:
    image: wurstmeister/kafka:0.11.0.1
    environment:
      KAFKA_CREATE_TOPICS: test:1:1
      KAFKA_ZOOKEEPER_CONNECT: <<HOST_IP>>:2181
      KAFKA_ADVERTISED_HOST_NAME: <<HOST_IP>>
      KAFKA_ADVERTISED_PORT: '9092'
      KAFKA_BROKER_ID: '999'
    stdin_open: true
    tty: true
    links:
    - zookeeper:zookeeper
    ports:
    - 9092:9092/tcp


回答2:

Don't use local IPs like 127.0.0.1. Use the true IP instead.