我有一个使用kafka-net (James Roland)的c#项目。我有一个docker撰写文件运行kafka在我的笔记本电脑上。我可以在windows命令行中使用kafka实例生成和消费消息。
当我尝试从c#项目中生成kafka主题的消息时,我得到这个错误
UnresolvedHostnameException: Could not resolve the following hostname: c60e8e54a7ca
System.AggregateException
HResult=0x80131500
Message=One or more errors occurred.
Source=mscorlib
StackTrace:
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at CSharpKafka.Form1.<>c__DisplayClass2_0.<button1_Click>b__0() in D:PlayKafkaCSharpKafkaForm1.cs:line 50
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
This exception was originally thrown at this call stack:
KafkaNet.DefaultKafkaConnectionFactory.GetFirstAddress(string, KafkaNet.IKafkaLog)
KafkaNet.DefaultKafkaConnectionFactory.Resolve(System.Uri, KafkaNet.IKafkaLog)
KafkaNet.BrokerRouter.UpdateInternalMetadataCache.AnonymousMethod__d(KafkaNet.Protocol.Broker)
System.Linq.Enumerable.WhereSelectListIterator<TSource, TResult>.MoveNext()
KafkaNet.BrokerRouter.UpdateInternalMetadataCache(KafkaNet.Protocol.MetadataResponse)
KafkaNet.BrokerRouter.RefreshTopicMetadata(string[])
KafkaNet.BrokerRouter.GetTopicMetadata(string[])
KafkaNet.BrokerRouter.SelectBrokerRoute(string, byte[])
KafkaNet.Producer.ProduceAndSendBatchAsync.AnonymousMethod__2a(KafkaNet.TopicMessage)
System.Linq.Enumerable.WhereSelectEnumerableIterator<TSource, TResult>.MoveNext()
...
[Call Stack Truncated]
Inner Exception 1:
AggregateException: One or more errors occurred.
Inner Exception 2:
UnresolvedHostnameException: Could not resolve the following hostname: c60e8e54a7ca
docker compose是
version: '2'
networks:
app-tier:
driver: bridge
services:
zookeeper:
container_name: zookeeper-server
image: 'bitnami/zookeeper:latest'
environment:
- ALLOW_ANONYMOUS_LOGIN=yes
networks:
- app-tier
kafka:
container_name: kafka-server
image: 'bitnami/kafka:latest'
environment:
- ALLOW_PLAINTEXT_LISTENER=yes
- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper-server:2181
networks:
- app-tier
ports:
- "9092:9092"
c#代码是
/*
Adapted from https://www.c-sharpcorner.com/article/apache-kafka-net-application/
*/
Uri uri = new Uri("http://127.0.0.1:9092");
string topic = "chat-messages";
string payload = textBox1.Text;
var sendMessage = new Thread(() => {
KafkaNet.Protocol.Message
msg = new KafkaNet.Protocol.Message(payload);
var options = new KafkaOptions(uri);
var router = new BrokerRouter(options);
var client = new Producer(router);
client.SendMessageAsync(topic, new List<KafkaNet.Protocol.Message> { msg }).Wait();
});
sendMessage.Start();
与错误信息相关的docker ps输出是
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c60e8e54a7ca bitnami/kafka:latest "/opt/bitnami/script…" 16 minutes ago Up 16 minutes 0.0.0.0:9092->9092/tcp, :::9092->9092/tcp kafka-server
6e97e28bd530 bitnami/zookeeper:latest "/opt/bitnami/script…" 16 minutes ago Up 16 minutes 2181/tcp, 2888/tcp, 3888/tcp, 8080/tcp zookeeper-server
我从powershell命令行中使用的命令来证明kafka实例正在运行
消费消息
docker exec -it kafka-server /opt/bitnami/kafka/bin/kafka-console-consumer.sh --topic chat-messages --from-beginning --bootstrap-server localhost:9092
生产消息
docker exec -it kafka-server /opt/bitnami/kafka/bin/kafka-console-producer.sh --topic chat-messages --bootstrap-server localhost:9092
寻求解决问题的帮助。提前感谢
感谢@David Brabant在环境变量和docker图像信息页面方面为我指明了正确的方向,我现在已经开始工作了。
c#代码可以工作。唯一的区别是我在docker组合文件中添加了以下环境变量:
- KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://127.0.0.1:9092
- KAFKA_BROKER_ID=1
- KAFKA_CFG_LISTENERS=PLAINTEXT://:9092
- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
我还从原来的docker撰写文件中删除了一些我认为不需要的东西。
我的docker组合现在看起来像这样,我不需要改变任何其他东西。这些变化在网页https://hub.docker.com/r/bitnami/kafka/中有详细说明,向下滚动到"Kafka开发设置示例"一节。
version: '2'
services:
zookeeper:
container_name: zookeeper-server
image: 'bitnami/zookeeper:latest'
environment:
- ALLOW_ANONYMOUS_LOGIN=yes
kafka:
container_name: kafka-server
image: 'bitnami/kafka:latest'
environment:
- ALLOW_PLAINTEXT_LISTENER=yes
- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper-server:2181
- KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://127.0.0.1:9092
- KAFKA_BROKER_ID=1
- KAFKA_CFG_LISTENERS=PLAINTEXT://:9092
- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
depends_on:
- zookeeper
ports:
- "9092:9092"
我不得不承认我没有意识到内部设置会以这种方式影响连接。
我在c#代码中的连接URI是Uri uri = new Uri("http://127.0.0.1:9092");