You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Core Docker化Kafka Producer抛出Local: Message timed out异常求助

Troubleshooting the "Local: Message timed out" Kafka Producer Issue

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_LISTENERS INTERNAL entry to MyKafka:9094 (matches container name)
  • Changed KAFKA_INTER_BROKER_LISTENER_NAME to INTERNAL

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 kafka network (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:
    docker exec -it <your-producer-container-name> ping MyKafka
    
    If this works, the network connectivity is solid.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 13:57:42