想要搭建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
暫時完畢~