温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

zk集群版服务启动实现Leader和Follower之间数据交互

发布时间:2021-06-26 10:14:40 来源:亿速云 阅读:194 作者:chen 栏目:大数据

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

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

zk
AI