likes
comments
collection
share

教你如何搞定springboot集成kafka

作者站长头像
站长
· 阅读数 27

安装kafka

启动Kafka本地环境需Java 8+以上

Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者在网站中的所有动作流数据。

Kafka启动方式有Zookeeper和Kraft,两种方式只能选择其中一种启动,不能同时使用。

Kafka下载downloads.apache.org/kafka/3.7.0…

教你如何搞定springboot集成kafka

解压tar -xzf kafka_2.13-3.7.0.tgz

教你如何搞定springboot集成kafka

一、Zookeeper启动Kafka(kafka内置zookeeper)

Kafka依赖Zookeeper

1、启动Zookeeper 2、启动Kafka

使用kafka自带Zookeeper启动

./zookeeper-server-start.sh ../config/zookeeper.properties &

./zookeeper-server-stop.sh ../config/zookeeper.properties

教你如何搞定springboot集成kafka

./kafka-server-start.sh ../config/server.properties &

教你如何搞定springboot集成kafka

./kafka-server-stop.sh ../config/server.properties

二、Zookeeper服务器启动Kafka

Zookeeper服务器安装

zookeeper.apache.org/

教你如何搞定springboot集成kafka

dlcdn.apache.org/zookeeper/z…

tar zxvf apache-zookeeper-3.9.2-bin.tar.gz

教你如何搞定springboot集成kafka

教你如何搞定springboot集成kafka

配置Zookeeper服务器

cp zoo_sample.cfg zoo.cfg

教你如何搞定springboot集成kafka

启动Zookeeper服务器

教你如何搞定springboot集成kafka

./zkServer.sh start

修改Zookeeper端口

教你如何搞定springboot集成kafka

Zoo.cfg添加内容

admin.serverPort=8099

apache-zookeeper-3.9.2-bin/bin目录下重启Zookeeper

教你如何搞定springboot集成kafka

Zookeeper服务器启动kafka

/opt/kafka_2.13-3.7.0/bin目录下

./kafka-server-start.sh ../config/server.properties &

教你如何搞定springboot集成kafka

Kafka配置文件server.properties

教你如何搞定springboot集成kafka

三、使用KRaft启动Kafka

教你如何搞定springboot集成kafka

UUID通用唯一识别码(Universally Unique Identifier)

1、生成Cluster UUID(集群UUID):./kafka-storage.sh random-uuid

教你如何搞定springboot集成kafka

2.格式化kafka日志目录:./kafka-storage.sh format -t 3pMJGNJcT0uLIBsZhbucjQ -c ../config/kraft/server.properties

教你如何搞定springboot集成kafka

3.启动kafka:./kafka-server-start.sh ../config/kraft/server.properties &

教你如何搞定springboot集成kafka

springboot集成kafka

教你如何搞定springboot集成kafka

创建topic时,若不指定topic的分区(partition)数量使,则默认为1个分区(partition)

教你如何搞定springboot集成kafka

修改server.properties文件

vim server.properties

listeners=PLAINTEXT://0.0.0.0:9092

教你如何搞定springboot集成kafka

advertised.listeners=PLAINTEXT://192.168.68.133:9092

教你如何搞定springboot集成kafka

教你如何搞定springboot集成kafka

教你如何搞定springboot集成kafka

springboot加入依赖kafka

<dependency><groupId>org.springframework.kafka</groupId><artifactId>spring-kafka</artifactId></dependency>

加入spring-kafka依赖后,springboot自动装配好kafkaTemplate的Bean

教你如何搞定springboot集成kafka

application.yml配置连接kafka

spring:kafka:bootstrap-servers: 192.168.68.133:9092

生产者

发送消息

教你如何搞定springboot集成kafka

@Resourceprivate KafkaTemplate<String,String> kafkaTemplate;@Testvoid kafkaSendTest(){kafkaTemplate.send("kafkamsg01","hello kafka");}

教你如何搞定springboot集成kafka

消费者

接收消息

教你如何搞定springboot集成kafka

@Componentpublic class KafkaConsumer {@KafkaListener(topics = {"kafkamsg01","test"},groupId = "123")public void consume(String message){System.out.println("接收到消息:"+message);}}

若没有配置groupid

Failed to start bean 'org.springframework.kafka.config.internalKafkaListenerEndpointRegistry'; nested exception is java.lang.IllegalStateException: No group.id found in consumer config, container properties, or @KafkaListener annotation; a group.id is required when group management is used.

教你如何搞定springboot集成kafka

教你如何搞定springboot集成kafka

@Componentpublic class KafkaConsumer {@KafkaListener(topics = {"kafkamsg01","test"},groupId = "123")public void consume(String message){System.out.println("接收到消息:"+message);}}

教你如何搞定springboot集成kafka

转载自:https://juejin.cn/post/7369397062863519795
评论
请登录