我是kafka的新手,我想尝试创建主题并将消息从.net应用程序发送到kafka。我正在使用kafka.net dll并成功创建主题使用此代码:
Uri uri = new Uri("http://localhost:9092");
string topic = "testkafka";
string payload = "test msg";
var sendMsg = new Thread(() =>
{
KafkaNet.Protocol.Message msg = new KafkaNet.Protocol.Message(payload);
var options = new KafkaOptions(uri);
var router = new KafkaNet.BrokerRouter(options);
var client = new Producer(router);
client.SendMessageAsync(topic, new List<KafkaNet.Protocol.Message> { msg }).Wait();
});
sendMsg.Start();
但是我看不到任何消息:bin / kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic testkafka --from-beginning
有人可以帮我举个例子吗?谢谢。
对于这两种操作,您都可以使用cofluent-kafka-dotnet
客户端。
为了以编程方式cofluent-kafka-dotnet
:
create a topic
为了产生消息:
static async Task CreateTopicAsync(string bootstrapServers, string topicName) {
using (var adminClient = new AdminClientBuilder(new AdminClientConfig { BootstrapServers = bootstrapServers }).Build()) {
try {
await adminClient.CreateTopicsAsync(new TopicSpecification[] {
new TopicSpecification { Name = 'myTopicName', ReplicationFactor = 1, NumPartitions = 1 } });
}
catch (CreateTopicsException e) {
Console.WriteLine($"An error occured creating topic {e.Results[0].Topic}: {e.Results[0].Error.Reason}");
}
}
}