Zookeeper cluster installation and configuration

Source: Internet
Author: User

1, Decompression Zookeeper

2. Create the Zoo.cfg file under $zookeeper_home/conf (refer to the configuration file: zoo_sample.cfg) with the content:

# CS Communication Heartbeat time, between Zookeeper servers or between the client and server to maintain the heartbeat interval, that is, each ticktime time will send a heartbeat. Ticktime the maximum number of heartbeats (ticktime) that can be tolerated by the initial connection between the follower server (F) in the cluster and the leader Server (L) in milliseconds ticktime=2000# LF initialization of the communication time limit initlimit=10 # LF Synchronous communication time limit, the maximum number of heartbeats (ticktime) that can be tolerated between the follower server in the cluster and the leader server between requests and responses. synclimit=5# data file directory, zookeeper save the Data directory, by default, zookeeper will write the data log file is also stored in this directory. datadir=/root/app/zookeeper/data# The client connection port, the client connects to the zookeeper server's port, zookeeper listens to the port and accepts the client's access request. clientport=2181# server name and address: Cluster information (server number, server address, LF communication port, election port) #这个配置项的书写格式比较特殊, the rules are as follows: #server. N=YYY:A:B server.1=hadoop.master:2888:3888server.2=hadoop.slave:2888:3888# Port #2181: Connection for Client # 2888:follower and leader Communication #3888:leader election port

3, according to ZOO.CFG in the path of DataDir configuration to create myID file, and input SERVER.N n content (for example: The current machine is Server.1, then enter 1 in myID)

4. Replicate the configured ZOOKEEPR to other nodes

Scp-r $ZOOKEEPER _home [email protected]:/home

5. Remember to modify myID after the copy is complete.

6. Start the script on each node separately:

$ZOOKEEPER _home/bin/zkserver.sh Start

7, check the command

7.1, #zkServer. SH status//If an error contacting service appears. It is probably not running. Error because the node only started one, unable to start cluster mode 7.2, #echo Ruok | NC localhost 2181//If the command succeeds returns Imok (I ' m OK)

8. ZK Client

$ZOOKEEPER _home/bin/zkcli.sh-server hostname:2181//Start a ZK client//Finally remember a help command to master the use of other commands (you need to know a file system-like Znode before operation, Simple description: All nodes in ZK are Znode nodes, and Znode nodes can also store data as directories. Create Znodeset settings znode data get query ZNODERMR Delete Znode ... And so on self query

9. Java Client

Related code:

import org.apache.zookeeper.createmode;import org.apache.zookeeper.watchedevent;import  org.apache.zookeeper.watcher;import org.apache.zookeeper.zoodefs.ids;import  org.apache.zookeeper.zookeeper;public class zkclient {    //  Connection Address     private final static String CONN =  "192.168.1.200:2181";     // client and ZK timeout period (unit:ms)     private final static  int session_timeout = 5000;    public static void main (String[] args)  THROWS EXCEPTION {        //1, Persistent node: When a node is created successfully, the node is persisted on the ZK         //2, temporary node: in the process of creating a node, if intentional or unintentional client timeout or shutdown Nodes are removed         zookeeper zk = new zookeeper (CONN,  session_timeout, new watcher ()  {&NBsp;            @Override              public void process (watchedevent event)  {                 //receive ZK all time notice                  system.out.println (" ------------------------");                 system.out.println ("Path:" +event.getpath ());                 system.out.println ("Type:" +event.gettype ());                 SYSTEM.OUT.PRINTLN ("state" + Event.getstate ());                 system.out.println ("WRapper: "+event.getwrapper ());                 system.out.println ("------------------------");             }        });         String path =  "/hello";         byte[] data =  "Testhello!". GetBytes ();         zk.create (Path,data, ids.open_acl_unsafe,  createmode.persistent);         zk.close ();     }}


Zookeeper cluster installation and configuration

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.