1: What is Zookeeper?
Answer: ZooKeeper is an open source distributed coordination service. It is the manager of the cluster and monitors the feedback information of the cluster nodes for the next reasonable operation.
Services provided by Zookeeper: manage data submitted by user programs; provide node monitoring services for user programs.
Application sites: master-slave coordination, dynamic online and offline of server nodes, load balancing, cluster management, etc. . .
2: Zookeeper features: consistency, atomicity, single view, reliability, real-time.
3: The roles of the Zookeeper cluster: Leader, follower and Observer
Leader: The only scheduler and processor of transaction requests, and the scheduler of internal services in the cluster.
followe: Participate in Leader election voting, process client’s non-transaction requests, forward transaction requests to the Leader server, participate in the vote of transaction request Proposal
Observer: a weakened version of Follower, does not participate Any voting is mainly for improving the throughput of non-transactional processing of the cluster without affecting the transaction processing capability of the cluster.
4: Zookeeper cluster mechanism: half mechanism: more than half of the machines in the cluster survive and the cluster is available.
5: Zookeeper provides a file system and notification mechanism
6: Zookeeper protocol: ZAB protocol, an atomic broadcast protocol that supports crash recovery.
7: Four types of data nodes-znode
PERSISTENT-persistent node
EPHEMERAL-temporary node
PERSISTENT_SEQUENTIAL-persistent sequence node
EPHEMERAL_SEQUENTIAL-temporary sequence Node
8: zookeeper-watcher-mechanism—-data change notification
Zookeeper< span style="font-family: 宋体">Allow the client to send to a certain Znode Register aWatcher listener, when some specified events on the server side trigger thisWatcher, the server will send an event notification to the specified client to realize the distributed notification function, and then the client will follow the < /span>WatcherNotify status and event types to make business changes.
working mechanism:Client registration watcher,server processingwatcher ,Client callbackwatcher
9: WatcherFeature summary:One-time, client-side serial execution, lightweight, asynchronous
10: Client RegistrationWatcherImplement
1. CallgetData()/getChildren()/ exist()threeAPI, incomingWatcherObject
2. Mark the requestrequest, packageWatchertoWatchRegistration
3. Package into PacketObject, send server to sendrequest span>
4. After receiving the response from the server, it will Watcherregister to ZKWatcherManagerManaged in Chinese
5. Request a return and complete the registration.
11 :Server-side processingWatcherimplementation
1. The server receivesWatcherand store
2. Watchertrigger
3. callprocessMethod to triggerWatcher
12: Client callbackWatcher
ClientSendThreadThreads receive event notifications and hand them over toEventThreadThread callback Watcher. The client’s Watcher mechanism is also one-time. Once triggered, the Watcher becomes invalid.
13:Zookeeper < span style="font-family: 宋体">下 ServerWorking status< /p>
server has four states, namelyLOOKING ,FOLLOWING,< span lang="EN-US">LEADING,OBSERVING.
LOOKING: Looking for< span lang="EN-US">LeaderStatus. When the server is in this state, it will think that there is no Leader in the current cluster, so it needs to enter< /span>LeaderElection status.
FOLLOWING: Follower status. Indicates that the current server role isFollower.
LEADING: Leader status. Indicates that the current server role isLeader.
OBSERVING: Observer status. Indicates that the current server role isObserver.
14:Zookeeper< span style="font-family: 宋体">Data synchronization is usually divided into four categories:
direct difference Synchronization (DIFFsynchronization),roll back first and then differentiated synchronization (TRUNC+DIFFsynchronization),Only rollback sync (TRUNCsync ),full synchronization (SNAPSync)
15:zookeeperHow to ensure the consistency of the order of transactions of?
zookeeperusing a global incremental transaction< /span>Id to identify, will first give other server Issue a transaction execution request , If more than half of the machines can be executed and can be successful, then it will start execution.
16:zookeeperLoad Balancing andnginxLoad balancing difference
zk load Balance can be adjusted,nginxOnly the weight can be adjusted, other things that need to be controllable need to be written by yourself Plug-in; butnginx throughput ratiozk is much larger, it should be said which way to choose according to business.
17:Zookeeper< span style="font-family: 宋体">Allow the client to send to a certain Znode Register aWatcher listener, when some specified events on the server side trigger thisWatcher, the server will send an event notification to the specified client to realize the distributed notification function, and then the client will follow the < /span>WatcherNotify status and event types to make business changes.
18: WatcherFeature summary: span>
1. once Sex,Whether it is the server or the client, once aWatcheris triggered,Zookeeper will save it from the corresponding storage Removed. This design effectively reduces the pressure on the server. Otherwise, for nodes that are updated very frequently, the server will continuously send event notifications to the client, which puts great pressure on the network and the server.
2.Client serial execution, ClientWatcherCallback The process is a serial synchronization process.
3.Lightweight,WatcherThe notification is very simple. It only tells the client that an event has occurred, but not the specific content of the event. The client registers with the serverWatcherwhen the client’s realWatcher object entity is not passed to the service At the end, it is only marked with the booleantype attribute in the client request.
4.watcher eventAsynchronous sending span>watcher‘s notification event fromserver span>Send toclientis asynchronous , There is a problem, different clients and servers communicate through socket, Due to network delays or other factors, the client listens to the event at the moment of failure, due to the Zookeeper itself ordering guarantee, that is, the client will only perceive what it is monitoring after listening to the event span>znode has changed. So we use Zookeeper can’t expect to be able to monitor every change of the node. Zookeeper can only guarantee final consistency, but cannot guarantee strong consistency.
5.Register< span lang="EN-US">watcher getData,exists,getChildren
6.triggerwatcher create,delete,setData
7.When a client connects to a new server,watch will be The session event is triggered. When the connection with a server is lost, it is impossible to receive the watch. And whenclientreconnects, if necessary, all previously registered watch will be re-registered. Usually this is completely transparent. Only in a special case, watch may be lost: for an uncreatedznode的exist watch< span style="font-family: 宋体">, if it is created during the disconnection of the client, and then deleted before the client connects, in this case, thiswatchThe event may be lost.