Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
481 views
in Technique[技术] by (71.8m points)

docker - Java & Kafka: Connection to node 999 could not be established. Broker may not be available

My docker-compose.yml is

version: "2.1"
services:
  pg-master:
    image: postgres-9.6:3
    ports:
     - "5432:5432"
    environment:
     - VIRTUAL_HOST=postgres.local
  web:
    build: .
    links:
      - pg-master:postgres.local
    ports:
     - "8080:8080"
  zookeeper:
    image: jplock/zookeeper:3.4.6
    ports:
      - "2181:2181"
  kafka:
    image: wurstmeister/kafka:1.0.0
    ports:
      - "9092:9092"
    environment:
      - KAFKA_ADVERTISED_HOST_NAME=localhost
      - KAFKA_ADVERTISED_PORT=9092
      - KAFKA_CREATE_TOPICS=TestTopic1:3:1
      - KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
      - KAFKA_BROKER_ID=999
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

In web docker container, the java kafka producer:

Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("acks", "all");
props.put("retries", 0);
props.put("batch.size", 16384);
props.put("linger.ms", 1);
props.put("buffer.memory", 33554432);
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
this.kafkaProducer = new KafkaProducer<String, String>(props);

But, I got [kafka-producer-network-thread | producer-2] WARN org.apache.kafka.clients.NetworkClient - [Producer clientId=producer-2] Connection to node -1 could not be established. Broker may not be available.

NO idea why. any suggestions welcomed. Thanks very much.

UPDATE

I can produce and consume messages by bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic TestTopic1 --from-beginning and bin/kafka-console-producer.sh --broker-list localhost:9092 --topic TestTopic1 inside and outside kafka docker container.

what is localhost inside docker container?

UPDATE

Now, I changed the KAFKA_ADVERTISED_HOST_NAME and bootstrap.servers of Kafka Java producer config manually to the docker host ip (got by ifconfig). Now, it is working. Now, I have no idea how to get docker host ip and set it in docker-compose files automatically by program or bash.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

We were able to resolve this issue by copying /etc/hosts from the host to the Docker container. Apparently Kubernetes DNS services were not correctly working on our setup.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...