使用Python的Kafka SSL:kafka.errors.NoBrokersAvailable:NoBrokersAvailable

时间:2018-11-07 11:47:20

标签: python python-3.x ssl apache-kafka

我正在尝试在Apache Kafka服务器和客户端之间进行安全通信。我创建了客户端和服务器信任区和密钥库,并通过以下链接进行了相应的配置-https://www.confluent.io/blog/apache-kafka-security-authorization-authentication-encryption/

这是我的以下代码:

from kafka import KafkaProducer, KafkaConsumer

producer = KafkaProducer(bootstrap_servers='cylc-vm:9092',
                          security_protocol='SSL',
                          ssl_cafile='CARoot.pem',
                          ssl_certfile='certificate.pem',
                          ssl_keyfile='key.pem')
producer.send('foobar1', b'some_message_bytes')
producer.flush()

consumer = KafkaConsumer('foobar1', auto_offset_reset='earliest', bootstrap_servers='cylc-vm:9092')

for each in consumer:
    print(each)

在上面的代码中,当使用者尝试连接时,它将引发以下异常:

Traceback (most recent call last):
  File "test.py", line 14, in <module>
    consumer = KafkaConsumer('foobar1', auto_offset_reset='earliest', bootstrap_servers='cylc-vm:9092')
  File "/usr/lib/python3.6/site-packages/kafka/consumer/group.py", line 324, in __init__
    self._client = KafkaClient(metrics=self._metrics, **self.config)
  File "/usr/lib/python3.6/site-packages/kafka/client_async.py", line 221, in __init__
    self.config['api_version'] = self.check_version(timeout=check_timeout)
  File "/usr/lib/python3.6/site-packages/kafka/client_async.py", line 826, in check_version
    raise Errors.NoBrokersAvailable()
kafka.errors.NoBrokersAvailable: NoBrokersAvailable

从Consumer类中删除ssl args的全部目的是验证SSL设置是否正常运行。我期待一些SSL Authenticate异常,但是引发的异常是另外一回事。我在这里错过了什么吗,或者这是预期的行为?并说明SSL在这里工作正常吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

您应该使用server.properties的侦听器中定义的端口。 9092是PLAINTEXT的默认设置。