当前位置: 首页 > news >正文

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-demokafka:bootstrap-servers: 192.168.200.130:9092producer:retries: 10key-serializer: org.apache.kafka.common.serialization.StringSerializervalue-serializer: org.apache.kafka.common.serialization.StringSerializerconsumer:group-id: ${spring.application.name}-testkey-deserializer: org.apache.kafka.common.serialization.StringDeserializervalue-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 {@AutowiredKafkaTemplate<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 {@AutowiredKafkaTemplate<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、打印测试结果

 

http://www.lryc.cn/news/117196.html

相关文章:

  • 本质矩阵E、基本矩阵F、单应矩阵H
  • Oracle database Linux自建环境备份至远端服务器自定义保留天数
  • SpringBoot异步任务(2)|(线程池使用)
  • 解决Windows:Call to undefined function exif_imagetype()
  • 【Spring】Spring AOP 初识及实现原理解析
  • 【Express.js】集成Redis
  • StringBuilder创建的对象如何清空
  • mybatis-plus实现mysql自定义IKeyGenerator
  • 山西电力市场日前价格预测【2023-08-11】
  • 浏览器无法连接网络问题
  • ZyjDataLink 全量MySQL同步程序 - 开发过程 01
  • 为什么说Python股票接口是连接投资与编程的桥梁?
  • kubectl,helm安装到window
  • 【BASH】回顾与知识点梳理(目录)
  • TFRecords详解
  • 【多维定向滤波器组和表面波】表面变换:用于高效表示多维 s 的多分辨率变换(Matlab代码实现)
  • 45.113.201.X服务器远程不上是什么原因,有什么办法解决?
  • 微信小程序 地图map(电子围栏圆形和多边形)
  • Dockerfile 文件
  • ssm学院党员管理系统源码和论文PPT
  • 文件数字水印,附一种纯文本隐写术数字水印方法
  • 测试开发(一) 使用Vue开发chrome插件
  • 游戏行业实战案例 4 :在线时长分析
  • 记一次图片压缩引发的生产问题
  • mybatis-flex探索
  • 用ClickHouse 文件表引擎快速查询分析文件数据
  • esp8266httpclient_get_post使用
  • 【Spring】创建一个Spring项目与Bean对象的存储
  • Docker的入门与使用
  • Smart HTML Elements 16.1 Crack