前提:
Linux版本:CentOS release 6.6 (Final)
JDK版本:jdk-7u71-linux-x64.tar.gz
zookeeper版本:zookeeper-3.3.6.tar.gz
kafka版本:kafka_2.11-0.9.0.1.tgz
1、安装JDK
步骤1:新建路径/usr/java, 并在其下解压 jdk-7u71-linux-x64.tar.gz
mkdir -p /usr/java
tar -zxvf jdk-7u71-linux-x64.tar.gz
- 1
- 2
步骤2:添加JDK到系统环境变量
[root@test java]# vi /etc/profile
新增以下内容:
export JAVA_HOME=/usr/java/jdk1.7.0_71
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
步骤3:使配置生效 source /etc/profile #使配置文件立即生效
**成功标志:[root@test java]# java -version
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)
2、安装zookeeper
步骤1:解压zookeeper安装包(路径无关,我放在/home/自己的路径下了)
tar -zxvf zookeeper-3.3.6.tar.gz
步骤2:配置环境变量
export zookeeper_home=/home/laoyang/zookeeper
步骤3:使配置生效
source /etc/profile
**成功标志:
[root@test bin]# sh $zookeeper_home/bin/zkServer.sh start
JMX enabled by default
Using config: /home/laoyang/zookeeper/bin/../conf/zoo.cfg
grep: /home/laoyang/zookeeper/bin/../conf/zoo.cfg: No such file or directory
Starting zookeeper ... STARTED
3、安装kafka
步骤1:解压&移动到/usr/local
tar -xzvf kafka_2.11-0.9.0.1.tgz
mv kafka_2.11-0.9.0.1 /usr/local/kafka
步骤2:配置kafka,修改server.properties
mkdir /usr/local/kafka/log/kafka #创建kafka日志目录
cd /usr/local/kafka/config #进入配置目录
vi server.properties #编辑修改相应的参数
broker.id=0
port=9092 #端口号
host.name=10.8.5.101 #服务器IP地址,修改为自己的服务器IP
log.dirs=/usr/local/kafka/log/kafka #日志存放路径,上面创建的目录
zookeeper.connect=localhost:2181 #zookeeper地址和端口,单机配置部署,localhost:2181
步骤3、配置kafka下的zookeeper
mkdir /usr/local/kafka/zookeeper #创建zookeeper目录
mkdir /usr/local/kafka/log/zookeeper #创建zookeeper日志目录
cd /usr/local/kafka/config #进入配置目录
vi zookeeper.properties #编辑修改相应的参数
dataDir=/usr/local/kafka/zookeeper #zookeeper数据目录
dataLogDir=/usr/local/kafka/log/zookeeper #zookeeper日志目录
clientPort=2181
maxClientCnxns=100
tickTime=2000
initLimit=10
步骤4:创建启动、关闭kafka脚本
1)创建启动kafka脚本
vi kafkastart.sh #编辑,添加以下代码
#!/bin/bash
#启动zookeeper
/usr/local/kafka/bin/zookeeper-server-start.sh /usr/local/kafka/config/zookeeper.properties &
sleep 3 #等3秒后执行
- 1
- 2
- 3
- 4
#启动kafka
/usr/local/kafka/bin/kafka-server-start.sh /usr/local/kafka/config/server.properties &
- 1
- 2
2)创建关闭kafka脚本
#!/bin/bash
#关闭zookeeper
/usr/local/kafka/bin/zookeeper-server-stop.sh /usr/local/kafka/config/zookeeper.properties &
sleep 3 #等3秒后执行
#关闭kafka
/usr/local/kafka/bin/kafka-server-stop.sh /usr/local/kafka/config/server.properties &
- 1
- 2
- 3
- 4
- 5
- 6
3)#添加脚本执行权限
chmod +x kafkastart.sh
chmod +x kafkastop.sh
- 1
- 2
4)设置脚本开机自动执行
vi /etc/rc.d/rc.local #编辑,在最后添加一行
sh /usr/local/kafka/kafkastart.sh & #设置开机自动在后台运行脚本
sh /usr/local/kafka/kafkastart.sh #启动kafka
sh /usr/local/kafka/kafkastop.sh #关闭kafka
至此,经过1,2,3Linux下Kafka单机安装配置完成。
成功标志:
见下测试。
4、测试kafka
步骤1:启动zookeeper
[root@test bin]# sh $zookeeper_home/bin/zkServer.sh start
JMX enabled by default
Using config: /home/laoyang/zookeeper/bin/../conf/zoo.cfg
grep: /home/laoyang/zookeeper/bin/../conf/zoo.cfg: No such file or directory
Starting zookeeper ... STARTED
步骤2:启动kafka
[root@test kafka]# ./kafkastart.sh
步骤3:测试创建topic
cd /usr/local/kafka/bin
./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
步骤4:通过list命令查看创建的topic
cd /usr/local/kafka/bin
./kafka-topics.sh –list –zookeeper localhost:2181
步骤5:生产消息测试
[root@test bin]# ./kafka-console-producer.sh --broker-list localhost:9092 --topic test
laoyang I love you!
步骤6:消费消息测试
[root@test bin]# ./kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
I'm laoyang #之前测试输入的内容
laoyang I love you!
经过以上6步,代表kafka安装成功。
5、停止kafka
步骤1:停止Kafka
cd /usr/local/kafka
[root@test kafka]# ./kafkastop.sh
步骤2:停止Zookeeper server
sh $zookeeper_home/bin/zkServer.sh stop
至此,启动,测试,停止全过程ok。
作者:铭毅天下