Kafka:springboot集成kafka收发消息

kafka环境搭建参考Kafka:安装和配置_moreCalm的博客-CSDN博客

1、springboot中引入kafka依赖

复制代码
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- kafkfa -->
    <dependency>
        <groupId>org.springframework.kafka</groupId>
        <artifactId>spring-kafka</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.apache.kafka</groupId>
                <artifactId>kafka-clients</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka-clients</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
    </dependency>
</dependencies>

2、配置application.yml

复制代码
server:
  port: 9991
spring:
  application:
    name: kafka-demo
  kafka:
    bootstrap-servers: 192.168.200.130:9092
    producer:
      retries: 10
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
      value-serializer: org.apache.kafka.common.serialization.StringSerializer
    consumer:
      group-id: ${spring.application.name}-test
      key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      value-deserializer: org.apache.kafka.common.serialization.StringDeserializer

传递String类型的消息

3、controller实现消息发送接口

复制代码
package com.heima.kafkademo.controller;

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.RestController;

@RestController
public class HelloController {

    @Autowired
    KafkaTemplate<String,String> kafkaTemplate;

    @GetMapping("/hello")
    public String addMessage(){
        kafkaTemplate.send("lakers-topic","湖人总冠军!");
        return "ok";
    }
}

4、component中实现接收类HelloListener

复制代码
package com.heima.kafkademo.component;

import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
public class HelloListener {

    @KafkaListener(topics = "lakers-topic")
    public void onMessage(String msg){
        System.out.println(msg);
    }

}

5、测试

浏览器访问该接口并查看控制台

接收成功

传递对象类型的消息

思路:在传递消息时,将对象转为json字符串,在接收时再解析

1、controller实现发送

复制代码
package com.heima.kafkademo.controller;

import com.alibaba.fastjson.JSON;
import com.heima.kafkademo.model.User;
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.RestController;

@RestController
public class HelloController {

    @Autowired
    KafkaTemplate<String,String> kafkaTemplate;

    @GetMapping("/hello")
    public String addMessage(){
        User user = new User();
        user.setName("勒布朗");
        user.setAge(37);
        user.setGender("男");
        user.setJob("NBA球员");
        kafkaTemplate.send("lakers-topic", JSON.toJSONString(user));
        return "ok";
    }
}

2、component实现接收类

复制代码
package com.heima.kafkademo.component;

import com.alibaba.fastjson.JSON;
import com.heima.kafkademo.model.User;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
public class HelloListener {

    @KafkaListener(topics = "lakers-topic")
    public void onMessage(String msg){
        System.out.println(JSON.parseObject(msg, User.class));
    }

}

3、打印测试结果

相关推荐
淡云微晴1 小时前
Zookeeper 分布式协调服务
分布式·zookeeper
Wang's Blog1 小时前
RabbitMQ: 集群网络分区的深度解析之意义、风险与处理策略
网络·分布式·rabbitmq
掉鱼的猫1 小时前
超越 SpringBoot 4.0了吗?OpenSolon v3.8, v3.7.4, v3.6.7 发布
java·spring boot
毕设源码-邱学长1 小时前
【开题答辩全过程】以 基于springboot的社区团购小程序设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
Wang's Blog2 小时前
RabbitMQ: 分布式事务消息处理框架之实现可靠消息方案 —— 枚举定义、实体建模、存储层实现与定时任务调度
分布式·rabbitmq
C++业余爱好者2 小时前
Spring Boot 应用程序中的进程与线程管理:从JAR启动到请求响应的完整分析
spring boot·后端·jar
要开心吖ZSH2 小时前
Spring Boot + JUnit 5 + Mockito + JaCoCo 单元测试实战指南
java·spring boot·junit·单元测试
搬砖的kk2 小时前
Flutter适配鸿蒙:跨平台力量为鸿蒙生态注入增长新动能
分布式·flutter·harmonyos
Wang's Blog2 小时前
Kafka: 动态配置刷新与分布式配置管理深度实践
分布式·kafka
enjoy编程2 小时前
Spring AI 深度重构 renren-security,基于 Java 21 虚拟线程打造极致高并发脚手架
java·spring boot·spring·重构·虚拟线程·spring boot 4·virtual thread