SpringBoot集成Kafka

1、maven依赖

xml 复制代码
<!-- 无需指定版本,跟随springboot版本自动引入适配版本 -->
<dependency>
    <groupId>org.springframework.kafka</groupId>
    <artifactId>spring-kafka</artifactId>
</dependency>

2、application.yml 配置

yaml 复制代码
spring:
  kafka:
    bootstrap-servers: 10.10.62.32:31175,10.10.62.32:31032,10.10.62.32:32224
    consumer:
      group-id: 'default-group-id'
      key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      enable-auto-commit: false
      max-poll-records: 100
      auto-offset-reset: latest
    listener:
      ack-mode: manual
      type: BATCH
    properties:
      security.protocol: PLAINTEXT
      #sasl.mechanism: PLAIN
    #jaas:
      #config: #config: 'org.apache.kafka.common.security.scram.ScramLoginModule required username="admin" password="Wt1g2+UNRPnV";'

3、消息生产者Producer

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Service;
import org.springframework.util.concurrent.ListenableFuture;


@Service
public class KafkaProducerService {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    public void sendMessage(String topic, String message) {
        //异步消息
        ListenableFuture<SendResult<String, String>> asyncResult = kafkaTemplate.send(topic, message);
        asyncResult.addCallback(
                success -> {
                    String topic1 = success.getRecordMetadata().topic();
                    int partition = success.getRecordMetadata().partition();
                    long offset = success.getRecordMetadata().offset();
                    System.out.println("异步发送成功: topic=" + topic + ", partition=" + partition + ", offset=" + offset);
                },
                failure -> {
                    System.out.println("异步发送失败: " + failure.getMessage());
                    // 可以记录日志、重试、告警等
                }
        );

        //同步消息
        try {
            SendResult<String, String> syncResult =  kafkaTemplate.send(topic, message).get();
            System.out.println("同步发送成功: " + syncResult.getRecordMetadata());
        } catch (Exception e) {
            System.out.println("发送失败: " + e.getMessage());
        }
    }
}

4、消息消费者Consumer

java 复制代码
import org.example.springboot.starter.common.User;
import org.springframework.stereotype.Service;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;

import java.util.List;

@Service
public class KafkaConsumerService {

    @KafkaListener(topics = "my-topic", groupId = "policy-sr-group")
    public void listen(String message, Acknowledgment ack) {
        try {
            System.out.println("消费者Received Message: " + message);
            // 处理业务逻辑
        } finally {
            // 手动提交 offset
            ack.acknowledge();
        }
    }

    @KafkaListener(topics = "user-topic", groupId = "policy-sr-group")
    public void listenUser(List<User> user, Acknowledgment ack) {
        System.out.println("消费者Received User: " + user);
        ack.acknowledge();
    }

}

🧩 @KafkaListener 核心属性一览

属性 类型 说明
topics String\[\] 监听的 topic 名称
topicPattern String 正则表达式匹配 topic(动态监听)
id String 消费者 ID(用于日志、管理)
groupId String 消费者组 ID(覆盖配置文件)
concurrency String 并发线程数(控制吞吐)
containerFactory String 指定 KafkaListenerContainerFactory
errorHandler Class 自定义错误处理器
ackMode String 手动提交模式(配合 Acknowledgment)
clientIdPrefix String 客户端 ID 前缀
batch boolean 是否启用批量消费
properties String\[\] 动态设置消费者属性
相关推荐
子兮曰2 天前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
子兮曰2 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
爱勇宝2 天前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
胡写代码2 天前
别再前后端各写一套表单校验了
java·后端
大勇前进2 天前
原生 PHP 还是 Laravel?小项目到底要不要上框架
后端
vipxieliang2 天前
ValidX 在 DDD 领域驱动设计中的实践
java·spring boot
yuzhi_liu2 天前
我用 LangGraph4j 实现 Multi-Agent Supervisor
后端
alsmile2 天前
Node-RED 之外,国产规则引擎的新方案:基于标准语法,Go 先行实现
后端·开源·go
大白802 天前
PHP 内存溢出排查思路:看懂报错日志,精准定位问题
后端
二月龙2 天前
PHP 接口返回统一响应封装,让前后端对接更省心
后端