DevOps | Cloud | Analytics | Open Source | Programming





How To Fix Kafka Python Error - "NoBrokersAvailable"



In this post , we will try to find How to Fix Kafka Python Error - "NoBrokersAvailable". While using Kafka with python, at times at gives the below error -


raise Errors.NoBrokersAvailable()
kafka.errors.NoBrokersAvailable: NoBrokersAvailable

It can occur in Any of the below instances while using the "kafka" package in python

Case-1



from kafka import KafkaProducer
producer = KafkaProducer(bootstrap_servers=['<KAFKA_BROKER_HOST>:<KAFKA_BROKER_IP>'], <OTHER_CONFIG_PARMS>)

Case-2




from kafka import KafkaConsumer
consumer = KafkaConsumer(bootstrap_servers='<KAFKA_BROKER_HOST>:<KAFKA_BROKER_IP>', <OTHER_CONFIG_PARMS>)

First thing first, you need to check if the Kafka Broker Host & Ip used in the "bootstrap_servers" are correct . Many a times , due to changing network , the Host & Ip might be different (case of a Public or Static IP thing) every time you boot your application.  

  • Ensure and Add all your Kafka bootstrap servers in the server.properties file -

config. listeners=PLAINTEXT://<Broker_ip:<Port>
advertised.listeners=PLAINTEXT://<Broker_ip:<Port>

  • For localhost broker , you could also use hardcoded value in api_version=(0, 10, 1) . BUT this is Not a RIGHT way of doing . This simply bypasses the TCP socket checks and hence there is high chances of the issue resurfacing.
Case-1



from kafka import KafkaProducer

producer = KafkaProducer(bootstrap_servers=['localhost:ip'], 
           api_version=(0, 10, 1) , <OTHER_CONFIG_PARMS>)


Case-2




from kafka import KafkaConsumer

producer = KafkaConsumer(bootstrap_servers=['localhost:ip'],
           api_version=(0, 10, 1) , <OTHER_CONFIG_PARMS>)

Hope this helps to solve the issue.  

Other Interesting Reads -


kafka nobrokersavailable , nobrokersavailable kafka python , kafka python nobrokersavailable