邱县专业做网站,网站建设开发综合实训小结,非遗网页设计作品欣赏,手机网站用什么开发【README】 本文基于centos8 搭建
1#xff0c;其他linux版本#xff0c;命令可能不同#xff1b;
2#xff0c;集群包括3个节点#xff0c;如下#xff08;因为采用NAT模型进行网络连接#xff0c;需要让windows和linux机器在同一个网段#xff09;#xff1a;
ce…【README】 本文基于centos8 搭建
1其他linux版本命令可能不同
2集群包括3个节点如下因为采用NAT模型进行网络连接需要让windows和linux机器在同一个网段
centos01: 192.168.163.201
centos02: 192.168.163.202
centos02: 192.168.163.203
【1】 软件准备
1、远程文件传输软件
window 与 centos 文件传输 sz rz yum -y install lrzsz yum -y install vim yum -y install scp
yum -y install rsync 远程同步文件
2、安装java vim /etc/profile # JVM ENV JAVA_HOME/usr/local/java/jdk1.8 JRE_HOME$JAVA_HOME/jre CLASSPATH.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib PATH$PATH:$JAVA_HOME/bin:$JRE_HOME/bin export JAVA_HOME CLASSPATH PATH JRE_HOME
source /etc/profile 更新
java -version
【2】搭建单机模式的zk
步骤1 下载 zookeeper 框架包 本文使用 zookeeper-3.4.10 , https://archive.apache.org/dist/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz [rootlocalhost module]# ls
zookeeper-3.4.10.tar.gz
步骤2解压位置在 /opt/module
tar -zxvf zookeeper-3.4.10.tar.gz
[rootlocalhost module]# ls
zookeeper-3.4.10 zookeeper-3.4.10.tar.gz
[rootlocalhost module]# pwd
/opt/module
[rootlocalhost module]# cd zookeeper-3.4.10
[rootlocalhost zookeeper-3.4.10]# pwd
/opt/module/zookeeper-3.4.10
步骤3 修改 zookeeper 配置文件
[rootlocalhost zookeeper-3.4.10]# cd conf
[rootlocalhost conf]# ls
configuration.xsl log4j.properties zoo_sample.cfg
[rootlocalhost conf]# cp zoo_sample.cfg zoo.cfg
[rootlocalhost conf]# vim zoo.cfg
设置zoo.cfg 如下修改 dataDir/opt/module/zookeeper-3.4.10/zkdata zkdata为自定义文件夹
-- zoo.cfg[rootlocalhost conf]# cat zoo.cfg
# The number of milliseconds of each tick
tickTime2000
# The number of ticks that the initial
# synchronization phase can take
initLimit10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir/opt/module/zookeeper-3.4.10/zkdata
# the port at which the clients will connect
clientPort2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns60
#
# 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.snapRetainCount3
# Purge task interval in hours
# Set to 0 to disable auto purge feature
#autopurge.purgeInterval1
步骤4 新建 /opt/module/zookeeper-3.4.10/zkdata 文件夹
[rootlocalhost zookeeper-3.4.10]# pwd
/opt/module/zookeeper-3.4.10
[rootlocalhost zookeeper-3.4.10]# mkdir zkdata
步骤5 启动zk并查看zk状态
[rootlocalhost zookeeper-3.4.10]# bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[rootlocalhost zookeeper-3.4.10]#
[rootlocalhost zookeeper-3.4.10]# bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: standalone
standalone 为单机模式
提醒这里可能报错 Error contacting service. It is probably not running.
解决方法1 关闭防火墙
查看防火墙状态firewall-cmd --state
关闭防火墙systemctl stop firewalld.service解决方法2 https://blog.csdn.net/PacosonSWJTU/article/details/111260961
懂的查看 zk 目录下的 zookeeper.out 日志文件
步骤6 启动zk客户端 并退出quit
[rootlocalhost zookeeper-3.4.10]# bin/zkCli.sh
Connecting to localhost:2181
Welcome to ZooKeeper!
......
WATCHER::WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper][zk: localhost:2181(CONNECTED) 1] quit
Quitting...
2020-12-20 03:42:33,745 [myid:] - INFO [main:ZooKeeper684] - Session: 0x1767c83c15e0000 closed
2020-12-20 03:42:33,747 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread519] - EventThread shut down for session: 0x1767c83c15e0000
[rootlocalhost zookeeper-3.4.10]#
步骤7停止zk线程或有
[rootlocalhost zookeeper-3.4.10]# bin/zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
补充zoo.cfg 配置解析
序号属性描述1tickTime2000心跳2000毫秒一个心跳2秒2initLimit1010个2秒 20秒是Leader和Follower 刚开始初始化通信的最大延时时间 如果超过这个时间 则zk服务器认为 两者连接失败 3syncLimit55个2秒 10秒是 Leader 和 Follower的在集群正常启动后的通信的最大延时时间 如果超过这个时间 则zk服务器认为 两者连接失败 4dataDir/opt/module/zookeeper-3.4.10/zkdata设置zk数据文件存储路径 5clientPort2181客户端访问 zk 服务的所在主机的端口号【3】搭建集群模式的zk
步骤1201机器在zoo.cfg下增加集群配置
server.AB.C.D 如 server.1192.168.163.201:2888:3888 其中A 等于myid文件的值1 B表示本服务器地址 192.168.163.201 C表示本服务器与集群中 Leader服务器交换信息的端口 两者同步的数据是 数据副本 2888 D表示 在选举Leader服务器时服务器间相互通信的端口 3888
小结添加如下配置
server.1192.168.163.201:2888:3888
server.2192.168.163.202:2888:3888
server.3192.168.163.203:2888:3888
[rootlocalhost module]# cat zookeeper-3.4.10/conf/zoo.cfg
# The number of milliseconds of each tick
tickTime2000
# The number of ticks that the initial
# synchronization phase can take
initLimit10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir/opt/module/zookeeper-3.4.10/zkdata
# the port at which the clients will connect
clientPort2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns60
#
# 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.snapRetainCount3
# Purge task interval in hours
# Set to 0 to disable auto purge feature
#autopurge.purgeInterval1
server.1192.168.163.201:2888:3888
server.2192.168.163.202:2888:3888
server.3192.168.163.203:2888:3888
步骤2201机器设置服务器编号在zkdata文件夹下新建myid内容为1
[rootlocalhost zkdata]# pwd
/opt/module/zookeeper-3.4.10/zkdata
[rootlocalhost zkdata]# echo 1 myid
步骤3把201机器下的 zookeeper 文件夹同步到202,203机器
[rootlocalhost module]# rsync -azv zookeeper-3.4.10/ root192.168.163.202:/opt/module/zookeeper-3.4.10/
[rootlocalhost module]# rsync -azv zookeeper-3.4.10/ root192.168.163.203:/opt/module/zookeeper-3.4.10/
步骤4修改 202机器的myid为2 203机器的myid为3
-- 202机器
[rootlocalhost zookeeper-3.4.10]# cat zkdata/myid
2
-- 203 机器
[rootlocalhost zookeeper-3.4.10]# cat zkdata/myid
3步骤5 启动zk集群
-- 201机器-启动失败
[rootlocalhost zookeeper-3.4.10]# bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[rootlocalhost zookeeper-3.4.10]# bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.
[rootlocalhost zookeeper-3.4.10]#
-- 202机器-启动成功-并且成为leader
[rootlocalhost zookeeper-3.4.10]# bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[rootlocalhost zookeeper-3.4.10]# bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: leader
[rootlocalhost zookeeper-3.4.10]#
注 zk的选举机制自行google 这里不再赘述
-- 203机器-启动成功-follower
[rootlocalhost zookeeper-3.4.10]# bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[rootlocalhost zookeeper-3.4.10]#
[rootlocalhost zookeeper-3.4.10]# bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower我们再回头查看201机器的zk状态 当202,203启动zk线程后 我们再次查看201的zk状态为 成功且是 follower
-- 201
[rootlocalhost zookeeper-3.4.10]# bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[rootlocalhost zookeeper-3.4.10]# bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.
[rootlocalhost zookeeper-3.4.10]# -- ### 我是分割线 202,203开启zk线程后[rootlocalhost zookeeper-3.4.10]# bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower
[rootlocalhost zookeeper-3.4.10]#
至此zk集群启动成功 【4】其他
【4.1】zookeeper 选举机制
1半数机制 当有半数以上机器存活则集群可用否则不可用 不包括半数建议zookeeper 集群的机器数量为奇数 2虽然配置文件没有指定 master 和 slave但在工作时 zookeeper临时选择一条机器作为 master或 leader 其他机器为 slave或follower
【4.2】zk客户端命令行
-- zk客户端常用命令
ls create get delete rmr set
登录zk客户端
[rootlocalhost zookeeper-3.4.10]# bin/zkCli.sh
1 ls 查看节点
ls2 查看节点详情
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 1] ls2 /
[zookeeper]
cZxid 0x0
ctime Thu Jan 01 08:00:00 CST 1970
mZxid 0x0
mtime Thu Jan 01 08:00:00 CST 1970
pZxid 0x0
cversion -1
dataVersion 0
aclVersion 0
ephemeralOwner 0x0
dataLength 0
numChildren 1
[zk: localhost:2181(CONNECTED) 2]
stat结构体 序号 key 描述 1 cZxid 创建事务编号 2 ctime 创建时间 3 mZxid 修改事务编号 4 mtime 修改时间 5 pZxid 最后更新的子节点 6 cversion 子节点变化编号 7 dataVersion 数据变化编号 8 aclVersion 访问控制列表的变化号 9 ephemeralOwner 若是临时节点表示的是znode拥有者的session id如果不是则是0 10 dataLength znode的数据长度 11 numChildren znode的子节点个数 2create 创建节点
[zk: localhost:2181(CONNECTED) 2] create /sichuan sichuan
Created /sichuan
[zk: localhost:2181(CONNECTED) 3] ls /
[sichuan, zookeeper]
3get 获取节点
[zk: localhost:2181(CONNECTED) 4] get /sichuan
sichuan
cZxid 0x100000002
ctime Sun Dec 20 04:22:16 CST 2020
mZxid 0x100000002
mtime Sun Dec 20 04:22:16 CST 2020
pZxid 0x100000002
cversion 0
dataVersion 0
aclVersion 0
ephemeralOwner 0x0
dataLength 7
numChildren 0
[zk: localhost:2181(CONNECTED) 5]
4delete 删除节点
[zk: localhost:2181(CONNECTED) 5] create /sichuan/cd chengdu
Created /sichuan/cd
[zk: localhost:2181(CONNECTED) 7] create /sichuan/leshan leshan4
Created /sichuan/leshan
[zk: localhost:2181(CONNECTED) 8] ls /sichuan
[cd, leshan]
[zk: localhost:2181(CONNECTED) 9] delete /sichuan/leshan
[zk: localhost:2181(CONNECTED) 10]
[zk: localhost:2181(CONNECTED) 10] ls /sichuan
[cd]
[zk: localhost:2181(CONNECTED) 11]
5rmr 递归删除当前目录及其下的所有子目录
[zk: localhost:2181(CONNECTED) 10] ls /sichuan
[cd]
[zk: localhost:2181(CONNECTED) 11] create /sichuan/my mianyang
Created /sichuan/my
[zk: localhost:2181(CONNECTED) 12] ls /sichuan
[cd, my]
[zk: localhost:2181(CONNECTED) 13] rmr /sichuan
[zk: localhost:2181(CONNECTED) 14]
[zk: localhost:2181(CONNECTED) 14] ls /sichuan
Node does not exist: /sichuan
6set 设置节点存储的值
[zk: localhost:2181(CONNECTED) 18] create /china china
Created /china
[zk: localhost:2181(CONNECTED) 19] [zk: localhost:2181(CONNECTED) 19] get /china
china
cZxid 0x10000000b
ctime Sun Dec 20 04:28:10 CST 2020
mZxid 0x10000000b
mtime Sun Dec 20 04:28:10 CST 2020
pZxid 0x10000000b
cversion 0
dataVersion 0
aclVersion 0
ephemeralOwner 0x0
dataLength 5
numChildren 0[zk: localhost:2181(CONNECTED) 20] set /china china xiongqi
cZxid 0x10000000b
ctime Sun Dec 20 04:28:10 CST 2020
mZxid 0x10000000c
mtime Sun Dec 20 04:28:35 CST 2020
pZxid 0x10000000b
cversion 0
dataVersion 1
aclVersion 0
ephemeralOwner 0x0
dataLength 13
numChildren 0[zk: localhost:2181(CONNECTED) 21] get /china
china xiongqi
cZxid 0x10000000b
ctime Sun Dec 20 04:28:10 CST 2020
mZxid 0x10000000c
mtime Sun Dec 20 04:28:35 CST 2020
pZxid 0x10000000b
cversion 0
dataVersion 1
aclVersion 0
ephemeralOwner 0x0
dataLength 13
numChildren 0
[zk: localhost:2181(CONNECTED) 22]
7zk有4种节点类型
1、类型1-持久目录节点 客户端与服务器断开后创建的节点不删除2、类型2-持久化顺序编号目录节点客户端与服务器断开连接后 该节点依旧存在只是zookeeper 给该节点名称进行顺序编号 应用场景在分布式系统中顺序号可以被用于为所有的事件进行全局排序这样客户端可以通过顺序号推断事件的顺序 哪台服务器先上哪台服务器后上3、类型3-临时目录节点两者断开后 创建的节点被删除 应用场景 zk 服务器监听某台客户端机器的状态 若客户端机器下线则zk服务器节点也删除 集群从而间接知晓 客户端机器下线服务器节点动态上下线 4、类型4-临时顺序编号目录节点客户端与服务器断开后该节点被删除 只是 zk 对该服务器节点名称进行了顺序编号 8荔枝 创建有序节点 -s 创建临时节点 -e
有序节点
-- 创建有序节点
[zk: localhost:2181(CONNECTED) 24] ls /china
[]
[zk: localhost:2181(CONNECTED) 25] create -s /china/beijing beijing
Created /china/beijing0000000000
[zk: localhost:2181(CONNECTED) 26] create -s /china/beijing beijing
Created /china/beijing0000000001
[zk: localhost:2181(CONNECTED) 27] create -s /china/beijing beijing
Created /china/beijing0000000002
[zk: localhost:2181(CONNECTED) 28] create -s /china/beijing beijing
Created /china/beijing0000000003
[zk: localhost:2181(CONNECTED) 29] ls /china
[beijing0000000001, beijing0000000000, beijing0000000003, beijing0000000002]
创建临时有序节点 chongqing
[zk: localhost:2181(CONNECTED) 31] create -e -s /china/chongqing chongqing4
Created /china/chongqing0000000004
[zk: localhost:2181(CONNECTED) 32]
[zk: localhost:2181(CONNECTED) 32] ls /china
[beijing0000000001, beijing0000000000, chongqing0000000004, beijing0000000003, beijing0000000002]
退出 当前客户端 quit 并再次登录客户端 发现chognqing节点被删除了
[zk: localhost:2181(CONNECTED) 33] quit
Quitting...
2020-12-20 04:38:48,888 [myid:] - INFO [main:ZooKeeper684] - Session: 0x1767ca215950000 closed
2020-12-20 04:38:48,890 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread519] - EventThread shut down for session: 0x1767ca215950000
[rootlocalhost zookeeper-3.4.10]#
[rootlocalhost zookeeper-3.4.10]# bin/zkCli.sh[zk: localhost:2181(CONNECTED) 0] ls /china
[beijing0000000001, beijing0000000000, beijing0000000003, beijing0000000002]
[zk: localhost:2181(CONNECTED) 1] 还有话说 为啥 -s是有序-e是临时。看下zk的java api 的节点类型常量命名就知道了如下
/**** CreateMode value determines how the znode is created on ZooKeeper.*/
public enum CreateMode {/*** The znode will not be automatically deleted upon clients disconnect.*/PERSISTENT (0, false, false),/*** The znode will not be automatically deleted upon clients disconnect,* and its name will be appended with a monotonically increasing number.*/PERSISTENT_SEQUENTIAL (2, false, true),/*** The znode will be deleted upon the clients disconnect.*/EPHEMERAL (1, true, false),/*** The znode will be deleted upon the clients disconnect, and its name* will be appended with a monotonically increasing number.*/EPHEMERAL_SEQUENTIAL (3, true, true);
最后 可以通过 zk提供的javaapi 连接 zk集群 refer 2 https://blog.csdn.net/PacosonSWJTU/article/details/111404364