zookeeper 官网:http://zookeeper.apache.org/ 现在最新版本是 3.4.6 ,但是这个版本我没有运行起来,可能是那配置出现问题了,现在我用的是3.4.5 http://apache.fayea.com/apache-mirror/zookeeper/zookeeper-3.4.5/
环境:windows 8.1 、zookeeper3.4.5
现在要做的是 单机伪集群(木有办法 没有那么多机器啊)
-
修改配置文件,在conf中
将zoo_sample.cfg文件复制一个重命名为zoo.cfg
-
修改zoo.cfg 内容
打开zoo.cfg:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<code class=
"hljs vala"
style=
"padding:0.5em; margin:0px; display:block; color:rgb(101,123,131); overflow-x:auto; background:rgb(253,246,227)"
><span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# The number of milliseconds of each tick</span>
tickTime=<span class=
"hljs-number"
style=
"padding:0px; margin:0px; color:rgb(42,161,152)"
>2000<
/span
>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# The number of ticks that the initial </span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# synchronization phase can take</span>
initLimit=<span class=
"hljs-number"
style=
"padding:0px; margin:0px; color:rgb(42,161,152)"
>10<
/span
>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# The number of ticks that can pass between </span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# sending a request and getting an acknowledgement</span>
syncLimit=<span class=
"hljs-number"
style=
"padding:0px; margin:0px; color:rgb(42,161,152)"
>5<
/span
>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# the directory where the snapshot is stored.</span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# do not use /tmp for storage, /tmp here is just </span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# example sakes.</span>
dataDir=
/tmp/zookeeper
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# the port at which the clients will connect</span>
clientPort=<span class=
"hljs-number"
style=
"padding:0px; margin:0px; color:rgb(42,161,152)"
>2181<
/span
>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
#</span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# Be sure to read the maintenance section of the </span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# administrator guide before turning on autopurge.</span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
#</span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance</span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
#</span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# The number of snapshots to retain in dataDir</span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
#autopurge.snapRetainCount=3</span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# Purge task interval in hours</span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# Set to "0" to disable auto purge feature</span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
#autopurge.purgeInterval=1</span></code>
|
修改后:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<code class=
"hljs vala"
style=
"padding:0.5em; margin:0px; display:block; color:rgb(101,123,131); overflow-x:auto; background:rgb(253,246,227)"
><span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# The number of milliseconds of each tick</span>
tickTime=<span class=
"hljs-number"
style=
"padding:0px; margin:0px; color:rgb(42,161,152)"
>2000<
/span
>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# The number of ticks that the initial </span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# synchronization phase can take</span>
initLimit=<span class=
"hljs-number"
style=
"padding:0px; margin:0px; color:rgb(42,161,152)"
>10<
/span
>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# The number of ticks that can pass between </span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# sending a request and getting an acknowledgement</span>
syncLimit=<span class=
"hljs-number"
style=
"padding:0px; margin:0px; color:rgb(42,161,152)"
>5<
/span
>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# the directory where the snapshot is stored.</span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# do not use /tmp for storage, /tmp here is just </span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# example sakes.</span>
dataDir=E:
/zookeepercluster/servcer001/data
dataLogDir=E:
/zookeepercluster/servcer001/logs
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# the port at which the clients will connect</span>
clientPort=<span class=
"hljs-number"
style=
"padding:0px; margin:0px; color:rgb(42,161,152)"
>2181<
/span
>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
#</span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# Be sure to read the maintenance section of the </span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# administrator guide before turning on autopurge.</span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
#</span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance</span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
#</span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# The number of snapshots to retain in dataDir</span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
#autopurge.snapRetainCount=3</span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# Purge task interval in hours</span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
# Set to "0" to disable auto purge feature</span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
#autopurge.purgeInterval=1</span>
server.1=localhost:<span class=
"hljs-number"
style=
"padding:0px; margin:0px; color:rgb(42,161,152)"
>8881<
/span
>:<span class=
"hljs-number"
style=
"padding:0px; margin:0px; color:rgb(42,161,152)"
>7771<
/span
>
server.2=localhost:<span class=
"hljs-number"
style=
"padding:0px; margin:0px; color:rgb(42,161,152)"
>8882<
/span
>:<span class=
"hljs-number"
style=
"padding:0px; margin:0px; color:rgb(42,161,152)"
>7772<
/span
>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
#server.3=192.168.192.7:8883:7773</span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
#server.4=192.168.192.7:8884:7774</span>
<span class=
"hljs-preprocessor"
style=
"padding:0px; margin:0px; color:rgb(203,75,22)"
>
#server.5=192.168.192.7:8885:7775</span></code>
|
主要是在下边添加几个服务器的ip地址,因为我的都是本机,所以我这都是一样的地址。
参数解释:
-
tickTime:发送心跳的间隔时间,单位:毫秒
-
dataDir:zookeeper保存数据的目录。
-
clientPort:客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
-
initLimit:这个配置项是用来配置 Zookeeper 接受客户端(这里所说的客户端不是用户连接 Zookeeper 服务器的客户端,而是 Zookeeper 服务器集群中连接到 Leader 的 Follower 服务器)初始化连接时最长能忍受多少个心跳时间间隔数。当已经超过 5个心跳的时间(也就是 tickTime)长度后 Zookeeper 服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 5*2000=10 秒
-
syncLimit:这个配置项标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度,总的时间长度就是 2*2000=4 秒
-
server.A=B:C:D:其中 A 是一个数字,表示这个是第几号服务器;B 是这个服务器的 ip 地址;C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口;D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式,由于 B 都是一样,所以不同的 Zookeeper 实例通信端口号不能一样,所以要给它们分配不同的端口号。
3. 在data下面创建一个myid文件,内容为1
这个内容对应的是这个zookeeper的顺序,第一个zookeeper就是1,第二个zookeeper就是2,没有先后顺序,只是不能重复。
这个最好是和 server.x 中的x对应
依次修改配置文件 简历server02,server03,server04节点 目录结构如下:
├── server001
│ ├── data
│ ├── logs
│ └── zookeeper-3.4.5
├── server002
│ ├── data
│ ├── logs
│ └── zookeeper-3.4.5
├── server003
│ ├── data
│ ├── logs
│ └── zookeeper-3.4.5
├── server004
│ ├── data
│ ├── logs
│ └── zookeeper-3.4.5
└── server005
├── data
├── logs
└── zookeeper-3.4.5
这样就配置好了,windows下的单机伪集群。
下面就是启动了:
进入bin下双击 :zkServer.cmd
报这个错误是因为还有节点没有启动起来,都启动了就会不报错了
运行 zkServer.cmd status 可以查看节点的角色 是leader 还是follower
这样一个伪集群就成功了,如果有不对的地方希望大家批评指正,ORZ。
参考文章:
http://www.cnblogs.com/haippy/archive/2012/07/19/2599989.html
http://zookeeper.apache.org/doc/current/zookeeperStarted.html
http://zookeeper.apache.org/doc/current/index.html
http://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/