.NET Core Docker化Kafka Producer抛出Local: Message timed out异常求助
Let's break down the issues in your setup step by step — the root causes are mostly misconfigured Kafka listener settings and Docker network DNS mismatches:
1. Kafka Container Name vs. Advertised Listener Mismatch
First, your Kafka container is named MyKafka, but in the KAFKA_ADVERTISED_LISTENERS environment variable, you set the INTERNAL listener to kafka:9094. In Docker's custom network (kafka), containers are resolved by their container name as the DNS hostname. So kafka doesn't point to your Kafka container — MyKafka does. This is a critical DNS resolution issue that prevents your producer from reaching the broker.
2. Incorrect Inter-Broker Listener Configuration
You set KAFKA_INTER_BROKER_LISTENER_NAME=EXTERNAL, but this should use the INTERNAL listener. The inter-broker listener is for communication between Kafka brokers (even if you only have one, this setting still matters for proper internal routing). Using the EXTERNAL listener here can cause routing conflicts and timeouts.
3. Producer Bootstrap Servers Mismatch
Your .NET producer is using kafka:9094 as the bootstrap server, which again doesn't match your Kafka container's name (MyKafka). Even if you fixed the Kafka advertised listeners, this would still fail due to DNS resolution.
Fixed Kafka Run Command
Let's correct the Kafka container startup command first:
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://MyKafka:9094,EXTERNAL://localhost:9092 \ -e KAFKA_LISTENER_SECURITY_PROTOCOL_MAP=INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT \ -e KAFKA_INTER_BROKER_LISTENER_NAME=INTERNAL \ confluentinc/cp-kafka:5.5.0
Key changes:
- Updated
KAFKA_ADVERTISED_LISTENERSINTERNAL entry toMyKafka:9094(matches container name) - Changed
KAFKA_INTER_BROKER_LISTENER_NAMEtoINTERNAL
Fixed Producer Configuration
Update your .NET producer's BootstrapServers to use the correct container hostname (since the producer is in the same kafka Docker network):
ProducerConfig conf = new ProducerConfig { BootstrapServers = "MyKafka:9094", MessageSendMaxRetries = 2, MessageTimeoutMs = 500 };
Additional Validation Steps
- Delete existing containers first to start fresh:
docker stop MyKafka MyZookeeper docker rm MyKafka MyZookeeper - Recreate the ZooKeeper container with your original command, then run the corrected Kafka command.
- Verify the producer container is in the
kafkanetwork (your Docker Compose already sets this, which is good). - You can test DNS resolution from the producer container by exec'ing into it and pinging
MyKafka:
If this works, the network connectivity is solid.docker exec -it <your-producer-container-name> ping MyKafka
Why localhost:9092 Didn't Work
When your producer is running inside a Docker container, localhost refers to the container itself, not your Windows host machine. To use the EXTERNAL listener from inside the container, you'd need to use your Windows host's IP address instead of localhost — but using the INTERNAL listener (MyKafka:9094) is the cleaner, correct approach for containers on the same network.
内容的提问来源于stack exchange,提问作者Veoxer

