本篇内容主要讲解“zk集群版服务启动实现Leader和Follower之间数据交互”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“zk集群版服务启动实现Leader和Follower之间数据交互”吧!
集群版服务启动过程

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启动交互过程

到此,相信大家对“zk集群版服务启动实现Leader和Follower之间数据交互”有了更深的了解,不妨来实际操作一番吧!这里是天达云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!