0

Given this doker-compose.yml

version: '2'
services:
zookeeper:
  image: confluentinc/cp-zookeeper:latest
  environment:
    ZOOKEEPER_CLIENT_PORT: 2181
    ZOOKEEPER_TICK_TIME: 2000

kafka:
  image: confluentinc/cp-kafka:latest
  depends_on:
    - zookeeper
  ports:
    - 9092:9092
  environment:
    KAFKA_BROKER_ID: 1
    KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
    KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092,PLAINTEXT_HOST://localhost:29092
    KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
    KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
    KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1

I tried to send some data to docker consumer :

    val props = new Properties()
    props.put("bootstrap.servers", "kafka:9092")
    props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
    props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")
    val producer = new KafkaProducer[String, String](props)
    val record = new ProducerRecord[String, String]("topic-name", "key", "my_awsome_docker")
    producer.send(record)

But it failed.

Failed to construct kafka producer org.apache.kafka.common.KafkaException: Failed to construct kafka producer

  • 1
    How about changing `kafka:9092` to `localhost:9092`. The `kafka` domain only resolvable inside docker but not your local environment. You kafka image has already mapped port 9092 of the kafka container to the 9092 of the host. So simply use the port mapped to access kafka. – Truong Hua Oct 24 '21 at 11:03
  • Can you share the whole error message? There's likely a root cause saying that `kafka:9092` is unreachable because of what @Truong Hua said above. – Gaël J Oct 24 '21 at 11:35
  • this the error when i changed kafka:9092 by localhost:9092 : 2021/10/24 14:10:28,383 WARN [kafka-producer-network-thread | producer-1] org.apache.kafka.clients.NetworkClient - [Producer clientId=producer-1] Error connecting to node kafka:9092 (id: 1 rack: null) java.net.UnknownHostException: kafka: nodename nor servname provided, or not known – moez skanjii Oct 24 '21 at 12:12
  • Because you actually need to use 29092 because that's the advertised listener you've configured – OneCricketeer Oct 24 '21 at 13:09

0 Answers0