linux搭建zookeeper集群环境日常记录

1、安装JDK

tar -zxvf 解压 压缩包

# vi /etc/profile 配置环境变量

2、安装zookeeper

本次采用一主二从

配置 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=/tmp/zookeeper/zk1
dataLogDir=/usr/local/var/run/zookeeper/zk1
# 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=/tmp/zookeeper/zk2
dataLogDir=/usr/local/var/run/zookeeper/zk2
# 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=/tmp/zookeeper/zk3
dataLogDir=/usr/local/var/run/zookeeper/zk3
# 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

配置 对应的myid  myid文件路径对应每一个zoo.cfg中的 dataDir=/tmp/zookeeper/zk3 即 路径为/tmp/zookeeper/zk3/myid

myid文件中 只需放一个值 对应每个zookeeper的id值 会设计到选举 如果 没有事务ID的情况下 myid值越大 越容易被选举选举一般采用半数选举 3个 节点 A启动默认选自己为leader B 启动 参与投票 和A比较myid值 B 胜出 为leader C节点启动 就默认就为follower

 

3个节点 leader负责写请求 两个follower 负责写请求 如果 follower 获取了 写的请求 将会把这个请求转发 给leader 完成写的请求

节点如何实现同步的?

leader 在拿到写请求时候 会生成一个zxid(全局 唯一 顺讯 事务ID) 然后 将事务ID通知到 所有从节点 从节点 收到 回做反馈 主节点 得到半数以上的反馈 就会去做 提交操作 然后 通知从节点 从节点 也完成 提交操作 完成同步

如果主节点 挂了 从节点 如何进行选举?

leader挂了 从节点 将进行重新选举投票 首先选举自己为leader 然后 比较zxid 事务ID 由于事务ID 是有序的 就是 事务ID越大 该节点的数据就是越新 该节点 就会被选举为leader 如果 事务ID相同 则比较 对应节点的myid 获得新的leader (事务ID 一样的话 就pk服务器ID)

 

 

zookeeper 也可以 做为 分布式 配置中心 在某一节点存储数据 实时修改 则订阅者 将实时获得更新后的值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值