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\[\] 动态设置消费者属性
相关推荐
考虑考虑7 小时前
tcpdump指令
运维·后端·自动化运维
Wellijohn8 小时前
2000 个请求、30% 失败率:我把 Python 批量并发的限流和重试参数算了一遍
后端·python·性能优化
皮皮林5519 小时前
YouTube 是如何保存巨量视频文件的?
后端
学长毕设9 小时前
基于SpringBoot 车辆租赁系统平台-附源码
java·spring boot·后端
码途行者9 小时前
每天10分钟学Go:第22课 自定义错误
开发语言·后端·golang
Csvn10 小时前
Nginx 性能调优与压测:连接账本、找拐点与调优前后对比
后端·nginx
ServBay13 小时前
AI 做的小工具,怎样交给同事用?
后端·aigc·ai编程
用户693717500138413 小时前
AI 越强,为什么我们反而越来越担心失业?
前端·后端·github
夜郎king13 小时前
Shapefile 一键生成 GeoServer 样式:GeoTools + Spring Boot 实现 YSLD 自动生成器
spring boot·gis·geotools·ysld
她的男孩14 小时前
Spring Boot 接 Flowable:请假单要同时走财务和行政,包容分支和默认分支怎么配
java·spring boot·工作流引擎