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

测试打印所有消息:

相关推荐
IT_陈寒9 小时前
React子组件莫名其妙重渲染?你可能漏了这个Hook
前端·人工智能·后端
小兵金林10 小时前
AI 生成代码的思考与实践
后端·ai编程
码事漫谈10 小时前
DeepSeek 明天又降价(涵历史价格对比)
后端
前端精髓10 小时前
NestJS 是什么(对着 Spring Boot 一起学习)
spring boot·后端·学习
国奉10 小时前
从零设计一个 iOS 文件浏览器:Sandbox、FileManager、Document Picker 与文件架构
后端
前端兰博10 小时前
04-数据库-MySQL
后端·mysql
她的男孩10 小时前
接口加密做成框架级能力有多难?我扒了 3600 行源码:从 RSA 握手到落库密文迁移
人工智能·后端·架构
掘金挖土10 小时前
前端手摸手跑路之 AI 应用开发(二)
前端·后端
遨翔在知识的海洋里11 小时前
nest(5)-文件上传和静态资源
后端
遨翔在知识的海洋里11 小时前
nest(3)-jwt和RBAC
后端