Java &Kafka:无法建立到节点 999 的连接.经纪人可能不可用 [英] Java & Kafka: Connection to node 999 could not be established. Broker may not be available

查看:38
本文介绍了Java &Kafka:无法建立到节点 999 的连接.经纪人可能不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 docker-compose.yml 是

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

在 web docker 容器中,java kafka 生产者:

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);

但是,我得到了 [kafka-producer-network-thread |producer-2] WARN org.apache.kafka.clients.NetworkClient - [Producer clientId=producer-2] 无法建立到节点 -1 的连接.代理可能不可用.

不知道为什么.欢迎任何建议.非常感谢.

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

更新

我可以通过 bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic TestTopic1 --from-beginningbin/kafka 生成和消费消息-console-producer.sh --broker-list localhost:9092 --topic TestTopic1 kafka docker 容器内外.

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.

docker 容器内的 localhost 是什么?

what is localhost inside docker container?

更新

现在,我将 Kafka Java 生产者配置的 KAFKA_ADVERTISED_HOST_NAMEbootstrap.servers 手动更改为 docker 主机 ip(由 ifconfig 获取).现在,它正在发挥作用.现在,我不知道如何通过程序或 bash 自动获取 docker 主机 ip 并将其设置在 docker-compose 文件中.

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.

推荐答案

我们能够通过将 /etc/hosts 从主机复制到 Docker 容器来解决这个问题.显然 Kubernetes DNS services 在我们的设置中没有正确工作.

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.

这篇关于Java &amp;Kafka:无法建立到节点 999 的连接.经纪人可能不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆