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()))