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

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

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

1、zookeeper单机部署java

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

二、【安装】:mysql

2.一、下载地址:http://apache.fayea.com/zookeeper/sql

下载的安装包放到c盘根目录(能够放到任意盘),减压到本地。数据库

2.二、配置文件apache

修改C:\zookeeper-3.4.6\conf下的zoo_sample.cfg,并重命名为zoo.cfg,名称随便取。编程

内容为:红色部分文件夹须要手工建立api

# 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.6\bin>zkServer.cmd

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

2.三、客户端链接测试

上面咱们在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

三、查看路径

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

四、更新数据

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

到此为止,咱们已经完成了第一步,至关于咱们能够把配置文件任意的存在这个zookeeper server上了。

 

可是,还有两个问题须要解决:

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

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

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

【解释】:

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

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

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

这时就须要部署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/

相关文章
相关标签/搜索