mac zookeeper伪集群

zookeepermac单机伪集群配置

 

 

杜伟

目录

一、配置............................................................................................ 1

二、启动zookeeper伪集群的所有服务器................................................ 2

三、接入客户端................................................................................... 3

四、编写启动脚本................................................................................. 3

 

 


一、 配置

zookeeper下载地址:http://mirrors.cnnic.cn/apache/zookeeper/zookeeper-3.4.6/

在一台机器上部署3server,在指定文件夹下建立一个文件夹zookeeperLab,在zookeeperLab文件夹里建立三个文件夹server1 server2 server3,然后每个文件夹里面解压一个zookeeper下载包,并且还建立几个文件夹,总体结构如下:

datadataLog logszookeeper-3.4.6

1.   进入data目录,创建一个名为myid的文件,里面写入一个数字,比如该目录为server1,那么就写一个1server2对应的myid文件就写入2server3对应的myid文件就写入3

2.   进入zookeeper-3.4.6/conf目录,将zoo_sample.cfg文件copy改为zoo.cfg,打开zoo.cfg,内容如下:

  conf  cat zoo.cfg

# The number of milliseconds of each tick

#控制心跳和超时,默认情况下最小的会话超时时间为两倍的 tickTime

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=/Users/duwei/software/zookeeperLab/server1/data

dataLogDir=/Users/duwei/software/zookeeperLab/server1/dataLog

# 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

server.1=127.0.0.1:2888:3888

server.2=127.0.0.1:2889:3889

server.3=127.0.0.1:2890:3890

注意:

clientPort这个端口如果你是在1台机器上部署多个server,那么每台机器都要不同的clientPort,比如server12181server22182server32183dataDirdataLogDir也需要区分下。

最后几行需要注意就是server.X这个数字就是对应data/myid中的数字。你在3servermyid文件中分别写入了123,那么每个server中的zoo.cfg都配置server.1server.2server3OK了。因为在同一台机器上,后面连着的2个端口3server都不要一样,否则端口冲突,其中第一个端口用来集群成员的信息交换,第二个端口是在leader挂掉时专门用来进行选举leader所用。

 

二、 启动zookeeper伪集群的所有服务器

分别进入三个服务器的zookeeper-3.4.6/bin目录下,启动服务

sh  zkServer.sh start

启动完成后,查看服务器状态

sh  zkServer.sh status

 

三、 接入客户端

进入任意一个服务器的zookeeper/bin目录下,启动一个客户端,接入服务。

 

./zkCli.sh -server 127.0.0.1:2181

 

四、编写启动脚本

vi zkCluster.sh

#!/bin/sh

zkpath=/Users/duwei/software/zookeeperLab

returnValue=0

cd $zkpath

pid=`ps -ef |grep -v "grep"|grep "zookeeperLab/server" |awk '{print $2}'`

start(){

    sh ./server1/zookeeper-3.4.6/bin/zkServer.sh start

    sh ./server2/zookeeper-3.4.6/bin/zkServer.sh start

    sh ./server3/zookeeper-3.4.6/bin/zkServer.sh start

}

stop(){

    kill -9 ${pid}

}

case "$1" in

start)

    start

    ;;

stop)

    stop

    ;;

restart)

    stop

    start

    ;;

hup)

    hup

    ;;

*)

  printf 'Usage: %s {start|stop|restart}\n'

  exit 1

  ;;

esac

exit "$returnValue"

 

猜你喜欢

转载自duwei118.iteye.com/blog/2250323