簡體   English   中英

為什么Kafka消費者shell沒有消費我的ProducerBuilder類在.Net Core控制台中的消息?

[英]Why is my ProducerBuilder class's message in .Net Core console not consumed by Kafka consumer shell?

我已經在 Ubuntu 上安裝了 Kafka,並設法僅使用 shell 文件測試了一個簡單的場景:

  • 使用默認配置啟動 zookeeper
  • 使用默認配置啟動節點或代理
  • 創建了一個主題,為其命名,具有單個分區和復制因子 1,並將其與 Zookeeper 默認地址相關聯
  • 開了一個生產者和一個消費者:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM