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

SpringBoot3集成Kafka

标签:Kafka3.Kafka-eagle3;

一、简介

Kafka是一个开源的分布式事件流平台,常被用于高性能数据管道、流分析、数据集成和关键任务应用,基于Zookeeper协调的处理平台,也是一种消息系统,具有更好的吞吐量、内置分区、复制和容错,这使得它成为大规模消息处理应用程序的一个很好的解决方案;

二、环境搭建

1、Kafka部署

1、下载安装包:kafka_2.13-3.5.0.tgz2、配置环境变量open -e ~/.bash_profileexport KAFKA_HOME=/本地路径/kafka3.5
export PATH=$PATH:$KAFKA_HOME/binsource ~/.bash_profile3、该目录【kafka3.5/bin】启动zookeeper
zookeeper-server-start.sh ../config/zookeeper.properties4、该目录【kafka3.5/bin】启动kafka
kafka-server-start.sh ../config/server.properties

2、Kafka测试

1、生产者
kafka-console-producer.sh --broker-list localhost:9092 --topic test-topic
>id-1-message
>id-2-message2、消费者
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test-topic
id-1-message
id-2-message3、查看topic列表
kafka-topics.sh --bootstrap-server localhost:9092 --list
test-topic4、查看消息列表
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test-topic --from-beginning --partition 0
id-1-message
id-2-message

3、可视化工具

配置和部署

1、下载安装包:kafka-eagle-bin-3.0.2.tar.gz2、配置环境变量open -e ~/.bash_profileexport KE_HOME=/本地路径/efak-web-3.0.2
export PATH=$PATH:$KE_HOME/binsource ~/.bash_profile3、修改配置文件:system-config.propertiesefak.zk.cluster.alias=cluster1
cluster1.zk.list=localhost:2181
efak.url=jdbc:mysql://127.0.0.1:3306/kafka-eagle4、本地新建数据库:kafka-eagle,注意用户名和密码是否一致5、启动命令
efak-web-3.0.2/bin/ke.sh start
命令语法: ./ke.sh {start|stop|restart|status|stats|find|gc|jdk|version|sdate|cluster}6、本地访问【localhost:8048】 username:admin password:123456

KSQL语句测试

select * from `test-topic` where `partition` in (0)  order by `date` desc limit 5

select * from `test-topic` where `partition` in (0) and msg like '%5%' order by `date` desc limit 3

三、工程搭建

1、工程结构

2、依赖管理

这里关于依赖的管理就比较复杂了,首先spring-kafka组件选择与boot框架中spring相同的依赖,即6.0.10版本,在spring-kafka最近的版本中3.0.8符合;

但是该版本使用的是kafka-clients组件的3.3.2版本,在Spring文档的kafka模块中,明确说明spring-boot:3.1要使用kafka-clients:3.4,所以从spring-kafka组件中排除掉,重新依赖kafka-clients组件;

<dependency><groupId>org.springframework.kafka</groupId><artifactId>spring-kafka</artifactId><version>${spring-kafka.version}</version><exclusions><exclusion><groupId>org.apache.kafka</groupId><artifactId>kafka-clients</artifactId></exclusion></exclusions>
</dependency>
<dependency><groupId>org.apache.kafka</groupId><artifactId>kafka-clients</artifactId><version>${kafka-clients.version}</version>
</dependency>

3、配置文件

配置kafka连接地址,监听器的消息应答机制,消费者的基础模式;

spring:# kafka配置kafka:bootstrap-servers: localhost:9092listener:missing-topics-fatal: falseack-mode: manual_immediateconsumer:group-id: boot-kafka-groupenable-auto-commit: falsemax-poll-records: 10properties:max.poll.interval.ms: 3600000

四、基础用法

1、消息生产

模板类KafkaTemplate用于执行高级的操作,封装各种消息发送的方法,在该方法中,通过topickey以及消息主体,实现消息的生产;

@RestController
public class ProducerWeb {@Resourceprivate KafkaTemplate<String, String> kafkaTemplate;@GetMapping("/send/msg")public String sendMsg (){try {// 构建消息主体JsonMapper jsonMapper = new JsonMapper();String msgBody = jsonMapper.writeValueAsString(new MqMsg(7,"boot-kafka-msg"));// 发送消息kafkaTemplate.send("boot-kafka-topic","boot-kafka-key",msgBody);} catch (JsonProcessingException e) {e.printStackTrace();}return "OK" ;}
}

2、消息消费

编写消息监听类,通过KafkaListener注解控制监听的具体信息,在实现消息生产和消费的方法测试后,使用可视化工具kafka-eagle查看topic和消息列表;

@Component
public class ConsumerListener {private static final Logger log = LoggerFactory.getLogger(ConsumerListener.class);@KafkaListener(topics = "boot-kafka-topic")public void listenUser (ConsumerRecord<?,String> record, Acknowledgment acknowledgment) {try {String key =  String.valueOf(record.key());String body = record.value();log.info("\n=====\ntopic:boot-kafka-topic,key{},body:{}\n=====\n",key,body);} catch (Exception e){e.printStackTrace();} finally {acknowledgment.acknowledge();}}
}

五、参考源码

文档仓库:
https://gitee.com/cicadasmile/butte-java-note源码仓库:
https://gitee.com/cicadasmile/butte-spring-parent
http://www.lryc.cn/news/129778.html

相关文章:

  • css学习1
  • rust踩雷笔记(1)——切片传参和解引用赋值
  • 安全 1自测
  • 寻路算法小游戏
  • CSS基础 知识点总结
  • 自动执行探索性数据分析 (EDA),更快、更轻松地理解数据
  • 【自定义系统服务】【android13】添加自定义java系统服务
  • 【Sklearn】基于随机梯度下降算法的数据分类预测(Excel可直接替换数据)
  • 44、TCP报文(二)
  • 目标检测(Object Detection)
  • vue中实现文字检索时候将搜索内容标红
  • PCL protocol composition logic
  • 聊聊看React和Vue的区别
  • OSPF在广播类型的网络拓扑中DR和BDR的选举
  • 系统学习Linux-Mariadb高可用MHA
  • 慢SQL的原因
  • php正则替换文章的图片
  • 57 | TAPTAP客户端分析
  • 开源了一套基于springboot+vue+uniapp的商城,包含分类、sku、商户管理、分销、会员、适合企业或个人二次开发
  • Android进阶之多级列表
  • Stochastic: Distribution-Expectation-Inequalities
  • Java算法_ 二叉树的最大深度(LeetCode_Hot100)
  • 行业追踪,2023-08-18
  • js将项目中的图片上传到服务器
  • 【C语言】指针的进阶
  • 【Windows系统编程】03.远线程注入ShellCode
  • 第1天----验证一个字符串是否是另一个字符串的子串
  • 项目实战第四十三讲:使用模版模式优雅实现财务编辑费用
  • [JavaWeb]【六】web后端开发-请求响应
  • uniapp websocket机制 心跳 重连