ZooKeeper1 uses virtual machines to build its own zookeeper cluster

Source: Internet
Author: User

Objective:

Some time ago, I refer to the article on the Internet, combing a distributed environment based on the deployment of business systems in the resolution of data consistency, one of the scenarios is the use of zookeeper, in addition to large data processing, zookeeper does play a role in coordinating services, so the use of weekend rest time, I simply set up a zookeeper cluster on the virtual machine and learn about it.

=============================================================== Long split-line ========================================== ==========================

Body:

  This article mainly involves three pieces of content: First, is to build a virtual machine environment; Second, install zookeeper, start zookeeper, and use built-in commands to operate zookeeper; third, use the Java API to complete the operation in order to understand how to use it in the actual project.

The purpose of this time is to use VMware to build a zookeeper cluster that belongs to you. This time we choose is VMware10, the specific installation steps we can go online search, a lot of resources.

The first step is to determine the target:

Zookeeperone 192.168.224.100 CentOS

Zookeepertwo 192.168.224.201 CentOS

Zookeeperthree 192.168.224.201 CentOS

We installed the zookeeper is 3.4.6 version, can download zookeeper-3.4.6 from here; Version of JDK installed is version 1.7

Second step, install the CentOS environment, JDK1.7, configure the network:

Here you can refer to the article on the Internet, or refer to an article I wrote earlier HADOOP4 use VMware to build your own Hadoop cluster, the first step to the eighth step in this article involves the steps to install the basic environment and methods. We just need to pay attention to two points:

(1). Since we are building three servers in VMware, we can first build one, to be configured after okay, and then use the cloning function of VMware, directly cloned two other, cloned two only need to pay attention to modify/etc/sysconfig/ HWADDR properties, IPAddr, and/etc/sysconfig/network host names in Network-scripts/ifcfg-eth0 hostname

(2). JDK installation note Using JDK1.7, some zookeeper versions are not supported under JDK1.6, so simply let's use JDK1.7 directly.

Step three, install the configuration zookeeper:

(1). Download the zookeeper-3.4.6 from here and unzip the file without installing it. In my case, my zookeeper path is:/myself_settings/zookeeper3.4.6/zookeeper-3.4.6, details such as:

(2). Need to execute VI ~/.bash_profile, edit the environment variables zookeeper, after editing the source ~/.bash_profile to make the configuration effective, details such as:

(3). In the Zookeeper directory (1), there is a conf folder with a zoo_sample.cfg file in it, and we use the command MV Zoo_sample.cfg zoo.cfg Copy out a zoo.cfg file, this file is zookeeper boot is the default to load the configuration file, we can open zoo.cfg to see what the configuration, and simply understand the meaning of the configuration, and then we need to change the configuration as in the configuration:

(4). As shown in the figure in (3), in addition to the properties in the red circle in the diagram I need you to add or modify, the rest of the properties can be used by default. For DataDir and Datalogdir This even a configuration, as the name implies, respectively, is the zookeeper data and log directory, this path can be arbitrarily configured, my own directory is/opt/zookeeper4.3.6/data and/opt/ Zookeeper4.3.6/log; For the last three rows in the configuration, each line is formatted as: Server.id=host:port1:port2, where host is your host name and Port1 represents the port from the (follower) machine connected to the primary (leader) machine. Port2 represents the port used for leader elections, noting the difference between the two ports and the clientport=2181 port. And the ID in the server.id identifies a different server, in the DataDir this configuration path, the establishment of a file named myID, in the file contains only one line of content, that is, the specified ID value, such as the server "1" should be written in the myID file "1".

(5). The remaining two servers, note the modifications (4) mentioned in these need to change the configuration.

(6). After the above configuration, we can prepare to start the zookeeper, we can start the zookeeper by executing zkserver.sh start, the rest of the server will be directly started with the same command. We can also use the zkserver.sh start to specify the path of the zoo.cfg file command to start zookeeper. The boot success details are as follows:

The fourth step is to access the client using the client command:

(1). We can execute Zkcli.sh-server 192.168.224.170:2181 on the server to enter the client, we can see Welcome to zookeeper! and other text, details such as:

(2). In the client we understand the following several commands, you can do their own experience on the client, for GET/ZK This command to return the results, the meaning of each attribute value can be referred to this article for a preliminary understanding.

A. ls/ This command is used to view the content contained in the current zookeeper

B. Create/zk myData The command is to create a new Znode node ZK, whose content is MyData

C. Get/zk The command is to get the information of the created node

D. set/zk hahaha the command is to set the content of the ZK node

E. Delete/zk the command is to delete a node

Fifth step, use the Java API to access zookeeper:

In fact, the Java API provides basic functionality is based on the top of the client command to achieve, original aim, I put my collation of the online example to the following, you can execute in the local Java project, to understand the calling method. One thing to note here is that we need to introduce zookeeper to install the jar packages in the Zookeeper-3.4.6.jar and LIB directories in the package root directory in the local Java project.

1 Importjava.util.List;2 3 ImportOrg.apache.zookeeper.CreateMode;4 Importorg.apache.zookeeper.WatchedEvent;5 ImportOrg.apache.zookeeper.Watcher;6 ImportOrg.apache.zookeeper.ZooDefs.Ids;7 ImportOrg.apache.zookeeper.ZooKeeper;8 ImportOrg.apache.zookeeper.data.Stat;9 Ten  One /** A * @Title Zookeeperdemo0001.java - * @Package -  * @authorzhangyi03 the * @description to complete the first Zookeeper program based on zookeeper3.4.6 - * @date 2015-9-19 pm 11:14:20 -  * @versionv1.0 -  */ +  Public classZookeeperDemo0001 { -     //session time-out, set to system default time consistent +     Private Static Final intSession_timeout = 30000; A      at     //creating an Zookeeper instance - ZooKeeper ZK; -      -     //creating an Watcher instance -Watcher WH =NewWatcher () { -          Public voidprocess (Watchedevent event) { in System.out.println (event.tostring ()); -         } to     }; +      -     //initializing an Zookeeper instance the     Private voidCreatezkinstance ()throwsException { *ZK =NewZooKeeper ("192.168.224.170:2181", Session_timeout, wh); $     }Panax Notoginseng      -     Private voidZkoperations ()throwsException { theSystem.out.println ("\n1. Create ZooKeeper node (ZNODE:ZOO2, Data: myData2, "+ +"Permissions: Open_acl_unsafe, node Type: persistent)"); AZk.create ("/zoo2", "MyData2". GetBytes (), Ids.open_acl_unsafe, createmode.persistent); the          +System.out.println ("\n2. To see if the creation was successful: "); -System.out.println (NewString (Zk.getdata ("/zoo2",false,NULL))); $          $System.out.println ("\n3. Modify node Data "); -Stat Setdatastat = Zk.setdata ("/zoo2", "zhangyi03201509192339". GetBytes (),-1); -          the          -System.out.println ("\n4. To see if the modification was successful: ");WuyiSystem.out.println (NewString (Zk.getdata ("/zoo2",false,NULL))); the          -System.out.println ("\n5. Add child node "); Wu          for(intindex = 0; Index < 3; index++) { -Zk.create ("/zoo2/" + "Child_" + (index+1), ("Child_" + (index+1) . GetBytes (), About Ids.open_acl_unsafe, createmode.persistent); $         } -          -System.out.println ("\n6. View all child nodes: "); -list<string> childlist = Zk.getchildren ("/zoo2",false); A          for(String child:childlist) { +System.out.println ("\t\t\t child node:" +Child ); the         } -          $System.out.println ("\n7. Delete all child nodes "); the          for(String child:childlist) { theZk.delete ("/zoo2" + "/" + Child,-1); the         } the          -System.out.println ("\n8. Delete Node "); inZk.delete ("/zoo2", 1); the          theSystem.out.println ("\n9. To see if the node was deleted: "); AboutSYSTEM.OUT.PRINTLN ("Node state: [" + zk.exists ("/zoo2",false) + "]"); the     } the      the     Private voidZkclose ()throwsException { + zk.close (); -     } the     Bayi     /** the      * @authorzhangyi03 the * @date 2015-9-19 pm 11:14:01 - * @description -      * @paramargs the      */ the      Public Static voidMain (string[] args)throwsException { theZookeeperDemo0001 demo =NewZookeeperDemo0001 (); the demo.createzkinstance (); - Demo. Zkoperations (); the Demo. Zkclose (); the     } the}

Through the above five steps, we have basically been on how to install zookeeper and use zookeeper have a certain understanding, that is, zookeeper core basic content. Here are a few more questions that you need to do in your Java code to work again:

(1). The meaning of Znode and watches in zookeeper

(2). Meaning and difference between permanent nodes and temporary nodes in zookeeper

(3). The meaning of the ACL in zookeeper, and the meaning of several properties, such as Ids.open_acl_unsafe in the corresponding Java code

(4). The meaning of time stamps such as CZXID, MZXID, etc. in zookeeper; the meaning of version, cversion, aversion, and so on

(5). How do I start by using the Zookeeper node data version number and Java API to implement data locks

These 5 questions, all of me in the installation and implementation of the test code in the process of thinking, find the answer is also very simple oh, come on!

At the same time, I am looking at the lock project in the Zookeeper-3.4.6\recipes directory under the Zookeeper installation directory, this is a "write lock" Implementation, the basic idea of the process to understand, but the process of reading the code is not very deep understanding, we can also look at. Alas, today really does not want to see again, writes the blog to play the game to go, tomorrow evening to look again that example.

Dear colleagues, refueling ha!!!

ZooKeeper1 uses virtual machines to build its own zookeeper cluster

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.