栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

zookeeper集群中的server是如何选举的?

zookeeper集群中的server是如何选举的?

zookeeper的leader选举只有两种情况才会发起
1.zookeeper集群初始化
2.leader机器崩溃
首先就是集群初始化时候的选举,从zk的启动脚本能看到,
zk集群的启动的入口是org.apache.zookeeper.server.quorum.QuorumPeerMain

if [ "x$JMXDISABLE" = "x" ] || [ "$JMXDISABLE" = 'false' ]
then
  echo "ZooKeeper JMX enabled by default" >&2
  if [ "x$JMXPORT" = "x" ]
  then
    # for some reason these two options are necessary on jdk6 on Ubuntu
    #   accord to the docs they are not necessary, but otw jconsole cannot
    #   do a local attach
    ZOOMAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=$JMXLOCALonLY org.apache.zookeeper.server.quorum.QuorumPeerMain"
  else
    if [ "x$JMXAUTH" = "x" ]
    then
      JMXAUTH=false
    fi
    if [ "x$JMXSSL" = "x" ]
    then
      JMXSSL=false
    fi
    if [ "x$JMXLOG4J" = "x" ]
    then
      JMXLOG4J=true
    fi
    echo "ZooKeeper remote JMX Port set to $JMXPORT" >&2
    echo "ZooKeeper remote JMX authenticate set to $JMXAUTH" >&2
    echo "ZooKeeper remote JMX ssl set to $JMXSSL" >&2
    echo "ZooKeeper remote JMX log4j set to $JMXLOG4J" >&2
    if [ "x$JMXHOSTNAME" = "x" ]

我们关注的选举源码就在这里

  @Override
    public synchronized void start() {
        loadDatabase();
        cnxnFactory.start();        
        startLeaderElection();
        super.start();
    }


synchronized public void startLeaderElection() {
    	try {
    		currentVote = new Vote(myid, getLastLoggedZxid(), getCurrentEpoch());
    	} catch(IOException e) {
    		RuntimeException re = new RuntimeException(e.getMessage());
    		re.setStackTrace(e.getStackTrace());
    		throw re;
    	}
//下面的getView就是读取配置文件中的集群地址,并且根据配置文件信息,赋值server地址
        for (QuorumServer p : getView().values()) {
            if (p.id == myid) {
                myQuorumAddr = p.addr;
                break;
            }
        }
        if (myQuorumAddr == null) {
            throw new RuntimeException("My id " + myid + " not in the peer list");
        }
        if (electionType == 0) {
            try {
                udpSocket = new DatagramSocket(myQuorumAddr.getPort());
                responder = new ResponderThread();
                responder.start();
            } catch (SocketException e) {
                throw new RuntimeException(e);
            }
        }
        this.electionAlg = createElectionAlgorithm(electionType);
    }

这段代码中会读取同目录下的myid的配置文件,初始化的配置文件
是需要手动调整的,需要全集群唯一。
 

autopurge.snapRetainCount=3
autopurge.purgeInterval=0
server.1=XXX:2888:3888
server.2=XXX:2888:3888
server.3=XXX:2888:3888

quorumListenonAllIPs=true

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/753276.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号