---
title: Kafka Streams Exactly-Once v2 事务处理与状态容错实践
keywords: exactly_once_v2, processing.guarantee, transactions, state store, topology
description: 配置 Kafka Streams 使用 Exactly-Once v2 事务保障端到端一致性,示例构建拓扑并验证状态容错与重启恢复。
tags:
- Kafka
- exactly_once_v2
- processing.guarantee
- state store
- topology
- transactions
- 数据流
- 流处理
categories:
- 文章资讯
- 技术教程
---
应用属性与拓扑:
Properties props = new Properties();
props.put(StreamsConfig.APPLICATION_ID_CONFIG, "orders-app");
props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(StreamsConfig.PROCESSING_GUARANTEE_CONFIG, StreamsConfig.EXACTLY_ONCE_V2);
props.put(StreamsConfig.REPLICATION_FACTOR_CONFIG, 3);
props.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, 100);
props.put(StreamsConfig.producerPrefix("transaction.timeout.ms"), 60000);
StreamsBuilder builder = new StreamsBuilder();
KStream<String, String> src = builder.stream("orders");
KStream<String, Long> agg = src.groupByKey()
.count(Materialized.as("orders-count"))
.toStream();
agg.to("orders_count", Produced.with(Serdes.String(), Serdes.Long()));
KafkaStreams streams = new KafkaStreams(builder.build(), props);
streams.start();

发表评论 取消回复