請(qǐng)確保在啟動(dòng)Kafka服務(wù)器前烁设,Zookeeper實(shí)例已經(jīng)準(zhǔn)備好并開始運(yùn)行
1.Zookeeper搭建
1.1 Zookeeper
Zookeeper簡(jiǎn)單介紹: Zookeeper wiki
Zookeeper主頁(yè): Apache Zookeeper主頁(yè)
IBM developerWorks: 分布式服務(wù)框架 Zookeeper -- 管理分布式環(huán)境中的數(shù)據(jù)
1.2 Zookeeper搭建方式
Zookeeper安裝方式有三種灾部,單機(jī)模式和集群模式以及偽集群模式桐玻。
單機(jī)模式:Zookeeper只運(yùn)行在一臺(tái)服務(wù)器上号杏,適合測(cè)試環(huán)境瓤摧;
偽集群模式:就是在一臺(tái)物理機(jī)上運(yùn)行多個(gè)Zookeeper實(shí)例薪者;
集群模式:Zookeeper運(yùn)行于一個(gè)集群上纵苛,適合生產(chǎn)環(huán)境,這個(gè)計(jì)算機(jī)集群被稱為一個(gè)“集合體”(ensemble)
1.3 搭建步驟
采用偽集群的模式搭建.
1.下載Zookeeper: Zookeeper
2.解壓Zookeeper文件
3.創(chuàng)建文件目錄
4.在data文件下創(chuàng)建文本文件myid,里面只含數(shù)字攻人,表明自己是哪臺(tái)服務(wù)器取试,server0目錄里的myid為0,server1目錄里的myid為1怀吻,server2目錄里的myid為2
5.分別修改三個(gè)zookeeper-3.4.6/conf/zoo_sample.cfg為zoo.cfg
6.修改zoo.cfg里的文件內(nèi)容
server0/zookeeper-3.4.6/conf/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=/xxx/xxx/zk/server0/data
dataLogDir=/xxx/xxx/zk/server0/dataLog
# 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
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
server.0=127.0.0.1:2880:3880
server.1=127.0.0.1:2881:3881
server.2=127.0.0.1:2882:3882
server1/zookeeper-3.4.6/conf/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=/xxx/xxxx/zk/server1/data
dataLogDir=/xxx/xxxx/zk/server1/dataLog
# the port at which the clients will connect
clientPort=2182
# 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
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
server.0=127.0.0.1:2880:3880
server.1=127.0.0.1:2881:3881
server.2=127.0.0.1:2882:3882
server2/zookeeper-3.4.6/conf/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=/xxx/xxx/zk/server2/data
dataLogDir=/xxx/xxxx/zk/server2/dataLog
# the port at which the clients will connect
clientPort=2183
# 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
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
server.0=127.0.0.1:2880:3880
server.1=127.0.0.1:2881:3881
server.2=127.0.0.1:2882:3882
每個(gè)配置相當(dāng)于一臺(tái)服務(wù)器瞬浓,所以每個(gè)配置文件里的dataDir,dataLgDir, clientPort都不一樣蓬坡,每個(gè)配置文件的最后三行是一樣的猿棉。
server.A=B:C:D:其中 A 是一個(gè)數(shù)字,就是myid里的那個(gè)數(shù)字屑咳,表示這個(gè)是第幾號(hào)服務(wù)器铺根;B 是這個(gè)服務(wù)器的 ip 地址,C和D是兩個(gè)端口乔宿,C和D兩個(gè)端口是用來(lái)交換信息與leader選舉的。
Finally, note the two port numbers after each server name: “ 2888” and “3888”. Peers use the former port to connect to other peers. Such a connection is necessary so that peers can communicate, for example, to agree upon the order of updates. More specifically, a ZooKeeper server uses this port to connect followers to the leader. When a new leader arises, a follower opens a TCP connection to the leader using this port. Because the default leader election also uses TCP, we currently require another port for leader election. This is the second port in the server entry.
7.啟動(dòng)服務(wù)
進(jìn)入每個(gè)目錄/zookeeper-3.4.6访雪,執(zhí)行bin/zkServer.sh start详瑞,開啟服務(wù)
8.接入客戶端
可以進(jìn)入server2/zookeeper-3.4.6,去連接server0臣缀,
執(zhí)行bin/zkCli.sh -server 127.0.0.1:2181
2.kafka搭建
2.1 kafka 簡(jiǎn)介
kafka 主頁(yè)
IBM Apache kafka 工作原理介紹
kafka入門介紹
2.2 搭建步驟
1.下載 kafka
2.解壓 kafka
3.啟動(dòng)zk坝橡,可以用kafka自帶的zookeeper(bin/zookeeper-server-start.sh config/zookeeper.properties &)
4.啟動(dòng)kafka server
bin/kafka-server-start.sh config/server.properties &
5.創(chuàng)建主題
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic 20170517
6.查看已經(jīng)創(chuàng)建的主題
bin/kafka-topics.sh --list --zookeeper localhost:2181.
7.啟動(dòng)生產(chǎn)者
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic 20170517
8.啟動(dòng)消費(fèi)者
另開一個(gè)終端,bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic 20170517 --from-beginning
在生產(chǎn)者下輸入消息后回車精置,消費(fèi)者就可以看到消息了
9.用監(jiān)控工具查看kafka
下載 KafkaOffsetMonitor-assembly-0.2.0.jar
啟動(dòng)監(jiān)控服務(wù)
java -cp KafkaOffsetMonitor-assembly-0.2.0.jar \
com.quantifind.kafka.offsetapp.OffsetGetterWeb \
--zk 127.0.0.1:2181 \
--port 8089 \
--refresh 10.seconds \
--retain 1.days