Mac系统 本机搭建Kafka+zookeeper

Mac系统 本机搭建Kafka+zookeeper

1zookeeper 安装

本次使用的是zookeeper-3.4.10(百度搜索下载即可)

conf路径下zoo_sample.cfg修改为zoo.cfg

增加如下配置(Windows环境下,亲测,如下配置,启动服务会自动在zookeeper所在路径盘符下创建logs路径。当然直接写绝对路径也可以)

dataDir=/kafka_zk/data/zookeeper
dataLogDir=/kafka_zk/logs/zookeepeer

 2Kafka安装

本次使用的是kafka_2.11-1.0.0(百度搜索下载即可)

打开 kafka_2.11-1.0.0/config/server.properties

添加advertised.listeners=PLAINTEXT://127.0.0.1:9092 IP修改为服务器公网IP,如本地服务,则改为本机ip地址,注意如果只是本机连接使用可以使用 127.0.0.1,如果要为局域网其他机器提供服务,则配置为该机局域网ip

advertised.listeners=PLAINTEXT://127.0.0.1:9092

zookeeper.connect=127.0.0.1:2181

ip直接用127.0.0.1即可

log.dirs=/kafka_zk/logs/kafka
注意: log 地址配置原理同 zookeeper


3、启动

shell脚本启动zookeeper + Kafka ,以下有shell脚本,各位修改zkHome和kfkHome即可。

zkHome=/Downloads/zookeeper-3.4.10
kfkHome=/Downloads/kafka_2.11-1.0.0
zkCount=0
kfkCount=0

echo "starting zookeeper..."
$zkHome/bin/zkServer.sh start

zkCount=`ps -ef | grep zookeeper | grep -v grep | wc -l`
if [[ $zkCount -gt 0 ]]; then
	echo "zookeeper is started..."
	sleep 5
	$zkHome/bin/zkServer.sh status
else
	echo "zookeeper is not started,Please check the zookeeper URL"
	exit 0
fi

echo "starting kafka..."
nohup $kfkHome/bin/kafka-server-start.sh $kfkHome/config/server.properties > /dev/null 2>&1 &
sleep 5
kfkCount=`ps -ef | grep kafka.Kafka | grep -v grep | wc -l`
if [[ $kfkCount -gt 0 ]]; then
	echo "kafka is started..."
else
	echo "kafka is not started,Please check the kafka URL"
fi
exit 0
4、停止服务shell脚本

zkHome=/Users/a11130321050035/Downloads/zookeeper-3.4.10
kfkHome=/Users/a11130321050035/Downloads/kafka_2.11-1.0.0
zkCount=0
kfkCount=0

echo "stopping kafka..."
ps -ef | grep kafka.Kafka | grep -v grep | awk '{print $2}' | xargs kill -9
kfkCount=`ps -ef | grep kafka.Kafka | grep -v grep | wc -l`
if [[ $kfkCount -gt 0 ]]; then
	echo "kafka is not stopped,Please check the kafka URL"
else
	echo "kafka is stopped..."
fi

echo "stopping zookeeper..."
$zkHome/bin/zkServer.sh stop
sleep 5
zkCount=`ps -ef | grep zookeeper | grep -v grep | wc -l`
if [[ $zkCount -gt 0 ]]; then
	echo "zookeeper is not stopped,Please check the zookeeper URL"
else
	echo "zookeeper is stopped..."
	sleep 5
	$zkHome/bin/zkServer.sh status
fi

exit 0
5、常用命令

查看topic(在Kafka路径下执行,以后不再赘述)

/bin/kafka-topics.sh --list --zookeeper 127.0.0.1:2181
创建topic

/bin/kafka-server-start.sh /Users/a11130321050035/Downloads/kafka_2.11-1.0.0/config/server.properties

启动生产者

/bin/kafka-console-producer.sh --broker-list 127.0.0.1:9092 --topic test
启动消费者

/bin/kafka-console-consumer.sh --zookeeper 127.0.0.1:2181 --topic test --from-beginning
注意:生产者、消费者打开窗口不要关闭,启动成功后,在生产者窗口输入消息,消费者端可以看到,则说明本地Kafka服务启动成功。


多谢关注!





你可能感兴趣的:(大数据,消息队列)