看一看,想一想,学一学,再想一想!

KafKa消费开发

 KafKa消费开发配置

以下代码需要写完整,不完整会出现中断,假死现象,长时间不处理问题。(实际项目代码)

        
        /// <summary>
        ///         - offsets 是自动提交的。
        ///         - consumer.Poll / OnMessage 是用于消息消费的。
        ///         - 没有为轮询循环创建(Poll)二外的线程,当然可以创建
        /// </summary>
        public static void Run_Poll(string brokerList, List<string> topics)
        {
            var config = new ConsumerConfig
            {
                BootstrapServers = brokerList,
                GroupId = $"Consumer_{topics[0]}_test", //_{topics[0]}  //修改此值会导致数据重新计算
                EnableAutoCommit = false, // 禁止AutoCommit
                //Acks = Acks.Leader, // 假设只需要Leader响应即可
                AutoOffsetReset = AutoOffsetReset.Earliest, // 从最早的开始消费起
                StatisticsIntervalMs=5000,// 5秒内无数据输出侦听中
                SessionTimeoutMs=6000,
                //EnablePartitionEof=true
            };

            try
            {
                using (var consumer = new ConsumerBuilder<Ignore, string>(config).SetErrorHandler((_,e)=> { 
                    LogInfo($"ErrorHandler:{e.Reason}");
                    Console.WriteLine($"ErrorHandler:{e.Reason}");
                }).SetStatisticsHandler((_, json) => {
                    //LogInfo($"{DateTime.Now}  消息监听中...");
                    Console.WriteLine($"{DateTime.Now}  消息监听中...");
                }).Build())
                {
                    consumer.Subscribe(topics);
                    try
                    {
                        while (true)
                        {

                            Stopwatch swIshttp = new Stopwatch();
                            swIshttp.Start();

                            try
                            {
                                System.Threading.CancellationToken cancellationToken = default;
                                var consumeResult = consumer.Consume(cancellationToken);
                                bool returnBl = true;
                                if (consumeResult.Message != null)
                                {
                                    returnBl = SenMsg("", consumeResult.Offset.ToString(), consumeResult.Topic, consumeResult.Message.Value, consumeResult.Timestamp.UtcDateTime);
                                }
                                //Console.WriteLine($"Consumed message '{consumeResult.Message?.Value}' at: '{consumeResult?.TopicPartitionOffset}'.");
                                if (consumeResult.IsPartitionEOF)
                                {
                                    Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 已经到底了:{consumeResult.Topic}, partition {consumeResult.Partition}, offset {consumeResult.Offset}.");
                                    Thread.Sleep(1000);
                                    continue;
                                }

                                if (returnBl)//处理完成,做标记
                                {
                                    try
                                    {
                                        consumer.Commit(consumeResult);
                                    }
                                    catch (KafkaException e)
                                    {
                                        LogInfo($"Commit 处理完成,做标记失败 : {e.Message}--{e.StackTrace}");
                                        //Console.WriteLine(e.Message);
                                    }
                                }
                            }
                            catch (ConsumeException e)
                            {
                                Console.WriteLine($"Consume error: {e.Error.Reason}");
                                LogInfo($"Consume error: {e.Error.Reason}");
                            }
                            swIshttp.Stop();
                            if (swIshttp.ElapsedMilliseconds > ElapsedMillisecondsMax)
                            {
                               // Console.WriteLine($"consumer.Consume 用时过长 {swIshttp.ElapsedMilliseconds}");
                            }
                        }
                    }
                    catch (OperationCanceledException ee)
                    {
                        Console.WriteLine("Closing consumer.");
                        LogInfo($"Closing consumer.: {ee.Message}--{ee.StackTrace}");
                        consumer.Close();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Run_Poll ERR.{ex.Message}");
                        LogInfo($"Run_Poll .: {ex.Message}--{ex.StackTrace}");
                    }
                }
            }catch(Exception ex)
            {
                Console.WriteLine($"Run_Poll 处理失败.{ex.Message}");
                LogInfo($"Run_Poll 处理失败  {ex.Message}  {ex.StackTrace} "); 
            }
        }

  

posted on 2023-06-06 08:47  JCLiang  阅读(28)  评论(0编辑  收藏  举报

导航