无法从其 docker 容器外部连接到 kafka

Cannot connect to kafka from ourside of its docker container

这是我的 docker 撰写文件:

version: "3.7"

services:
  zookeeper:
    image: 'bitnami/zookeeper:latest'
    container_name: zookeeper
    ports: 
      - 2181:2181
    env_file: 
      - zookeeper.env
  
  kafka:
    image: 'bitnami/kafka:latest'
    container_name: kafka
    env_file:
      - kafka.env
    ports: 
      - 9093:9092
    depends_on:
      - zookeeper

还有这些是我的 .env 文件:

kakfa.env

KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
KAFKA_CFG_LISTENERS=INTERNAL://kafka:9092,EXTERNAL://localhost:9093
KAFKA_CFG_ADVERTISED_LISTENERS=INTERNAL://kafka:9092,EXTERNAL://localhost:9093
KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT
KAFKA_CFG_INTER_BROKER_LISTENER_NAME=INTERNAL
KAFKA_CFG_OFFSETS_TOPIC_REPLICATION_FACTOR=1
ALLOW_PLAINTEXT_LISTENER=yes

zookeeper.env

ZOOKEEPER_CLIENT_PORT=2181
ZOOKEEPER_TICK_TIME=2000
ALLOW_ANONYMOUS_LOGIN=yes

当我编写撰写文件时,一切似乎都很好,并且我在 docker 容器之外,在我的本地主机上,到 9093 端口有很好的 telnet 结果形式。

我写了一个简单的生产者python文件如下:

import logging

from kafka import KafkaProducer

logging.basicConfig(level=logging.INFO)


class Producer():
    def __init__(self) -> None:
        self.conn = KafkaProducer(bootstrap_servers="localhost:9093")

    def produce(self):
        while True:
            try:
                self.conn.send("test", b'test')
            except KeyboardInterrupt:
                logging.info("Producer finished")
                break


if __name__ == "__main__":
    producer = Producer()
    producer.produce()

当我 运行 我的代码时,我得到这个错误:

WARNING:kafka.conn:DNS lookup failed for kafka:9092, exception was [Errno -3] Temporary failure in name resolution. Is your advertised.listeners (called advertised.host.name before Kafka 9) correct and resolvable?
ERROR:kafka.conn:DNS lookup failed for kafka:9092 (AddressFamily.AF_UNSPEC)

我也阅读了 this post 但我无法解决我的错误,我不知道我应该怎么做才能摆脱这个错误。

您转发了错误的端口

主机上的 9093 需要映射到 localhost:9093 广告端口

否则,您将连接到 9093,即 returns kafka:9092,如博客中所述。主机无法解析容器主机名,默认