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());
    }
}

测试打印所有消息:

相关推荐
xlecho23 分钟前
从单一语言到全域全栈,AI凭全能实力,淘汰旧时代语言工程师
人工智能·后端·开源
uzong1 小时前
AI 当下,为什么如此焦虑,是怕被替代,还是提前转行,或者保持冷静并做好布局
后端
齿轮3 小时前
Agent 管理范式演进:从管一句话到管整个系统
人工智能·后端
亦暖筑序3 小时前
AI 客服系统升级实战:多 Agent 路由 + 多轮记忆 + 敏感词过滤
java·后端
啷咯哩咯啷3 小时前
纯本地运行的私人文档知识库
前端·人工智能·后端
Determined_man3 小时前
项目中异常什么时候打印错误和抛出?
后端
阿丰资源3 小时前
基于SpringBoot的房产销售系统设计与实现(附源码+数据库+文档,一键运行)
数据库·spring boot·后端
aLTttY4 小时前
Spring Boot整合AI大模型实现智能问答系统实战
人工智能·spring boot·后端
小江的记录本4 小时前
【微服务与云原生架构】DevOps、CI/CD流水线、GitOps 系统性知识体系
分布式·后端·ci/cd·微服务·云原生·架构·devops