我正在尝试为manual offset commit
上收到的消息实现kafka
。我已将偏移量提交设置为false
,但偏移量值仍在不断增加。
不确定是什么原因。需要解决此问题的帮助。
下面是代码
application.yml
spring:
application:
name: kafka-consumer-sample
resources:
cache:
period: 60m
kafka:
bootstrapServers: localhost:9092
options:
enable:
auto:
commit: false
KafkaConfig.java
@Bean
public ConsumerFactory<String, String> consumerFactory() {
Map<String, Object> config = new HashMap<>();
config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
config.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
config.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
return new DefaultKafkaConsumerFactory<>(config);
}
@Bean
public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory();
factory.setConsumerFactory(consumerFactory());
return factory;
}
KafkaConsumer.java
@Service
public class KafkaConsumer {
@KafkaListener(topics = "#{'${kafka-consumer.topics}'.split(',')}", groupId = "${kafka-consumer.groupId}")
public void consume(ConsumerRecord<String, String> record) {
System.out.println("Consumed Kafka Record: " + record);
record.timestampType();
System.out.println("record.timestamp() = " + record.timestamp());
System.out.println("***********************************");
System.out.println(record.timestamp());
System.out.println("record.key() = " + record.key());
System.out.println("Consumed String Message : " + record.value());
}
}
输出如下
Consumed Kafka Record: ConsumerRecord(topic = test, partition = 0, offset = 31, CreateTime = 1573570989565, serialized key size = -1, serialized value size = 2, headers = RecordHeaders(headers = [], isReadOnly = false), key = null, value = 10)
record.timestamp() = 1573570989565
***********************************
1573570989565
record.key() = null
Consumed String Message : 10
Consumed Kafka Record: ConsumerRecord(topic = test, partition = 0, offset = 32, CreateTime = 1573570991535, serialized key size = -1, serialized value size = 2, headers = RecordHeaders(headers = [], isReadOnly = false), key = null, value = 11)
record.timestamp() = 1573570991535
***********************************
1573570991535
record.key() = null
Consumed String Message : 11
属性如下。
auto.commit.interval.ms = 100000000
auto.offset.reset =最早 bootstrap.servers = [localhost:9092]
check.crcs = true
connections.max.idle.ms = 540000 enable.auto.commit =否 exclude.internal.topics = true
fetch.max.bytes = 52428800 fetch.max.wait.ms = 500
fetch.min.bytes = 1
group.id =我的群组 heartbeat.interval.ms = 3000
这是在重新启动使用者之后。我希望也能打印出较早的数据。
我的理解正确吗? 请注意,我正在重新启动springboot应用程序,希望消息从头开始。而且我的kafka服务器和zookeeper都没有终止。
答案 0 :(得分:2)
如果通过使用此属性auto
禁用了ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG
确认,则必须将容器级别的确认模式设置为MANUAL
,并且不要提交{{1} },因为默认情况下它设置为offset
BATCH.
因为禁用了自动确认container级别确认设置为@Bean
public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory();
factory.setConsumerFactory(consumerFactory());
factory.getContainerProperties().setAckMode(AbstractMessageListenerContainer.AckMode.MANUAL);
return factory;
}
公共无效setAckMode(ContainerProperties.AckMode ackMode)
设置自动确认(在配置属性中)为false时使用的确认模式。
参数:
ackMode-ContainerProperties.AckMode;默认的BATCH。
提供了多个用于提交偏移量的选项。如果enable.auto.commit消费者属性为true,则Kafka根据其配置自动提交偏移量。 如果为false,则容器支持几种AckMode设置(在下面的列表中描述)。默认的AckMode是BATCH。。从2.3版开始,除非在配置中明确设置,否则框架会将enable.auto.commit设置为false。以前,如果未设置该属性,则使用Kafka默认值(true)。
如果您想始终从头开始阅读,则必须将此属性BATCH
设置为auto.offset.reset
earliest
注意::请确保config.put(ConsumerConfig. AUTO_OFFSET_RESET_CONFIG, "earliest");
必须是新的,并且在kafka中没有任何偏移量