python 实例权限认证连接 Kafka

背景:为保证kafka topic 生产和消费的安全,需增加权限认证功能

问题的解决:

            step1: kafka server增加权限认证配置: 链接

            step2: 代码实例中增加认证参数

 

1. 生产者

import time
from kafka import KafkaProducer

 

topic_name = ‘test_group_topic’

producer = KafkaProducer(
                         sasl_mechanism=”PLAIN”,
                         security_protocol=’SASL_PLAINTEXT’,
                         sasl_plain_username=”producer”,
                         sasl_plain_password=”prod-sec”,
                         bootstrap_servers=[‘120.78.163.200:9092’]
                         )

# 生产数据

for i in range(5):
    t = time.strftime(‘%Y%m%d%H%M%S’, time.gmtime())
    data = t + ‘—%d–test’ % i
    print(‘—%d—‘ % i)
    producer.send(topic_name, data.encode())

producer.close()

 

2. 消费者

from kafka import KafkaConsumer

topic_name = ‘test_group_topic’

consumer = KafkaConsumer(topic_name,
                         # group_id=’test_id’,                   # 同名组多个消费者消费同一个topic
                         # enable_auto_commit=True,
                         # auto_commit_interval_ms=2,
                         sasl_mechanism=”PLAIN”,
                         security_protocol=’SASL_PLAINTEXT’,
                         sasl_plain_username=”consumer”,
                         sasl_plain_password=”cons-sec”,
                         bootstrap_servers=[‘120.78.163.200:9092’],
                         )

for msg in consumer:
    print(“%s:%d:%d: key=%s value=%s” % (msg.topic, msg.partition, msg.offset, msg.key, msg.value.decode()))

    原文作者:梅山学子
    原文地址: https://blog.csdn.net/LeonTom/article/details/82797198
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞