Kafka + Spring Boot 终极整合指南

Kafka + Spring Boot 终极整合指南

基于 Spring Boot 3.2 + Kafka 3.7生产级 实践 含 Docker 快速启动、自动配置源码、高级特性、压测调优 全流程


目录

  1. 导语
  2. 环境准备
  3. Docker 一键启动 Kafka
  4. Spring Boot 项目搭建
  5. 自动配置源码走读
  6. 生产者(Producer)高级特性
  7. 消费者(Consumer)高级特性
  8. 事务与幂等性
  9. Kafka Streams 微服务
  10. 压测与调优
  11. 常见问题 FAQ
  12. 总结

导语

"Spring Boot + Kafka" = 高吞吐、低延迟、水平扩展事件驱动 架构基石。 本文从 Docker 启动生产调优 一条龙的 可复制 方案。


环境准备

组件 版本
JDK 17+
Spring Boot 3.2.x
Kafka 3.7.x
Maven 3.9+
Docker 24+

Docker 一键启动 Kafka

bash 复制代码
# 1. 启动 ZooKeeper(Kafka 3.7 仍支持 ZK 模式)
docker run -d --name zookeeper -p 2181:2181 zookeeper:3.8

# 2. 启动 Kafka
docker run -d --name kafka \
  -p 9092:9092 \
  -e KAFKA_BROKER_ID=1 \
  -e KAFKA_ZOOKEEPER_CONNECT=host.docker.internal:2181 \
  -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://host.docker.internal:9092 \
  -e KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1 \
  confluentinc/cp-kafka:7.6.0

验证:docker exec kafka kafka-topics.sh --list --bootstrap-server localhost:9092


Spring Boot 项目搭建

1. 引入依赖

xml 复制代码
<dependencies>
    <!-- Web 测试用 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Kafka -->
    <dependency>
        <groupId>org.springframework.kafka</groupId>
        <artifactId>spring-kafka</artifactId>
    </dependency>
    <!-- JSON 序列化 -->
    <dependency>
        <groupId>org.springframework.kafka</groupId>
        <artifactId>spring-kafka</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.apache.kafka</groupId>
                <artifactId>kafka-clients</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka-clients</artifactId>
        <version>3.7.0</version>
    </dependency>
</dependencies>

2. 配置(application.yml

yaml 复制代码
spring:
  kafka:
    bootstrap-servers: host.docker.internal:9092
    producer:
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
      value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
      acks: all
      retries: 3
      batch-size: 16384
      linger-ms: 5
    consumer:
      group-id: order-group
      auto-offset-reset: earliest
      key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
      properties:
        spring.json.trusted.packages: "*"

自动配置源码走读

1. 入口:KafkaAutoConfiguration

java 复制代码
@AutoConfiguration
@ConditionalOnClass(KafkaTemplate.class)
@EnableConfigurationProperties(KafkaProperties.class)
public class KafkaAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public ProducerFactory<?, ?> producerFactory(KafkaProperties properties) {
        return new DefaultKafkaProducerFactory<>(properties.buildProducerProperties());
    }

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

2. 消费者工厂

java 复制代码
@Bean
@ConditionalOnMissingBean(ConsumerFactory.class)
public ConsumerFactory<?, ?> consumerFactory(KafkaProperties properties) {
    return new DefaultKafkaConsumerFactory<>(properties.buildConsumerProperties());
}

默认 单例配置绑定条件注入 三板斧。


生产者(Producer)高级特性

1. 异步发送 + 回调

java 复制代码
@RestController
@RequestMapping("/send")
public class ProducerController {

    @Autowired
    private KafkaTemplate<String, Object> template;

    @GetMapping("/{msg}")
    public CompletableFuture<String> send(@PathVariable String msg) {
        return template.send("demo-topic", msg)
                       .thenApply(result -> "Sent: " + result.getRecordMetadata().offset());
    }
}

2. 事务消息

java 复制代码
@Transactional
public void sendInTransaction(List<Order> orders) {
    orders.forEach(o -> template.send("order-topic", o));
}

需开启事务:spring.kafka.producer.transaction-id-prefix=tx-


消费者(Consumer)高级特性

1. 并发消费

java 复制代码
@KafkaListener(topics = "demo-topic", groupId = "demo-group", concurrency = "6")
public void listen(String data, Acknowledgment ack) {
    process(data);
    ack.acknowledge(); // 手动提交
}

2. 批量消费

java 复制代码
@KafkaListener(topics = "batch-topic")
public void batchListen(List<ConsumerRecord<String, Object>> records) {
    records.forEach(r -> process(r.value()));
}

配置:spring.kafka.listener.type=batch


事务与幂等性

1. 幂等 Producer

properties 复制代码
spring.kafka.producer.acks=all
spring.kafka.producer.retries=3
spring.kafka.producer.enable-idempotence=true

2. 事务样例

java 复制代码
@Transactional
public void createOrder(Order order) {
    template.executeInTransaction(t -> {
        t.send("order-topic", order);
        t.send("stock-topic", order);
        return null;
    });
}

Kafka Streams 微服务

1. 引入依赖

xml 复制代码
<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka-streams</artifactId>
</dependency>

2. 拓扑定义

java 复制代码
@Bean
public KStream<String, Order> stream(StreamsBuilder builder) {
    KStream<String, Order> source = builder.stream("order-topic");
    source.filter((k, v) -> v.getAmount() > 100)
          .to("big-order-topic");
    return source;
}

压测与调优

1. 压测命令

bash 复制代码
kafka-producer-perf-test.sh \
  --topic demo-topic --num-records 1000000 --record-size 1024 \
  --throughput -1 --producer-props bootstrap.servers=localhost:9092

2. 调优清单

参数 建议值 说明
batch.size 32 KB 提高吞吐
linger.ms 5-20 ms 延迟换吞吐
compression.type lz4 压缩比 + CPU 平衡
fetch.min.bytes 1 KB 减少空拉取

常见问题 FAQ

问题 解决
消息丢失 acks=all + 手动提交
重复消费 幂等 Producer + 事务
消费延迟 增加分区 + 提高并发

总结

Spring Boot + Kafka = 事件驱动高吞吐 架构 从 Docker 启动生产调优 ,本文提供 全流程可复制 方案。 掌握 自动配置、事务、Streams、压测 四板斧,即可轻松构建 万级 TPS实时系统

相关推荐
步行cgn16 分钟前
Spring Boot 项目打包部署详解
java·spring boot·后端
我的xiaodoujiao1 小时前
Django 基础知识详细图文教程 4-Django 视图定义与使用
开发语言·数据库·后端·测试工具·django·sqlite
Apifox1 小时前
Apifox 8 月更新|调试、权限与协作体验持续优化
前端·后端·测试
kyson_1 小时前
后端登录凭证设计:Session-Cookie、Redis-Token 与 JWT
后端
对象存储与RustFS1 小时前
RustFS 后台扫描器与自愈调参:五档速度、位腐检测周期与并发上限
后端·rust·开源
天天喝旺仔1 小时前
Docker 镜像瘦身实战:多阶段构建把体积缩小 90%
运维·后端·ci/cd·docker·云原生·容器·性能优化
Lost of 程序猿1 小时前
ASP.NET Core Saga 分布式事务深度实战:备件采购跨服务长流程,如何保证“要么全成,要么全回“
分布式·后端·asp.net
卷无止境1 小时前
Coding Agent 里的上下文 Compact,到底在压缩什么
后端·python
爱勇宝1 小时前
公司没给活干,却因为员工看手机把人开了:法院判赔11万元
前端·后端·程序员
2601_962071002 小时前
【Java EE】SpringBoot的创建与简单使用
spring boot·后端·java-ee