简体   繁体   English

卡夫卡消费者没有消费消息

[英]Kafka consumer is not consuming message

I am new in Kafka.我是卡夫卡的新手。 kafka consumer is not reading message from the given topic. kafka 消费者没有从给定主题读取消息。 I am checking with kafka console as well.我也在检查 kafka 控制台。 it is not working.它不工作。 i donot understand the problem.我不明白这个问题。 it was working fine earlier.它早些时候工作正常。

public string MessageConsumer(string brokerList, List<string> topics, CancellationToken cancellationToken)
    {

        //ConfigurationManager.AutoLoadAppSettings("", "", true);
        string logKey = string.Format("ARIConsumer.StartPRoducer ==>Topics {0} Key{1} =>", "", string.Join(",", topics));

        string message = string.Empty;
        var conf = new ConsumerConfig
        {
            BootstrapServers = "localhost:9092",
            GroupId = "23",
            EnableAutoCommit = false,                
            AutoOffsetReset = AutoOffsetResetType.Latest,
        };

        using (var c = new Consumer<Ignore, string>(conf))
        {
            try
            {
                c.Subscribe(topics);
                bool consuming = true;
                // The client will automatically recover from non-fatal errors. You typically
                // don't need to take any action unless an error is marked as fatal.
                c.OnError += (_, e) => consuming = !e.IsFatal;
                while (consuming)
                {
                    try
                    {
                        TimeSpan timeSpan = new TimeSpan(0, 0, 5);

                        var cr = c.Consume(timeSpan);
                        // Thread.Sleep(5000);
                        if (cr != null)
                        {
                            message = cr.Value;
                            Console.WriteLine("Thread" + Thread.CurrentThread.ManagedThreadId + "Message : " + message);

                            CLogger.WriteLog(ELogLevel.INFO, $"Consumed message Partition '{cr.Partition}' at: '{cr.TopicPartitionOffset} thread: { Thread.CurrentThread.ManagedThreadId}'. Message: {message}");
                            //Console.WriteLine($"Consumed message Partition '{cr.Partition}' at: '{cr.TopicPartitionOffset}'. Topic: { cr.Topic} value :{cr.Value} Timestamp :{DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture)} GrpId: { conf.GroupId}");
                            c.Commit();
                        }
                        Console.WriteLine($"Calling the next Poll ");
                    }

                    catch (ConsumeException e)
                    {
                        CLogger.WriteLog(ELogLevel.ERROR, $"Error occured: {e.Error.Reason}");

                        Console.WriteLine($"Error occured: {e.Error.Reason}");
                    }
                    //consuming = false;
                }
                // Ensure the consumer leaves the group cleanly and final offsets are committed.
                c.Close();
            }
            catch (Exception ex)
            {

            }
        }

        return message;
    }

What is the issue with this code or there is installation issue with kafka此代码有什么问题,或者 kafka 存在安装问题

Is there a Producer actively sending data?是否有生产者主动发送数据?

Your consumer is starting from the latest offsets based on the AutoOffsetReset, so it wouldn't read existing data in the topic您的消费者从基于 AutoOffsetReset 的最新偏移量开始,因此它不会读取主题中的现有数据

The console consumer also defaults to the latest offset控制台消费者也默认使用最新的偏移量

And if you haven't changed the GroupId, then your consumer might have worked once, then you consumed data, then commited the offsets for that group.如果您没有更改 GroupId,那么您的消费者可能已经工作过一次,然后您消费了数据,然后提交了该组的偏移量。 When the consumer starts again in the same group, it will only resume from the end of the topic, or the offset of the last commit当consumer在同一个组中再次启动时,只会从topic的末尾,或者最后一次commit的offset处恢复

You also have an empty catch (Exception ex) , which might be hiding some other error您还有一个空的catch (Exception ex) ,它可能隐藏了一些其他错误

尝试从消费方法中删除时间跨度。

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

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