一,kafka解压到/usr/local/下后,修改配置文件
vim config/zookeeper.properties
修改如下几行:
dataDir=/usr/local/zookeeper/zkdata
dataLogDir=/usr/local/zookeeper/logs
# the port at which the clients will connect
clientPort=2181
创建zookeeper数据目录,日志目录
mkdir -p /usr/local/zookeeper/logs
修改kafka配置文件:
vim config/server.properties
#修改以下几行:
broker.id=0
listeners=PLAINTEXT://0.0.0.0:9092
advertised.listeners=PLAINTEXT://0.0.0.0:9092
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/tmp/kafka-logs
num.partitions=1
num.recovery.threads.per.data.dir=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
zookeeper.connect=localhost:2181
zookeeper.connection.timeout.ms=6000
二,编辑启动脚本
1,zk启动脚本
cat /etc/init.d/zookeeper
#!/bin/bash
export JAVA_HOME=/data/jdk
export PATH=$JAVA_HOME/bin:$PATH
#chkconfig:2345 20 90
#description:zookeeper
#processname:zookeeper
case $1 in
start)
/usr/local/kafka/bin/zookeeper-server-start.sh -daemon /usr/local/kafka/config/zookeeper.properties
;;
*)
echo "require start|stop|status|restart"
;;
esac
2,kafka启动脚本
cat /etc/init.d/kafka
#!/bin/bash
export JAVA_HOME=/data/jdk
export PATH=$JAVA_HOME/bin:$PATH
case $1 in
start)
/usr/local/kafka/bin/kafka-server-start.sh -daemon /usr/local/kafka/config/server.properties
;;
*)
echo "require start|stop|status|restart"
;;
esac
3,如果要加入到开机自启动中,可在rc.local中添加
/etc/init.d/zookeeper start
/etc/init.d/kafka start
请注意启动顺序,先启动zk,后启动kafka。
三,kafka消息测试
1,创建topic
查看命令的帮助信息:直接输入bin/kafka-topics.sh,而后enter,即可以查看所有的参数信息
bin/kafka-topics.sh --create --zookeeper 192.168.20.20:2181 --replication-factor 1 --partitions 1 --topic summer
注释:参数说明
- replication-factor:设置副本数
- partitions:设置分区数
2,查看当前所有的topic列表
bin/kafka-topics.sh --list --zookeeper 192.168.20.20:2181
3,查看topic的介绍信息
bin/kafka-topics.sh --describe --zookeeper 192.168.20.20:2181
查看指定topic:
bin/kafka-topics.sh --describe --zookeeper 192.168.20.20:2181 --topic summer
4,发送消息
bin/kafka-console-producer.sh --broker-list 192.168.20.20:9092 --topic summer
5,消费消息
bin/kafka-console-consumer.sh --zookeeper 192.168.20.20:2181 --topic summer --from-beginning
此方法会有警告信息,表示已废弃的用法,建议使用新的用法【bootstrap-server】
bin/kafka-console-consumer.sh --bootstrap-server 192.168.20.20:9092 --topic summer
- --from-beginning参数的作用:重新开始消费消息,不论能否已经被消费了