kafka-事务

1. 事务的5个API

// 1初始化事务
void initTransactions();

// 2开启事务
void beginTransaction() throws ProducerFencedException;

// 3在事务内提交已经消费的偏移量(主要用于消费者)
void sendOffsetsToTransaction(Map<TopicPartition, OffsetAndMetadata> offsets,
                              String consumerGroupId) throws ProducerFencedException;

// 4提交事务
void commitTransaction() throws ProducerFencedException;

// 5放弃事务(类似于回滚事务的操作)
void abortTransaction() throws ProducerFencedException;

2. 幂等性和事务性的关系

事务属性实现前提是幂等性,即在配置事务属性transaction id时,必须还得配置幂等性;但是幂等性是可以独立使用的,不需要依赖事务属性。

  1. 幂等性引入了Porducer ID
  2. 事务属性引入了Transaction Id属性。

使用场景

  1. enable.idempotence = true,transactional.id不设置:只支持幂等性。
  2. enable.idempotence = true,transactional.id设置:支持事务属性和幂等性
  3. enable.idempotence = false,transactional.id不设置:没有事务属性和幂等性的kafka
  4. enable.idempotence = false,transactional.id设置:无法获取到PID,此时会报错

3. 参考

kafka幂等生产者及事务_51CTO博客_kafka事务

猜你喜欢

转载自blog.csdn.net/qq_40382400/article/details/132122372