前言:当我们想要做zookeeper集群模式下的一些实践,但是又没有多个主机,或者不想配置虚拟机的时候,就可以选择在一台主机下进行测试。
准备:下载,解压,配置好zookeeper。
操作步骤:
- 拷贝一份zookeeper/conf/zoo_sample.cfg
HERMIALU-MB0:cd /usr/local/zookeeper/
HERMIALU-MB0:zookeeper huiminlu$ ls
LICENSE.txt README_packaging.txt docs
NOTICE.txt bin lib
README.md conf
HERMIALU-MB0:zookeeper huiminlu$ cd conf/
HERMIALU-MB0:conf huiminlu$ ls
configuration.xsl log4j.properties zoo_sample.cfg
HERMIALU-MB0:conf huiminlu$ cp zoo_sample.cfg zoo.cfg
- 建立两个目录
HERMIALU-MB0:zookeeper huiminlu$ mkdir dataDir
HERMIALU-MB0:zookeeper huiminlu$ mkdir dataLogDir
- 在dataDir目录下新建myid文件,赋值1
HERMIALU-MB0:dataDir huiminlu$ cat myid
1
- 拷贝dataDir和dataLogDir
HERMIALU-MB0:zookeeper huiminlu$ cp -r dataDir dataDir2
HERMIALU-MB0:zookeeper huiminlu$ cp -r dataDir dataDir3
HERMIALU-MB0:zookeeper huiminlu$ cp -r dataLogDir dataLogDir2
HERMIALU-MB0:zookeeper huiminlu$ cp -r dataLogDir dataLogDir3
HERMIALU-MB0:zookeeper huiminlu$ LS
LICENSE.txt conf dataLogDir2
NOTICE.txt dataDir dataLogDir3
README.md dataDir2 docs
README_packaging.txt dataDir3 lib
bin dataLogDir logs
- 修改dataDir2/myid 内容为2,修改dataDir3/myid内容为3
- 修改zoo.cfg的内容
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper/dataDir
dataLogDir=/usr/local/zookeeper/dataLogDir
# the port at which the clients will connect
clientPort=2181
#2888,zookeeper通信的端口号
#3888,zookeeper选举leader时使用的端口号
server.1=localhost:2887:3887
server.2=localhost:2888:3888
server.3=localhost:2889:3889
说明:设置dataDir,dataLogDir,设置三个服务器的地址。由于我们是在本机开启三个zookeeper服务,所以注意三个服务的clientPort,server端口号是不同的。
将zoo.cfg拷贝两份,作为第二个和第三个个服务的配置:
HERMIALU-MB0:conf huiminlu$ cp zoo.cfg zoo2.cfg
HERMIALU-MB0:conf huiminlu$ cp zoo.cfg zoo3.cfg
zoo2.cfg内容如下:
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper/dataDir2
dataLogDir=/usr/local/zookeeper/dataLogDir2
# the port at which the clients will connect
clientPort=2182
#2888,zookeeper通信的端口号
#3888,zookeeper选举leader时使用的端口号
server.1=localhost:2887:3887
server.2=localhost:2888:3888
server.3=localhost:2889:3889
zoo3.cfg内容如下:
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper/dataDir3
dataLogDir=/usr/local/zookeeper/dataLogDir3
# the port at which the clients will connect
clientPort=2183
#2888,zookeeper通信的端口号
#3888,zookeeper选举leader时使用的端口号
server.1=localhost:2887:3887
server.2=localhost:2888:3888
server.3=localhost:2889:3889
- 开启三个服务端
HERMIALU-MB0:bin huiminlu$ ./zkServer.sh start ../conf/zoo.cfg
ZooKeeper JMX enabled by default
Using config: ../conf/zoo.cfg
Starting zookeeper ... STARTED
HERMIALU-MB0:bin huiminlu$ ./zkServer.sh start ../conf/zoo2.cfg
ZooKeeper JMX enabled by default
Using config: ../conf/zoo2.cfg
Starting zookeeper ... STARTED
HERMIALU-MB0:bin huiminlu$ ./zkServer.sh start ../conf/zoo3.cfg
ZooKeeper JMX enabled by default
Using config: ../conf/zoo3.cfg
Starting zookeeper ... STARTED
- 查看三个服务端的状态:
HERMIALU-MB0:bin huiminlu$ ./zkServer.sh status ../conf/zoo.cfg
ZooKeeper JMX enabled by default
Using config: ../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
HERMIALU-MB0:bin huiminlu$ ./zkServer.sh status ../conf/zoo2.cfg
ZooKeeper JMX enabled by default
Using config: ../conf/zoo2.cfg
Client port found: 2182. Client address: localhost.
Mode: leader
HERMIALU-MB0:bin huiminlu$ ./zkServer.sh status ../conf/zoo3.cfg
ZooKeeper JMX enabled by default
Using config: ../conf/zoo3.cfg
Client port found: 2183. Client address: localhost.
Mode: follower
可以看到第二个服务是leader
10. 开启客户端,这里如果没有指定服务器,会连端口号2181的服务
HERMIALU-MB0:bin huiminlu$ ./zkCli.sh
Connecting to localhost:2181
- 设置一些值:
[zk: localhost:2181(CONNECTED) 1] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 2] create /zk_test my_data1
Created /zk_test
[zk: localhost:2181(CONNECTED) 3] ls /
[zk_test, zookeeper]
[zk: localhost:2181(CONNECTED) 4] get /zk_test
my_data1
[zk: localhost:2181(CONNECTED) 5] create /zk_test/demo1
Created /zk_test/demo1
- 断开客户端,连第二个服务,看看值是否一致。
[zk: localhost:2181(CONNECTED) 10] quit
HERMIALU-MB0:bin huiminlu$ ./zkCli.sh -server 127.0.0.1:2182
Connecting to 127.0.0.1:2182
[zk: 127.0.0.1:2182(CONNECTED) 0] ls /
[zk_test, zookeeper]
[zk: 127.0.0.1:2182(CONNECTED) 1] ls /zk_test
[demo1]
自此结束。
参考:
https://blog.51cto.com/aiilive/1684145
http://zookeeper.apache.org/doc/current/zookeeperStarted.html
MAC下安装和启动zookeeper