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 and configure Zookeeper

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces how to install Zookeeper configuration, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

I. brief introduction

Zookeeper is a distributed service framework, which is mainly used to solve some data management problems often encountered in distributed applications, such as unified naming service, state synchronization service, cluster management, distributed application configuration item management and so on.

The goal of ZooKeeper is to encapsulate complex and error-prone key services and provide users with simple and easy-to-use interfaces and systems with efficient performance and stable functions.

ZooKeeper contains a simple set of primitives, and [1] provides an interface between Java and C.

The ZooKeeper code version provides interfaces for distributed exclusive locks, elections, and queues in zookeeper-3.4.3\ src\ recipes. There are two versions of distribution lock and queue, Java and C, and only Java version is elected.

Second, working principle

The core of Zookeeper is broadcasting, and this mechanism ensures synchronization between Server. The protocol that implements this mechanism is called the Zab protocol. There are two modes of Zab protocol, which are recovery mode (primary mode) and broadcast mode (synchronization). When the service starts or after the leader crashes, the Zab enters the recovery mode, and when the leader is elected and most of the Server finishes synchronizing with the leader, the recovery mode ends. State synchronization ensures that leader and Server have the same system state. In order to ensure the order consistency of transactions, zookeeper uses an incremental transaction id number (zxid) to identify transactions. All proposals (proposal) are made with zxid when they are made. In the implementation, zxid is a 64-bit number, and its high 32-bit epoch is used to identify whether the leader relationship has changed. Each time a leader is selected, it will have a new epoch that identifies the current period of leader rule. The lower 32 bits are used to increment the count.

Each Server has three states during its operation:

LOOKING: currently Server doesn't know who leader is and is searching for it.

LEADING: the current Server is the elected leader.

FOLLOWING:leader has been elected and the current Server is synchronized with it.

III. Basic configuration

The installation of zookeeper is the same as before, and there are three ways to install it. It is divided into stand-alone, pseudo-cluster and cluster mode.

1. First, go to apache's official website to download zookeeper. The latest version is 3.4.8, http://www.apache.org/dyn/closer.cgi/zookeeper/.

2. Unzip it to the directory you need to install, for example, I put it under / home/admin1/ download / zookeeper-3.4.8.

3. Configure in the conf directory:

Zoo.cfg

(1) if you are in stand-alone mode, you need to do the following configuration.

TickTime=2000dataDir=/home/admin1/ download / zookeeper-3.4.8/datadataLogDir=/home/admin1/ download / zookeeper-3.4.8/logsclientPort=2181

(2) if you are in pseudo-distribution mode, you need to do the following configuration: zoo.cfg

Pseudo-cluster means to start multiple zookeeper processes in a single machine and form a cluster. Take starting 3 zookeeper processes as an example.

TickTime=2000initLimit=10syncLimit=5dataDir=/home/admin1/ download / zookeeper-3.4.8/tmp/zookeeperclientPort=2181server.1=localhost:2287:3387server.2=localhost:2288:3388server.3=localhost:2289:3389

1 initLimit: the zookeeper cluster contains multiple server, one of which is leader, and the rest of the server in the cluster is follower. The initLimit parameter configures the maximum heartbeat time between follower and leader when initializing the connection. At this point, the parameter is set to 10, indicating that the time limit is 10 times tickTime, that is, 10*2000=20000ms=20s.2 syncLimit: this parameter configures the maximum length of time for sending messages, requests and replies between leader and follower. At this point, the parameter is set to 5, indicating that the time limit is 5 times tickTime, that is, 10000ms.3 server.X=A:B:C, where X is a number, indicating the number of server. An is the IP address where the server is located. B configure the port that the server uses to exchange messages with the leader in the cluster. C configure the port used to elect leader. Because the pseudo-cluster mode is configured, the B and C parameters of each server must be different. Refer to zookeeper0/conf/zoo.cfg, configure zookeeper1/conf/zoo.cfg, and zookeeper2/conf/zoo.cfg files. Just change the parameters dataDir, dataLogDir, and clientPort. Create a new myid file in the previously set dataDir and write a number indicating the number server. The number must correspond to the X in the server.X in the zoo.cfg file. / home/admin1/ download / zookeeper1/data/myid file write 1 to home/admin1/ / download / zookeeper2/data/myid file to write 2 to 2 in zookeeper3/data/myid file to write 3. Go to / home/admin1/ download / / zookeeper1/bin, / home/admin1/ download / / zookeeper2/bin, / home/admin1/ download / / zookeeper3/bin, respectively, and start server.

Bin/zkServer.sh start

The shutdown is followed by stop.

Use jps to view the process after a successful startup

It is right to have ZooKeeperMain (if there is no other configuration

In the case of a machine)

You can start the client test:

Bin/zkCli.sh-server localhost:2181

(note: if it is a remote connection, just change the localhost to the specified IP)

Then, you can test it with some basic commands, such as ls, create, delete, get (for these commands, you can check the documentation)

(3) the configuration of cluster mode is basically the same as that of pseudo-cluster.

Because in cluster mode, each server is deployed on different machines, the conf/zoo.cfg files of each server can be exactly the same.

IV. Development and testing

There are three ways to test the connection.

4.1 zkCli.sh

You can use the help group command to enter various operations.

4.2. Operate on eclipse:

To create a new java project, you need to import the jar package, zookeeper-3.4.8.jar, in zookeeper.

Create a new createSession.java

Public class CreateSession implements Watcher {private static ZooKeeper zookeeper; public static void main (String [] args) throws IOException, InterruptedException {zookeeper = new ZooKeeper ("localhost:2181", 5000 CreateSession ()); System.out.println (zookeeper.getState ()); Thread.sleep (Integer.MAX_VALUE) } private void doSomething () {System.out.println ("do something");} @ Override public void process (WatchedEvent event) {System.out.println ("received event:" + event) If (event.getState () = = KeeperState.SyncConnected) {if (event.getType () = = EventType.None & & null==event.getPath ()) {doSomething ();}

Another interface is required:

MyWatcher.java

Public class MyWatcher implements Watcher {@ Override public void process (WatchedEvent event) {/ / TODO Auto-generated method stub}}

The running result is as follows: it indicates that the connection is successful.

4.3Use ZKClient

Download the source code package in github, https://github.com/sgroschupf/zkclient

Just add library dependencies to the newly created eclipse project.

Test code

Import org.I0Itec.zkclient.ZkClient;import org.I0Itec.zkclient.serialize.SerializableSerializer;public class CreateSession {public static void main (String [] args) {ZkClient zc = new ZkClient ("localhost", 10000 System.out.println ("conneted ok!") }} Thank you for reading this article carefully. I hope the article "how to install and configure Zookeeper" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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

Servers

Wechat

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

12
Report