Hello, friends. I'm here again. Share Zookeeper's comprehensive exercise with you this time!!! It's a summary of learning zookeeper these days.
Learn together and make progress together. Continue to precipitate, slowly strong. I hope this article will be helpful to you. If there is something wrong with it, please comment on it.
Shortly after I started blogging, I was Yang Zhanhao. This is my tenth blog. Come on.
The first step is to open zookeeper's cluster service, and here we will not repeat the demonstration of zookeeper's cluster service setup and start-up. Refer to the blogger's last zookeeper article: https://my.oschina.net/u/3901188/blog/3103383.
zookeeper cluster service startup effect chart:
After the zookeeper cluster service starts successfully, the project code is written:
Write the code of zookeeper server: DistributeServer.java
package com.java8090.zookeeper; import org.apache.zookeeper.*; import java.io.IOException; public class DistributeServer { public static void main(String[] args) throws Exception { DistributeServer server = new DistributeServer(); // Create an instance of zookeeper connection object server.initZookeeperClient(); // Register service information to zookeeper cluster (create nodes and write corresponding node data) server.registerServerInfo(args[0]); // Processing business logic code (tests are written as thread dormancy) server.handleService(); } // Information IP: Port Connecting to zookeeper Cluster Service private String zookeeperClusterInfo = "192.168.100.72:2181,192.168.100.73:2181,192.168.100.74:2181"; // Connecting zookeeper Cluster Service Timeout 40s private int connectTimeout = 40000; private ZooKeeper zooKeeper; private void initZookeeperClient() throws IOException { zooKeeper = new ZooKeeper(zookeeperClusterInfo, connectTimeout, (watchedEvent) -> { // The change of service status exists in the monitoring cluster, and the corresponding processing is carried out. }); } private void registerServerInfo(String arg) throws KeeperException, InterruptedException { System.out.println("Input data:"+ arg); String zNode = zooKeeper.create("/servers/server", arg.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); System.out.println("Create node information:" + zNode); } private void handleService() throws InterruptedException { Thread.sleep(Long.MAX_VALUE); } }
Code the zookeeper client: DistributeClient.java
package com.java8090.zookeeper; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.ZooKeeper; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class DistributeClient { public static void main(String[] args) throws Exception { DistributeClient server = new DistributeClient(); // Create an instance of zookeeper connection object server.initZookeeperClient(); // Get node data for the specified node server.getChildren(); // Processing business logic code (tests are written as thread dormancy) server.handleService(); } // Information IP: Port Connecting to zookeeper Cluster Service private String zookeeperClusterInfo = "192.168.100.72:2181,192.168.100.73:2181,192.168.100.74:2181"; // Connecting zookeeper cluster service timeout 60s private int connectTimeout = 60000; private ZooKeeper zooKeeper; private void initZookeeperClient() throws IOException { zooKeeper = new ZooKeeper(zookeeperClusterInfo, connectTimeout, watchedEvent -> { // The change of service status exists in the monitoring cluster, and the corresponding processing is carried out. try { getChildren(); } catch (KeeperException | InterruptedException e) { e.printStackTrace(); } }); } private void getChildren() throws KeeperException, InterruptedException { List<String> zNodeInfoList = new ArrayList<>(); List<String> zNodeList = zooKeeper.getChildren("/servers", true); for(String zNode : zNodeList){ String zNodeInfo = new String(zooKeeper.getData("/servers/" + zNode, false, null)); zNodeInfoList.add(zNodeInfo); } System.out.println(zNodeInfoList); } private void handleService() throws InterruptedException { Thread.sleep(Long.MAX_VALUE); } }
Start the client in one of the cluster servers of the virtual machine and create a node/servers.
Then start the written zookeeper client code, DistributeClient.java
After the client program starts successfully, zookeeper cluster service monitoring is performed. And one of the servers in the virtual machine connects with the client, creating and deleting nodes in turn. The results are as follows:
In the zookeeper cluster, while the client creates and deletes nodes successfully, the program also successfully monitors the update of node data. Print out the console at the same time. Manual creation and deletion of nodes through the client is successful!!!
Then start a written zookeeper server code, DistributeServer.java
Because of the code, I configure the transfer through args, so I need to configure the incoming parameters:
At the same time, client programs that have been monitoring zookeeper cluster services have also received corresponding node update notifications:
Nodes are normally created and deleted through programs.