(一)zookeeper分布式协调系统——zookeeper集群的搭建

4 篇文章 0 订阅
1 篇文章 0 订阅

一、什么是zookeeper?

ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. All of these kinds of services are used in some form or another by distributed applications. Each time they are implemented there is a lot of work that goes into fixing the bugs and race conditions that are inevitable. Because of the difficulty of implementing these kinds of services, applications initially usually skimp on them, which make them brittle in the presence of change and difficult to manage. Even when done correctly, different implementations of these services lead to management complexity when the applications are deployed

以上是zookeeper官网对zookeeper的解释,就是说他是针对大型分布式系统的可靠协调系统,提供的功能包括:配置服务、命名服务、分布式同步、组服务等。实际项目中应用十分广泛,且市场很多主流的技术框架都会涉及到zookeeper。hbase、kafka等等很多都会用到zookeeper。

二、zookeeper集群搭建

工欲善其事必先利其器,要想系统的学习zookeeper,那就从环境搭建开始吧,就像学习java一样,没有jre环境,估计学习java会很吃力。

1、搭建zookeeper服务端

我这里准备了4台服务器,1台leader、2台follower、1台observer。ZooKeeper 集群中的所有机器通过一个 Leader 选举过程来选定一台称为 “Leader” 的机器,Leader 既可以为客户端提供写服务又能提供读服务。除了 Leader 外,Follower 和 Observer 都只能提供读服务。Follower 和 Observer 唯一的区别在于 Observer 机器不参与 Leader 的选举过程,也不参与写操作的“过半写成功”策略,因此 Observer 机器可以在不影响写性能的情况下提升集群的读性能。

首先安装zookeeper,可以到官网下载,下载完成后,在linux上解压。以下操作4台服务器都要做。

tar -zxvf apache-zookeeper-3.6.3-bin.tar.gz

解压完毕后,到conf下新建一个zoo.cfg文件,可以用sample文件复制一份。

 接下来要配置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=/usr/local/zookeeper/apache-zookeeper-3.6.3-bin/data
dataLogDir=/usr/local/zookeeper/apache-zookeeper-3.6.3-bin/log
# 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

## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true

server.0=0.0.0.0:2888:3888
server.1=132.224.144.248:2888:3888
server.2=45.32.113.14:2888:3888
server.3=13.42.115.250:2888:3888:observer

针对用到的配置项我们做个简单的说明:

tickTime=2000,配置单元时间,单元时间是zk的时间计算单元,其他的时间间隔都是用ticktime的倍数来表示,单位是毫秒ms

initTime=10,节点的初始化时间,该参数用于follower节点的启动,并完成与leader进行数据同步的时间。

syncLimit=5,心跳监测最大延时时间,leader通过心跳监测follower节点是否存活,如果再这时间内无法获取响应,则认为follower脱离组织了。

clientPort=2181,服务端的端口

server.0=0.0.0.0:2888:3888
server.1=132.224.144.248:2888:3888
server.2=45.32.113.143:2888:3888
server.3=13.42.115.250:2888:3888:observer

上面的配置是集群的信息,其中的0、1、2、3表示节点的标识符,要与myid本机文件中配置的数值一致。注意本机配置0.0.0.0的ip,这4个配置在不同服务器中根据本机ip进行修改。

本次项目中,我配置了一台obsever服务器。注意配置写法。

dataDir=/usr/local/zookeeper/apache-zookeeper-3.6.3-bin/data
dataLogDir=/usr/local/zookeeper/apache-zookeeper-3.6.3-bin/log

这2个配置一个数存放数据,一个是日志。上面我们提到的myid文件要配置在这个data目录下,myid中的id对应server.id,范围1-255,只能写一个数字。

最好在etc/profile文件中配置全局

至此,zookeeper基础设施搭建完毕,接下来启动4台服务器即可。通过xshell的同步命令输入一起启动即可

 启动完毕后,可以通过zkServer.sh status命令查看结果

4台服务器的角色都有了。可以登录zookeeper客户端进行相关api操作。

做个测试,在其中一台follower写入一个节点,在leader上查看是否收到

 到bin目录下运行zkCli客户端

follower-1

 leader

observer

测试成功,我们的zookeeper集群就此搭建完毕

下一章一起学习下 java操作zookeeper的开源客户端Curator

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值