zookeeper+kafka集群搭建

文/高哥

zookeeper是一个为分布式应用提供一致性服务的软件,它是开源的Hadoop项目的一个子项目,并根据google发表的一篇论文来实现的。zookeeper为分布式系统提供了高笑且易于使用的协同服务,它可以为分布式应用提供相当多的服务,诸如统一命名服务,配置管理,状态同步和组服务等。zookeeper接口简单,我们不必过多地纠结在分布式系统编程难于处理的同步和一致性问题上,你可以使用zookeeper提供的现成(off-the-shelf)服务来实现来实现分布式系统额配置管理,组管理,Leader选举等功能。(部分概念来源与网络)


kafka主要用来处理消息的同时对消息保存时根据Topic进行归类,发送消息者成为Producer,消息接受者成为Consumer,此外kafka集群有多个kafka实例组成,每个实例(server)成为broker。


去官网下载最新版本的zookeeper包和kafka包

zookeeper安装包:http://mirror.bit.edu.cn/apache/zookeeper/

640?wx_fmt=png

640?wx_fmt=png

kafka安装包:http://kafka.apache.org/downloads.html

640?wx_fmt=png

安装包下载后进行开始搭建集群,本次采用三台机器来搭建。分别在三台服务器上创建zookeeper和kafka目录并解压安装包。解压安装包后进入conf目录,conf/zoo_sample.cfg拷贝一份命名为zoo.cfg,同时也放在conf下面。

开始配置zookeeper:

 
 

zookeeper配置文件:
# 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=/app/zookeeper/zookeeperdata
dataLogDir=/app/zookeeper/zookeeperlog
# the port at which the clients will connect
clientPort=2185
# 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.1=192.168.0.1:3888:4888
server.2=192.168.0.2:5888:6888
server.3=192.168.0.3:7888:8888

640?wx_fmt=png

重点:dataDir,dataLogDir目录开始是不存在,需要使用mkdir命令创建相应的目录。并且在该目录下创建文件myid,serve1,server2,server3该文件内容分别为1,2,3。针对服务器server2,server3可以将server1复制到相应的目录,不过需要注意dataDir,dataLogDir目录,并且文件myid内容分别为2,3。

 
 

如在serrver1上:
cd /app/zookeeper/zookeeperdata
touch myid
echo 1 >> myid


server1上启动zookeeper:

依次用bin/zkServer.sh start启动三台服务器即可。

启动成功后可以用命令bin/zkCli.sh -server 192.168.0.1:2185去链接服务。链接成功后会出现type:None path:null的字样。

640?wx_fmt=png

用 ls / 命令查看zookeeper内部结构:

640?wx_fmt=png

创建zookeeper测试节点:

640?wx_fmt=png

按照同样的步骤在server2,server3上进行一样的步骤。



接下来进行kafka集群搭建和配置:

解压后进入kafka目录下的conf目录下进行修改server.properties这个文件

配置内容如下:

 
 

broker.id=2
port=9092
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/app/kafka/kafka-logs
num.partitions=1
num.recovery.threads.per.data.dir=1
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
zookeeper.connect=192.168.0.1:2185,192.168.0.2:2185,192.168.0.3:2185
zookeeper.connection.timeout.ms=6000000
group.initial.rebalance.delay.ms=0
kafka.metrics.polling.interval.secs=5
kafka.metrics.reporters=kafka.metrics.KafkaCSVMetricsReporter
kafka.csv.metrics.dir=/tmp/kafka_metrics
kafka.csv.metrics.reporter.enabled=false

配置重点:kafka配置文件中zookeeper.connect这里的端口指的是zookeeper中的客户端端口。


kafka启动:

bin/kafka-server-start.sh config/server.properties &

640?wx_fmt=png

640?wx_fmt=png

同样的步骤在server2和server3上进行配置和启动。




kafka中创建topic:

bin/kafka-topics.sh --create --zookeeper 192.168.0.1:2185 --replication-factor 1 --partitions 1 --topic gyltest

640?wx_fmt=png

查看指定Topic状态

bin/kafka-topics.sh --describe --zookeeper 192.168.0.1:2185 --topic gyltest

640?wx_fmt=png

创建发送者

bin/kafka-console-producer.sh --broker-list 192.168.0.1:2185 --topic gyltest

640?wx_fmt=png

创建消费者:

bin/kafka-console-consumer.sh --zookeeper 192.168.0.1:2185  --from-beginning --topic gyltest


消费者如果能查询到消息,代表就是一切正常的。




简单做下笔记,今天在工作中干了一件蠢事,给自己挖了个坑,以后一定得细心,再细心。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值