Kafka Consumer Commit 我自己的偏移量 ID 不起作用 - commitSync(最终 Map<TopicPartition, OffsetAndMetadata> 偏移量)

Kakfa Consumer Commit on my own offset id not working - commitSync(final Map<TopicPartition, OffsetAndMetadata> offsets)

我正在使用 poll() 从主题中获取一堆消息(比如说 100 条)。我在我的配置中将自动提交设置为 false 并将 max.poll.records 设置为 100。

我将使用我收到的 100 条消息中的 10 条消息进行处理。因此,我希望我收到的剩余 90 条消息成为下一个后续民意调查响应的一部分。

因此我提取了第 10 个消息偏移量并将其作为参数传递给 commitSync() API,如下所示。

consumer.commitSync(Collections.singletonMap(
                            new TopicPartition(message.topic(), message.partition()), 
                            new OffsetAndMetadata(message.offset() + 1, message.leaderEpoch(), "")));

但是,在下一次后续投票中,我收到的消息是从第 101 条消息开始的,而不是从第 11 条消息开始的。

请帮助我在这里做错了什么。 我不想在这里使用 seek() 方法。

我的 Class 文件

public class KafkaMessageConsumerImpl implements IKafkaConsumer {


    private Logger logger = LoggerFactory.getLogger(KafkaMessageConsumerImpl.class);

    private final String appGroupName;

    private Map<String, String> configs;

    private KafkaConsumer<String, Message> consumer;

    private final static int  maxFetchRecords = 100;


    @Override
    public List<Message> poll(int maxMessages)throws CommunicationException {

        int totalMesssages = 0;
        try {
            logger.debug("going poll invoked with request count "+maxMessages);
            final ConsumerRecords<String, Message> messages = consumer.poll(Duration.ofMillis(Long.MAX_VALUE));
            logger.debug("poll completed with count: " +messages.count());

            final List<Message> _messages = new ArrayList<>();
            for (ConsumerRecord<String, Message> message : messages) {
                logger.debug("Received" + message.toString());

                String actorName = message.value().getMessageDetail().getActorName();
                if(actorName.equals(this.actor)) {
                    _messages.add(message.value());
                    totalMesssages++;
                }

                if(totalMesssages == maxMessages) {

                    /**
                     * commitSync is not working :(
                     */
                    consumer.commitSync(Collections.singletonMap(
                            new TopicPartition(message.topic(), message.partition()), 
                            new OffsetAndMetadata(message.offset() + 1, message.leaderEpoch(), "")));
                    break;
                }
            }
            return _messages;

        } catch (Throwable e) {
            logger.error("issue while fetching data from Kafka", e);
            throw e;
        }
    }


    private KafkaConsumer<String, Message> createConsumer(final CommunicationConfig config, 
            final String actor) throws CommunicationException {

        Properties props = new Properties();

        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, configs.get("kafka.bootstrapServers"));

        props.put("group.id", this.appGroupName+"_"+actor);
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", "impl.MessageDeserializer");
        props.put("enable.auto.commit", false);
        props.put("max.poll.records", maxFetchRecords);

        logger.info("consumer group {} "+this.appGroupName+"_"+actor);
        KafkaConsumer<String, Message> consumer = new KafkaConsumer<String, Message>(props);

        consumer.subscribe(Arrays.asList(this.configs.get("TOPIC_NAME")));
        return consumer;
    }

}

日志文件:-

[Thread-5] INFO org.apache.kafka.clients.consumer.ConsumerConfig - ConsumerConfig values: 
    auto.commit.interval.ms = 5000
    auto.offset.reset = latest
    bootstrap.servers = [192.168.112.219:9092]
    check.crcs = true
    client.dns.lookup = default
    client.id = 
    connections.max.idle.ms = 540000
    default.api.timeout.ms = 60000
    enable.auto.commit = false
    exclude.internal.topics = true
    fetch.max.bytes = 52428800
    fetch.max.wait.ms = 500
    fetch.min.bytes = 1
    group.id = harisurya221_dd
    heartbeat.interval.ms = 3000
    interceptor.classes = []
    internal.leave.group.on.close = true
    isolation.level = read_uncommitted
    key.deserializer = class org.apache.kafka.common.serialization.StringDeserializer
    max.partition.fetch.bytes = 1048576
    max.poll.interval.ms = 300000
    max.poll.records = 100
    metadata.max.age.ms = 300000
    metric.reporters = []
    metrics.num.samples = 2
    metrics.recording.level = INFO
    metrics.sample.window.ms = 30000
    partition.assignment.strategy = [class org.apache.kafka.clients.consumer.RangeAssignor]
    receive.buffer.bytes = 65536
    reconnect.backoff.max.ms = 1000
    reconnect.backoff.ms = 50
    request.timeout.ms = 30000
    retry.backoff.ms = 100
    sasl.client.callback.handler.class = null
    sasl.jaas.config = null
    sasl.kerberos.kinit.cmd = /usr/bin/kinit
    sasl.kerberos.min.time.before.relogin = 60000
    sasl.kerberos.service.name = null
    sasl.kerberos.ticket.renew.jitter = 0.05
    sasl.kerberos.ticket.renew.window.factor = 0.8
    sasl.login.callback.handler.class = null
    sasl.login.class = null
    sasl.login.refresh.buffer.seconds = 300
    sasl.login.refresh.min.period.seconds = 60
    sasl.login.refresh.window.factor = 0.8
    sasl.login.refresh.window.jitter = 0.05
    sasl.mechanism = GSSAPI
    security.protocol = PLAINTEXT
    send.buffer.bytes = 131072
    session.timeout.ms = 10000
    ssl.cipher.suites = null
    ssl.enabled.protocols = [TLSv1.2, TLSv1.1, TLSv1]
    ssl.endpoint.identification.algorithm = https
    ssl.key.password = null
    ssl.keymanager.algorithm = SunX509
    ssl.keystore.location = null
    ssl.keystore.password = null
    ssl.keystore.type = JKS
    ssl.protocol = TLS
    ssl.provider = null
    ssl.secure.random.implementation = null
    ssl.trustmanager.algorithm = PKIX
    ssl.truststore.location = null
    ssl.truststore.password = null
    ssl.truststore.type = JKS
    value.deserializer = impl.MessageDeserializer

[Thread-5] INFO org.apache.kafka.common.utils.AppInfoParser - Kafka version: 2.2.1
[Thread-5] INFO org.apache.kafka.common.utils.AppInfoParser - Kafka commitId: 55783d3133a5a49a

Kafka 为消费者位置和提交的偏移量维护 2 个指针。

提交偏移量与当前位置无关。

如果你关闭消费者并重新打开,你会得到你想要的结果。

如果想从11号位置重新fetch,不关闭consumer,必须执行seek操作重新设置位置。