Zookeeper Getting Started-java version HelloWorld example

Source: Internet
Author: User

The previous article introduced, the basic concept of zookeeper, how to start, how to solve several problems that may be encountered.
This article, according to the online code, collated an example, Zookeeper's HelloWorld.


The following code, or relatively simple, the core class is org.apache.zookeeper.ZooKeeper.


I prefer this "framework", the most core of services, with a few classes or interfaces for developers to use.
Developers don't need to know so much about code and details.
This also prompts us, writes the code time, the external service and internally realizes, separates, the design is good.


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";p ublic static void Main (string[] args) throws Exception{zookeeper Zook        Eeper = new ZooKeeper (HOST, time_out, NULL);        System.out.println ("========= Create node ==========="); if (zookeeper.exists ("/test", false) = = null) {zookeeper.create ("/test", "Znode1". GetBytes (), Ids.open_acl_u        Nsafe, createmode.persistent);        } System.out.println ("============= to see if the node is successfully installed ===============");                System.out.println (New String (Zookeeper.getdata ("/test", false, null)));        SYSTEM.OUT.PRINTLN ("========= Modify node Data ==========");                String data = "ZNode2"; Zookeeper.setdata ("/test", Data.getbytes (),-1);        System.out.println ("======== to see if the modified node succeeded ========="); System.out.prinTLN (New String (Zookeeper.getdata ("/test", false, null)));        System.out.println ("======= Delete node ==========");                Zookeeper.delete ("/test",-1);        System.out.println ("========== to see if the node was deleted ============");                System.out.println ("Node Status:" + zookeeper.exists ("/test", false));    Zookeeper.close (); } }




Run results
Log4j:warn No Appenders could is found for logger (org.apache.zookeeper.ZooKeeper).
Log4j:warn Initialize the log4j system properly.
Log4j:warn See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
========= Creating a node ===========
============= to see if the node was successfully installed ===============
Znode1
========= Modifying a node's data ==========
======== See if the modified node is successful =========
ZNode2
======= Deleting a node ==========
========== to see if the node is deleted ============
Node Status: null


The above code, can not understand, there is no relationship.
Understand the concept, know the approximate API usage, followed by deep learning.
Zookeeper how to design, how to use the API, how to achieve "distributed lock."
With a good beginning, on the road, the step by step to improve.


The last time I bought a book of this zookeeper, I had a special lecture on zookeeper, before I could see it.
Recently read Android books, research and run code. Look at Object-c and iOS, and then write the code, too busy.
Zookeeper further in-depth study, it may take some time.




MAVEN Configuration
Pom.xml
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >< Modelversion>4.0.0</modelversion><groupid>cn.fansunion</groupid><artifactid> Zookeeperdemo</artifactid><version>0.0.1-snapshot</version><build><sourcedirectory >src</sourcedirectory><plugins><plugin><artifactid>maven-compiler-plugin</ Artifactid><version>3.1</version><configuration><source/><target/></ configuration></plugin></plugins></build><dependencies><dependency>< Groupid>org.apache.zookeeper</groupid><artifactid>zookeeper</artifactid><version> 3.4.6</version></dependency><dependency><groupid>org.slf4j</groupid>< Artifactid>slf4j-log4j12</artifactid><version>1.7.5</version></dependency><dependency><groupid>log4j</groupid>< artifactid>log4j</artifactid><version>1.2.17</version></dependency></ Dependencies></project>

Zookeeper Getting Started-java version HelloWorld example

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.