[英]Why is my ProducerBuilder class's message in .Net Core console not consumed by Kafka consumer shell?
我已经在 Ubuntu 上安装了 Kafka,并设法仅使用 shell 文件测试了一个简单的场景:
bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic mytopicname
其次是:
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic mytopicname --from-beginning
一切都好,一切正常。 现在,我想关闭生产者并在 asp.net 核心应用程序中编写生产者的代码。
using System;
using System.ComponentModel;
using System.Net;
using Confluent.Kafka;
namespace KafkaTraining
{
class Program
{
static void Main(string[] args)
{
var config = new ProducerConfig
{
BootstrapServers = "localhost:9092"
};
using (var producer = new ProducerBuilder<string, string>(config).Build())
{
producer.Produce("mytopicname", new Message<string, string> { Value = "a log message" });
}
Console.ReadLine();
}
}
}
在这里找到灵感:// https://docs.confluent.io/current/clients/dotnet.html#
So, the above C# code is supposed to be equivalent with the shell command, right, plus the message I wrote in C# that should be consumed by the consumer (the consumer is still open in a terminal window thanks to a shell command).
如果我之前发布的 shell 命令有效,即bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic mytopicname
仅包含与 localhost:9092(服务地址)和主题名称相关的附加信息,为什么 C# 程序不能成功替代它,它应该产生消息“日志消息”并且终端应该使用它,但没有。 我怎么能调试这个?
PS。 I have installed Kafka on Linux Ubuntu from this address: https://www.apache.org/dyn/closer.cgi?path=/kafka/2.6.0/kafka_2.12-2.6.0.tgz whereas in my Asp. Net Core 3.1 控制台应用程序,如您所见,我已经安装了 1.5.0 版,不确定这是否起作用,但不知道如何开始调试......谢谢任何指针。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<StartupObject>KafkaDemo.Program</StartupObject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Confluent.Kafka" Version="1.5.0" />
</ItemGroup>
</Project>
您的消息没有被生成,因为您在消息传递之前处置了生产者。 Produce
方法异步发送消息并且不等待响应 - 它立即返回。 为了确保它被发送,您可以使用Flush()
- 它会阻塞,直到所有正在进行的消息都被传递,或者您可以使用await ProduceAsync()
。
尝试这个:
using (var producer = new ProducerBuilder<string, string>(config).Build())
{
producer.Produce("mytopicname", new Message<string, string> { Value = "a log message" });
producer.Flush();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.