繁体   English   中英

Kafka的Spring Boot只需一次处理

[英]Spring boot with Kafka with Exactly Once Processing

我正在使用kafka设置Spring Boot应用程序。 如何实现“恰好一次交货保证”。

我已经阅读并尝试按照“ https://www.baeldung.com/kafka-exactly-once ”来实现,但仍停留在producer.initTransactions();上。

// NotificationKafkaConsumerConfig.java

@Bean
public ConsumerFactory < String, String > consumerFactory() {
    Map < String, Object > props = new HashMap < > ();
    props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
    props.put(ConsumerConfig.GROUP_ID_CONFIG, "notification_group");
    props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "false");
    props.put(ConsumerConfig.ISOLATION_LEVEL_CONFIG, "read_committed");
    return new DefaultKafkaConsumerFactory < > (props);
}

@Bean
public ConcurrentKafkaListenerContainerFactory < String, String > kafkaListenerContainerFactory() {

    ConcurrentKafkaListenerContainerFactory < String, String > factory = new ConcurrentKafkaListenerContainerFactory < > ();
    factory.setConsumerFactory(consumerFactory());
    return factory;
}
// NotificationKafkaProducerConfig
@Bean
public ProducerFactory < String, String > producerFactory() {

    Map < String, Object > configProps = new HashMap < > ();
    configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
    configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    return new DefaultKafkaProducerFactory < > (configProps);
}

@Bean
public KafkaTemplate < String, String > kafkaTemplate() {
    return new KafkaTemplate < > (producerFactory());
}

// NotificationKafkaListner
@KafkaListener(topics = "notification", groupId = "notification_group")
public void listen(String message) {

    try {} catch (Exception e) {}
}

// NotificationController
@SuppressWarnings({
    "rawtypes",
    "unchecked"
})
@PostMapping(path = "/send")
public ResponseEntity << ? > send(@Valid @RequestBody NotificationRequest notificationReq) {
    log.debug("NotificationController: invoked to send notification");
    kafkaTemplate.send(notificationTopic, message);
}

如何进行交易

考虑使用KafkaTransactionManager

 * {@link org.springframework.transaction.PlatformTransactionManager} implementation for a
 * single Kafka {@link ProducerFactory}. Binds a Kafka producer from the specified
 * ProducerFactory to the thread, potentially allowing for one thread-bound producer per
 * ProducerFactory.

在参考手册中查看更多信息: https : //docs.spring.io/spring-kafka/docs/current/reference/html/#transactional-listener-container-and-exactly-once-processing

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM