zookeeper学习与实战(一)环境部署

【背景】:最近需要做这样一件事,在一台服务器上通过客户端生成配置文件,然后需要将该配置文件实时的传输到上百台应用服务器,供应用程序读取。同时,该配置文件是不定时更新内容,只要有更新,远程应用服务器应该及时感知文件的变化,并进行同步到本地。

这时,zookeeper就派上用场了。本篇只介绍zookeeper的部署安装使用。对于zookeeper是什么还不清楚的,请参考这里

一、zookeeper单机部署

1、【环境说明】:window 7 64bit(10.249.9.19)

2、【安装】:

2.1、下载地址:http://apache.fayea.com/zookeeper/

下载的安装包放到c盘根目录(可以放到任意盘),减压到本地。

2.2、配置文件

修改C:zookeeper-3.4.6conf下的zoo_sample.cfg,并重命名为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=C:\zookeeper-3.4.6\data
dataLogDir=C:\zookeeper-3.4.6\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

tickTime

Zookeeper使用的基本时间,时间单位为毫秒。它用于心跳机制,并且设置最小的session超时时间为两倍心跳时间

dataDir

保存内存数据库快照信息的位置,如果没有其他说明,更新的事务日志也保存到数据库。

clientPort

监听客户端连接的端口。

安装非常简单,到这里已经安装好了,下面就是把它运行起来:

C:zookeeper-3.4.6in>zkServer.cmd

到安装目录的bin目录下,运行zkServer.cmd即可。

2.3、客户端连接测试

上面我们在10.249.9.19上安装了zookeeper服务并已经启动,下面另起一个cmd,作为客户端进行连接:

zkCli.sh  -server 10.249.9.19:2181

到这里就已经连接成功了!表明我们zookeeper server安装正确。下面进行简单的命令行测试:

1)添加数据:

[zk: 10.249.9.19:2181(CONNECTED) 1] create /test1 'helloworld'      
Created /test1

2)查看数据:

[zk: 10.249.9.19:2181(CONNECTED) 7] get /test1
'helloworld'
cZxid = 0x25
ctime = Thu Nov 24 18:40:52 CST 2016
mZxid = 0x25
mtime = Thu Nov 24 18:40:52 CST 2016
pZxid = 0x25
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 12
numChildren = 0

3、查看路径

[zk: 10.249.9.19:2181(CONNECTED) 8] ls /
[zk, zookeeper, test1, db]

4、更新数据

[zk: 10.249.9.19:2181(CONNECTED) 9] set /test1 hellowzookeeper

到此为止,我们已经完成了第一步,相当于我们可以把配置文件任意的存在这个zookeeper server上了。

但是,还有两个问题需要解决:

1、如果要传输文件到其他服务器,怎么传输呢?那么远程的那么多应用服务器怎么知道我这个zookeeper上配置文件有过更新呢?怎么同步呢?

回答:这些问题就需要我们利用zookeeper提供的客户端api进行编程了。通过java或者c等客户端语言进行编程来同步文件。具体内容下篇演示。

先来看看下面的整体结构图:

【解释】:

【ZooKeeper Service部分】:表示一组zookeeper server集群,它提供服务,因为它是分布式的,所以部署多台防止单点故障,能够高可用。就相当于mysql数据库的主从复制架构,主库挂了可以切换到从库进行服务。当然如果业务不重要,部署一台也是可以的。(对应于运维人员)

【Client部分】:指我们利用编程语言如java,c,python,通过zookeeper提供的api进行编程,对zookeeper server上的数据进行增删改查数据,可以理解为php连接mysql服务器进行操作一样,php作为客户端。(对应于开发人员)

2、我们上面只部署了单机版本,对于业务比较重要的,如何避免单点故障?

这时就需要部署zookeeper server集群了,zookeeper天生就是分布式的,不用它的分布式集群简直就是暴殄天物。具体我们演示以后补上。

【参考文档】

     http://www.cnblogs.com/chengxin1982/p/3997706.html

     http://zookeeper.majunwei.com/document/3.4.6/GettingStarted.html

   (原理)http://cailin.iteye.com/blog/2014486/

原文地址:https://www.cnblogs.com/mysql-dba/p/6099021.html