如何将 Python 使用者连接到 AWS MSK [关闭]
Posted
技术标签:
【中文标题】如何将 Python 使用者连接到 AWS MSK [关闭]【英文标题】:How to connect Python consumer to AWS MSK [closed] 【发布时间】:2021-05-28 05:10:27 【问题描述】:我正在尝试将我的 python 使用者连接到 AWS MSK 集群。我该怎么做?
运行 AWS MSK 集群 我正在尝试使用 python 和 kafka python 从 MSK 集群中消费消息。
我遇到的错误
Traceback (most recent call last):
File "consumer.py", line 23, in <module>
for message in consumer:
File "/home/ubuntu/.local/lib/python3.6/site-packages/kafka/consumer/group.py", line 1193, in __next__
return self.next_v2()
File "/home/ubuntu/.local/lib/python3.6/site-packages/kafka/consumer/group.py", line 1201, in next_v2
return next(self._iterator)
File "/home/ubuntu/.local/lib/python3.6/site-packages/kafka/consumer/group.py", line 1116, in _message_generator_v2
record_map = self.poll(timeout_ms=timeout_ms, update_offsets=False)
File "/home/ubuntu/.local/lib/python3.6/site-packages/kafka/consumer/group.py", line 655, in poll
records = self._poll_once(remaining, max_records, update_offsets=update_offsets)
File "/home/ubuntu/.local/lib/python3.6/site-packages/kafka/consumer/group.py", line 675, in _poll_once
self._coordinator.poll()
File "/home/ubuntu/.local/lib/python3.6/site-packages/kafka/coordinator/consumer.py", line 270, in poll
self.ensure_coordinator_ready()
File "/home/ubuntu/.local/lib/python3.6/site-packages/kafka/coordinator/base.py", line 258, in ensure_coordinator_ready
self._client.poll(future=future)
File "/home/ubuntu/.local/lib/python3.6/site-packages/kafka/client_async.py", line 582, in poll
self._maybe_connect(node_id)
File "/home/ubuntu/.local/lib/python3.6/site-packages/kafka/client_async.py", line 392, in _maybe_connect
conn.connect()
File "/home/ubuntu/.local/lib/python3.6/site-packages/kafka/conn.py", line 429, in connect
if self._try_handshake():
File "/home/ubuntu/.local/lib/python3.6/site-packages/kafka/conn.py", line 508, in _try_handshake
self._sock.do_handshake()
File "/usr/lib/python3.6/ssl.py", line 1077, in do_handshake
self._sslobj.do_handshake()
File "/usr/lib/python3.6/ssl.py", line 689, in do_handshake
self._sslobj.do_handshake()
OSError: [Errno 0] Error
【问题讨论】:
你试过什么?你被困在哪里了?您是否在搜索引擎中搜索过“Python Kafka”? 目前我在 EC2 中运行 Kafka,并让 python 消费者监听多个主题和处理。但我需要运行 AWS MSK 集群并连接到它。 所以把你Python代码中的Kafka连接地址改成MSK集群地址。如果这不起作用,请在您的问题中添加更多信息。 这不起作用,这就是这里的问题的原因...... 如果它不起作用,请在您的问题中提供错误消息。这可能根本不是 Python 问题,而是 AWS 网络配置问题,因此您也需要提供该信息。 【参考方案1】:使用 kafka-python:
from kafka import KafkaConsumer
if __name__ == '__main__':
topic_name = 'example-topic'
consumer = KafkaConsumer(topic_name, auto_offset_reset='earliest',
bootstrap_servers=['kafka2:9092'], api_version=(0, 10), consumer_timeout_ms=1000)
for msg in consumer:
print(msg.value)
if consumer is not None:
consumer.close()
from time import sleep
from kafka import KafkaProducer
# publish messages on topic
def publish_message(producer_instance, topic_name, key, value):
try:
key_bytes = bytes(key, encoding='utf-8')
value_bytes = bytes(value, encoding='utf-8')
producer_instance.send(topic_name, key=key_bytes, value=value_bytes)
producer_instance.flush()
print('Message ' + key + ' published successfully.')
except Exception as ex:
print('Exception in publishing message')
print(str(ex))
# establish kafka connection
def connect_kafka_producer():
_producer = None
try:
_producer = KafkaProducer(bootstrap_servers=['kafka1:9092'])
except Exception as ex:
print('Exception while connecting Kafka')
print(str(ex))
finally:
return _producer
if __name__ == '__main__':
kafka_producer = connect_kafka_producer()
x = 0
while True:
publish_message(kafka_producer, 'raw_recipes', str(x), 'This is message ' + str(x))
x += 1
if kafka_producer is not None:
kafka_producer.close()
【讨论】:
使用 AWS MSK 无法正常工作以上是关于如何将 Python 使用者连接到 AWS MSK [关闭]的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Python 连接到 AWS RDS MySql 数据库
如何从 AWS Lambda Python 连接到 Informix DB
如何使用 IAM Auth 将 python websocket 连接到 AppSync 订阅?