代码之家  ›  专栏  ›  技术社区  ›  Panadol Chong

无法建立到节点1(localhost/127.0.0.1:9092)的连接。经纪人可能不在。甚至传入正确的端点[重复]

  •  0
  • Panadol Chong  · 技术社区  · 2 年前

    我在本地机器上设置了一个单节点Kafka Docker容器,如中所述 the Confluent documentation (步骤2-3)。

    此外,我还公开了Zookeeper的端口2181和Kafka的端口9092,这样我就可以从本地机器上运行的客户端连接到它们:

    $ docker run -d \
        -p 2181:2181 \
        --net=confluent \
        --name=zookeeper \
        -e ZOOKEEPER_CLIENT_PORT=2181 \
        confluentinc/cp-zookeeper:4.1.0
    
    $ docker run -d \
        --net=confluent \
        --name=kafka \
        -p 9092:9092 \
        -e KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181 \
        -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://kafka:9092 \
        -e KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1 \
        confluentinc/cp-kafka:4.1.0
    

    问题: 当我试图从主机连接到卡夫卡时,连接失败,因为 can't resolve address: kafka:9092 .

    以下是我的Java代码:

    Properties props = new Properties();
    props.put("bootstrap.servers", "localhost:9092");
    props.put("client.id", "KafkaExampleProducer");
    props.put("key.serializer", LongSerializer.class.getName());
    props.put("value.serializer", StringSerializer.class.getName());
    KafkaProducer<Long, String> producer = new KafkaProducer<>(props);
    ProducerRecord<Long, String> record = new ProducerRecord<>("foo", 1L, "Test 1");
    producer.send(record).get();
    producer.flush();
    

    例外情况:

    java.io.IOException: Can't resolve address: kafka:9092
        at org.apache.kafka.common.network.Selector.doConnect(Selector.java:235) ~[kafka-clients-2.0.0.jar:na]
        at org.apache.kafka.common.network.Selector.connect(Selector.java:214) ~[kafka-clients-2.0.0.jar:na]
        at org.apache.kafka.clients.NetworkClient.initiateConnect(NetworkClient.java:864) [kafka-clients-2.0.0.jar:na]
        at org.apache.kafka.clients.NetworkClient.ready(NetworkClient.java:265) [kafka-clients-2.0.0.jar:na]
        at org.apache.kafka.clients.producer.internals.Sender.sendProducerData(Sender.java:266) [kafka-clients-2.0.0.jar:na]
        at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:238) [kafka-clients-2.0.0.jar:na]
        at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:176) [kafka-clients-2.0.0.jar:na]
        at java.lang.Thread.run(Thread.java:748) [na:1.8.0_144]
    Caused by: java.nio.channels.UnresolvedAddressException: null
        at sun.nio.ch.Net.checkAddress(Net.java:101) ~[na:1.8.0_144]
        at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:622) ~[na:1.8.0_144]
        at org.apache.kafka.common.network.Selector.doConnect(Selector.java:233) ~[kafka-clients-2.0.0.jar:na]
        ... 7 common frames omitted
    

    问题: 如何连接在Docker中运行的卡夫卡?我的代码是从主机运行的,不是Docker。

    注意:我知道理论上我可以使用DNS设置和 /etc/hosts 但这是一个变通办法——不应该是这样。

    还有类似的问题 here 然而,它是基于 ches/kafka 形象我用 confluentinc 基于不同的图像。

    0 回复  |  直到 4 年前