Linux builds zookeeper cluster __linux

Source: Internet
Author: User
Tags zookeeper
1. Prepare 3 virtual machines and close their firewalls

The system can be CentOS or red Hat

This example uses CENTOS7


2. Download Zookeeper

wget http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.8/zookeeper-3.4.8.tar.gz


3. Decompression

TAR-ZXVF zookeeper-3.4.8.tar.gz


new zoo.cfg under 4.zookeeper-3.4.8/conf

Three machines to do the same operation!

CD zookeeper-3.4.8/conf

VI zoo.cfg

The contents are as follows:

ticktime=2000
Datadir=/usr/soft/data-zookeeper
clientport=2181
Initlimit=5
synclimit=2
server.1=192.168.100.15:2888:3888
server.2=192.168.100.16:2888:3888
server.3=192.168.100.17:2888:3888


5. New Data Directory/usr/soft/data-zookeeper

Cd/usr/soft/data-zookeeper New myID files

3 virtual machines define myID file contents respectively, 192.168.100.15--> 1;192.168.100.16--> 2;192.168.100.17 3;


6. Three machines are activated separately

./zkserver.sh Start


7. Check Status

./zkserver.sh Status

Results:



8. Test

Mode:leader has read and write access, mode:follower only Read permission

Test Script Java

8.1 Add Maven Dependencies

<dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper< /artifactid>
            <version>3.4.8</version>
 </dependency>

8.2

public class Zookeepertest {private static final String HOST2 = "192.168.100.15:2181";
    private static final String HOST1 = "192.168.100.16:2181";

    private static final Integer TIMEOUT = 2000; public static void Main (string[] args) {try {zookeeper zooKeeper1 = new Zookeeper (HOST1, TIMEOUT, Nu
            ll);
            Zookeeper ZooKeeper2 = new Zookeeper (HOST2, TIMEOUT, NULL);

            SYSTEM.OUT.PRINTLN ("16 Create Node ..."); if (zookeeper2.exists ("/tests", false) = null) {zookeeper2.create ("/tests", "ZNode1". GetBytes (), zoodefs .
            Ids.open_acl_unsafe, createmode.persistent);
            } System.out.println ("========= See if the node was created successfully ========");



            System.out.println (New String (Zookeeper1.getdata ("/tests", false, null));
            SYSTEM.OUT.PRINTLN ("======== modify node's data ========");
            SYSTEM.OUT.PRINTLN ("16 Modify Node ...");
            String data = "ZNode2"; Zookeeper2.setdata ("/tests", Data.getbytes (),-1);
            System.out.println ("======== See if the modified node was successful =========");


            System.out.println (New String (Zookeeper1.getdata ("/tests", false, null));
            System.out.println ("======== Delete node ========");
            SYSTEM.OUT.PRINTLN ("16 Modify Node ...");
            Zookeeper2.delete ("/tests",-1);
            System.out.println ("======== See if the node is deleted =========");


            SYSTEM.OUT.PRINTLN ("Node state:" + zookeeper2.exists ("/tests", false));
            System.out.println ("========= see if the node exists ========");
            System.out.println ("15 View node ...");
        System.out.println (New String (Zookeeper1.getdata ("/tests", false, null));
        catch (IOException e) {e.printstacktrace ();
        catch (Interruptedexception e) {e.printstacktrace ();
        catch (Keeperexception e) {e.printstacktrace (); }


    }
}

Test results:



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.