Kafka Producer.Net Core Dockerized throws exception Local: Message timed out

Viewed 592

on my windows machine I have a kafka container that i created running this command :

docker run -d --network kafka --name=MyKafka -p 9092:9092 -p 9094:9094 
-e ALLOW_PLAINTEXT_LISTENER=yes -e KAFKA_ZOOKEEPER_CONNECT=MyZookeeper:2181 
-e KAFKA_LISTENERS=INTERNAL://:9094,EXTERNAL://:9092 
-e KAFKA_ADVERTISED_LISTENERS=INTERNAL://kafka:9094,EXTERNAL://localhost:9092 
-e KAFKA_LISTENER_SECURITY_PROTOCOL_MAP=INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT 
-e KAFKA_INTER_BROKER_LISTENER_NAME=EXTERNAL 
confluentinc/cp-kafka:5.5.0 

And a zookeeper container created running this command :

docker run -d --network kafka --name=MyZookeeper -p 2181:2181 -e ALLOW_ANONYMOUS_LOGIN=yes -e ZOOKEEPER_CLIENT_PORT=2181 confluentinc/cp-zookeeper:5.5.0 

The docker compose file of my .net Core producer app is the following :

version: '1.0'

services:
  api:
    image: ${DOCKER_REGISTRY}cds.Producer.api
    ports:
      - "65200:65200"
      - "65201:65201"
    build:
      context: .
      dockerfile: src/Api/Dockerfile
    environment:
      - ASPNETCORE_URLS=http://+:65200
      - ASPNETCORE_HTTP_PORT=65200
      - MANAGEMENT_HTTP_PORT=65201
      - ASPNETCORE_ENVIRONMENT=Development
networks:
  default:
    external:
      name: kafka

On this app I'm using the nugget Confluent.Kafka and I'm running the following code :

            ProducerConfig conf = new ProducerConfig
            {
                BootstrapServers = "kafka:9094",
                MessageSendMaxRetries = 2,
                MessageTimeoutMs = 500
            };
            
            IProducer<string, string> producer ??= new ProducerBuilder<string, string>(conf).Build();
            
            try
            {
                Message<string, string> message = new Message<string, string> { Key = "testkey",  Value = "test message" };

                await producer.ProduceAsync("mytopic", message, cancellationToken);
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.ToString());
            }

For some reason I keep getting an exception of

Local: Message timed out

I tried localhost:9092 and it didn't work either.

1 Answers

You've defined a network, but not attached your service to it

You need to add a networks block to the API service

networks: 
  - kafka

I'd also suggest putting your broker and zookeeper in the compose file as well

Related