一、部署
1、下载并解压安装文件:tar -zxvf kafka_2.11-0.10.1.1.tgz
2、目录介绍
a、/bin 操作kafka的可执行脚本,还包含windows下脚本
b、/config 配置文件所在目录
c、/libs 依赖库目录
d、/logs 日志数据目录,目录kafka把server端日志分为5种类型,分为:server,request,state,log-cleaner,controller
3、配置
a、zookeeper: /config/zookeeper.properties
dataDir=/home/pht/kafka_2.11-0.10.1.1/zk-logs(数据目录)
clientPort=2181(服务端口,可修改)
maxClientCnxns=0(单个客户端与单台服务器之间的连接限制,ip级别,默认为60,0则为不限制)
b、kafka:/config/server.properties-2 (多broker伪集群,可通过修改brokerid、port、log.dirs实现)
broker.id=0(必需,唯一)
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=/home/pht/kafka_2.11-0.10.1.1/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
host.name=xxx.xxx.xxx.xxx(服务地址)
port=9092(服务端口,可修改)
二、启动(可根据需求把kafka添加环境变量或写成启动脚本,方便快捷使用,就不用在意路径了)
1、先启动zookeeper,再启动kafka(加后缀 & ,放在后台运行)
bin/zookeeper-server-start.sh config/zookeeper.properties
bin/kafka-server-start.sh config/server.properties
2、netstat ano | grep '2181\|9092',可通过查看监听端口判断是否启动成功
三、命令操作
1、创建topic:bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 3 --topic well
zookeeper:zk地址,可写多个,用逗号分隔。
replication-factor:副本数,必须不大于broker总数。
partitions:分区数,创建时命令行参数可覆盖配置文件参数。
topic:消费主题,最好唯一。
2、查看topic列表:bin/kafka-topics.sh --list --zookeeper localhost:2181
3、查看topic描述:bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic well
4、生产消息:bin/kafka-console-producer.sh --broker-list localhost:9092,localhost:9093,localhost:9094 --topic well
5、消费消息:bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic well --from-beginning
6、消息分区及副本(以topic“hello”为例,0、1、2表示有三个分区;副本是以broker界定)
7、消息存储(分区中 *.log文件存储有生产的消息,顺序消费是以分区为单位的)