安装
kafka:
tar -zxvf kafka_2.11-2.1.0.tgz
mv kafka_2.11-2.1.0 /home/kafka-2.11
zookeeper:
tar -zxvf zookeeper-3.4.13.tar.gz
mv zookeeper-3.4.13 /home/zookeeper-3.4.13
配置
kafka => server.properties
#每一个broker在集群中的唯一表示,要求是正数。当该服务器的IP地址发生改变时,broker.id没有变化,则不会影响consumers的消息情况
broker.id=0
#192.168.1.199-内网ip,PLAINTEXT协议
advertised.listeners=PLAINTEXT://192.168.1.199:9092
#broker处理消息的最大线程数,一般情况下数量为cpu核数
num.network.threads=2
#broker处理磁盘IO的线程数,数值为cpu核数2倍
num.io.threads=8
#socket的发送缓冲区,socket的调优参数SO_SNDBUFF
socket.send.buffer.bytes=1048576
#socket的接受缓冲区,socket的调优参数SO_RCVBUFF
socket.receive.buffer.bytes=1048576
#socket请求的最大数值,防止serverOOM,message.max.bytes必然要小于socket.request.max.bytes,会被topic创建时的指定参数覆盖
socket.request.max.bytes=104857600
#kafka数据的存放地址,多个地址的话用逗号分割,多个目录分布在不同磁盘上可以提高读写性能 /data/kafka-logs-1,/data/kafka-logs-2
log.dirs=/tmp/kafka-logs
#每个topic的分区个数,若是在topic创建时候没有指定的话会被topic创建时的指定参数覆盖
num.partitions=2
#数据文件保留多长时间, 存储的最大时间超过这个时间会根据log.cleanup.policy设置数据清除策略log.retention.bytes和log.retention.minutes或log.retention.hours任意一个达到要求,
#都会执行删除,有2删除数据文件方式:按照文件大小删除:log.retention.bytes,按照2中不同时间粒度删除:分别为分钟,小时
log.retention.hours=168
#topic的分区是以一堆segment文件存储的,这个控制每个segment的大小,会被topic创建时的指定参数覆盖
log.segment.bytes=536870912
#文件大小检查的周期时间,是否处罚 log.cleanup.policy中设置的策略
log.retention.check.interval.ms=60000
#是否开启日志清理
log.cleaner.enable=false
#zookeeper集群的地址,可以是多个,多个之间用逗号分割 hostname1:port1,hostname2:port2,hostname3:port3
zookeeper.connect=localhost:2181
#ZooKeeper的连接超时时间
zookeeper.connection.timeout.ms=1000000
#ZooKeeper集群中leader和follower之间的同步实际那
zookeeper.sync.time.ms =2000
zookeeper => zoo.cfg
添加环境变量
vi /etc/profile
添加
ZOOKEEPER_HOME=/home/zookeeper-3.4.13
export ZOOKEEPER_HOME
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$ZOOKEEPER_HOME/lib:
配置
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/home/zookeeper-3.4.13/data
dataLogDir=/home/zookeeper-3.4.13/logs
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
配置说明:
tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。
dataDir:顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
dataLogDir: log目录, 同样可以是任意目录. 如果没有设置该参数, 将使用和dataDir相同的设置。
clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
启动
进入zookeeper下bin目录,执行
./zkServer.sh start #启动
netstat -tunlp|grep 2181 #查看zookeeper端口
./zkServer.sh stop #停止
扫描二维码关注公众号,回复: 4821343 查看本文章
进入kafka下bin目录,执行
./kafka-server-start.sh ../config/server.properties
或
nohup ./kafka-server-start.sh ../config/server.properties & (后台启动)
kafka简单测试
1.创建一个topic
参数信息:Zookeeper的信息,Topic的名字,Topic的Partition数,复制因子(复制因子必须小于等于Broker数目)
./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
2.查询topic列表
./kafka-topics.sh --list --zookeeper localhost:2181
3.Producer创建消息
启动时,除了打印SLF4J之外,没有别的。下面可以直接输入生产的数据 (生产消息时不需要指定Partition,Kafka自动做Partition路由,每个Partition都是有Lead Partition和Follower Partitions组成,Lead Partition负责读写,而Follower Partitions只做复制,在Lead Partition挂了之后,自动做 Failover )
[xulu@SZB-L0032015 bin]$ ./kafka-console-producer.sh --broker-list localhost:9092 --topic test
4.Consumer消费消息
启动时,除了打印SLF4J之外,没有别的 –from-beginning实际上是指定offset的读取策略。
smallest和largest策略表示Zookeeper上的offset还没有初始化为正确值时,如何初始化offset的问题?试想,Producer生产了一批消息到Kafka中,但是Kafka尚未由任何Consumer读取,而Kafka的Offset是由Consumer进行初始化和赋值的,因此此时的Zookeeper上的offset并没有预期的0(0表示尚未读取过),而是一个不正确的随机数,那么Consumer来读取消息时,是从头开始读还是从最大的位置等待Producer创建消息后再读取,此时就产生了两个选择,smallest表示从头读,largest表示从最大位置读
auto.offset.reset(默认是largest):
What to do when there is no initial offset in ZooKeeper or if an offset is out of range:
smallest : automatically reset the offset to the smallest offset
largest : automatically reset the offset to the largest offset
./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
参考:1.kafka配置参数说明
2.kafka安装
3.kafka,server.properties配置