Zookeeper pseudo-distributed cluster installation

Source: Internet
Author: User

Get Zookeeper installation package

: Http://apache.dataguru.cn/zookeeper

Choose a stable version to download, I download here is the zookeeper-3.4.6 version.

Zookeeper pseudo-distributed cluster installation

Pseudo-distributed clusters: In a single server, start multiple instances of zookeeper.

Upload and unzip the installation package
Cd/usrrz-bytar XF zookeeper-3.4.6.tar.gz
Create an instance configuration file
CD ZOOKEEPER-3.4.6/CONFCP zoo_sample.cfg ZOO1.CFGCP zoo_sample.cfg ZOO2.CFGCP zoo_sample.cfg zoo3.cfg
Modifying a configuration file

Configuration of Instance 1:

VI zoo1.cfgticktime=2000initlimit=10synclimit=5datadir=/tmp/zookeeper/d_1clientport=2181datalogdir=/usr/ zookeeper-3.4.6/logs_1server.1=localhost:2887:3887server.2=localhost:2888:3888server.3=localhost:2889:3889

Configuration of Instance 2:

VI zoo2.cfgticktime=2000initlimit=10synclimit=5datadir=/tmp/zookeeper/d_2clientport=2182datalogdir=/usr/ zookeeper-3.4.6/logs_2server.1=localhost:2887:3887server.2=localhost:2888:3888server.3=localhost:2889:3889

Configuration of Instance 3:

VI zoo3.cfgticktime=2000initlimit=10synclimit=5datadir=/tmp/zookeeper/d_3clientport=2183datalogdir=/usr/ zookeeper-3.4.6/logs_3server.1=localhost:2887:3887server.2=localhost:2888:3888server.3=localhost:2889:3889
Prepare to start the environment
Mkdir/tmp/zookeeper/d_1mkdir/tmp/zookeeper/d_2mkdir/tmp/zookeeper/d_3mkdir/usr/zookeeper-3.4.6/logs_1mkdir/usr /zookeeper-3.4.6/logs_2mkdir/usr/zookeeper-3.4.6/logs_3echo "1" >/tmp/zookeeper/d_1/myidecho "2" >/tmp/ Zookeeper/d_2/myidecho "3" >/tmp/zookeeper/d_3/myid
Start the cluster
/usr/zookeeper-3.4.6/bin/zkserver.sh start zoo1.cfg/usr/zookeeper-3.4.6/bin/zkserver.sh start zoo2.cfg/usr/ zookeeper-3.4.6/bin/zkserver.sh Start Zoo3.cfg
To see if it started successfully
jps# see a process similar to the following indicates that 3 instances are started successfully 13419 QuorumPeerMain13460 QuorumPeerMain13561 Jps13392 quorumpeermain# If it does not start successfully, You can view the log information for startup failure in the Zookeeper.out file.
View node Status
[Email protected] ~]#/usr/zookeeper-3.4.6/bin/zkserver.sh status zoo1.cfgjmx enabled by defaultusing Config:/usr/ zookeeper-3.4.6/bin/. /conf/zoo1.cfgmode:follower[[email protected] ~]#/usr/zookeeper-3.4.6/bin/zkserver.sh status ZOO2.CFGJMX enabled by defaultusing config:/usr/zookeeper-3.4.6/bin/. /conf/zoo2.cfgmode:leader[[email protected] ~]#/usr/zookeeper-3.4.6/bin/zkserver.sh status ZOO3.CFGJMX enabled by defaultusing config:/usr/zookeeper-3.4.6/bin/. /conf/zoo3.cfgmode:follower# found instance 2 is leader, instance 1 and instance 3 are follower

At this point, a ZK's pseudo-distributed cluster has been built.

Java Client Test

You need to introduce the two jar packages of Zookeeper-3.4.6.jar and Slf4j-api-1.61.jar.

package com.cjw.demo;import org.apache.zookeeper.createmode;import  org.apache.zookeeper.watchedevent;import org.apache.zookeeper.watcher;import  org.apache.zookeeper.zookeeper;import org.apache.zookeeper.zoodefs.ids;public class  Zookeeperclient {    public static void main (String[] args)   throws exception {                 watcher watcher = new watcher ()  {              @Override              public void process (watchedevent event)  {                 system.out.println (Event.toString ());             }                    };                 zookeeper zk  = new zookeeper ("192.168.157.22:2181",  3000, watcher);         system.out.println ("= = Create Node");         zk.create ("/ CJW ", " Znode1 ". GetBytes (),  ids.open_acl_unsafe, createmode.persistent);         system.out.println ("= = See if node is installed successfully");         System.out.println (New string (Zk.getdata ("/CJW",  false, null));         system.out.println ("= = = Modify node data");         zk.setdata ("/CJW",  "cjw2015". GetBytes (),  -1);         System.out.println ("= =" to see if the modified node succeeded");         system.out.println (New string (Zk.getData ("/CJW ",  False, null));         system.out.println ("= = delete Node");         zk.delete ("/CJW",  -1);         system.out.println ("= = See if node is deleted");         SYSTEM.OUT.PRINTLN ("Node state:"  + zk.exists ("/CJW",  false));                 zk.close ();     }}

Operation Result:

= = = = = Watchedevent state:syncconnected Type:none path:null==== See if the node is installed successfully znode1==== modify the data of the node to see if the modified node was successful cjw2015 = = = Delete Node = = To see if the node is deleted node state: null
Configuration file Description
Initlimit:follower connects and synchronizes the initial connection time to leader, which is represented by a multiple of ticktime. The connection fails when the connection time is initialized beyond the set time. The length of time to request and answer when a message is sent between Synclimit:follower and leader, if follower cannot communicate with leader within the set time frame, then the follower is discarded and is configured as a multiple of ticktime. Ticktime: Defines the time interval for the heartbeat, note: ZK's client and server also have a similar session concept to web development, while the minimum session expiration time in ZK is twice times ticktime DataDir: The database snapshot feature is stored in memory. ClientPort: Listen to the port number of the client connection Datalogdir:zk run the relevant log write directory, set the configuration, then the Datalog log directory will be invalid, dedicated log storage path, for the performance and stability of ZK benefit.


Zookeeper pseudo-distributed cluster installation

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.