1、安装
下载压缩包并解压
可以解压到 /usr/local/kafka
2、配置
vim conf/server.properties
#设置brokerid(从0开始,3个节点分别设为0,1,2,不能重复)
broker.id=0
#设置data目录,最好不要用默认的/tmp/kafka-logs
log.dirs=/usr/local/kafka/kafka-logs
#设置注册地址(重要,默认会把本机的hostanme注册到zk中,客户端连接时需要解析该hostanme,所以这里直接注册本机的IP地址,避免hostname解析失败,报错java.nio.channels.UnresolvedAddressException或java.io.IOException: Can not resolve address)
advertised.host.name=192.168.xxx
#设置zookeeper地址
zookeeper.connect=zk1.yazuoyw.com:2181,zk2.yazuoyw.com:2181,zk3.yazuoyw.com:2181
3、启动关闭
#启动
/usr/local/kafka/bin/kafka-server-start.sh -daemon /usr/local/kafka/config/server.properties
#添加开机启动
echo ‘
# start kafka
/usr/local/kafka/bin/kafka-server-start.sh -daemon /usr/local/kafka/config/server.properties
‘ >> /etc/rc.local
#关闭
/usr/local/kafka/bin/kafka-server-stop.sh
4、常用命令
#查看tocpic列表(--zookeeper指定任意一个zk节点即可,用于获取集群信息)
/usr/local/kafka/bin/kafka-topics.sh --zookeeper zk1.yazuoyw.com:2181 --describe
#创建topic(--replication-factor表示复制到多少个节点,--partitions表示分区数,一般都设置为2或与节点数相等,不能大于总节点数)
/usr/local/kafka/bin/kafka-topics.sh --zookeeper zk1.yazuoyw.com:2181 --create --topic topic1 --replication-factor 2 --partitions 2
#发送消息(--topic 指定topic)
/usr/local/kafka/bin/kafka-console-producer.sh --broker-list kafka1.yazuoyw.com:9092,kafka2.yazuoyw.com:9092,kafka3.yazuoyw.com:9092 --topic topic1
message1
message2
#消费消息
/usr/local/kafka/bin/kafka-console-consumer.sh --zookeeper zk1.yazuoyw.com:2181 --topic topic1
#replica检查
/usr/local/kafka/bin/kafka-replica-verification.sh --broker-list kafka1.yazuoyw.com:9092,kafka2.yazuoyw.com:9092,kafka3.yazuoyw.com:9092