【发布时间】:2021-04-21 15:46:42
【问题描述】:
在我的 Windows 机器上,我创建了一个运行此命令的 kafka 容器:
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
并创建了一个运行此命令的 zookeeper 容器:
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
我的 .net Core producer 应用程序的 docker compose 文件如下:
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
在这个应用程序上,我正在使用块 Confluent.Kafka 并且我正在运行以下代码:
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());
}
由于某种原因,我不断收到异常
Local: Message timed out
我试过localhost:9092,也没用。
【问题讨论】:
标签: docker .net-core apache-kafka