Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 对多个消息使用者使用手动提交_Spring Boot_Apache Kafka_Consumer - Fatal编程技术网

Spring boot 对多个消息使用者使用手动提交

Spring boot 对多个消息使用者使用手动提交,spring-boot,apache-kafka,consumer,Spring Boot,Apache Kafka,Consumer,我对卡夫卡很陌生。 使用SpringBootKafka,我开发了一个发布者和一个消费者,使用一个消息对象和手动确认。我的代码使用spring注释。这很好用。 现在,当我连接到生产代理时,这个代理不会发送一条消息,而是一个消息列表。 我的侦听器方法具有以下签名: @KafkaListener (topics="MessagesTopic", containerFactory="messageContainerfactory") public void lis

我对卡夫卡很陌生。 使用SpringBootKafka,我开发了一个发布者和一个消费者,使用一个消息对象和手动确认。我的代码使用spring注释。这很好用。 现在,当我连接到生产代理时,这个代理不会发送一条消息,而是一个消息列表。 我的侦听器方法具有以下签名:

@KafkaListener (topics="MessagesTopic", containerFactory="messageContainerfactory")
public void listen(@Payload Message message, Acknowledgment ack)
所以我可以确认每一条信息。好。 但现在看来我必须用

@KafkaListener (topics="MessagesTopic", containerFactory="messageContainerfactory")
public void listen(@Payload List<Message> messages, Acknowledgment ack)
@KafkaListener(topics=“MessagesTopic”,containerFactory=“messageContainerfactory”)
公共无效侦听(@有效负载列表消息,确认)
即使遵循文档,我似乎也应该使用

@KafkaListener (topics="MessagesTopic", containerFactory="messageContainerfactory")
public void listen(@Payload List<Message> messages, Acknowledgment ack, Consumer<?,?> consumer)
@KafkaListener(topics=“MessagesTopic”,containerFactory=“messageContainerfactory”)
public void listen(@Payload List消息、确认确认、使用者)
  • 我应该将batchmode设置为true吗
  • 现在的问题是:当这条消息被完全处理后,我如何确认每条消息

  • 非常感谢您的帮助

    如果您确实想手动提交偏移量,类似此功能也可以帮助您

    如果不需要,则将
    setAckMode
    切换到其他值

    这件事是用春天的方式做的

    CoreAutoConfiguration
    class:

    @Configuration
    @Import({KafkaAutoConfiguration.class})
    public class CoreAutoConfiguration {
    
    @Bean("batchKafkaListenerContainerFactory")
    public ConcurrentKafkaListenerContainerFactory<?, ?> batchKafkaListenerContainerFactory(ConcurrentKafkaListenerContainerFactoryConfigurer configurer, ConsumerFactory<Object, Object> kafkaConsumerFactory) {
        ConcurrentKafkaListenerContainerFactory<Object, Object> factory = new ConcurrentKafkaListenerContainerFactory<>();
        configurer.configure(factory, kafkaConsumerFactory);
        factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.MANUAL_IMMEDIATE);
        factory.setBatchListener(true);
        return factory;
        }
    }
    
    @Configuration
    @Import({
            CoreAutoConfiguration.class,
            KafkaAutoConfiguration.class,
    })
    @EnableKafka
    @EnableRetry
    public class Config {
    }
    
    最后,消费者:

    @KafkaListener(
            topics = "MessagesTopic",
            containerFactory = "batchKafkaListenerContainerFactory"
    )
    public void dataReceived(@Payload List<String> payload) throws RuntimeException {
        yourService.processIncomingData(payload);
    }
    

    非常感谢您的回复。但是,我看不到消息是否提交到哪里……在这种情况下,没有抛出异常=ack。如果需要
    batch.ack()。但我怀疑这项修正案是否值得——如果没有例外,为什么不允许spring为您提交批处理
    
    spring.kafka.bootstrap-servers=localhost:9092
    spring.kafka.consumer.group-id=helloworld
    spring.kafka.listener.type=batch
    spring.kafka.consumer.enable-auto-commit=false
    # this is size of incoming list if broker has this many entries, can be lower eventually
    spring.kafka.consumer.max-poll-records=100
    spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
    spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer