SpringBoot集成kafka接收消息

SpringBoot集成kafka接收消息

1、SpringBoot集成kafka接收消息

生产者

java 复制代码
package com.power.producer;

import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class EventProducer {

    @Resource
    private KafkaTemplate<String,String> kafkaTemplate;

    public void sendEvent(){
        kafkaTemplate.send("helloTopic","hello kafka");
    }

}

application.yml配置文件

java 复制代码
spring:
  application:
    #应用名称
    name: spring-boot-02-kafka-base

  #kafka连接地址(ip+port)
  kafka:
    bootstrap-servers: 47.116.35.15:9092
    #配置生产者(有24个配置)
#    producer:


    #配置消费者(有24个配置)
#    consumer:

测试类

java 复制代码
package com.power;

import com.power.producer.EventProducer;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

@SpringBootTest
public class SpringBoot02KafkaBaseApplication {

    @Resource
    private EventProducer eventProducer;

    @Test
    void sendInterceptor(){
        eventProducer.sendEvent();
    }

}

2、@Payload注解接收消息体内容

消费者:

java 复制代码
package com.power.consumer;

import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

@Component
public class EventConsumer {

    //采用监听的方式接收事件(消息,数据)
    @KafkaListener(topics = {"helloTopic"},groupId="helloGroup")
    public void onEvent(@Payload String event){
        System.out.println("读取/消费到的事件:"+event);
    }
}

测试结果:

3、@Header注解接收消息头内容

注意,不太版本kafak使用@Header注解读取partition时不一样:

  • kafka3.0以下版本使用KafkaHeaders.RECEIVED_PARTITION_ID获取分区
  • kafka3.0以上版本使用KafkaHeaders.RECEIVED_PARTITION获取分区

消费者:

java 复制代码
package com.power.consumer;

import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

@Component
public class EventConsumer {

    //采用监听的方式接收事件(消息,数据)
    @KafkaListener(topics = {"helloTopic"},groupId="helloGroup")
        public void onEvent(@Payload String event,
                            @Header(value=KafkaHeaders.RECEIVED_TOPIC) String topic,
                            @Header(value=KafkaHeaders.RECEIVED_PARTITION_ID) String partition){
        System.out.println("读取/消费到的事件:"+event+",topic:"+topic+",partition:"+partition);
    }
}

测试结果:

4、接收消息所有内容


消费者:

java 复制代码
package com.power.consumer;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.util.function.Consumer;

@Component
public class EventConsumer {

    //采用监听的方式接收事件(消息,数据)
    @KafkaListener(topics = {"helloTopic"},groupId="helloGroup")
        public void onEvent(@Payload String event,
                            @Header(value=KafkaHeaders.RECEIVED_TOPIC) String topic,
                            @Header(value=KafkaHeaders.RECEIVED_PARTITION_ID) String partition,
                            ConsumerRecord<String,String> record){
        System.out.println("读取/消费到的事件:"+event+",topic:"+topic+",partition:"+partition);
        System.out.println("读取/消费到的事件:"+record.toString());
    }
}

测试打印所有消息:

相关推荐
砍材农夫42 分钟前
物联网实战:Spring Boot MQTT | MQTT 设备模拟器演示(附源码)
java·spring boot·后端·物联网·spring·netty
我叫黑大帅1 小时前
解决聊天页内部滚轮改为页面滚动问题
javascript·后端·面试
YDS8291 小时前
DeepSeek RAG&MCP + Agent智能体项目 —— Agent执行链路设计之ReAct Loop
java·spring boot·ai·agent·deepseek
IT_陈寒2 小时前
Python的线程池居然把我坑在了垃圾回收这块
前端·人工智能·后端
zhangxingchao2 小时前
AI应用开发八:RAG相关技术总结
前端·人工智能·后端
吴佳浩2 小时前
Go史上最大“打脸”现场来了:泛型方法终于实现了
后端·go
Huyuejia3 小时前
runtime-ask
后端
Rust研习社3 小时前
90% 的 Rust 新手都不知道的 3 个实用开发技巧
后端·rust·编程语言
ZengLiangYi3 小时前
sql.js WASM 深度解析
javascript·数据库·后端
Stick_ZYZ3 小时前
从“能调用工具”到“能稳定执行任务”:Agent 工程化的下一步
java·人工智能·后端·spring·ai