0

I'm searching for a solution to use Kafka to display my API (localhost) to my docker.

My producer file works fine and here it is

Consumer file on the other hand returns this... I tried creating a new test container in powershell to debug, gave me nobrokersavailable

Producer:

from kafka import KafkaProducer
import json

producer = KafkaProducer(bootstrap_servers='localhost:9092', 
                         value_serializer=lambda v: json.dumps(v).encode('utf-8'))

with open('data/New York_0.json', 'r') as f:
    data = json.load(f)

producer.send('my_topic', data)
producer.flush()


Consumer:

from kafka import KafkaConsumer
import json
import traceback

# To consume latest messages and auto-commit offsets
try:
    consumer = KafkaConsumer('my_topic',
                             bootstrap_servers='localhost:9092',
                             value_deserializer=lambda m: json.loads(m.decode('utf-8')),
                             consumer_timeout_ms=6000,
                             enable_auto_commit=False)
    
    print("Consumer created, start consuming")
    for message in consumer:
        print(message.value)

except Exception as e:
    print(f"An error occurred: {e}")
    print(f"Exception Type: {type(e).__name__}")
    traceback.print_exc()
finally:
    print("Closing the consumer")
    consumer.close()

enter image description here

  • Created test container to run a test_producer.py file, gave me nobrokersavailable

  • disabled firewall when this happened, same thing

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Please [don't upload text as image](https://meta.stackoverflow.com/a/285557/13447). Edit your question to contain all the information in text form - consider to use the editor's formatting options. Also see [ask]. – Olaf Kock Jun 27 '23 at 09:59
  • Your code looks okay, other than the fact that consumer should be started before the producer. Your broker error would be solved with your docker setup. Firewall isn't relevant for localhost connections – OneCricketeer Jun 27 '23 at 12:15

0 Answers0