软件的安装参考单机环境
Zookeeper之单机环境
复制配置:
[root@bogon ~]#cd /usr/local/zookeeper/conf/
cp zoo_sample.cfg zoo1.cfg
cp zoo1.cfg zoo2.cfg
cp zoo1.cfg zoo3.cfg
更新配置:
[root@bogon ~]#cat /usr/local/zookeeper/conf/zoo1.cfg
dataDir=/var/zookeeper/data1
clientPort=2181
server.1=127.0.0.1:2881:3881
server.2=127.0.0.1:2882:3882
server.3=127.0.0.1:2883:3883
[root@bogon ~]#cat /usr/local/zookeeper/conf/zoo2.cfg
dataDir=/var/zookeeper/data2
clientPort=2182
server.1=127.0.0.1:2881:3881
server.2=127.0.0.1:2882:3882
server.3=127.0.0.1:2883:3883
[root@bogon ~]#cat /usr/local/zookeeper/conf/zoo3.cfg
dataDir=/var/zookeeper/data3
clientPort=2183
server.1=127.0.0.1:2881:3881
server.2=127.0.0.1:2882:3882
server.3=127.0.0.1:2883:3883
注意
- dataDir: 实例的dataDir目录单独分开。
- clientPort: 客户端连接服务端的端口要单独分开(单机集群)。
- server.: 集群的各个实例的的ip和端口,由于是伪集群,所以IP都定义的是127.0.0.1,端口单独分开。
创建相关目录和myid文件
[root@bogon ~]#mkdir -pv /var/zookeeper/data1
[root@bogon ~]#mkdir -pv /var/zookeeper/data2
[root@bogon ~]#mkdir -pv /var/zookeeper/data3
[root@bogon ~]#echo 1 > /var/zookeeper/data1/myid
[root@bogon ~]#echo 2> /var/zookeeper/data2/myid
[root@bogon ~]#echo 3> /var/zookeeper/data3/myid
目录结果如下:
[root@bogon ~]# tree /var/zookeeper/
/var/zookeeper/
├── data1
│ └── myid
├── data2
│ └── myid
└── data3
└── myid
myid 文件记录的是实例的id值
启动服务
[root@bogon ~]# zkServer.sh start /usr/local/zookeeper/conf/zoo1.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/conf/zoo1.cfg
Starting zookeeper ... STARTED
[root@bogon ~]# zkServer.sh start /usr/local/zookeeper/conf/zoo2.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/conf/zoo2.cfg
Starting zookeeper ... STARTED
[root@bogon ~]# zkServer.sh start /usr/local/zookeeper/conf/zoo3.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/conf/zoo3.cfg
Starting zookeeper ... STARTED
查看进程
[root@bogon conf]# jps
4500 QuorumPeerMain
4580 Jps
4549 QuorumPeerMain
4473 QuorumPeerMain
测试
2181 写入数据
[root@bogon conf]# zkCli.sh -server 127.0.0.1:2181
...
[zk: 127.0.0.1:2181(CONNECTED) 2] ls /
[zookeeper]
[zk: 127.0.0.1:2181(CONNECTED) 3] create /node1 "master_db_shop_1"
Created /node1
.....
**2182 查看更新 **
[zk: 127.0.0.1:2182(CONNECTED) 0] ls /
[zookeeper, node1]
[zk: 127.0.0.1:2182(CONNECTED) 1] get /node1
master_db_shop_1
cZxid = 0x200000009
ctime = Wed Jun 07 00:56:32 CST 2017
mZxid = 0x200000009
mtime = Wed Jun 07 00:56:32 CST 2017
pZxid = 0x200000009
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 16
numChildren = 0
[zk: 127.0.0.1:2182(CONNECTED) 2] set /node1 "master_db_shop_2"
cZxid = 0x200000009
ctime = Wed Jun 07 00:56:32 CST 2017
mZxid = 0x20000000c
mtime = Wed Jun 07 00:59:13 CST 2017
pZxid = 0x200000009
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 16
numChildren = 0
2183 查看2182的更新
[zk: 127.0.0.1:2183(CONNECTED) 2] get /node1
master_db_shop_2
cZxid = 0x200000009
ctime = Wed Jun 07 00:56:32 CST 2017
mZxid = 0x20000000c
mtime = Wed Jun 07 00:59:13 CST 2017
pZxid = 0x200000009
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 16
numChildren = 0
伪集群搭建完成!