Skip to main content
 首页 » 编程设计

python之设置 Python KafkaProducer sasl 机制属性

2024年11月24日26txw1958

我们使用的 sasl 机制是 SCRAM-SHA-256 但 kafka 生产者将只接受 sasl_mechanism 作为 PLAIN, GSSAPI, OAUTHBEARER

下面的配置会报错

sasl_mechanism must be in PLAIN, GSSAPI, OAUTHBEARER 

配置

    ssl_produce = KafkaProducer(bootstrap_servers='brokerCName:9093', 
                     security_protocol='SASL_SSL', 
                     ssl_cafile='pemfilename.pem', 
                     sasl_mechanism='SCRAM-SHA-256', 
                     sasl_plain_username='password', 
                     sasl_plain_password='secret') 

我需要知道如何指定正确的 sasl 机制。

谢谢

请您参考如下方法:

kafka-python v2.0.0+ 的更新答案

自 2.0.0 , kafka-python 同时支持 SCRAM-SHA-256 和 SCRAM-SHA-512。


旧版本 kafka-python 的先前答案

据我了解,您正在使用 kafka-python客户。来自source code ,我可以看到 sasl_mechanism='SCRAM-SHA-256' 不是有效选项:

    """ 
    ... 
    sasl_mechanism (str): Authentication mechanism when security_protocol 
        is configured for SASL_PLAINTEXT or SASL_SSL. Valid values are: 
        PLAIN, GSSAPI, OAUTHBEARER. 
    ... 
    """ 
 
    if self.config['security_protocol'] in ('SASL_PLAINTEXT', 'SASL_SSL'): 
        assert self.config['sasl_mechanism'] in self.SASL_MECHANISMS, ( 
            'sasl_mechanism must be in ' + ', '.join(self.SASL_MECHANISMS))  

一个快速的解决方法是使用 confluent-kafka支持 sasl_mechanism='SCRAM-SHA-256' 的客户端:

from confluent_kafka import Producer  
 
# See https://github.com/edenhill/librdkafka/blob/master/CONFIGURATION.md 
conf = { 
    'bootstrap.servers': 'localhost:9092', 
    'security.protocol': 'SASL_SSL', 
    'sasl.mechanisms': 'SCRAM-SHA-256', 
    'sasl.username': 'yourUsername', 
    'sasl.password': 'yourPassword',  
    # any other config you like .. 
} 
 
p = Producer(**conf) 
  
# Rest of your code goes here..