Python Kafka 客户端无法连接到远程 Kafka 服务器

Python Kafka client cannot connect to remote Kafka server

我在云上有一个 Ubuntu VM,我从 Official Kafka site and followed the instructions in Kafka's official quickstart guide.

下载了 Kafka 2.8.1 版

我正在使用 python 客户端来使用我在快速入门指南中创建的主题之一。当我在 VM 上 运行 时,一切 运行 都很好,但是,当我在本地系统上 运行 相同的程序时,我得到以下错误

Unable connect to node with id 0: [Errno 8] nodename nor servname provided, or not known
Traceback (most recent call last):
...
...
File "/Path/python3.9/site-packages/aiokafka/client.py", line 547, in check_version
   raise KafkaConnectionError(
kafka.errors.KafkaConnectionError: KafkaConnectionError: No connection to node with id 0

我正在使用的python程序:

import asyncio
import aiokafka

async def consume(self):
    consumer = aiokafka.AIOKafkaConsumer(
        "quickstart-events", bootstrap_servers="IP:9092"
    )
    try:
        await consumer.start()
        async for msg in self.consumer:
            print(
                "consumed: ",
                msg.topic,
                msg.partition,
                msg.offset,
                msg.key,
                msg.value,
                msg.timestamp,
            )
    finally:
        await consumer.stop()

asyncio.run(consume())

我已确保 Ubuntu 上的必要端口 (9022) 已打开 - 我检查过我可以从我的本地系统远程登录到端口 9022。

我不确定我无法通过互联网访问 Kafka 的原因是什么。我是否漏掉了一些明显的东西?

config/server.properties 中的以下属性更改为您在代码中使用的 bootstrap 服务器地址。

advertised.listeners = PLAINTEXT://IP or FQDN:9092