Spring boot 启动spring boot Kafka项目时出错
启动spring boot kafka项目时获取错误 弹簧靴:2.1.2.1释放 春季卡夫卡版本:2.2.5.0发布 无法为ackMode手动\u立即自动提交配置使用者 消费者配置Spring boot 启动spring boot Kafka项目时出错,spring-boot,kafka-consumer-api,spring-kafka,Spring Boot,Kafka Consumer Api,Spring Kafka,启动spring boot kafka项目时获取错误 弹簧靴:2.1.2.1释放 春季卡夫卡版本:2.2.5.0发布 无法为ackMode手动\u立即自动提交配置使用者 消费者配置 @Bean public Map<String, Object> consumerConfigs() { Map<String, Object> props = new HashMap<String, Object>(); props.put(ConsumerCon
@Bean
public Map<String, Object> consumerConfigs() {
Map<String, Object> props = new HashMap<String, Object>();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer);
props.put(ConsumerConfig.GROUP_ID_CONFIG, consumerGroup);
props.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, 30000);
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
return props;
}
@Bean
公共地图使用者配置(){
Map props=newhashmap();
put(ConsumerConfig.BOOTSTRAP\u SERVERS\u CONFIG,bootstrapServer);
props.put(ConsumerConfig.GROUP\u ID\u CONFIG,consumerGroup);
props.put(ConsumerConfig.SESSION\u TIMEOUT\u MS\u CONFIG,30000);
put(ConsumerConfig.KEY\u反序列化程序\u类\u配置,StringDeserializer.CLASS);
put(ConsumerConfig.VALUE\u反序列化程序\u类\u配置,StringDeserializer.CLASS);
返回道具;
}
卡夫卡侦听器容器厂
@Bean("kafkaListenerContainerFactory")
public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory(RetryTemplate retryTemplate) {
ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());
factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.MANUAL_IMMEDIATE);
factory.setRetryTemplate(retryTemplate);
factory.setRecoveryCallback(context -> {
log.error("Maximum retry policy has been reached {}", context.getAttribute("record"));
Acknowledgment ack = (Acknowledgment) context.getAttribute(RetryingMessageListenerAdapter.CONTEXT_ACKNOWLEDGMENT);
ack.acknowledge();
return null;
});
factory.setConcurrency(Integer.parseInt(kafkaConcurrency));
return factory;
}
@Bean(“kafkaListenerContainerFactory”)
公共ConcurrentKafkaListenerContainerFactory kafkaListenerContainerFactory(RetryTemplate RetryTemplate){
ConcurrentKafkListenerContainerFactory=新ConcurrentKafkListenerContainerFactory();
setConsumerFactory(consumerFactory());
factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.MANUAL\u立即);
factory.setRetryTemplate(retryTemplate);
factory.setRecoveryCallback(上下文->{
log.error(“已达到最大重试策略{}”,context.getAttribute(“记录”);
确认确认=(确认)context.getAttribute(RetryingMessageListeneraAdapter.context\u确认);
确认();
返回null;
});
setConcurrency(Integer.parseInt(kafkanconcurrency));
返回工厂;
}
对于手动\u立即
确认模式(对于任何手动模式,基本上),必须关闭消费者配置.ENABLE\u AUTO\u COMMIT\u CONFIG
消费者属性
这就是那个例外的原因
我想你可以不在你的
应用程序中使用spring.kafka.consumer.enableAutoCommit
。properties
异常导致:java.lang.IllegalStateException:consumer无法配置为ackMode的自动提交手动\u立即将收到相同的错误..成功了,谢谢我更新了我的consumer配置..非常感谢分享一个简单的项目,让我们复制和玩?是的,请分享我想知道重试配置和确认是如何处理的..不,你分享你关于自动提交的问题。重试和确认是完全不同的故事,应该有自己的SO线程。谢谢你的理解!这一部分已经完成了。我还有一个疑问,我正在读一本书,在这本书中,当您成功地使用消息时,您执行了由Kafka本身处理的asyncCommit()。如果出现异常,我们需要执行syncCommit()。但是当我在这种情况下使用spring retry时,当我执行acknowledge it perform asyncCommit()。。