您的位置:首页 > 其它

Windows下zookeeper伪集群环境搭建

2016-06-27 16:02 351 查看
最近一直在关注集群与分布式的原理,难点在于服务的管理与协调, 现在应用最广的服务发现的方案就是Zookeeper,必须掌握。

集群与分布式的区别是集群提升的是可靠性,就是当部分的机器宕机不会导致服务不可用,每台机器提供完整的服务。分布式更强调的是将不同的业务拆分成不同的模块,不同的机器承载的业务也不同,因此分布式更多的提高性能。集群前面通常有一个负载均衡算法,将请求适当的分配到集群中去,对于用户来说,性能并没有太大的提升,但是对于分布式来说,一般会提升相应速度。分布式一定是集群,但是集群并不一定是分布式。

这次我们搭建一个Zookeeper实例。(以下简称ZK)。

zk主要的特点是统一管理集群和分布式,服务的发现和订阅、发布。运用了一系列算法保证了集群的一致性,和集群的可靠性。工作模式有三种,单机模式,伪集群模式,集群模式。

单机模式很简单,由于手头机器有限,我们来配置一个伪集群模式,集群模式与伪集群模式差不多。

伪集群模式就是在一台电脑上配置多个zk实例。

网上的教程大都是基于linux去搭建,windows和linux的配置还是有一些不同的,这次演示在windows环境下的配置。

我们这里建立三个server实例。下载好了zk的压缩包然后解压到任意一个盘(笔者这里解压到D盘)并在目录下创建data与log目录,文件夹结构为:

zookeeper-3.4.8 — data

|—bin

|—log

|…

我们再在data文件夹下建立三个文件夹(1,2,3),在三个文件夹里分别创建myid文件(无后缀),这里是每个server的标识,输入1-255之间的一个数。这里我分别给每个server分别写入1,2,3。

然后找到zookeeper-3.4.8(自己zk的版本),打开conf文件夹下的zoo_sample.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=/tmp/zookeeper
# 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


我们复三个文件,分别取名为zoo1.cfg,zoo2.cfg,zoo3.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.
# 注意windwos还是linux分隔符
dataDir=D:\\zookeeper-3.4.8\\data\\1
dataLogDir=D:\\zookeeper-3.4.8\\log\\1
# the port at which the clients will connect
clientPort=2181
#
# 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=127.0.0.1:8881:7771
server.2=127.0.0.1:8882:7772
server.3=127.0.0.1:8883:7773


zk程序有两个部分,一个是维护连接,一个是选举leader,这里的server.1=127.0.0.1:8881:7771。1对应的myid文件里的值,8881端口维护心跳,7771在leader挂掉的时候使用端口,由于是一台机器,客户端写入程序使用2181.因此另外两个配置文件分别:

# 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=D:\\zookeeper-3.4.8\\data\\2
dataLogDir=D:\\zookeeper-3.4.8\\log\\2
# the port at which the clients will connect
clientPort=2182
#
# 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=127.0.0.1:8881:7771
server.2=127.0.0.1:8882:7772
server.3=127.0.0.1:8883:7773


# 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=D:\\zookeeper-3.4.8\\data\\3
dataLogDir=D:\\zookeeper-3.4.8\\log\\3
# the port at which the clients will connect
clientPort=2183
#
# 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=127.0.0.1:8881:7771
server.2=127.0.0.1:8882:7772
server.3=127.0.0.1:8883:7773


之前由于配置文件的路径没写对,一直报盘配置错误,后来查看源码发现是myid找不到,再后来发现网上的教程大都是基于linux,在windows一定要注意文件分隔符。

然后进入bin下复制三个zkServ.cmd与zkEnv.cmd,起名为zkServer1.cmd,zkServer2.cmd,zkServer3.cmd和zkEnv1.cmd,zkEnv2.cmd,zkEnv3.cmd。编辑zkEnv1.cmd,修改set ZOOCFG=%ZOOCFGDIR%\zoo1.cfg,其他两个也一样修改。再编辑zkServer1.cmd,修改setlocal

call “%~dp0zkEnv1.cmd”,其他两个也分别修改。

然后分别启动zkServer1.cmd,zkServer2.cmd,zkServer3.cmd即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: