Docker Kafka w/ Python consumer

前端 未结 3 1732
醉梦人生
醉梦人生 2020-12-31 00:43

I am using dockerized Kafka and written one Kafka consumer program. It works perfectly when I run Kafka in docker and application at my local machine. But when I configured

3条回答
  •  青春惊慌失措
    2020-12-31 01:19

    In my case I wanted to access the Kafka Container from an external python client running locally (as a producer) and here is the combination of containers and python code that worked for me (Platform MAC OS and docker version 2.4.0):

    zookeeper container:

    docker run -d \
    -p 2181:2181 \
    --name=zookeeper \
    -e ZOOKEEPER_CLIENT_PORT=2181 \
    confluentinc/cp-zookeeper:5.2.3
    

    kafka container:

    docker run -d \ 
    -p 29092:29092 \ 
    -p 9092:9092 \ 
    --name=kafka \ 
    -e KAFKA_ZOOKEEPER_CONNECT=host.docker.internal:2181 \ 
    -e KAFKA_LISTENER_SECURITY_PROTOCOL_MAP=BROKER:PLAINTEXT,PLAINTEXT:PLAINTEXT \ 
    -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://kafka:29092,BROKER://localhost:9092 \ 
    -e KAFKA_INTER_BROKER_LISTENER_NAME=BROKER \ 
    -e KAFKA_BROKER_ID=1 \ 
    -e KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1 \ 
    -e KAFKA_CREATE_TOPICS="test:1:1" \ 
    confluentinc/cp-enterprise-kafka:5.2.3
    

    python client:

    from kafka import KafkaProducer
    import json
    producer = KafkaProducer(bootstrap_servers=['localhost:29092'],
    value_serializer=lambda v: json.dumps(v).encode('utf-8'),
    security_protocol='PLAINTEXT')
    acc_ini = 523416
    print("Sending message")
    producer.send('test', {'model_id': '1','acc':str(acc_ini), 'content':'test'})
    producer.flush()
    

提交回复
热议问题