Author: Regan Yue
Source: Hang Seng LIGHT cloud community
Ultra fine! A case of Zookeeper election
Today, let's take you to the case of realizing election with Zookeeper to help you better learn Zookeeper.
1, Install required third-party libraries
To use Go to operate Zookeeper, you need to use Go get GitHub com/samuel/Go-Zookeeper/zk. According to its description on GitHub, it is a Zookeeper client~
Of course, now its latest warehouse address is https://github.com/go-zookeeper/zk
You can also download the third-party library of this warehouse.
2, Define the structure of configuration information and election management information
type ZookeeperConfig struct { Servers []string RootPath string MasterPath string } type ElectionManager struct { ZKClinet *zk.Conn ZkConfig *ZookeeperConfig IsMaster chan bool }
The configuration information includes the address, root path and Master node path of the zookeeper cluster server.
The election management information includes the connection information of the zookeeper, the configuration information of the zookeeper, and the pipeline for transmitting the election information.
3, Program main logic
func main() { zkconfig := &ZookeeperConfig{ Servers: []string{"node01:2181", "node02:2181", "node03:2181"}, RootPath: "/test04", MasterPath: "/master", } isMasterChan := make(chan bool) electionManager := NewElectionManager(zkconfig, isMasterChan) go electionManager.Run() var isMaster bool for { select { case isMaster = <-isMasterChan: if isMaster { fmt.Println("Implement specific business logic") } } } }
This zkconfig is used to fill in the zookeeper cluster configuration information we want to use. Ismastechan establishes a channel to return the selection results, and then creates an election manager. Just as there is an election committee in our ordinary election, the election of zookeeper also needs an election manager. Then open a process to vote: go election manager Run(), in addition to electing the master node every time the cluster is started, you also need to monitor the master node. If there is a problem with the master node, you need to elect a new master node immediately. The following isMaster determines whether it is the master node through the return value of the channel. Then, the following for loop is to continuously read the election results from the pipeline. Whether it is successful or not. If it is successful, it means that the cluster can operate normally, and the specific business logic can be realized. If it is not successful, it can only wait all the time.
4, Create election Manager
func NewElectionManager(zkConfig *ZookeeperConfig, isMaster chan bool) *ElectionManager { electionManager := &ElectionManager{ nil, zkConfig, isMaster, } electionManager.initConnection() return electionManager }
Creating an election manager is relatively simple. You can enter some configuration information to initialize the connection, and then pass the initialized election manager.
5, Initialize Zookeeper connection
The method of initializing Zookeeper connection is used in creating election manager. The implementation is as follows:
func (electionManager *ElectionManager) initConnection() error { if !electionManager.isConnected() { conn, connChan, err := zk.Connect(electionManager.ZKConfig.Servers, time.Second*5) if err != nil { return err } for { isConnected := false select { case connEvcent := <-connChan: if connEvcent.State == zk.StateConnected { isConnected = true fmt.Println("zk Connection succeeded") } case _ = <-time.After(time.Second * 3): return errors.New("zk Connection timeout!") } if isConnected { break } } electionManager.ZKClientConn = conn } return nil }
First, judge whether the zookeeper has been connected. If there is a problem with the connection or there is no connection, connect the zookeeper.
Let's introduce this ZK Connect, which is used to establish a new connection to the Zookeeper server pool. The first parameter is the server cluster address, and the second parameter is the amount of time that the current session is still considered valid after losing the connection with the server. Before the session times out, you can re-establish connections to different servers and keep the same session. This allows you to maintain and monitor any temporary nodes.
Then you enter the cycle and keep going to connChan to get things. When connChan is ZK Stateconnected indicates that the connection is successful, and isConnected is assigned a value of true. If it fails for 3 seconds, an error is reported, and the connection times out. When the connection is successful, the for loop will jump out and the electionmanager Zkclientconn = conn indicates the connection of the obtained connection to the election manager.