Spring Boot 集成 Kafka 极简入门教程
这是最简洁、最实用的 Spring Boot 整合 Kafka 教程,只保留核心代码,新手直接复制就能跑通。
一、环境准备
- 已启动 Kafka 服务(localhost:9092)
- 已创建 Topic:
test-topic(命令行创建即可) - Spring Boot 版本:2.7+ / 3.0+ 都支持
二、Maven 依赖(核心)
在 pom.xml 中只需要这一个依赖:
<dependencies>
<!-- Spring Boot Kafka 核心依赖 -->
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
</dependencies>
三、application.yml 配置(最简可用)
spring:
kafka:
# Kafka 地址
bootstrap-servers: localhost:9092
# 生产者(发送消息)配置
producer:
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
# 消费者(接收消息)配置
consumer:
group-id: my-test-group # 消费者组(必须)
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
auto-offset-reset: earliest # 从头消费消息
四、发送消息(生产者)
直接使用 Spring 提供的 KafkaTemplate 发送消息:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class KafkaProducerController {
// Spring 自动注入,直接用
@Autowired
private KafkaTemplate<String, String> kafkaTemplate;
// 测试接口:浏览器访问发送消息
@GetMapping("/send")
public String sendMessage() {
String topic = "test-topic";
String message = "Spring Boot 发送 Kafka 消息!";
// 发送消息
kafkaTemplate.send(topic, message);
return "消息发送成功:" + message;
}
}
五、接收消息(消费者)
使用 @KafkaListener 注解监听主题,自动接收消息:
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;
@Service
public class KafkaConsumerService {
// 监听 test-topic 主题
@KafkaListener(topics = "test-topic", groupId = "my-test-group")
public void consume(String message) {
System.out.println("=====================================");
System.out.println("收到 Kafka 消息:" + message);
System.out.println("=====================================");
}
}
六、启动类(无需修改)
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class KafkaSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(KafkaSpringBootApplication.class, args);
}
}
七、测试运行
- 启动 Spring Boot 项目
- 浏览器访问:
http://localhost:8080/send
- 查看控制台,能看到消费者打印出消息,即成功!
六、高频实用功能(企业常用)
1. 发送带 Key 的消息
kafkaTemplate.send("test-topic", "key1", "带Key的消息");
2. 监听多个主题
@KafkaListener(topics = {"test-topic", "other-topic"}, groupId = "my-group")
3. 消费完整消息对象(获取分区、偏移量)
@KafkaListener(topics = "test-topic")
public void consume(ConsumerRecord<String, String> record) {
System.out.println("主题:" + record.topic());
System.out.println("分区:" + record.partition());
System.out.println("偏移量:" + record.offset());
System.out.println("消息:" + record.value());
}
七、常见问题
- 连不上 Kafka:检查 Kafka 是否启动、端口 9092 是否通
- 收不到消息:确认 Topic 名称一致、消费者组配置正确
- 乱码/序列化错误:确保生产者和消费者序列化器一致
总结
- 依赖:
spring-kafka - 配置:
bootstrap-servers + 序列化方式 - 发送:
KafkaTemplate.send() - 接收:
@KafkaListener(topics = "xxx")
全程无复杂配置、无底层API操作,Spring Boot 全部封装好,直接用!