ZooKeeper cluster installation and configuration

http://blog.csdn.net/shirdrn/article/details/7183503/

https://www.ibm.com/developerworks/cn/data/library/bd -zookeeper/

https://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/

1. Download

Official website: http://zookeeper.apache.org/releases.html
Download: zookeeper-3.4.8.tar.gz

2. Installation

Because of the limited resources, I created 3 directories server1, server2, server3 on the same server to simulate a 3 server cluster.
cd server1
tar -zxvf zookeeper-3.4.8.tar.gz
mkdir data
mkdir dataLog
data is the data directory, and dataLog is the log directory. 3. Configuration

cd zookeeper-3.4.8/conf
Create a file zoo.cfg, the content is as follows

tickTime: The basic time unit used in zookeeper, millisecond value. initLimit: This configuration item is used to configure Zookeeper to accept clients (the client mentioned here is not the client connecting to the Zookeeper server, but the Follower server connected to the Leader in the Zookeeper server cluster) initialization The maximum number of tickTime intervals that can be tolerated when connecting. Here is set to 5 table names, the longest tolerance time is 5 * 2000 = 10 seconds. syncLimit: This configuration identifies the length of the request and response time for sending messages between Leader and Follower. The maximum length cannot exceed the length of tickTime. The total length of time is 2 * 2000 = 4 seconds. dataDir and dataLogDir will know what they are doing just by looking at the configuration, without explanation. clientPort: The port number that monitors the client connection. The client mentioned here is the code program that connects to Zookeeper. server.{myid}={ip}:{Port for the leader server to exchange information}:{When the leader server is down, the port for leader election} maxClientCnxns: For one The client's connection limit, the default is 60, which is sufficient for most of the time. However, in our actual use, we found that this number is often exceeded in the test environment. After investigation, it was found that some teams deployed dozens of applications to one machine to facilitate testing, so this number was exceeded.

Modifying zoo.cfg is very simple, and then you need to create a myid file: cd server1 echo 1> myid

Then copy server1 to server2 and server3, and modify the zoo.cfg configuration , Of course, also modify the content of myid to 2 and 3.


The contents of zoo.cfg of 3 servers are given below:







Java program demo

Create a Maven project
Open the pom.xml file to add zookeeper dependency


Create Demo.java, the code is as follows:


There is a special case of handling ideas:
There are three services: server1, server2, and server3. When the client connects to zk, there is no problem in the initialization process of pointing to server1. However, when the initialization is just completed and it is about to connect to server1, server1 hangs due to network and other reasons. .
However, for the client, it will use the configuration of server1 to request a connection. At this time, it will definitely report an exception that the connection has been rejected, which will cause it to start and exit.
So the elegant way to solve this problem is to "judge the connection status when connecting. If the connection is not successful, the program automatically uses other connections to request the connection", so as to avoid this rare abnormal problem.

The code is as follows:



The above code is based on the library API provided by zk for you to use. In order to make it easier to use, someone wrote the open source zkclient, we can use it directly to operate zk.
zkclient open source address: https://github.com/sgroschupf/zkclient
maven dependency configuration:


zkClient repackaged the one-time watcher of zk, and then defined three monitors: stateChanged, znodeChanged, and dataChanged.

  1. Monitor children changes
  2. Monitor node data changes
  3. Monitor connection status changes

The code is as follows:


ZkClient has made a convenient packaging and enhanced Watch.
subscribeChildChanges actually follows two events through exists and getChildren. In this way, when create("/path"), the listener registered by getChildren on the corresponding path will also be called. In addition, subscribeDataChanges is actually only registered for events through exists. Because it can be seen from the above table that for an update, the watchers registered through exists and getData will either trigger or neither will trigger.

As for the issue of session timeout, ZkClient still seems to deal with Session Expired, in the ZkClient.processStateChanged method. Although you can reconnect, but the connection is a new session, the originally created ephemeral znode and watch will be deleted. You may need to deal with this problem programmatically.


Finally, I will say a few notes about ZkClient:
1. When creating a node, you must first determine whether the node exists. If you directly use zkclient to create an existing node, an exception will be thrown.
2. When using zkclient to create a node, the path described by path and all parent nodes before the pre-added final node must exist, otherwise an exception will be thrown. So deal with it according to your needs.

tickTime=2000 initLimit=5syncLimit= 2dataDir=/opt/zookeeper/server1/datadataLogDir=/opt/zookeeper/server1/dataLog< span class="hljs-constant">clientPort=2181server.1=zk1server.emind.com:2888:3888server.2=zk2server.emind.com:2888:3888server.3=zk3server.emind.com:2888 :3888echo "1"> /var/lib/zookeeper/myidecho "2"> /var/lib/zookeeper/myidecho "3"> /var/lib/zookeeper/myid# server1 tickTime=2000initLimit=5syncLimit< /span>=2dataDir=/opt/zookeeper/server1/datadataLogDir=/opt/zookeeper/ server1/dataLogclientPort=2181server.1=127.0.0.1:2888:3888server.2=127.0.0.1:2889:3889server.3=127 .0.0.1:2890:3890# server2tickTime=2000initLimit=5syncLimit=2dataDir=/opt/zookeeper/server2/data< span class="hljs-constant">dataLogDir=/opt/zookeeper/server2/dataLogclientPort=2182server.1=127.0.0.1:2888:3888server .2=127.0.0.1:2889:3889server.3=127.0.0.1:2890:3890# server3tickTime=2000initLimit=5syncLimit=2dataDir span>=/opt/zookeeper/server3/datadataLogDir=/opt/zookeeper/server3/dataLogclientPort =2183server.1=127.0.0.1:2888:3888server.2=127.0.0.1:2889:3889server.3=127.0.0.1:2890:3890<dependenc y> <groupId>org.apache.zookeepergroupId> <artifactId >zookeeperartifactId> <version>3.4.6version< /span>> dependency> package com.shanhy.demo.zookeeper;import java.io.IOException;import org .apache.zookeeper.CreateMode;import org.apache.zookeeper.KeeperException;import org.apache.zookeeper .WatchedEvent;import org.apache.zookeeper.Watcher;import org.apache.zookeeper.ZooDefs.Ids;import org. apache.zookeeper.ZooKeeper;/** * Zookeeper test* * @create March 10, 2016*/public class Test { // Session timeout, set to be consistent with the system default time private static final int SESSION_TIMEOUT = 30 * 1000; // Create ZooKeeper instance span> private ZooKeeper zk; // Create Watcher instance private Watcher wh = new Watcher() {/** * Watched events*/ public void process(WatchedEvent event) {System.out.println("WatchedEvent >>> " + event.toString());} }; // Initialize ZooKeeper instance private void createZKInstance() throws IOException {// Connect to ZK service, multiple can be separated by commas to write zk = new ZooKeeper("192.168.19.130:2181,192.168.19.130:2182,192.168.19.130:2183", Test.SESSION_TIMEOUT, this.wh);} private void ZKOperations() throws IOException, InterruptedException, KeeperException {System.out.println("
1. Create ZooKeeper node (znode: zoo2, data: myData2, permission: OPEN_ACL_UNSAFE, node type: Persistent"
); zk.create("/zoo2", "myData2".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); System.out.println( "
2. Check whether the creation is successful: "
); System.out.println(new String(zk.getData("/zoo2", this.wh, null)));< span class="hljs-comment">// Add Watch
// In the previous line, we added monitoring of the /zoo2 node, so when we modify /zoo2 , Will trigger the Watch event. System.out.println("
3. Modify node data"
); zk.setData("/ zoo2", "shanhy20160310".getBytes(), -1); // Modify here again, the Watch event will not be triggered. This is a feature of our verification ZK "one-time trigger", that is to say, set up a monitoring, only once for the next operation effect. System.out.println("
3-1. Modify node data again"
); zk.setData("/zoo2", "shanhy20160310-ABCD".getBytes(), -1 ); System.out.println("
4. Check whether the modification is successful: "
); System.out.println( new String(zk.getData("/zoo2", false, null))); System.out.println("
5. Delete node"
); zk.delete("/zoo2", -1); System.out.println("
6. Check whether the node is deleted: "
); System.out.println(" Node status: [" + zk.exists(< span class="hljs-string">"/zoo2"
, false) + "]" span>); } private void ZKClose() throws InterruptedException {zk.close();} public static void main(String[] args) throws< /span> IOException, InterruptedException, KeeperException {Test dm = new Test(); dm.createZKInstance(); dm.ZKOperations(); dm.ZKClose();} }// Initialize ZooKeeper instance private void createZKInstance() throws IOException {// Connect to ZK service , Multiple can be separated and written by commas zk = new ZooKeeper("192.168.19.130:2181,192.168.19.130:2182,192.168.19.130:2183", Test.SESSION_TIMEOUT, this span>.wh); if(!zk.getState().equals(States.CONNECTED)){ while span>(true){ if(zk.getState().equals(States.CONNECTED)){ break;} try {TimeUnit.SECONDS.sleep(5 );} catch (InterruptedException e) {e.printStackTrace();}}} } <dependency> <groupId>com.101tecgroupId> <artifactId>zkclientartifactId> <version>0.7version> dependency span>>package com.shanhy.demo.zookeeper;import java.util.List ;import java.util.concurrent.TimeUnit;import org.I0Itec.zkclient.DataUpdater;import org.I0Itec.zkclient.IZkChildListener;import org.I0Itec.zkclient.IZkDataListener;import org.I0Itec.zkclient. IZkStateListener;import org.I0Itec.zkclient.ZkClient;import org.apache.zookeeper.Watcher.Event. KeeperState;/** * ZkClient use test* * @author Shan Hongyu (365384722) * @myblog http://blog.csdn.net/catoop/ * @create March 11, 2016*/public class ZkClientTest { public static void main(String[] args) {ZkClient zkClient = new ZkClient(< span class="hljs-string">"192.168.19.130:2181,192.168.19.130:2182,192.168.19.130:2183"); String node = "/myapp " ; // Subscribe to listener events childChangesListener(zkClient, node); dataChangesListener(zkClient, node); stateChangesListener(zkClient); if (!zkClient.exists(node)) {zkClient.createPersistent(node, "hello zookeeper");} System.out.println(zkClient.readData (node)); zkClient.updateDataSerialized(node, new DataUpdater() {public String < span class="hljs-title">update(String currentData) {return currentData + "-123" ;} }); System.out.println(zkClient.readData(node)); try {TimeUnit.SECONDS.sleep(3);} catch (Interrupted Exception e) {e.printStackTrace();}} /** * Subscribe to children changes* * @param zkClient *< span class="hljs-javadoctag"> @param path * @author SHANHY * @create March 11, 2016*/ public static void childChangesListener(ZkClient zkClient, final String path) {zkClient.subscribeChildChanges( path, new IZkChildListener() {public void handleChildChange(String parentPath, List currentChilds) throws Exception {System.out.println(< span class="hljs-string">"clil dren of path " + parentPath + ":" + currentChilds);} });} /** * Subscribe to node data changes* * @param zkClient * @param path * @author SHANHY * @create March 11, 2016*/ public static void dataChangesListener( ZkClient zkClient, final String path){ zkClient.subscribeDataChanges(path, new IZkDataListener(){ public void handleDataChange(String dataPath, Object data) < span class="hljs-keyword">throws Exception {System.out.printl n("Data of " + dataPath + " has changed.");} public void handleDataDeleted(String dataPath) throws Exception {System.out.println("Data of " + dataPath + " has changed.");} });} /** * Subscription status changes* * @param zkClient * @author SHANHY * @create March 11, 2016*/ public static void stateChangesListener(ZkClient zkClient){ zkClient.subscribeStateChanges(new IZkStateListener() {public void handleStateChanged( KeeperState state) throws Exception {System.out.println("handleStateChanged");} public void handleSessionEstablishmentError(Throwable error) throws Exception {System.out.println("handleSessionEstablishmentError");} public< /span> void handleNewSession() throws Exception {System.out.println("handleNewSession");} }); }}

Leave a Comment

Your email address will not be published.