zk java数据交互 实例_zk集群版服务启动,Leader和Follower之间数据交互

集群版服务启动过程

0d1df80d8e09a28d30700103943a92eb.png

LeaderElection算法

完成自己投票和投票算法的获取

集群特有,zookeeper首先会根据自身服务器id(sid)

最新的zxid(lastloggedZxid)和当前的服务器epoch(currentEpoch)

来生成一个初始化投票

初始化过程中,每个服务都会给自己投票,然后,根据zoo.cfg的配置

创建相应Leader选举算法实现

zk配置中提供默认三种算法(Leaderelection,AuthFastLeaderElection,FastLeaderElection).通过zoo.cfg的electionalg属性指定,默认支持FastLeaderElection

在初始化阶段,会创建选举需要的网络i/o层 QuorumCnxManager,等待其他服务的连接

@Override

public synchronized void start() {

if (!getView().containsKey(myid)) {

throw new RuntimeException("My id " + myid + " not in the peer list");

}

//加载数据库

loadDataBase();

//启动服务连接工厂

startServerCnxnFactory();

try {

adminServer.start();

} catch (AdminServerException e) {

LOG.warn("Problem starting AdminServer", e);

System.out.println(e);

}

//开始选举

startLeaderElection();

startJvmPauseMonitor();

super.start();

}

public synchronized void startLeaderElection() {

try {

if (getPeerState() == ServerState.LOOKING) {

//认为当前是leader

currentVote = new Vote(myid, getLastLoggedZxid(), getCurrentEpoch());

}

} catch (IOException e) {

RuntimeException re = new RuntimeException(e.getMessage());

re.setStackTrace(e.getStackTrace());

throw re;

}

this.electionAlg = createElectionAlgorithm(electionType);

}

Leader和Follower启动交互过程

4996048ffa1b93a6390087c519158d47.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值