Zookeeper client base operations and Java calls

Source: Internet
Author: User

# # Zookeeper

> Zookeeper is currently used to do data synchronization before each service synchronizes key information

I. Client-side operation

1. Create
Create [-S] [-e] path data ACL
-S is the sequential node-e as a temporary node (the temporary node disappears after the session disappears)
ACL is permission control, permissions control is troublesome, and only exists at this node and this level, its child nodes are not controlled

2. Read
Ls:
LS Path
List all child nodes under this node
Get
Get path
Get the contents and properties of this node
3. Update
Set path data [version]
Version is optional and is based on which edition updates are selected
4. Delete
Delete path [version]
5. Permissions
On the No. 215 page of Zookeeper's book, we have a detailed description.
The digest encryption method is used here, using the method provided by the zookeeper to encrypt

II. Java Client Operations

Code Address: Zkservice.java

1. Initializing the Client
"' Java
Private Curatorframework client = Curatorframeworkfactory.builder ()
. connectstring (IP of zookeeper)
. Retrypolicy (New Exponentialbackoffretry (1000, 6))
. SESSIONTIMEOUTMS (5000)
. namespace (which address is the base)
. Authorization (encryption method, password)
. build ();
```
2. Register for monitoring
"' Java
private void Watch (String path) {
Executorservice TP = Executors.newfixedthreadpool (2);
System.out.println (Thread.CurrentThread (). GetName ());
Pathchildrencache cache = New Pathchildrencache (here the initialized client is passed in, here the listener's address, true, FALSE, TP);
try {
Cache.start (PathChildrenCache.StartMode.NORMAL);
} catch (Exception e) {
E.printstacktrace ();
}
Cache.getlistenable (). AddListener (New Pathchildrencachelistener () {
public void Childevent (Curatorframework client,
Pathchildrencacheevent event) throws Exception {
Switch (Event.gettype ()) {
Case child_added://Here are three switch corresponding to three operations, which write the implementation of the business logic
Listenservice.inituser (New String (Event.getdata ())); Event.getdata () is the fetch data
Break
Case child_updated:
Break
Case child_removed:
Break
Default
Break
}
}
});
}
```

3. Create
```
String Path = "Custom";
try {
Client.getclient (). Create ()
. Withmode (createmode.persistent_sequential)//This is a sequential persistent node (automatically adds a number to the node in order), sequential is a simple persistent node
. Forpath (address here, "transmit data here");
} catch (Exception e) {
E.printstacktrace ();
}
```

Zookeeper client base operations and Java calls

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.