Kafka问题记录

xxsdnol / 2023-08-10 / 原文

重复消费

可能导致的原因

  1. kafka默认一次消费的时长为5s

    解决:加快业务处理速度,可使用多线程,修改默认消费的时间

  2. 自动提交

    解决:如果第一点按上面要求无法解决,将自动提交修改为手动提交,即可解决
    kafka.consumer.enableauto-commit: false #这是将自动提交关闭
    kafka.lintener.ack-mode: manual  #使用ack提交需要设置这个属性

  3. 幂等性校验

    由于topic过多可能出现同一topic被消费多次,原因是Kafka默认的消费方式是从头开始(看个人设置), 使用数据库或者缓存记录消费的唯一ID。

首次定义topic后启动报错

Error while fetching metadata with correlation id 11 : {xxxx=UNKNOWN_TOPIC_OR_PARTITION}

原因:未开启topic自动创建,topic不存在

引用:https://stackoverflow.com/questions/36441768/how-to-create-topics-in-apache-kafka

set the property auto.create.topics.enable=true in your server.properties file, if you have multiple brokers do thee same for all the server.properties file and restart your kafka-server. But make sure you set the partitions for an appropriate number in the server.properties num.partitions=int, otherwise there will be a performance issue if you increase the partitions later.

  1. 第一种解决:修改kafka配置kafka/config/server.properties -> auto.create.topics.enable=true (默认false,修改为true),既为没有创建的topic由kafka自动创建

  2. 第二种解决:手动创建新的topic:  kafka-topic.sh --bootstrap-server 1.1.1.1:8080 --create --topic myTopic --replication-factor 1 --partitions 1