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兼容有问题,特此记录!

相关推荐
想做富婆23 分钟前
大数据,Hadoop,HDFS的简单介绍
大数据·hadoop·分布式
小白的一叶扁舟1 小时前
Kafka 入门与应用实战:吞吐量优化与与 RabbitMQ、RocketMQ 的对比
java·spring boot·kafka·rabbitmq·rocketmq
霍格沃兹测试开发学社测试人社区1 小时前
软件测试丨消息管道(Kafka)测试体系
软件测试·分布式·测试开发·kafka
oulaqiao2 小时前
语言集成查询LINQ
c#·linq
weisian1512 小时前
消息队列篇--原理篇--RocketMQ和Kafka对比分析
分布式·kafka·rocketmq
ShareBeHappy_Qin3 小时前
ZooKeeper 中的 ZAB 一致性协议与 Zookeeper 设计目的、使用场景、相关概念(数据模型、myid、事务 ID、版本、监听器、ACL、角色)
分布式·zookeeper·云原生
黄名富12 小时前
Kafka 日志存储 — 日志索引
java·分布式·微服务·kafka
DM很小众12 小时前
Kafka 和 MQ 的区别
分布式·kafka
sjsjsbbsbsn13 小时前
基于注解实现去重表消息防止重复消费
java·spring boot·分布式·spring cloud·java-rocketmq·java-rabbitmq