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\[\] 动态设置消费者属性
相关推荐
学长毕业设计6 小时前
基于SpringBoot的公益基金管理系统(源码+文档+讲解视频)
java·spring boot·后端
东小西7 小时前
【SAA实战】第 3 篇 · 工具调用全攻略:把业务能力交给 Agent 自己调度
java·后端·spring
东小西7 小时前
【SAA实战】第 4 篇 · Agent 短期记忆:saver 让 Agent 跨轮记得住(threadId 隔离)
java·后端·spring
阿kun要赚马内7 小时前
MySQL 索引基础
后端·mysql
码事漫谈8 小时前
我啥都能做,却不知道做什么了
后端
geovindu8 小时前
CSharp: 万年历
开发语言·后端·c#·.net
独泪了无痕9 小时前
SpringBoot Event事件机制,轻松实现业务解耦
spring boot·后端·spring
悲且狂10 小时前
SpringBoot项目改造注意事项(旧项目框架复用)
java·spring boot·后端
敲代码的嘎仔11 小时前
28届后端开发-海康威视日常实习一面(已OC)
java·开发语言·后端·面试·海康威视·实习·大厂
IT_陈寒11 小时前
JavaScript数组排序踩的坑,差点让我加班到凌晨
前端·人工智能·后端