想要搭建kafka集群,必须具备zookeeper集群,关于zookeeper集群的搭建,在Kafka学习之(五)搭建kafka集群之Zookeeper集群搭建博客有说明。需要具备两台以上装有zookeeper的服务器,其次,稳定可靠的kafka版本,本篇使用kafka_2.11-0.9.0.1。
准备
192.168.244.131、192.168.244.133、192.168.244.134,kafka下载地址 https://archive.apache.org/dist/kafka/0.9.0.1/kafka_2.11-0.9.0.1.tgz(wget下载)
创建目录并解压
我习惯把压缩包下载到/usr/local/src下,然后我把下载好的kafka tag包做了目录拷贝。
# cd /usr/local
# mkdir kafka
# cd kafka
# mkdir kafkalogs
# cp /usr/local/kafka......tar.gz /usr/local/kafka
编辑配置文件
cd /usr/local/kafka/kafka_2.11-0.9.0.1/config/ //进入config目录
主要关注:server.properties 这个文件即可,我们可以发现在目录下:
有很多文件,这里可以发现有Zookeeper文件,我们可以根据Kafka内带的zk集群来启动,但是建议使用独立的zk集群。
broker.id=0 #当前机器在集群中的唯一标识,和zookeeper的myid性质一样
port=19092 #当前kafka对外提供服务的端口默认是9092,这里改大
host.name=192.168.244.131 #这个参数默认是关闭的,在0.8.1有个bug,DNS解析问题,在解析的时候会解析成ip,这里会有失败率。
num.network.threads=3 #这个是borker进行网络处理的线程数
num.io.threads=8 #这个是borker进行I/O处理的线程数
log.dirs=/usr/local/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.244.131:12181,192.168.244.133:12181,192.168.244.134:12181 #设置zookeeper的连接端口
上面的参数只是作为了解一下,实际搭建中需要根据场景来配置。
现在只需要对每台机器的配置文件做如下更改
#broker.id=0 //每台服务器的broker.id都不能相同,从0开始,分别0、1、2
#listeners=PLAINTEXT://:9092
#hostname
host.name=192.168.244.131 //分别对应ip
log.dirs=/usr/local/kafka/kafkalogs //日志目录
#在log.retention.hours=168 //下面新增下面三项
message.max.byte=5242880
default.replication.factor=2
replica.fetch.max.bytes=5242880
#设置zookeeper的连接端口
zookeeper.connect=192.168.244.131:12181,192.168.244.133:12181,192.168.244.134:12181
启动服务
在启动kafka之前,确保每台机器的防火墙关闭,其次zookeeper集群也是启动中,确保以上条件成立然后执行命令启动kafka
#./kafka-server-start.sh -daemon ../config/server.properties //在kafka的目录下bin目录执行。 表示后台以server.properties配置文件执行。 三台分别执行
# jps //检查服务是否启动
3506 Jps
3443 Kafka
3397 QuorumPeerMain
创建Topic来验证是否创建成功
这是官方文档说明:http://kafka.apache.org/quickstart
//在192.168.244.131 创建Topic
# ./kafka-topics.sh --create --zookeeper 192.168.244.131:12181 --replication-factor 2 --partitions 1 --topic wt645631686
Created topic "wt645631686".
//解释
--replication-factor 2 #复制两份
--partitions 1 #创建1个分区
--topic #主题为wt645631686
//分别在192.168.244.133和192.168.244.134上创建订阅者
# ./kafka-console-consumer.sh --zookeeper 192.168.244.134:12181 --topic wt645631686
//在192.168.244.131上创建发布者
# ./kafka-console-producer.sh --broker-list 192.168.244.131:19092 --topict 645631686
在192.168.244.131上发布测试,分别观察另外两台机器。
topic其他说明
# ./kafka-topics.sh --list --zookeeper localhost:12181 //显示我们创建的所有topic
# ./kafka-topics.sh --describe --zookeeper 192.168.244.131:12181 --topic wt645631686 //查看指定topic情况
Topic:wt645631686 PartitionCount:1 //一个分区 ReplicationFactor:2 Configs:
Topic: wt645631686 Partition: 0 Leader: 2 Replicas: 2,0 Isr: 2,0
到这里kafka集群就搭建完了~
日志说明
默认kafka的日志是保存在/opt/kafka/kafka_2.10-0.9.0.0/logs目录下的,这里说几个需要注意的日志
- server.log ,kafka的运行日志
- state-change.log ,因为kafka他是用zookeeper来保存状态,所以他可能会进行切换,切换的日志就保存在这里
- controller.log ,kafka选择一个节点作为“controller”,当发现有节点down掉的时候它负责在游泳分区的所有节点中选择新的leader,这使得Kafka可以批量的高效的管理所有分区节点的主从关系。如果controller down掉了,活着的节点中的一个会备切换为新的controller.
查看zookeeper情况
进入zookeeper的项目目录
# pwd
/usr/local/zookeeper/zookeeper-3.4.6/bin
# ./zkCli.sh -server 127.0.0.1:12181 //默认是不用加’-server‘参数的因为我们修改了他的端口
Connecting to 127.0.0.1:12181
2018-02-15 15:12:57,356 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT
2018-02-15 15:12:57,360 [myid:] - INFO [main:Environment@100] - Client environment:host.name=localhost
2018-02-15 15:12:57,360 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_91
2018-02-15 15:12:57,363 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2018-02-15 15:12:57,363 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/local/jdk1.8.0_91/jre
2018-02-15 15:12:57,363 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/usr/local/zookeeper/zookeeper-3.4.6/bin/../build/classes:/usr/local/zookeeper/zookeeper-3.4.6/bin/../build/lib/*.jar:/usr/local/zookeeper/zookeeper-3.4.6/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/zookeeper/zookeeper-3.4.6/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/zookeeper/zookeeper-3.4.6/bin/../lib/netty-3.7.0.Final.jar:/usr/local/zookeeper/zookeeper-3.4.6/bin/../lib/log4j-1.2.16.jar:/usr/local/zookeeper/zookeeper-3.4.6/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper/zookeeper-3.4.6/bin/../zookeeper-3.4.6.jar:/usr/local/zookeeper/zookeeper-3.4.6/bin/../src/java/lib/*.jar:/usr/local/zookeeper/zookeeper-3.4.6/bin/../conf:.:/usr/local/jdk/lib/dt.jar:/usr/local/jdk/lib/tools.jar
2018-02-15 15:12:57,363 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2018-02-15 15:12:57,363 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2018-02-15 15:12:57,364 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2018-02-15 15:12:57,364 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2018-02-15 15:12:57,364 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2018-02-15 15:12:57,365 [myid:] - INFO [main:Environment@100] - Client environment:os.version=2.6.32-358.el6.x86_64
2018-02-15 15:12:57,365 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
2018-02-15 15:12:57,366 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
2018-02-15 15:12:57,366 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/usr/local/zookeeper/zookeeper-3.4.6/bin
2018-02-15 15:12:57,368 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=127.0.0.1:12181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@799f7e29
Welcome to ZooKeeper!
2018-02-15 15:12:57,404 [myid:] - INFO [main-SendThread(127.0.0.1:12181):ClientCnxn$SendThread@975] - Opening socket connection to server 127.0.0.1/127.0.0.1:12181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2018-02-15 15:12:57,481 [myid:] - INFO [main-SendThread(127.0.0.1:12181):ClientCnxn$SendThread@852] - Socket connection established to 127.0.0.1/127.0.0.1:12181, initiating session
2018-02-15 15:12:57,496 [myid:] - INFO [main-SendThread(127.0.0.1:12181):ClientCnxn$SendThread@1235] - Session establishment complete on server 127.0.0.1/127.0.0.1:12181, sessionid = 0x167509a9cdd0004, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: 127.0.0.1:12181(CONNECTED) 0]
[zk: 127.0.0.1:12181(CONNECTED) 1] ls / //查看目录情况 执行“ls /”
[controller, controller_epoch, brokers, zookeeper, admin, isr_change_notification, consumers, config]
//上面的显示结果中:只有zookeeper是,zookeeper原生的,其他都是Kafka创建的
[zk: 127.0.0.1:12181(CONNECTED) 12] ls /
[controller, controller_epoch, brokers, zookeeper, admin, isr_change_notification, consumers, config]
[zk: 127.0.0.1:12181(CONNECTED) 13] ls /brokers/topics
[wt645631686]
[zk: 127.0.0.1:12181(CONNECTED) 14] ls /brokers/topics/wt645631686
[partitions]
[zk: 127.0.0.1:12181(CONNECTED) 15] ls /brokers/topics/wt645631686/partitions
[0]
[zk: 127.0.0.1:12181(CONNECTED) 16] get /brokers/ids/0
{"jmx_port":-1,"timestamp":"1543245597011","endpoints":["PLAINTEXT://192.168.244.131:19092"],"host":"192.168.244.131","version":2,"port":19092}
cZxid = 0x200000017
ctime = Mon Nov 26 07:19:57 PST 2018
mZxid = 0x200000017
mtime = Mon Nov 26 07:19:57 PST 2018
pZxid = 0x200000017
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x167509a9cdd0000
dataLength = 143
numChildren = 0
[zk: 127.0.0.1:12181(CONNECTED) 17] get /brokers/topics/wt645631686/partitions/0 //查看partion
null
cZxid = 0x200000029
ctime = Mon Nov 26 07:29:16 PST 2018
mZxid = 0x200000029
mtime = Mon Nov 26 07:29:16 PST 2018
pZxid = 0x20000002a
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1
暂时完毕~