python kafka权限校验client.id

kafka集群有权限校验,在连接时需要加入client.id。但pykafka不能配置该选项。搜索了一下,需要使用confluent-kafka

链接: https://blog.csdn.net/lanyang123456/article/details/80639625

#coding:utf-8

from confluent_kafka import Consumer, KafkaError

mybroker = "127.0.0.1:9092"  #host
client_id = "校验id"
my_topic = "你的topic"

c = Consumer({
    'bootstrap.servers': mybroker,
    'group.id': 'mygroup',
    'client.id': client_id ,
    'default.topic.config': {
    'auto.offset.reset': 'smallest'
    }
})

c.subscribe([my_topic])

while True:
    msg = c.poll(1.0)

    if msg is None:
        continue
    if msg.error():
        if msg.error().code() == KafkaError._PARTITION_EOF:
            continue
        else:
            print(msg.error())
            break

    print('Received message: {}'.format(msg.value().decode('utf-8')))

c.close()

clientid是接入kafka集群的client的身份标志(申请填写单业务申请单个,多个topic可以共用一个, 由于kafka集群会通过clientid对带宽速率限制,请谨慎填写)

 

 

posted on 2021-06-29 17:37  ExplorerMan  阅读(652)  评论(0编辑  收藏  举报

导航