1.先介绍linux环境的,单机版
1.下载安装kafka,Kafka内带zookeeper,可以使用内带就可以,如果生产环境最好使用单独配置的zookeeper集群环境
wget http://mirrors.tuna.tsinghua.edu.cn/apache/kafka/0.10.1.1/kafka_2.10-0.10.1.1.tgz
进入到config目录
主要关注:server.properties 这个文件即可,我们可以发现在目录下:
有很多文件,这里可以发现有Zookeeper文件,我们可以根据Kafka内带的zk集群来启动,但是建议使用独立的zk集群
-rw-r--r-- 1 root root 906 12月 16 02:04 connect-console-sink.properties
-rw-r--r-- 1 root root 909 12月 16 02:04 connect-console-source.properties
-rw-r--r-- 1 root root 2760 12月 16 02:04 connect-distributed.properties
-rw-r--r-- 1 root root 883 12月 16 02:04 connect-file-sink.properties
-rw-r--r-- 1 root root 881 12月 16 02:04 connect-file-source.properties
-rw-r--r-- 1 root root 1074 12月 16 02:04 connect-log4j.properties
-rw-r--r-- 1 root root 2061 12月 16 02:04 connect-standalone.properties
-rw-r--r-- 1 root root 1199 12月 16 02:04 consumer.properties
-rw-r--r-- 1 root root 4369 12月 16 02:04 log4j.properties
-rw-r--r-- 1 root root 1900 12月 16 02:04 producer.properties
-rw-r--r-- 1 root root 5336 12月 16 02:04 server.properties
-rw-r--r-- 1 root root 1032 12月 16 02:04 tools-log4j.properties
-rw-r--r-- 1 root root 1023 12月 16 02:04 zookeeper.properties
属性解释
broker.id=0 #当前机器在集群中的唯一标识,和zookeeper的myid性质一样
port=19092 #当前kafka对外提供服务的端口默认是9092
host.name=192.168.7.100 #这个参数默认是关闭的,在0.8.1有个bug,DNS解析问题,失败率的问题。
num.network.threads=3 #这个是borker进行网络处理的线程数
num.io.threads=8 #这个是borker进行I/O处理的线程数
log.dirs=/opt/kafka/kafkalogs/ #消息存放的目录,这个目录可以配置为“,”逗号分割的表达式,上面的num.io.threads要
大于这个目录的个数这个目录,如果配置多个目录,新创建的topic他把消息持久化的地方是,当前以逗号分割的目录中,那个分
区数最少就放那一个
socket.send.buffer.bytes=102400 #发送缓冲区buffer大小,数据不是一下子就发送的,先回存储到缓冲区了到达一定的大小后
在发送,能提高性能
socket.receive.buffer.bytes=102400 #kafka接收缓冲区大小,当数据到达一定大小后在序列化到磁盘
socket.request.max.bytes=104857600 #这个参数是向kafka请求消息或者向kafka发送消息的请请求的最大数,这个值不能超过
java的堆栈大小
num.partitions=1 #默认的分区数,一个topic默认1个分区数
log.retention.hours=168 #默认消息的最大持久化时间,168小时,7天
message.max.byte=5242880 #消息保存的最大值5M
default.replication.factor=2 #kafka保存消息的副本数,如果一个副本失效了,另一个还可以继续提供服务
replica.fetch.max.bytes=5242880 #取消息的最大直接数
log.segment.bytes=1073741824 #这个参数是:因为kafka的消息是以追加的形式落地到文件,当超过这个值的时候,kafka会新
起一个文件
log.retention.check.interval.ms=300000 #每隔300000毫秒去检查上面配置的log失效时间(log.retention.hours=168 ),到
目录查看是否有过期的消息如果有,删除
log.cleaner.enable=false #是否启用log压缩,一般不用启用,启用的话可以提高性能
zookeeper.connect=192.168.7.100:12181,192.168.7.101:12181,192.168.7.107:1218 #设置zookeeper的连接端口
这里单机测试环境,不做修改
1、启动服务,启动kafka之前必须先启动zookeeper
> bin/zookeeper-server-start.sh config/zookeeper.properties &
#从后台启动Kafka
>bin/kafka-server-start.sh config/server.properties &
停止Kafka server
bin/kafka-server-stop.sh
2、检查服务是否启动
#jps
20348 Jps
4233 QuorumPeerMain
18991 Kafka
3、创建Topic来验证是否创建成功
#创建Topic
> bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
#解释
--replication-factor 1 #复制1份
--partitions 1 #创建1个分区
--topic #主题为test
#查看topic
> bin/kafka-topics.sh --list --zookeeper localhost:2181
test
在一台服务器上创建一个发布者'''
#创建一个broker,发布者
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
在一台服务器上创建一个订阅者'''
> bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
下面介绍zookeeper的安装配置和常用命令
1.zookeeper单机安装与配置
下载zookeeper二进制安装包
http://www.apache.org/dist/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz
或者zookeeper在线安装
#wget http://www.apache.org/dist/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz
#tar zxvf zookeeper-3.4.6.tar.gz
# cd zookeeper-3.4.6
# mkdir data
# chmod 777 data
# cd conf
# cp zoo_sample.cfg zoo.cfg
# vi zoo.cfg
---------------
# 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=/root/zookeeper-3.4.6/data #这是我修改后的
# the port at which the clients will connect
clientPort=2181
#
# 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
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
-------------------
配置说明:
tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会
发送一个心跳。
dataDir:顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
启动zookeeper
# ./zkServer.sh start #启动
当这些配置项配置好后,你现在就可以启动zookeeper了:
# netstat -at|grep 2181 #查看zookeeper端口
# netstat -nat #查看端口信息
# cd /root/zookeeper-3.4.6/bin
# jps #查看启动的服务名称
#./zkServer.sh status #查看状态
# ./zkServer.sh stop #关闭
至此zookeeper单机环境搭建完毕,集群环境见:http://www.cnblogs.com/linjiqin/p/5861599.html
windows环境
1.官方文档http://kafka.apache.org/quickstart
cmd,进入解压好的目录,E:\Download\soft\kafka_2.10-0.10.1.1\bin\windows
先启动zookeeper,在启动kafka
bin/windows/zookeeper-server-start.bat ../../config/zookeeper.properties
再开个cmd
bin/windows/kafka-server-start.bat ../../config/server.properties
创建topic
bin/windows/kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
#解释
--replication-factor 1 #复制1份
--partitions 1 #创建1个分区
--topic #主题为test
#查看topic
bin/windows/kafka-topics.bat --list --zookeeper localhost:2181
在一台服务器上创建一个发布者
#创建一个broker,发布者
bin/windows/kafka-console-producer.bat --broker-list localhost:9092 --topic test
输入,发布内容,aaa,bbb,等
在一台服务器上创建一个订阅者
bin/windows/kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test --from-beginning