catalogue
1, Environment construction
- Create a normal Maven project
- Import log4j The properties log file to the root directory of the project or under the resource file.
- In POM Add Zookeeper dependency in XML:
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.5.7</version> </dependency> <!--junit unit testing --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.1</version> </dependency>
- In addition to maven's project dependencies, you can also deploy the Zookeeper environment by importing Zookeeper related jar packages. The jar packages required by Zookeeper are as follows:
- ZOOKEEPER_HOME directory:
zookeeper-3.5.7.jar
- ZOOKEEPER_HOME/lib Directory:
jline-0.9.94.jar,
log4j-1.2.16.jar,
netty-3.10.5.Final.jar,
slf4j-api-1.6.1.jar,
slf4j-log4j12-1.6.1.jar
Copy the above jar package to the lib directory of the project. And build, import the project.
2, API operation Zookeeper
After the environment is built, you can use Java code to operate Zookeeper! The first step, of course, is to connect Zookeeper.
1. Connect Zookeeper
-
preface
The Java API only needs one step to connect Zookeeper: create a Zookeeper object.
First, enter the Zookeeper class and you can see:
public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher) throws IOException { this(connectString, sessionTimeout, watcher, false); }
To create a Zookeeper object, you need to pass in three parameters:
- connectString: address of the connection, including host name and port number.
- sessionTimeout: indicates the longest time Zookeeper waits for the client to communicate. If the client does not communicate with the server after this time, it is considered that the client has terminated. The general setting value is 5~10s. The unit is milliseconds.
- watcher: listener. The interface used to receive session events needs to be defined and implemented by the process() method.
With the above foundation, you can connect Zookeeper!
-
Connect Zookeeper
Zookeeper zkClient = ""; String connectStr = "node1:2181,node2:2181,node3:2181"; zkClient = new ZooKeeper(connectStr, 2000, new Watcher() { @Override public void process(WatchedEvent watchedEvent) { } });
Successfully connected:
[note]
- In the string, two paragraphs must not add spaces, otherwise an error will be reported
- If you want to use node1, node2 and node3 host names instead of the input host ip on windows, you must configure the host ip mapping locally on windows. Windows configuration host mapping directory C:\Windows\System32\drivers\etc\hosts. Just add the "ip host name" field under the file.
At this point, you can ping the host name directly on the Windows command line.
2. Create node
-
preface
The method required to create a node is create(). Four parameters need to be passed in here:
public String create(String path, byte[] data, List<ACL> acl, CreateMode createMode) throws KeeperException, InterruptedException {}
The four parameters represent:
- String path: node path
- byte[] data,: node data --- byte array
- List < ACL > ACL: node permissions
- Createmode: node type
Among them, the basic parameters for setting file permissions are interpreted as follows:
(1) OPEN_ACL_UNSAFE: fully open. In fact, the world authentication mode is adopted here. Since each zk connection has a world authentication mode, znode sets open_ ACL_ When unsafe, it is open to all connections.
(2) CREATOR_ALL_ACL: give all permissions to create this znode connection. In fact, auth authentication mode is adopted here, and sessionID is used for authentication. So creator is set_ ALL_ When creating an ACL, the connection of the znode can be modified.
(3) READ_ACL_UNSAFE: all clients are readable. In fact, the world authentication mode is adopted here. Since each zk connection has a world authentication mode, znode sets read_ ACL_ When unsafe, all connections can read the znode.
The basic parameters for setting file types are interpreted as follows:
Observing the CreateMode source code, we can find that CreateMode is an enumeration class. The types of nodes created include: persistent Node, persistent Node with sequence number, transient Node and transient Node with sequence number
-
Create node
- Create a persistent node without sequence number
@Test public void createNode() throws InterruptedException, KeeperException { String node = zkClient.create("/javaClient", "javaTest".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISENT); System.out.println(node); }
Persistent node created successfully:
-
To create a persistent node with sequence numbers:
@Test public void createNode() throws InterruptedException, KeeperException { String node = zkClient.create("/javaClient1", "javaTest".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISENT_SEQUENTIAL); System.out.println(node); }
Created successfully:
-
To create a node without sequence number:
@Test public void createNode() throws InterruptedException, KeeperException { String node = zkClient.create("/javaClient2", "javaTest".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); System.out.println(node); }
Created successfully:
[note] after the creation is successful, the / javaClient2 node is not found in the host. Because the client service is shut down after the Java operation is completed, the temporary node has been deleted, and it is no longer available when you view it again. At this time, you can set a program sleep time to enable another client to see the file during the sleep time.
@Test public void createNode() throws InterruptedException, KeeperException { String node = zkClient.create("/javaClient3", "javaTest".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); System.out.println(node); Thread.sleep(10000); }
-
Create a short numbered node
To create a temporary node with sequence number, you can refer to the creation of a persistent node with sequence number. You only need to set the value of CreateMode to "EPHEMERAL_SEQUENTIAL"
3. Delete node
-
preface
To delete a node, you can use the delete() method in Zookeeper
public void delete(String path, int version) throws InterruptedException, KeeperException {}
Two parameters: the first is the path of the node to be deleted, and the second is the version number of the node to be deleted. The version number can be used to view the node details through ls -s path. cversion is the corresponding version value.
If the written version is incorrect, the following error will be reported:
[note] the delete method is used to delete a single node. Iterative deletion is not allowed.
There is a child node under the / test node here
If you use delete to delete, the following error will be reported:
-
Delete node
public void deleteNode() throws InterruptedException, KeeperException { zkClient.delete("/test20000000006", 0); }
Deleted successfully: