In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to get started with Zookeeper? I believe many inexperienced people don't know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
What is Zookeeper?
According to the official document, zookeeper is a distributed service framework and a sub-project of Apache Hadoop. It 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 above explanation is a bit abstract, simply speaking, the zookeeper= file system + snooping notification mechanism.
1. File system
Zookeeper maintains a file system-like data structure:
Every subdirectory item such as NameService is called znode (directory node). Like the file system, we are free to add and delete znode, and add and delete sub-znode under a znode. The only difference is that znode can store data.
There are four types of znode:
Use client commands to operate zookeeper
1. Use the ls command to view the content contained in the current ZooKeeper
2. Create a new znode, using create / zkPro myData
3. Use the ls command again to view what is now contained in zookeeper:
4. Let's run the get command to confirm whether the znode created in step 2 contains the string we created:
5. Let's set the string associated with zk through the set command:
6. Let's delete the znode we just created
Use Java API to operate zookeeper
To manipulate zookeeper with Java API, you need to reference the following package
Let's implement the distributed configuration center mentioned above:
1. Add a directory node to zookeeper and store the configuration information in it
2. Start two zookeeper client programs with the following code
Import java.util.concurrent.CountDownLatch
Import org.apache.zookeeper.WatchedEvent
Import org.apache.zookeeper.Watcher
Import org.apache.zookeeper.Watcher.Event.EventType
Import org.apache.zookeeper.Watcher.Event.KeeperState
Import org.apache.zookeeper.ZooKeeper
Import org.apache.zookeeper.data.Stat
Public class ZooKeeperProSync implements Watcher {
Private static CountDownLatch connectedSemaphore = new CountDownLatch (1)
Private static ZooKeeper zk = null
Private static Stat stat = new Stat ()
Public static void main (String [] args) throws Exception {
/ / zookeeper configuration data storage path
String path = "/ username"
/ / Connect to zookeeper and register a default listener
Zk = new ZooKeeper ("192.168.31.100 purl 2181", 5000, / /
New ZooKeeperProSync ()
/ / wait for notification of successful zk connection
ConnectedSemaphore.await ()
/ / get the configuration data of the path directory node and register the default listener
System.out.println (new String (zk.getData (path, true, stat)
Thread.sleep (Integer.MAX_VALUE)
}
Public void process (WatchedEvent event) {
If (KeeperState.SyncConnected = = event.getState ()) {/ / zk connection successful notification event
If (EventType.None = = event.getType () & & null = = event.getPath ()) {
ConnectedSemaphore.countDown ()
} else if (event.getType () = = EventType.NodeDataChanged) {/ / zk directory node data change notification event
Try {
System.out.println ("configuration has been modified, the new value is:" + new String (zk.getData (event.getPath (), true, stat)
} catch (Exception e) {
}
}
}
}
}
After starting, both programs correctly read the data 'qingfeng'' under the / username directory node of zookeeper
3. We modify the data under the directory node / username in zookeeper
After the modification is completed, we see that both programs receive the changed values of the directory node data they are listening to in time, as shown below
Zookeeper cluster mode installation
In this example, the pseudo-cluster mode is built, that is, three zookeeper instances are launched on one machine to form a cluster. The real cluster mode is that the instance IP address is different, and there is no difference in the construction method.
Step1: configure JAVA environment, verify environment: java-version
Step2: download and extract zookeeper
# cd / usr/local# wget http://mirror.bit.edu.cn/apache/zookeeper/stable/zookeeper-3.4.12.tar.gz# tar-zxvf zookeeper-3.4.12.tar.gz# cd zookeeper-3.4.12
Step3: renaming zoo_sample.cfg fil
# cp conf/zoo_sample.cfg conf/zoo-1.cfg
Step4: modify the configuration file zoo-1.cfg, which is found in the original configuration file, and change it to the following value. If not, add
# vim conf/zoo-1.cfgdataDir=/tmp/zookeeper-1clientPort=2181server.1=127.0.0.1:2888:3888server.2=127.0.0.1:2889:3889server.3=127.0.0.1:2890:3890
Configuration description
TickTime: this time is used as the interval between Zookeeper servers or between clients and servers to maintain a heartbeat, that is, a heartbeat is sent at each tickTime time.
InitLimit: this configuration item is used to configure the Zookeeper accept client (the client here is not the client that the user connects to the Zookeeper server, but the Follower server in the Zookeeper server cluster that connects to the Leader) the maximum number of heartbeat intervals that can be tolerated when initializing the connection. When the Zookeeper server has not received a return message from the client after the length of more than 10 heartbeats (that is, tickTime), the client connection failed. The total length of time is 10 "2000" 20 seconds.
SyncLimit: this configuration item identifies the length of time for sending messages, requests and replies between Leader and Follower, which cannot exceed the maximum number of tickTime. The total length of time is 5, 000, 000, 10 seconds.
DataDir: as the name implies, it is the directory where Zookeeper saves the data, and by default, Zookeeper saves log files for writing data to this directory as well.
ClientPort: this port is the port on which the client connects to the Zookeeper server. Zookeeper listens on this port and accepts requests for access from the client.
Server.A=B:C:D: where An is a number indicating which server this is, B is the ip address of this server, and C represents the port on which the server exchanges information with the Leader servers in the cluster D means that in case the Leader server in the cluster dies, a port is needed for re-election to select a new Leader, and this port is the port through which the servers communicate with each other during the election. If it is a pseudo-cluster configuration, because B is all the same, the communication port numbers of different Zookeeper instances cannot be the same, so they should be assigned different port numbers.
Step4: copy two configuration files, zoo-2.cfg and zoo-3.cfg, from zoo-1.cfg, simply modify the difference between dataDir and clientPort
# cp conf/zoo-1.cfg conf/zoo-2.cfg# cp conf/zoo-1.cfg conf/zoo-3.cfg# vim conf/zoo-2.cfgdataDir=/tmp/zookeeper-2clientPort=2182# vim conf/zoo-2.cfgdataDir=/tmp/zookeeper-3clientPort=2183
Step5: identifies Server ID
Create three folders / tmp/zookeeper-1,/tmp/zookeeper-2,/tmp/zookeeper-2, create a file myid file in each directory, and write to the server id of the current instance, that is, 1.2.3
# cd / tmp/zookeeper-1# vim myid1# cd / tmp/zookeeper-2# vim myid2# cd / tmp/zookeeper-3# vim myid3
Step6: start three zookeeper instances
# bin/zkServer.sh start conf/zoo-1.cfg# bin/zkServer.sh start conf/zoo-2.cfg# bin/zkServer.sh start conf/zoo-3.cfg
Step7: check the status of the cluster, or you can directly use the command "zkCli.sh-server IP:PORT" to connect to the zookeeper server to detect
After reading the above, have you mastered how to get started with Zookeeper? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.