kafka单机环境搭建和测试
文章目录
前言
环境准备
在安装单机版kafka时,本机已具备以下环境基础。
- Linux版本:CentOS 7
- JDK版本: jdk-7u71-linux-x64.tar.gz
安装包准备
在Kafka的设计中,选择了使用Zookeeper来进行所有Broker的管理。所以需要下载以下两个依赖包
需要下载的安装包:
-
zookeeper-3.4.5.tar.gz
下载地址:https://archive.apache.org/dist/zookeeper/
-
kafka-2.1.0.tgz
下载地址:https://archive.apache.org/dist/kafka/2.1.0/
接下来就进入简单粗暴安装测试主题
1 安装zookeeper
1.1 解压安装包
上传安装包到/usr/app下,该路径自定义。解压
# tar -zxvf zookeeper-3.4.5.tar.gz
1.2 配置环境变量
# vim /etc/profile
添加环境变量:
export ZOOKEEPER_HOME=/usr/app/zookeeper-3.4.5
export PATH=$ZOOKEEPER_HOME/bin:$PATH
使得配置的环境变量生效:
# source /etc/profile
1.3 修改配置
进入安装目录的 conf/
目录下,拷贝配置样本并进行修改:
# cp zoo_sample.cfg zoo.cfg
指定数据存储目录和日志文件目录(目录不用预先创建,程序会自动创建),修改后完整配置如下:
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# 修改日志目录和数据目录,这两个目录待会配置kafka时会用到.
dataDir=/usr/local/zookeeper/data
dataLogDir=/usr/local/zookeeper/log
# 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
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
配置参数说明:
- tickTime:用于计算的基础时间单元。比如 session 超时:N*tickTime;
- initLimit:用于集群,允许从节点连接并同步到 master 节点的初始化连接时间,以 tickTime 的倍数来表示;
- syncLimit:用于集群, master 主节点与从节点之间发送消息,请求和应答时间长度(心跳机制);
- dataDir:数据存储位置;
- dataLogDir:日志目录;
- clientPort:用于客户端连接的端口,默认 2181
1.4 启动
由于已经配置过环境变量,直接使用下面命令启动即可:
zkServer.sh start
1.5 验证
使用 JPS 验证进程是否已经启动,出现 QuorumPeerMain
则代表启动成功。
[[email protected] bin]# jps
3814 QuorumPeerMain
2 安装验证kafka
2.1 解压安装包
上传安装包到/opt/software下,该路径自定义。解压
# tar -zxvf zookeeper-3.4.5.tar.gz
2.2 修改配置
#创建日志目录,在配置时可配置日志输出路径
mkdir /opt/logs/kafka
修改kafka/config下的server.properties
# The id of the broker. 集群中每个节点的唯一标识
broker.id=0
# 监听地址
listeners=PLAINTEXT://hadoop001:9092
# 数据的存储位置
log.dirs=/usr/local/kafka-logs/00
# Zookeeper连接地址
zookeeper.connect=hadoop001:2181
2.3 启动
bin/kafka-server-start.sh config/server.properties
2.4 测试
进入kafka安装的目录,创建测试主题:
注:hadoop001:2181 是本机安装的zookeeper地址
bin/kafka-topics.sh --create --zookeeper hadoop001:2181 --replication-factor 1 --partitions 1 --topic test
查看topic
kafka-topics.sh --list --zookeeper hadoop001:2181
bin/kafka-topics.sh --zookeeper hadoop001:2181 --describe --topic test
kafka生产消息
bin/kafka-console-producer.sh --broker-list hadoop001:9092 --topic test
bin/kafka-console-producer.sh --broker-list hadoop001:9092 --topic test
kafka消费信息
bin/kafka-console-consumer.sh --bootstrap-server hadoop001:9092 --from-beginning --topic test