一般情况下,当我们学习有关集群的知识时,都是在我们本地电脑来安装多个节点(有条件的可以运用不同的服务器去学习搭建),比如:mq、zookeeper、mysql等
今天我就动手搭建一下在自己的电脑上如何搭建一个伪分布式zookeeper集群(运用的是centos7.2环境)
首先我们需要下载zookeeper源码,我们可以先下载到Windows系统然后上传,当然我们也可以用wget来下载,这样相对来说比较方便,我在这就用wget下载了,再次我把zookeeper文件放置到/usr/local/soft下了
wget http://219.238.7.73/files/703900000A354B91/apache.fayea.com/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.tar.gz
第二步:
解压tar -zxvf zookeeper-3.4.9.tar.gz
我把文件放置到了/usr/local/zookeeper
下面需要创建log和data目录了,本人把他们直接放到了zookeeper目录下了
需要在log下创建三个目录,我们需要把三个节点分别指向不同的日志目录,data目录同样
在/usr/local/zookeeper/zookeeper/data下执行:
echo "1">./d_1/myid
echo "2">./d_2/myid
echo "3">./d_3/myid
此时前期准备工作已经差不多了,只剩下最后一步,需要配置各个节点的zoo.cfg
我们进入conf下 /usr/local/zookeeper/zookeeper/conf
我们拷贝
cp zoo_sample.cfg zoo1.cfg
cp zoo_sample.cfg zoo2.cfg
cp zoo_sample.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.
dataDir=/usr/local/zookeeper/zookeeper/data/d_1
dataLogDir=/usr/local/zookeeper/zookeeper/log/log_1
# 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=localhost:2187:2887
server.2=localhost:2188:2888
server.3=localhost:2189:2889
zoo2.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=/usr/local/zookeeper/zookeeper/data/d_2
dataLogDir=/usr/local/zookeeper/zookeeper/log/log_2
# the port at which the clients will connect
clientPort=2182
# 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=localhost:2187:2887
server.2=localhost:2188:2888
server.3=localhost:2189:2889
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.
dataDir=/usr/local/zookeeper/zookeeper/data/d_3
dataLogDir=/usr/local/zookeeper/zookeeper/log/log_3
# the port at which the clients will connect
clientPort=2183
# 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=localhost:2187:2887
server.2=localhost:2188:2888
server.3=localhost:2189:2889
zoo.cfg参数介绍:
tickTime: 每隔tickTime 时间就会发送一个心跳
syncLimit: Leader 和 follower 之间的通讯时长 最长不能超过initLimt*ticktime
initLimt: 接受客户端链接 zk 初始化的最长等待心跳时长initLimt*ticktime
dataDir: 数据目录
dataLogDir: 日志文件
clientPort: 客户端链接服务端端口号
Server.A=B:C:D A:服务器序号,即:myid中的值 B: 服务 IP C:代表 Leader 和 follower 通讯端口 D:备用选 leader 端口
这样配置就全部搞定啦。。是不是感觉很简单。。。下面我们启动三个节点并连接集群
首先需要进入到bin目录/usr/local/zookeeper/zookeeper/bin
执行
./zkServer.sh start ../conf/zoo1.cfg
./zkServer.sh start ../conf/zoo2.cfg
./zkServer.sh start ../conf/zoo3.cfg
ok,三个节点全部启动
下面可以用zkCli进行连接了
./zkCli.sh -server localhost:2181,localhost:2182,localhost:2183
可以看到如下信息
[root@VM_0_8_centos bin]# ./zkCli.sh -server localhost:2181,localhost:2182,localhost:2183
Connecting to localhost:2181,localhost:2182,localhost:2183
2018-04-14 09:46:37,118 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.9-1757313, built on 08/23/2016 06:50 GMT
2018-04-14 09:46:37,128 [myid:] - INFO [main:Environment@100] - Client environment:host.name=localhost
2018-04-14 09:46:37,128 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_162
2018-04-14 09:46:37,140 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2018-04-14 09:46:37,141 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/home/lierl/soft/jdk1.8.0_162/jre
2018-04-14 09:46:37,141 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/usr/local/zookeeper/zookeeper/bin/../build/classes:/usr/local/zookeeper/zookeeper/bin/../build/lib/*.jar:/usr/local/zookeeper/zookeeper/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/zookeeper/zookeeper/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/zookeeper/zookeeper/bin/../lib/netty-3.10.5.Final.jar:/usr/local/zookeeper/zookeeper/bin/../lib/log4j-1.2.16.jar:/usr/local/zookeeper/zookeeper/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper/zookeeper/bin/../zookeeper-3.4.9.jar:/usr/local/zookeeper/zookeeper/bin/../src/java/lib/*.jar:/usr/local/zookeeper/zookeeper/bin/../conf:
2018-04-14 09:46:37,141 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2018-04-14 09:46:37,141 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2018-04-14 09:46:37,141 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=
2018-04-14 09:46:37,141 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2018-04-14 09:46:37,141 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2018-04-14 09:46:37,141 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-693.21.1.el7.x86_64
2018-04-14 09:46:37,141 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
2018-04-14 09:46:37,141 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
2018-04-14 09:46:37,142 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/usr/local/zookeeper/zookeeper/bin
2018-04-14 09:46:37,143 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2181,localhost:2182,localhost:2183 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@69d0a921
2018-04-14 09:46:37,203 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
Welcome to ZooKeeper!
JLine support is enabled
2018-04-14 09:46:37,422 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@876] - Socket connection established to localhost/127.0.0.1:2181, initiating session
2018-04-14 09:46:37,456 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x162c1be1e200000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181,localhost:2182,localhost:2183(CONNECTED) 0]
[zk: localhost:2181,localhost:2182,localhost:2183(CONNECTED) 0] ls /
[zookeeper]
[zk: localhost:2181,localhost:2182,localhost:2183(CONNECTED) 1]
配置成功,如果你想更深入了解zookeeper,则搭建集群是首要条件,下面我会继续发布一些有关zookeeper的知识点,大家相互学习,一起努力吧
努力成就非凡,请不要在最该奋斗的年纪选择安逸