SpringBoot3下Kafka分组均衡消费实现

首先添加maven依赖:

XML 复制代码
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
            <version>2.8.11</version>
            <exclusions>
                <!--此处一定要排除kafka-clients,然后引入低版本client 不然后面分组消费功能会失效!-->
                <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.1.2</version>
        </dependency>
复制代码
KafkaMessageListener.java
java 复制代码
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.annotation.TopicPartition;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class KafkaMessageListener {
    @KafkaListener(topics = "test-sfk",groupId = "test")
    public void listen5(ConsumerRecord<String, String> message) {
        log.info("5------>Received message: {}", message.value());
    }
    @KafkaListener(topics = "test-sfk",groupId = "test")
    public void listen4(ConsumerRecord<String, String> message) {
        log.info("4------>Received message: {}", message.value());
    }
}
复制代码
TestController.java 用于发送测试消息
java 复制代码
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("test")
public class TestController {
    @Autowired
    private KafkaTemplate<String,Object> kafkaTemplate;
    @GetMapping("kafkaMessage")
    public String kafkaMessage(){
        for (int i = 0; i < 10; i++) {
            kafkaTemplate.send("test-sfk","test:"+i);
        }

        return "ok";
    }
}

启动后调用接口发送消息;此时看到控制台消息输出已经是均衡消费了

Matlab 复制代码
2024-03-15T13:49:01.034+08:00  INFO 19548 --- [ntainer#1-0-C-1] c.w.e.k.listener.KafkaMessageListener    : 4------>Received message: test:0
2024-03-15T13:49:01.035+08:00  INFO 19548 --- [ntainer#1-0-C-1] c.w.e.k.listener.KafkaMessageListener    : 4------>Received message: test:1
2024-03-15T13:49:01.035+08:00  INFO 19548 --- [ntainer#1-0-C-1] c.w.e.k.listener.KafkaMessageListener    : 4------>Received message: test:2
2024-03-15T13:49:01.295+08:00  INFO 19548 --- [ntainer#0-0-C-1] c.w.e.k.listener.KafkaMessageListener    : 5------>Received message: test:3
2024-03-15T13:49:01.297+08:00  INFO 19548 --- [ntainer#0-0-C-1] c.w.e.k.listener.KafkaMessageListener    : 5------>Received message: test:4
2024-03-15T13:49:01.300+08:00  INFO 19548 --- [ntainer#0-0-C-1] c.w.e.k.listener.KafkaMessageListener    : 5------>Received message: test:5
2024-03-15T13:49:01.301+08:00  INFO 19548 --- [ntainer#0-0-C-1] c.w.e.k.listener.KafkaMessageListener    : 5------>Received message: test:6
2024-03-15T13:49:01.303+08:00  INFO 19548 --- [ntainer#0-0-C-1] c.w.e.k.listener.KafkaMessageListener    : 5------>Received message: test:7
2024-03-15T13:49:01.304+08:00  INFO 19548 --- [ntainer#0-0-C-1] c.w.e.k.listener.KafkaMessageListener    : 5------>Received message: test:8
2024-03-15T13:49:01.306+08:00  INFO 19548 --- [ntainer#0-0-C-1] c.w.e.k.listener.KafkaMessageListener    : 5------>Received message: test:9

一开始不知道,直接引用的spring-kafka依赖,然后就分组消费失败,始终只能被其中一个消费端消费,这里不知道是新版本配置有差异,还是就是新版kafka-clients跟SpringBoot3兼容有问题,特此记录!

相关推荐
只因在人海中多看了你一眼16 分钟前
分布式缓存 + 数据存储 + 消息队列知识体系
分布式·缓存
zhixingheyi_tian3 小时前
Spark 之 Aggregate
大数据·分布式·spark
KevinAha4 小时前
Kafka 3.5 源码导读
kafka
求积分不加C4 小时前
-bash: ./kafka-topics.sh: No such file or directory--解决方案
分布式·kafka
nathan05294 小时前
javaer快速上手kafka
分布式·kafka
激流丶7 小时前
【Kafka 实战】Kafka 如何保证消息的顺序性?
java·后端·kafka
谭震鸿8 小时前
Zookeeper集群搭建Centos环境下
分布式·zookeeper·centos
天冬忘忧13 小时前
Kafka 工作流程解析:从 Broker 工作原理、节点的服役、退役、副本的生成到数据存储与读写优化
大数据·分布式·kafka
工业甲酰苯胺15 小时前
Python脚本消费多个Kafka topic
开发语言·python·kafka
B站计算机毕业设计超人16 小时前
计算机毕业设计SparkStreaming+Kafka新能源汽车推荐系统 汽车数据分析可视化大屏 新能源汽车推荐系统 汽车爬虫 汽车大数据 机器学习
数据仓库·爬虫·python·数据分析·kafka·数据可视化·推荐算法