ZNODE, ACL, ZAB protocol, Watcher, common command use, we now how to use ZooKeeper? I don’t know what he can do because I haven’t touched ZooKeeper before. Take scenarios as an example. Zookeeper is a highly available distributed data management and coordination framework that enables data consistency in distributed environments. Based on such features, let’s talk about what scenarios are implemented.

Source: github.com/limingios/n… [they are] github.com/limingios/n…

(I) Take e-commerce system as an example

There are many services in the e-commerce system: membership system, commodity system and transaction system.

  • 1.1 scenario

The system calls each other, the trading system calls the member system

If: member system break down how to do, call break down suddenly. The trading system has a direct impact on the trading system because there is something wrong with the member system called. The member system called in red cannot be used. For this reason, we do not know whether the member system is available, and whether it would be better if there is a mapping relationship.

Consider joining the cluster of ZooKeeper in the middle. My member system provides a service to query basic information externally. The service tells ZooKeeper and ZooKeeper gets this value. At the same time, the transaction system went to ZooKeeper to check and ask which member system is available. Zookeeper told me which service (that server) is available. In other words, we need to let the service provider membership system create a node zNode in ZooKeeper. Last time we talked about ZNode, create a temporary node, which means that if you have this node down, the session will be gone, and the node will disappear. As mentioned last time, temporary nodes will be automatically deleted by shutting down the client. According to this feature, can each machine register a service? Service is a node, when the machine goes down, the node will disappear naturally.

After obtaining the available server through the temporary node of ZooKeeper, I started to use the member system. However, when I pointed to the member system separately, the member system suddenly broke down. The trading system needs to find a new available member system, all red, is not available, ZooKeeper feature Watcher, callback method, if the member system is down, tell the trading system to subscribe again. This is a service registry.

  • 1.2 Distributed Service Registration and Subscription

In a distributed environment, in order to guarantee the high availability of service, usually of the same application or the same service provider will deploy more of service, to achieve equivalence and consumers will need to choose one in the peer server to perform the related business logic, is a typical service registry and subscribe, consumer and producer (to load balancing scheme). Conclusion: There is some kind of subscription relationship between systems. That’s how Dobbo works

#### (2) source demo

  • Step 2.1
# start zk
cd/ root/zookeeper - 3.4.10 / bin sh zkServer. Sh start# client create folderSh zkCli. Sh - server 192.168.69.101:2181192168 69.102:2181192168 69.103:2181Create the root node
create  /tl myData 
Copy the code

  • 2.2 Importing Projects Install is installed for each project

  • 2.3 run memberserver

  • 2.4 Ensure that OrderClient can be run while MemberServer is running

  • 2.5 Function Description

After the root node is created through the ZK client, ensure that the Linux ZK is running, start the server, Java will automatically create temporary nodes, orders can be directly accessed, if the server is shut down, the temporary node will automatically disappear.

PS: This time mainly for the scene, zK itself is a distributed framework, it rarely has the situation of downtime, unless external factors, such as memory hard disk burst.