问题描述

配置

Kafka-client 2.x, Spring-Kafka

默认配置

Kafka 三个partition, 使用KafkaListener按group消费。

现象

某天突然发现两个partition出现了Lag堆积,并且一直没有下降。看业务日志有相同消息在不断重复消费。

分析

看日志发现一直在刷,某个partition的任务超过了max.poll.intervals.ms, reassign other partition.

 OffsetAndMetadata{offset=3107, metadata=''}} failed: Commit cannot be completed since the group has already rebalanced and assigned the partitions to another member. This means that the time between subsequent calls to poll() was longer than the configured max.poll.interval.ms, which typically implies that the poll loop is spending too much time message processing. You can address this either by increasing the session timeout or by reducing the maximum size of batches returned in poll() with max.poll.records.

思考应该是消息后续的任务执行时间太长了,导致超过了默认时间,kafka server认为任务失败,不断指定新的partition去做,而之前在规定时间外做完的已经不可以提交offset了。

修复

做异步,使用线程池,将consumer收到的消息打到线程池去做,放进去之后即可立刻提交offset。

hotfix之后lag很快消费掉了,恢复正常。

优化

在consumer中最好加入partition, offset 信息到日志中,有助排查问题。

可以在消费时计算Lag信息,提供异常报警。

使用线程池异步在重启时有风险,可能会丢失数据。最好还是加长max.poll.intervals.ms。

对于关键业务统计耗时,并且设置max.poll.intervals.ms时留有余力。

KafkaListener默认一次消费一条 也可以指定 max.poll.records。

在producer端send时指定key,可以确定消费端唯一性。

Demo

Producer

   @Resource
private KafkaTemplate kafkaTemplate; @SuppressWarnings("unchecked")
public void sendMessage(String topic, final String message) {
LogUtil.info(LogTypeEnum.APPLICATION, "sending message='{}' to topic='{}'", message, topic);
kafkaTemplate.send(topic, message);
}

Consumer

  @KafkaListener(topics = "${topic}", groupId = "groupId",
containerFactory = "kafkaListenerContainerFactory",
autoStartup = "${kafka.consumer.listen.auto.start}",
properties = "max.poll.records=1")
public void businessConsumer(ConsumerRecord<Integer, String> consumerRecord,
@Header(KafkaHeaders.CONSUMER) KafkaConsumer<String, String> consumer) {
LogUtil.info(LogTypeEnum.APPLICATION,
"business log, offset is {}, partition id is {}, received message key is {}, param is {}",
consumerRecord.offset(), consumerRecord.partition(), consumerRecord.key(),
consumerRecord.value());
checkLag(consumer);
 } private void checkLag(KafkaConsumer<String, String> consumer) {
final Set<TopicPartition> topicPartitions = consumer.assignment();
Map<TopicPartition, Long> tailOffsetMap = consumer.endOffsets(topicPartitions);
for (final TopicPartition topicPartition : topicPartitions) {
final long tailOffset = tailOffsetMap.get(topicPartition);
final long currentOffset = consumer.position(topicPartition);
if (tailOffset < currentOffset || (tailOffset - currentOffset) > 30) {
LogUtil.error(LogTypeEnum.APPLICATION,
"kafka consumer lag exception, current offset is {}, max offset is {}",
currentOffset, tailOffset);
}
}
}

Kafka 实践的更多相关文章

  1. php下kafka实践

    Kafka是一种高吞吐的分布式发布订阅消息系统 kafka安装和简单测试 安装kafka 下载 wget https://www-us.apache.org/dist/kafka/2.1.1/kafk ...

  2. Kafka实践1--Producer

    一.Kafka设计原理参考: http://blog.csdn.net/suifeng3051/article/details/48053965?locationNum=2 http://www.cn ...

  3. Kafka实践

    1. kafka发送方法 @Component@Import(KafkaAutoProperties.class)public class KafkaProducer { @Autowired pri ...

  4. Kafka实践、升级和新版本(0.10)特性预研

    本文来自于网易云社区 一.消息总线MQ和Kafka (挡在请求的第一线) 1. 几个应用场景 case a:上游系统往下游系统推送消息,而不关心处理结果: case b:一份新数据生成,需要实时保存到 ...

  5. python操作kafka实践

    1.先看最简单的场景,生产者生产消息,消费者接收消息,下面是生产者的简单代码. ------------------------------------------------------------ ...

  6. Kafka入门 --安装和简单实用

    一.安装Zookeeper 参考: Zookeeper的下载.安装和启动 Zookeeper 集群搭建--单机伪分布式集群 二.下载Kafka 进入http://kafka.apache.org/do ...

  7. 02_Kafka单节点实践

    1.实践场景 开始前的准备条件: 1) 确认各个节点的jdk版本,将jdk升级到和kafka配套的版本(解压既完成安装,修改/etc/profile下的JAVA_HOME,source /etc/pr ...

  8. kafka消费者

    from kafka import KafkaConsumer,TopicPartition import json scrapy_kafka_hosts = ["ip:端口", ...

  9. 【AI】微软人工智能学习笔记(一)

    数据分析平台 01|数据平台概况图示 上面图中所示就是微软人工智能数据平台的相关的技术. 02.1| Cortana Intelligence Suite 从上面图中可以看到, 其中有一个Cortan ...

随机推荐

  1. asp.net 代码片段的

    片段标签                                                   说明 <%                                      ...

  2. vue项目在IE浏览器和360兼容模式下页面不显示问题,亲测有效

    解决方法:安装 "babel-polyfill" 1.命令:cnpm install --save-dev babel-polyfill 2.在入口main.js文件引入:impo ...

  3. mysql 利用 case 批量更新

  4. Spring Cloud(八)高可用的分布式配置中心 Spring Cloud Config

    在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config,它支持配 ...

  5. sys和system用户的权限区别

    http://blog.sina.com.cn/s/blog_869b0f460100uckp.html Oracle sys和system用户.sysdba 和sysoper系统权限.sysdba和 ...

  6. ThinkPHP模板继承和修改title

    先说下模板继承: 假定:在View文件夹中 -> Public  公共模块 —>base/header/top/footer 4个html文件 这下面base文件使用include引入其他 ...

  7. 第五章、Django之多表查询进阶与事务

    目录 第五章.Django之多表查询 一.聚合查询 二.分组查询 三.F与Q查询 四.查询优化 五.Django开启事务 六.自定义char字段 七.ORM常用字段 第五章.Django之多表查询 一 ...

  8. 在php中连接数据库 pdo

    在php中连接数据库 pdo //数据库信息 $mysql_conf = array( 'host' => '127.0.0.1', 'db' => 'meteorolog_foreign ...

  9. RobHess的SIFT环境配置

    平台:win10 x64 +VS 2015专业版 +opencv-2.4.11 + gtk_-bundle_2.24.10_win32 参考博客:https://www.cnblogs.com/cql ...

  10. django的几种缓存配置

    前言 首先说,为什么要用缓存的,由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返回值保存 ...