简体   繁体   English

无法连接到 MSK 上的 Kafka

[英]Failing to connect to Kafka on MSK

I have created a Kafka cluster on MSK and now I'm trying to connect to the cluster with python.我已经在 MSK 上创建了一个 Kafka 集群,现在我正在尝试使用 python 连接到该集群。

I wrote this shortcode:我写了这个简码:

from kafka import KafkaProducer
import json

producer = KafkaProducer(
    bootstrap_servers=['host1:9092', 'host2:9092'],
    value_serializer=lambda x: json.dumps(x).encode('utf-8'),
    api_version=(2, 4, 1)
)

producer.send('test', value={'hello':'world'})

The problem is that every time I running it I'm getting this error:问题是每次我运行它时都会收到此错误:

KafkaTimeoutError: Failed to update metadata after 60.0 secs.

I thought it might be related to Kafka creating topics so I added this line to the configuration.我认为这可能与 Kafka 创建主题有关,所以我将这一行添加到配置中。

auto.create.topics.enable=true

But I'm still getting the same error.但我仍然遇到同样的错误。

This is my full configuration file:这是我的完整配置文件:

default.replication.factor=3
min.insync.replicas=2
num.io.threads=8
num.network.threads=5
num.partitions=1
num.replica.fetchers=2
socket.request.max.bytes=104857600
unclean.leader.election.enable=true
auto.create.topics.enable=true
zookeeper.connection.timeout.ms=5000

What am I missing here?我在这里错过了什么? I read somewhere that is may relate to SSL authentication but at any step, there wasn't any.pem file, .ca file, or anything like that.我在某处读到可能与 SSL 身份验证有关,但在任何步骤中,都没有任何 .pem 文件、.ca 文件或类似文件。

You probably fail to connect to the MSK.您可能无法连接到 MSK。 The error you experience is a Timeout error.您遇到的错误是超时错误。

One wrong assumption when first starting to use MSK is, you can connect to it from outside AWS network.第一次开始使用 MSK 时的一个错误假设是,您可以从 AWS 网络外部连接到它。 That is a wrong assumption.这是一个错误的假设。 AWS has a detailed document on how can you access your MSK cluster . AWS 有一份关于如何访问 MSK 集群的详细文档。

If you're not running your client from an EC2 instance inside your MSK VPC , you won't be able to reach it.如果您没有从MSK VPC 内的 EC2 实例运行客户端,您将无法访问它。 Even if you open the relevant security rules in the MSK security group policy.即使你在 MSK 安全组策略中打开了相关的安全规则。

I have spend time trying multiple proxies to access the MSK from outside AWS, with no success.我花时间尝试多个代理从 AWS 外部访问 MSK,但没有成功。 Just follow the guide I referred above, and it will probably fix your connection issue.只需按照我上面提到的指南进行操作,它可能会解决您的连接问题。

Also, If you're new to MSK I highly suggest you to go through the Getting Started tutorial, or at least Steps 5 ( Create a Topic ) and 6 ( Produce and Consume Data ).此外,如果您是 MSK 的新手,我强烈建议您通过入门教程或至少第 5 步( 创建主题)和第 6 步( 生产和使用数据)来学习 go。

kafka-python + SASL auth MSK worked for me kafka-python + SASL auth MSK 为我工作

producer = KafkaProducer(
    bootstrap_servers=SASL_SERVERS,
    value_serializer=lambda v: json.dumps(v).encode('utf-8'),
    api_version=(2,8,1),
    security_protocol='SASL_SSL',
    sasl_mechanism='SCRAM-SHA-512',
    sasl_plain_username=os.getenv('KAFKA_USER'),
    sasl_plain_password=os.getenv('KAFKA_PASS'),
    retries=1
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM