手拉手安装Kafka2.13发送和消费消息

发布于:2024-04-23 ⋅ 阅读:(18) ⋅ 点赞:(0)

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

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

Kafka下载https://downloads.apache.org/kafka/3.7.0/kafka_2.13-3.7.0.tgz

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

一、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

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

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

二、Zookeeper服务器启动Kafka

Zookeeper服务器安装

https://zookeeper.apache.org/

https://dlcdn.apache.org/zookeeper/zookeeper-3.9.2/apache-zookeeper-3.9.2-bin.tar.gz

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

配置Zookeeper服务器

cp zoo_sample.cfg zoo.cfg

启动Zookeeper服务器

./zkServer.sh start

修改Zookeeper端口

Zoo.cfg添加内容

admin.serverPort=8099

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

Zookeeper服务器启动kafka

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

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

Kafka配置文件server.properties

三、使用KRaft启动Kafka

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

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

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

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

使用kafka-topics.sh脚本创建主题

./kafka-topics.sh --create --topic 主题名 --bootstrap-server localhost:9092

列出所有主题

./kafka-topics.sh --list --bootstrap-server localhost:9092

查看主题详情

./kafka-topics.sh --describe --topic 主题名 --bootstrap-server localhost:9092

删除主题

./kafka-topics.sh --delete --topic 主题名 --bootstrap-server localhost:9092

使用kafka-console-producer.sh脚本发送消息

./kafka-console-producer.sh --topic test01 --bootstrap-server localhost:9092

使用kafka-console-consumer.sh脚本消费消息

./kafka-console-consumer.sh --topic test01 --from-beginning  --bootstrap-server localhost:9092

--from-beginning

If the consumer does not already have an established offset to consume from, start with the earliest message present in the log rather than the latest message.

使用了--from-beginning参数(偏移量),是从最早消息开始读,不加--from-beginning参数从最新消息开始

Docker安装

列出已安装的docker

yum list installed | grep docker

删除旧版本docker

yum remove dockerxxx –y

安装最新版docker

yum install yum-utils -y

安装源

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

安装docker

yum -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y

查看docker镜像

docker images

docker搜索kafka镜像

docker search kafka

拉去镜像

docker pull apache/kafka:3.7.0

启动kafka容器

-p映射端口 主机端口:容器端口

docker run -p 9092:9092 apache/kafka:3.7.0

#后台运行 -d

docker run -p 9092:9092 -d apache/kafka:3.7.0

docker run [Options] image运行容器

docker run [Options] image

#参数说明

--name="名字"           指定容器名字

-d                     后台方式运行

-it                    使用交互方式运行,进入容器查看内容

-p                     指定容器的端口

       -p ip:主机端口:容器端口  配置主机端口映射到容器端口

       -p 主机端口:容器端口(常用)

       -p 容器端口

-P                     随机指定端口

-e                                  环境设置

-v                                   容器数据卷挂载

docker ps

移除镜像

docker rmi pache/kafka:3.7.0