简体   繁体   English

使用来自 Kafka 主题的消息 - 无响应

[英]Consume messages from Kafka Topic - no response

var configs = new Dictionary<string, string>
{
    {"bootstrap.servers", MY_SERVER},
    {"security.protocol", "SASL_PLAINTEXT"},
    {"sasl.mechanism", "SCRAM-SHA-256"},
    {"sasl.username", "MY_USERNAME"},
    {"sasl.password", "MY_PWD"},
    {"group.id", "sample_group"} // added
};
var consumerConfig = new ConsumerConfig(configs);    

using (var schemaRegistry = new CachedSchemaRegistryClient(schemaRegistryConfig))
using (var consumer = new ConsumerBuilder<string, MyModel>(consumerConfig)
           .SetKeyDeserializer(new AvroDeserializer<string>(schemaRegistry, avroSerializerConfig).AsSyncOverAsync())
           .SetValueDeserializer(new AvroDeserializer<MyModel>(schemaRegistry, avroSerializerConfig).AsSyncOverAsync())
           .Build())
{
      consumer.Subscribe(TOPIC_NAME);

      while (true)
      {
          var result = consumer.Consume(); //stuck here
          Console.WriteLine(result);
      }
 }

As stated in the code, there is no response coming from consumer.Consume() .如代码中所述, consumer.Consume()没有响应。 It does not throw any error message even during consumer.Subscribe() What will be the possible reason?即使在consumer.Subscribe()期间它也不会抛出任何错误消息,可能的原因是什么? (I am new to Kafka Consumer) (我是 Kafka 消费者的新手)

  1. Maybe there is no message in Topic, so nothing to receive?也许Topic里没有消息,所以什么也收不到?
  2. The code asked for missing 'group.id', so I added {"group.id", "sample_group"} in config and wrap with ConsumerConfig .代码要求缺少“group.id”,所以我在配置中添加了{"group.id", "sample_group"}并用ConsumerConfig包装。 Is random name ("sample_group") allowed for group.id or should it be something retrieved from Topic information? group.id 是否允许使用随机名称(“sample_group”),还是应该从主题信息中检索?
  3. anything else?还要别的吗?

Your code looks fine and the fact that no errors and Exceptions are showing up is also a good sign.您的代码看起来不错,而且没有出现错误和异常的事实也是一个好兆头。

"1. Maybe there is no message in Topic, so nothing to receive?" “1. 可能Topic里没有消息,所以什么都收不到?”

Even if there are no messages in the Kafka topic, your observation matches the expected behavior.即使 Kafka 主题中没有消息,您的观察也符合预期的行为。 In the while(true) loop you are continuously trying to fetch data from the topic and if nothing can be fetched the consumer will try again in the next iteration.while(true)循环中,您不断尝试从主题中获取数据,如果无法获取任何数据,则消费者将在下一次迭代中再次尝试。 Consumers on Kafka topics are meant to read a topic sequentially while running continuously. Kafka 主题上的消费者旨在在连续运行的同时顺序读取主题。 It is totally fine that some times the consumers has consumed all messages and stays idle for some time until new message arrive into the topic.有时消费者已经消费了所有消息并保持空闲一段时间直到新消息到达主题是完全没问题的。 During the waiting time the consumer will not stop or crash.在等待时间内,消费者不会停止或崩溃。

Keep in mind that messages in a Kafka topic have by default a retention period of 7 days.请记住,Kafka 主题中的消息默认保留 7 天。 After that time, the messages will be deleted.在那之后,消息将被删除。

"2. The code asked for missing 'group.id', so I added {"group.id", "sample_group"} in config and wrap with ConsumerConfig. Is random name ("sample_group") allowed for group.id or should it be something retrieved from Topic information?" “2. 代码要求缺少 'group.id',所以我在配置中添加了 {"group.id", "sample_group"} 并用 ConsumerConfig 包装。是否允许 group.id 使用随机名称(“sample_group”)或应该它是从主题信息中检索到的东西吗?”

Yes, the name "sample_group" is allowed as a ConsumerGroup name.是的,名称“sample_group”可以作为 ConsumerGroup 的名称。 There are no reserved consumer group names so this name will not cause any trouble.没有保留的消费者组名称,因此该名称不会造成任何麻烦。

"3. anything else?" “3、还有什么事吗?”

By default, a KafkaConsumer reads the messages from "latest" offset.默认情况下,KafkaConsumer 从“最新”偏移量读取消息。 That means, if you run a ConsumerGroup for the very first time it will not read all messages from beginning but rather from end.这意味着,如果您第一次运行 ConsumerGroup,它不会从头而是从尾读取所有消息。 Check the consumer configurations in the .net Kafka-API documentation for something like auto_offset_reset .检查 .net Kafka-API 文档中的消费者配置以了解auto_offset_reset类的auto_offset_reset You might set this configuration to "earliest" in case you want to read all messages from beginning.如果您想从头开始阅读所有消息,您可以将此配置设置为“最早”。 Please note, that as soon as you run your application with a given ConsumerGroup for the first time, the second time you run this application this configuration auto_offset_reset will not have any impact because the ConsumerGroup is now registered within Kafka.请注意,第一次使用给定的 ConsumerGroup 运行应用程序时,第二次运行此应用程序时,此配置auto_offset_reset不会产生任何影响,因为 ConsumerGroup 现在已在 Kafka 中注册。

What you can usually do, to ensure that the consumer should actually read messages is if you start your consumer before you start producing messages to that topic.您通常可以做的是确保消费者应该实际阅读消息,如果您开始为该主题生成消息之前启动您的消费者。 Then, (almost) independent of your configuration you should see data flowing through your application.然后,(几乎)独立于您的配置,您应该会看到数据流经您的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM