I have a C# project using kafka-net (James Roland). I have a docker compose file which runs kafka on my laptop. I can produce and consume messages with the kafka instance from windows command line without issue.
When I try producing messages to the topic in kafka from the C# project I get this error
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:\Play\Kafka\CSharpKafka\Form1.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
The docker compose is
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"
The C# code is
/*
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();
The docker ps output which relates to the error message is
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
The commands I have used from powershell command line to prove the kafka instance is running are
Consuming Messages
docker exec -it kafka-server /opt/bitnami/kafka/bin/kafka-console-consumer.sh --topic chat-messages --from-beginning --bootstrap-server localhost:9092
Producing Messages
docker exec -it kafka-server /opt/bitnami/kafka/bin/kafka-console-producer.sh --topic chat-messages --bootstrap-server localhost:9092
Looking for some help in solving. Thanks in advance