Zookeeper 原始碼(六)Leader-Follower-Observer
阿新 • • 發佈:2018-11-14
Zookeeper 原始碼(六)Leader-Follower-Observer
上一節介紹了 Leader 選舉的全過程,本節講解一下 Leader-Follower-Observer 伺服器的三種角色。經過 Leader 選舉後各伺服器都能確定自己的角色,下一步就是初始化各自的角色。
先回顧一下【QuorumPeer】的 run 方法選舉結束後建立對應的角色:
case OBSERVING: setObserver(makeObserver(logFactory)); observer.observeLeader(); break; case FOLLOWING: setFollower(makeFollower(logFactory)); follower.followLeader(); break; case LEADING: setLeader(makeLeader(logFactory)); leader.lead(); setLeader(null); break;
一、Leader
(1) Leader 初始化 【Leader】
protected Leader makeLeader(FileTxnSnapLog logFactory) throws IOException { return new Leader(this, new LeaderZooKeeperServer(logFactory, this, this.zkDb)); } // Leader 啟動時啟動 2888 的埠,用於伺服器內部通訊(如資料同步等) Leader(QuorumPeer self,LeaderZooKeeperServer zk) throws IOException { this.self = self; try { if (self.getQuorumListenOnAllIPs()) { ss = new ServerSocket(self.getQuorumAddress().getPort()); } else { ss = new ServerSocket(); } ss.setReuseAddress(true); if (!self.getQuorumListenOnAllIPs()) { ss.bind(self.getQuorumAddress()); } } catch (BindException e) { // 省略... } this.zk = zk; }
二、Observer
三、ZooKeeperServer
參考:
- 《Zookeeper原始碼分析之六 Leader/Follower初始化》:https://blog.csdn.net/haihongazar/article/details/52709244
- 從 Paxos 到 Zookeeper : 分散式一致性原理與實踐
每天用心記錄一點點。內容也許不重要,但習慣很重要!