kafka&spring批处理-如何只读取来自同一主题的未提交消息?

tuwxkamq  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(549)

我正在使用spring批处理和kafka处理一个小批处理,它从kafka主题读取json数据,将其转换为student对象,更改值并将其发送回kafka主题。一切都很好,但我唯一的问题是,我的消费者总是从乞讨的主题阅读。我需要它来读最后一条未被消费的信息。我已经添加了这些属性:

ConsumerConfig.AUTO_OFFSET_RESET_CONFIG to earliest
ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG to false
ConsumerConfig.GROUP_ID_CONFIG to a random value

但这似乎不起作用,在消费者启动时,它处理所有的信息。有人知道怎么用Spring批和Kafka吗?这是我的密码:
batchstudent.java文件:

@SpringBootApplication
@EnableBatchProcessing
@RequiredArgsConstructor
public class BatchStudent {
    public static void main(String[] args) {
        SpringApplication.run(BatchStudent.class, args);
    }

    private final JobBuilderFactory jobBuilderFactory;
    private final StepBuilderFactory stepBuilderFactory;
    private final KafkaTemplate<Integer, Student> template;
    private final KafkaProperties properties;

    @Value("${kafka.topic.consumer}")
    private String topic;

    @Bean
    public ItemProcessor<Student, Student> customItemProcessor() {
        return new CustomProcessor();
    }

    @Bean
    Job job() {
        return this.jobBuilderFactory.get("job")
                .start(start())
                .incrementer(new RunIdIncrementer())
                .build();
    }

    @Bean
    KafkaItemWriter<Integer, Student> writer() {
        return new KafkaItemWriterBuilder<Integer, Student>()
                .kafkaTemplate(template)
                .itemKeyMapper(Student::getId)
                .build();
    }

    @Bean
    public KafkaItemReader<Integer, Student> reader() {
        Properties props = new Properties();
        props.putAll(this.properties.buildConsumerProperties());

        return new KafkaItemReaderBuilder<Integer, Student>()
                .partitions(0)
                .consumerProperties(props)
                .name("students-consumer-reader")
                .saveState(true)
                .topic(topic)
                .build();
    }

    @Bean
    Step start() {
        return this.stepBuilderFactory
                .get("step")
                .<Student, Student>chunk(10)
                .writer(writer())
                .processor(customItemProcessor())
                .reader(reader())
                .build();
    }
}

应用程序yml

spring.batch.initialize-schema: always

# Conf Kafka Consumer

spring.kafka.consumer.key-deserializer: org.apache.kafka.common.serialization.IntegerDeserializer
spring.kafka.consumer.value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer

# spring.kafka.consumer.group-id: student-group

spring.kafka.consumer.properties.spring.json.trusted.packages: '*'
spring.kafka.consumer.properties.spring.json.value.default.type: com.org.model.Student

# Conf Kafka Producer

spring.kafka.producer.key-serializer: org.apache.kafka.common.serialization.IntegerSerializer
spring.kafka.producer.value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
spring.kafka.producer.bootstrap-servers: localhost:9092

# Conf topics

spring.kafka.template.default-topic: producer.student
kafka.topic.consumer: consumer.student

学生.java

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
    Integer id;
    Integer count;
}

自定义处理器.java

@NoArgsConstructor
public class CustomProcessor implements ItemProcessor<Student, Student> {

    @Override
    public Student process(Student studentRecieved) {
        final Student studentSent = new Student();
        studentSent.setId(studentRecieved.getId());
        studentSent.setCount(200);
        return studentSent;
    }
}

谢谢你的帮助!

ldioqlga

ldioqlga1#

一切都很好,但我唯一的问题是,我的消费者总是从乞讨的主题阅读。我需要它来读最后一条未被消费的信息。
springbatch4.3引入了一种使用kafka中存储的偏移量记录的方法。我在去年的springone演讲中谈到了这个特性:springbatch4.3有什么新特性?。您可以使用setPartitionOffset在每个分区中为kafka读取器配置一个自定义的起始偏移量:

Setter for partition offsets. This mapping tells the reader the offset to start reading
from in each partition. This is optional, defaults to starting from offset 0 in each
partition. Passing an empty map makes the reader start from the offset stored in Kafka
for the consumer group ID.

您可以在这个测试用例中找到一个完整的示例。

相关问题