分享一个朋友的人工智能教程。比较通俗易懂,风趣幽默,感兴趣的朋友可以去看看。
分布式应用程序可以基于Zookeeper实现例如配置管理、数据发布/订阅、负载均衡、命名服务、协调通知、集群管理、Master选举、分布式锁、分布式队列等功能。
https://zookeeper.apache.org/
###Zookeeper中重要的概念:
Zookeeper存储数据是以树形结构存储的,类似linux的目录,最上层是“/”也就是根节点,可以在根节点上创建子节点,在子节点上再创建子节点。在Zookeeper中每个节点被称为一个Znode,每个Znode包含:节点名称、节点值、值的长度、节点创建时间、节点修改时间、版本号、子节点数量等属性,可以创建节点,修改节点,删除节点等操作。
事件监听是Zookeeper中非常重要的一个特性,用来监听节点的操作,当对节点做了操作(例如创建节点、修改节点、删除节点等)就会触发节点上的监听,当监听到节点发生了变化就可以做一些相应的处理操作。该
机制是ZooKeeper实现分布式协调服务的重要特性。
https://blog.csdn.net/junchenbb0430/article/details/77583955
https://baike.baidu.com/item/Paxos 算法/10688635?fr=aladdin
简介相关文章:https://www.jianshu.com/p/8bf3b7ce3eaa
所谓单机搭建zookeeper集群其实就是在一台机器上启动多个zookeeper,在启动每个zookeeper时分别使用不同的配置文件zoo.cfg来启动,每个配置文件使用不同的配置参数(clientPort端口号、dataDir数据目录、dataLogDir数据日志目录)在同一台机器上启动多次。
zoo.cfg默认在/usr/local/etc/zookeeper路径下(不同的操作系统路径不一样,这里用的是mac的路径),在这个路径下创建zoo1.cfg、zoo2.cfg、zoo3.cfg三个配置文件。
zoo1.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/var/run/zookeeper/zk1/data
dataLogDir=/usr/local/var/run/zookeeper/zk1/logs
# 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:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389
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/var/run/zookeeper/zk2/data
dataLogDir=/usr/local/var/run/zookeeper/zk2/logs
# 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:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389
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/var/run/zookeeper/zk3/data
dataLogDir=/usr/local/var/run/zookeeper/zk3/logs
# 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:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389
创建/usr/local/var/run/zookeeper/zk1/data/myid文件
1
创建/usr/local/var/run/zookeeper/zk2/data/myid文件
2
创建/usr/local/var/run/zookeeper/zk3/data/myid文件
3
在zk1、zk2、zk3下创建一个logs目录
# 启动3个zookeeper服务
zkServer start /usr/local/etc/zookeeper/zoo1.cfg
zkServer start /usr/local/etc/zookeeper/zoo2.cfg
zkServer start /usr/local/etc/zookeeper/zoo3.cfg
# 查看每个zookeeper对应的角色
zkServer status /usr/local/etc/zookeeper/zoo1.cfg
zkServer status /usr/local/etc/zookeeper/zoo2.cfg
zkServer status /usr/local/etc/zookeeper/zoo3.cfg
# 停止zookeeper服务
zkServer stop /usr/local/etc/zookeeper/zoo1.cfg
# 连接服务器 zkCli -server IP:PORT
zkCli -server 127.0.0.1:2181
# 帮助命令
help
# 获取根节点列表,默认会有一个zookeeper节点,节点列表就像linux目录一样,根节点为/
ls /
# 创建znode并关联值, create /节点名称 “值”
create /myZnode "my znode"
# 获取znode
get /myZnode
# 修改znode
set /myZnode "my znode value string"
# 删除单个znode
delete /myZnode
# 递归删除(删除myZnode的子节点和myZnode节点)
rmr /myZnode
ZooKeeper 支持某些特定的四字命令字母与其的交互。它们大多是查询命令,用来获取 ZooKeeper 服务的当前状态及相关信息。用户在可以通过 telnet 或 nc 向 ZooKeeper 提交相应的命令。
ZooKeeper 四字命令 |
功能描述 |
conf |
输出相关服务配置的详细信息。 |
cons |
列出所有连接到服务器的客户端的完全的连接 / 会话的详细信息。包括“接受 / 发送”的包数量、会话 id 、操作延迟、最后的操作执行等等信息。 |
dump |
列出未经处理的会话和临时节点。 |
envi |
输出关于服务环境的详细信息(区别于 conf 命令)。 |
reqs |
列出未经处理的请求 |
ruok |
测试服务是否处于正确状态。如果确实如此,那么服务返回“imok ”,否则不做任何相应。 |
stat |
输出关于性能和连接的客户端的列表。 |
wchs |
列出服务器 watch 的详细信息。 |
wchc |
通过 session 列出服务器 watch 的详细信息,它的输出是一个与watch 相关的会话的列表。 |
wchp |
通过路径列出服务器 watch 的详细信息。它输出一个与 session相关的路径。 |
分享一个朋友的人工智能教程。比较通俗易懂,风趣幽默,感兴趣的朋友可以去看看。