Java/SpringBoot + Kafka + Zookeeper(生产者和消费者实现)

教程为本地虚拟机安装:通过MobaxTerm连接本地Vmware搭建的Centos7环境

一.Zookeeper安装

1.下载:ZookeeperApache

在这里插入图片描述

2.安装

# 1.解压zookeeper到安装目录
tar zxvf apache-zookeeper-3.8.0-bin.tar.gz
mv apache-zookeeper-3.8.0-bin /usr/local/zookeeper
# 2.修改配置文件(复制示例文件),指定持久的zookeeper数据目录,如:/mnt/data/zookeeper
cd /usr/local/zookeeper/conf
cp zoo_sample.cfg zoo.cfg
vi zoo.cfg
# 3.启动服务
cd ..
bin/zkServer.sh start conf/zoo.cfg
# 4.查看状态
bin/zkServer.sh status
# 5.客户端连接
bin/zkCli.sh
# 6.连接远程的zookeeper server
bin/zkCli.sh ‐ server ip:port

在这里插入图片描述

3.配置文件说明

# The number of milliseconds of each tick
# 最小时间片,单位毫秒
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
# Follower初始化连接Leader超时设置,表示Tick倍数,即:tickTime * initLimit (ms)
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
# Follower与Leader同步数据超时设置,表示Tick倍数,即:tickTime * initLimit (ms)
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
# zookeeper 数据存储目录
dataDir=/tmp/zookeeper
# the port at which the clients will connect
# zookeeper 服务端口
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
# 单个客户端与zookeeper最大并发连接数
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# https://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
# 保存数据快照数量 超出后回滚
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
# 自动触发清除任务时间间隔 单位小时 默认为 0 表示不清空
#autopurge.purgeInterval=1

## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpHost=0.0.0.0
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true

二.Kafka安装

1.下载:KafkaApache

在这里插入图片描述

2.安装

# 1.解压并移动
tar zxvf kafka_2.13-3.2.0.tgz
mv kafka_2.13-3.2.0 /usr/local/kafka
# 2.修改配置文件
cd /usr/local/kafka/
vi config/server.properties
# 3.启动服务
bin/kafka-server-start.sh -daemon config/server.properties
# 4.连接ZK客户端查看Kafka节点
bin/zkCli.sh
ls /brokers/ids
# 5.停止Kafka
bin/kafka-server-stop.sh

3.配置文件说明

# broker.id 属性在kafka集群中必须要是唯一
broker.id=0
# kafka 本机地址和端口
listeners=PLAINTEXT://192.168.184.128:9092
# kafka 的消息存储文件
log.dir=/mnt/data/kafka‐logs
# kafka 连接 zookeeper 的地址
zookeeper.connect=192.168.184.128:2181

4.本机Java服务查看

jps

5.创建主题

# 1.创建只有一个Partition 且备份因子数也设置为1的主题
bin/kafka-topics.sh --bootstrap-server 192.168.184.128:9092 --create --replication-factor 1 --partitions 1 --topic moon
# 2.查看Topic列表
bin/kafka-topics.sh --bootstrap-server 192.168.184.128:9092 --list
# 3.删除主题
bin/kafka-topics.sh --bootstrap-server 192.168.184.128:9092 --delete --topic moon

在这里插入图片描述

6.收发消息

# 1.生产者
bin/kafka-console-producer.sh --broker-list 192.168.184.128:9092 --topic moon
>i am msg
>this is a fish
>exit
>quit
# 2.消费者(默认为开始消费当前之后的数据)
bin/kafka-console-consumer.sh --bootstrap-server 192.168.184.128:9092 --topic moon
# 3.从头消费
bin/kafka-console-consumer.sh --bootstrap-server 192.168.184.128:9092 --from-beginning --topic moon

7.Kafka集群

通过创建多个Broker实例演示

# 1.复制服务端配置文件
cp server.properties server_1.properties
cp server.properties server_2.properties
# 2.编辑 broker.id 分别设为1、2 
#       修改日志目录 /mnt/data/kafka‐logs-1 /mnt/data/kafka‐logs-2
#       端口 9093 9094
vi server_1.properties
vi server_2.properties
# 3.启动服务
bin/kafka-server-start.sh -daemon config/server_1.properties
bin/kafka-server-start.sh -daemon config/server_2.properties

查看Zookeeper注册节点信息

bin/zkCli.sh
[zk: localhost:2181(CONNECTED) 0] ls /brokers/ids

在这里插入图片描述
Topic创建和查看

# 1.创建一个副本数为 3 和 分区数为 2 的主题
bin/kafka-topics.sh --bootstrap-server 192.168.184.128:9092 --create --replication-factor 3 --partitions 2 --topic sun
# 2.查看Topic信息
bin/kafka-topics.sh --bootstrap-server 192.168.184.128:9092 --describe --topic sun
# 3.向集群发送消息
bin/kafka-console-producer.sh --broker-list 192.168.184.128:9092,192.168.184.128:9093,192.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猪悟道

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值