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、打印测试结果

相关推荐
琴智冰17 分钟前
SpringBoot
java·数据库·spring boot
爱码少年19 分钟前
springboot工程中使用tcp协议
spring boot·后端·tcp/ip
《源码好优多》36 分钟前
基于SpringBoot+Vue+Uniapp的植物园管理小程序系统(2024最新,源码+文档+远程部署+讲解视频等)
vue.js·spring boot·uni-app
计算机学姐1 小时前
基于微信小程序的调查问卷管理系统
java·vue.js·spring boot·mysql·微信小程序·小程序·mybatis
奔跑吧邓邓子7 小时前
大数据利器Hadoop:从基础到实战,一篇文章掌握大数据处理精髓!
大数据·hadoop·分布式
2401_857622668 小时前
SpringBoot框架下校园资料库的构建与优化
spring boot·后端·php
2402_857589368 小时前
“衣依”服装销售平台:Spring Boot框架的设计与实现
java·spring boot·后端
哎呦没9 小时前
大学生就业招聘:Spring Boot系统的架构分析
java·spring boot·后端
编程、小哥哥10 小时前
netty之Netty与SpringBoot整合
java·spring boot·spring
IT学长编程11 小时前
计算机毕业设计 玩具租赁系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·玩具租赁系统