Getting started with Zookeeper-Example of Java version HelloWorld
The previous article introduced the basic concepts of Zookeeper, how to start Zookeeper, and how to solve several possible problems.
This article describes an example of Zookeeper HelloWorld Based on the online code.
The following code is relatively simple. The core class is org. apache. zookeeper. ZooKeeper.
I like this "Framework" and use a few classes or interfaces for developers to use the core services.
Developers do not need to know so much code and details.
This also reminds us that when writing code, we should separate external services from internal implementations and design them well.
Package cn. fansunion. zookeeper; import org. apache. zookeeper. createMode; import org. apache. zookeeper. zooDefs. ids; import org. apache. zookeeper. zooKeeper; public class ZooKeeperTest {private static final int TIME_OUT = 3000; private static final String HOST = "localhost: 2181"; public static void main (String [] args) throws Exception {ZooKeeper zookeeper = new ZooKeeper (HOST, TIME_OUT, null); System. out. println ("========= create a node ============"); if (zookeeper. exists ("/test", false) = null) {zookeeper. create ("/test", "znode1 ". getBytes (), Ids. OPEN_ACL_UNSAFE, CreateMode. PERSISTENT);} System. out. println ("================ check whether the node is successfully installed ======================= "); system. out. println (new String (zookeeper. getData ("/test", false, null); System. out. println ("========= modifying node data ==========="); String data = "zNode2"; zookeeper. setData ("/test", data. getBytes (),-1); System. out. println ("========= check whether the modified node is successful ======="); System. out. println (new String (zookeeper. getData ("/test", false, null); System. out. println ("======= delete a node ========"); zookeeper. delete ("/test",-1); System. out. println ("=========== check whether a node is deleted ============="); System. out. println ("node status:" + zookeeper. exists ("/test", false); zookeeper. close ();}}
Running result
Log4j: WARN No appenders cocould be found for logger (org. apache. zookeeper. ZooKeeper ).
Log4j: WARN Please initialize the log4j system properly.
Log4j: WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
========= Create a node ==================
================ Check whether the node has been successfully installed ======================
Znode1
========= Modify node data ============
======== Check whether the modified node is successful ========
ZNode2
======= Delete a node ============
============ Check whether a node is deleted ==============
Node status: null
The above Code does not understand and does not matter at all.
After understanding the concept and General API usage, I will go deep into it.
How to Design Zookeeper, how to use APIs, and how to implement distributed locks ".
With a good start, I got started and improved step by step.
The last time I bought a Zookeeper book, I used to explain how to read Zookeeper.
I recently read Android books to study and run code. It's too busy to read Object-c and iOS and write code again.
It may take some time for Zookeeper to further study.
Maven Configuration
Pom. xml
4.0.0
cn.fansunion
ZookeeperDemo
0.0.1-SNAPSHOT
src
maven-compiler-plugin
3.1
org.apache.zookeeper
zookeeper
3.4.6
org.slf4j
slf4j-log4j12
1.7.5
log4j
log4j
1.2.17