一、单机安装部署
- 下载安装
cd /data/program
wget https://mirror.bit.edu.cn/apache/kafka/2.6.0/kafka_2.13-2.6.0.tgz
tar -xzvf kafka_2.13-2.6.0.tgz
cd kafka_2.13-2.6.0
2.启动zookeeper
kafka需要依赖ZK,安装包中已经自带了一个ZK,也可以改成指定已运行的ZK。
如果改成指定的ZK需要修改修改 kafka 安装目录下的 config/server.properties 文件中的 zookeeper.connect 。这里使用自带的ZK。
nohup ./bin/zookeeper-server-start.sh config/zookeeper.properties >> zookeeper.nohup &
检查zookeeper是否启动成功:
ps -ef|grep zookeeper
- 启动kafka
修改相关配置
vi config/server.properties
Broker ID启动以后就不能改了
broker.id=1
取消注释,改成本机IP:
listeners=PLAINTEXT://192.168.184.48:9092
num.partitions后面增加2行。
发现不存在topic自动创建。允许永久删除topic。
num.partitions=1
auto.create.topics.enable=true
delete.topic.enable=true
后台启动kafka(kafka安装目录下),日志在logs目录下:
nohup ./bin/kafka-server-start.sh ./config/server.properties &
- 创建Topic
创建一个名为gptest的topic,只有一个副本,一个分区:
sh bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic gptest
查看已经创建的 topic:
sh bin/kafka-topics.sh -list -zookeeper localhost:2181
- 启动Producer
打开一个窗口,在kafka解压目录下:
sh bin/kafka-console-producer.sh --broker-list 192.168.184.48:9092 --topic gptest
- 启动Consumer
在一个新的远程窗口中:
sh bin/kafka-console-consumer.sh --bootstrap-server 192.168.184.48:9092 --topic gptest --from-beginning
- Producer窗口发送消息
输入hello world 回车
- 删除kafka全部数据步骤:
- 停止每台机器上的kafka;
- .删除kafka存储目录(server.properties文件log.dirs配置,默认为“/tmp/kafka-logs”)全部topic的数据目录;
- 删除zookeeper上与kafka相关的znode节点;除了/zookeeper
- 重启kafka。
二、kafka集群搭建
三个节点:
192.168.184.45
192.168.184.46
192.168.184.47
1.安装zk集群
1). 安装zookeeper
cd /data/program/
mkdir zookeeper
cd zookeeper/
#xftp上传 apache-zookeeper-3.6.2-bin.tar.gz
tar -zxvf apache-zookeeper-3.6.2-bin.tar.gz
2).修改配置文件zoo.cfg
cd /data/program/zookeeper/apache-zookeeper-3.6.2-bin/conf
cp zoo_sample.cfg zoo.cfg
----------------------------------------------------------------------------
# The number of milliseconds of each tick
#时间单元,zk中的所有时间都是以该时间单元为基础,进行整数倍配置(单位是毫秒,下面配置的是2秒)
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
#follower在启动过程中,会从leader同步最新数据需要的最大时间。如果集群规模比较大,可以调大该参数
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
#leader与集群中所有机器进行心跳检查的最大时间。如果超出该时间,某follower没有回应,则说明该follower下线
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
#事务日志输出目录
dataDir=/tmp/zookeeper
# the port at which the clients will connect
#客户端连接端口
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#需要保留文件数目,默认就是3个
autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#自动清理事务日志和快照文件的频率,这里是1个小时
autopurge.purgeInterval=1
#集群服务器配置,数字1/2/3需要与myid文件一致。右边两个端口,2888表示数据同步和通信端口;3888表示选举端口
server.1=192.168.184.45:2888:3888
server.2=192.168.184.46:2888:3888
server.3=192.168.184.47:2888:3888
3).创建数据目录和myid
mkdir -p /tmp/zookeeper
cd /tmp/zookeeper
#设置myid的文件内容为1,2,3
vi myid
4).启动zookeeper
cd /data/program/zookeeper/apache-zookeeper-3.6.2-bin/bin/
sh zkServer.sh start
5).查看节点状态
sh zkServer.sh status
2.安装kafka集群
1). 下载kafka
cd /data/program/kafka
wget https://mirror.bit.edu.cn/apache/kafka/2.6.0/kafka_2.13-2.6.0.tgz
tar -xzvf kafka_2.13-2.6.0.tgz
cd kafka_2.13-2.6.0
2). 修改配置
cd /data/program/kafka/kafka_2.13-2.6.0/config
vi server.properties
broker.id=1[2,3]
listeners=PLAINTEXT://192.168.184.45[46,47]:9092
num.partitions=1
auto.create.topics.enable=true
delete.topic.enable=true
zookeeper.connect=192.168.184.45:2181,192.168.184.46:2181,192.168.184.47:2181
3). 启动kafka
nohup ./bin/kafka-server-start.sh ./config/server.properties &
4).查看是否有kafka进程
5). 创建topic
sh bin/kafka-topics.sh --create --zookeeper 192.168.184.45:2181,192.168.184.46:2181,192.168.184.47:2181 --replication-factor 1 --partitions 3 --topic p3r1test
6).查看已经创建的 topic:
sh bin/kafka-topics.sh -list -zookeeper 192.168.184.45:2181,192.168.184.46:2181,192.168.184.47:2181
- 创建生产者Producer
打开一个窗口,在kafka解压目录下:
sh bin/kafka-console-producer.sh --broker-list 192.168.184.45:9092,192.168.184.46:9092,192.168.184.47:9092 --topic p3r1test
- 启动Consumer
在一个新的远程窗口中:
sh bin/kafka-console-consumer.sh --bootstrap-server 192.168.184.45:9092,192.168.184.46:9092,192.168.184.47:9092 --topic p3r1test --from-beginning
- Producer窗口发送消息