Zookeeper Distributed installation Configuration

Source: Internet
Author: User
Tags stop script zookeeper client

Zookeeper Distributed installation configuration


Zookeeper IntroductionOverview

1. Zookeeper is a Distributed , open-source Distributed Applications Coordination Services, is Google the chubby a Open Source implementation, is an important component of Hadoop and HBase. It is a software that provides consistent services for distributed applications, including configuration maintenance, domain name services, distributed synchronization, group services, and so on.

2. The goal of zookeeper is to encapsulate complex and error-prone services that provide users with easy-to-use interfaces and performance-efficient, robust systems.

3. The zookeeper contains a simple set of primitives that provide both Java and C interfaces.

4. The Zookeeper code version provides a distributed, exclusive lock, election, queue interface, and the code is zookeeper-3.4.3\src\recipes. Where the distribution locks and queues have Java and C two versions, the election is only Java version.


Basic Operating procedure of ZooKeeper:

1, election leader.

2. Synchronize data.

3. There are many algorithms in the process of election leader, but the election standards to be met are consistent.

4, leader to have the highest zxid.

5, most of the machines in the cluster get response and follow selected leader.

Zookeeper Download and install   Download

Official website: https://zookeeper.apache.org/

You can go to the official website to download the version you need, for example, I need to install the 3.4.6 Version:

installation

The zookeeper operation requires a Java environment, so you will need to install the JDK,JDK installation for reference:

http://blog.csdn.net/u011204847/article/details/51002072

Copy the downloaded compressed package to the directory you want to install and unzip it:

[Email protected] software]# TAR-XZF zookeeper-3.4.6.tar.gz


File directory after decompression:

Add Environment variables:

Vi/etc/profile

Export Zookeeper_home=/usr/local/software/zookeeper-3.4.6export path=.: $PATH: $ZOOKEEPER _home/bin


To make an environment variable effective:

Source/etc/profile


ZookeeperConfiguration

stand-alone configuration

Create directory data under the Zookeeper directory:

[[Email protected] software]# CD Zookeeper-3.4.6[[email protected] zookeeper-3.4.6]# mkdir data


Then to the zookeeper/conf directory, modify the zoo_sample.cfg name called Zoo.cfg

Then modify the configuration file Zoo.cfg:

VI zoo.cfg   //Standalone only need to modify datadir for the data directory created previously


The configuration is ready to start:

Switch to the bin directory of the zookeeper, and then

./zkserver.sh start    //Open Zookeeper

Other commands:

./zkserver.sh Stop    //close zookeeper./zkserver.sh status   //View zookeeper state./zkcli.sh   //Open Zookeeper Client



Distributed Configuration

Here I use three hosts for the zookeeper distributed configuration.

1, first, we continue to modify the configuration file on the basis of the previously configured standalone machine:

2. Add the following at the end of the previous configuration file:

server.0=master01:2888:3888   //master01 for your hostname, the previous 0 is for this host with the idserver.1=slave01:2888:3888    // Similarly slave01 is also the hostname, you can use ipserver.2=slave02:2888:3888

3. Then use the command in the data file we created earlier:

echo 0 myID   //Create myID file in the data directory and write 0 (corresponding to 0 in server.0 in the above configuration)

4, the host configuration of the zookeeper entire directory is copied to the other two hosts under the same path, and on those two tables modified myID 1 and 2.

5, modify the other two host/etc/profile files, so that their zookeeper environment configuration and the first host

6. After opening zookeeper on each host, check the status


After opening the zookeeper in three hosts, the view status will find a leader and two follower, at this time our zookeeper distributed environment and build up

Attention:

1, above I can use similar ssh slave01 command to log on to SLAVE01 and SLAVE02 host directly because I did a password-free login between three hosts.

2, the host name can be used directly in the configuration files and commands because I do the IP and host name Mapping.

3, we can log on to another host after the direct use of the zkserver.sh script is because we configured the zookeeper environment and make it effective.

Additional Configuration

After installing the zookeeper distributed environment, it is usually necessary to log on to each host to start zookeeper, which is cumbersome, so we can write a shell script to start and close this zookeeper cluster.

Before writing the script we need to solve a problem, if we use the script directly to start the other host zookeeper, we will find that the boot is unsuccessful. This involves the issue of logging into the shell and the non-login shell. For shell scripts and login/non-login shells, refer to:http://blog.csdn.net/u011204847/article/details/51184883

Here I directly write a workaround:

Method One:

Modify the zkenv.sh in the Zookeeper/bin directory to add your JDK path at the diagram location or at the beginning of the file.

Method Two: Echo the profile configuration information to. BASHRC ' Source/etc/profile ' >> ~/.BASHRC

Method Three: Add "source/etc/profile;" In the script code, for example: SSH $i "source/etc/profile;/usr/local/software/zookeeper-3.4.6/bin/ zkserver.sh Start "

Shell Startup script Example:

#!/bin/bashfor i in Master01 slave01 slave02do ssh $i "/usr/local/software/zookeeper-3.4.6/bin/zkserver.sh start" done

Script Execution Example:


Shell Stop Script Example:

#!/bin/bashfor i in Master01 slave01 slave02do        ssh $i "/usr/local/software/zookeeper-3.4.6/bin/zkserver.sh Stop" Done

Script Execution Example:


Java Code operations Zookeeper Example

Pom dependency:

<dependency>    <groupId>org.apache.curator</groupId>    <artifactId> Curator-framework</artifactid>    <version>2.8.0</version></dependency>


code example:

@Test   //test class: Test zookeeper Connection public void test13 () throws exception{    //Specify the address of the zookeeper cluster    String connectstring = "192.168.33.130:2181,192.168.33.131:2181,192.168.33.132:2181";    1000: Represents a retry interval of     3: Indicates the number of retries    exponentialbackoffretry retrypolicy = new Exponentialbackoffretry (3);    Use curator to create a ZK link    curatorframework client = curatorframeworkfactory.newclient (connectstring, retrypolicy);    Start Connection    Client.start ();    InetAddress localHost = Inetaddress.getlocalhost ();    String IP = localhost.gethostaddress ();    Client.create ()        . creatingparentsifneeded ()   //If the parent node does not exist, this creates the        . Withmode (createmode.ephemeral)  Create a node type of temporary node        . Withacl (Ids.open_acl_unsafe)        . Forpath ("/spider/" +ip);}

Printing results:

Attention:

This value disappears after the default of 40 seconds due to the temporary node being established. This feature of the temporary node can be used to monitor the running state of some programs.

Zookeeper Distributed installation 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.