Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install, build and use ZooKeeper in stand-alone and cluster environment

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to install, build and use ZooKeeper in stand-alone and cluster environments". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "how to install, build and use ZooKeeper in stand-alone and cluster environments" together!

1. Download

First we download the latest stable version of zookeeper

2. Decompression

Once the download is complete, we unzip the package

3. Create a configuration file

Find the conf folder under the extracted path, enter the conf folder and copy zoo_sample.cfg, named zoo.cfg

#milliseconds per tick. The following initLimit and syncLimit are both tickTime=2000#Duration of initialization synchronization phase initLimit=10#Duration of synchronization confirmation syncLimit=5#Snapshot storage path, do not use/tmp. #clientPort=2181#Maximum number of clients that can be connected #If you need to process more clinets, increase this number #maxClientCnxns=60# Be sure to read the maintenance section of the ## administrator guide before turning on autopurge.# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance## The number of snapshots to retain in dataDir#autopurge.snapRetainCount=3# Purge task interval in hours# Set to "0" to disable auto purge feature#autopurge.purgeInterval=1

For learning, there is generally no need to make modifications. Production environment, need to modify dataDir. Other parameters are subsequently tuned.

4, Start ZooKeeper

Run bin/zkServer.sh start in the ZooKeeper root directory

➜ zookeeper-3.4.12 bin/zkServer.sh startZooKeeper JMX enabled by defaultUsing config: /Users/yimingli/dev/tool/zookeeper-3.4.12/bin/../ conf/zoo.cfgStarting zookeeper ... STARTED5, Connect ZooKeeper zookeeper-3.4.12 bin/zkCli.sh6, Execute basic commands through client

1. Create a znode named/my_test, which carries testData

[zk: localhost:2181(CONNECTED) 0] create /my_test testData

2. View znode information

[zk: localhost:2181(CONNECTED) 2] get /my_test

Return as follows:

testDatacZxid = 0x2e9ctime = Mon Oct 29 10:50:46 CST 2018mZxid = 0x2e9mtime = Mon Oct 29 10:50:46 CST 2018pZxid = 0x2e9cversion = 0dataVersion = 0aclVersion = 0ephemeralOwner = 0x0dataLength = 8numChildren = 0

3. Modify znode data

[zk: localhost:2181(CONNECTED) 3] set /my_test testDataV2

4. Create a child znode

[zk: localhost:2181(CONNECTED) 0] create /my_test/tester1 testData

5. List child znodes

[zk: localhost:2181(CONNECTED) 5] ls /my_test[tester1]

6. Delete znode

Znodes with child nodes cannot be deleted directly, otherwise an error will be reported.

[zk: localhost:2181(CONNECTED) 6] delete /my_testNode not empty: /my_test

We delete/my_test/tester1.

[zk: localhost:2181(CONNECTED) 8] delete /my_test/tester1

Check out the znode again.

[zk: localhost:2181(CONNECTED) 9] get /my_test/tester1Node does not exist: /my_test/tester1

Has been successfully deleted.

Cluster Configuration and Startup

We configured and started a cluster of three servers.

1. Modify zoo.cfg file

tickTime=2000dataDir=/var/lib/zookeeperclientPort=2181initLimit=5syncLimit=2server.1=ip1:2888:3888server.2=ip2:2888:3888server.3=ip3:2888:3888

initLimit, zookeeper is used to limit the length of time zookeeper servers can connect to the leader.

syncLimit, how long a server expires at the leader.

The above two expiration times are both tickTime,

In this example, the initLimit duration is 5 tickTime=5*2000ms=10 seconds.

server.x lists all zookeeper services. Cluster startup It knows which server it is by looking at myid under data.

2888 is used to connect to other servers. 3888 is used for leader elections.

Copy two copies of Zookeeper to the other two servers.

3. Create a myid file under the configured dataDir path, and the content of the file is the x value corresponding to your server.x. For example, zoo1, this sever, myid file content is 1.

Start each zookeeper

5, check the status of the machine, is the leader or follower

➜ zookeeper-3.4.12 bin/zkServer.sh status

So far we have started the zookeeper cluster and verified that one is the leader and the other two are followers.

We can also do an experiment to see if zookeeper clusters work properly.

Connect to one of the servers and create a znode.

➜ zookeeper-3.4.12 bin/zkCli.sh -server ip1:2181[zk: ip1:2181(CONNECTED) 10] create /zk_test my_data

2. Connect another server and list all znodes under the root node.

➜ zookeeper-3.4.12 bin/zkCli.sh -server ip2:2181[zk: ip2:2181(CONNECTED) 10] ls /

The output is as follows:

[zk_test]

You can see that the cluster is working normally, and data replication between servers can be carried out normally.

The client connects to the cluster as follows:

➜ zookeeper-3.4.12 bin/zkCli -server ip1:2181,ip2:2181,ip3:2181

The client automatically selects a connection at random.

Thank you for reading, the above is "ZooKeeper in a single and cluster environment how to install and use" content, after the study of this article, I believe that everyone on ZooKeeper in a single and cluster environment how to install and use this problem has a deeper understanding, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report