Give me a bunch of learning documents first , Easy to check later
Official website document address encyclopedia :
OverView( summary )
http://zookeeper.apache.org/doc/r3.4.6/zookeeperOver.html
Getting Started( Get started )
http://zookeeper.apache.org/doc/r3.4.6/zookeeperStarted.html
Tutorial( course )
http://zookeeper.apache.org/doc/r3.4.6/zookeeperTutorial.html
Java Example(Java Example )
http://zookeeper.apache.org/doc/r3.4.6/javaExample.html
Programmer's Guide( Developer's Guide )
http://zookeeper.apache.org/doc/r3.4.6/zookeeperProgrammers.html
Recipes and Solutions( Skills and solutions )
http://zookeeper.apache.org/doc/r3.4.6/recipes.html
3.4.6 API online( On-line API Quick check )
http://zookeeper.apache.org/doc/r3.4.6/api/index.html
In addition, recommend Garden Friends sunddenly Of zookeeper series
http://www.cnblogs.com/sunddenly/category/620563.html
One 、 Installation and deployment
This paper simulates on a machine 3 individual zk server Cluster installation of
1.1 Download decompression
Unzip to 3 A catalog ( simulation 3 platform zk server):
/home/hadoop/zookeeper-1 /home/hadoop/zookeeper-2 /home/hadoop/zookeeper-3
1.2 Create each directory conf/zoo.cfg The configuration file
/home/hadoop/zookeeper-1/conf/zoo.cfg The contents are as follows :
tickTime=2000 initLimit=10 syncLimit=5 dataDir=/home/hadoop/tmp/zk1/data dataLogDir=/home/hadoop/tmp/zk1/log clientPort=2181 server.1=localhost:2287:3387 server.2=localhost:2288:3388 server.3=localhost:2289:3389
/home/hadoop/zookeeper-2/conf/zoo.cfg The contents are as follows :
tickTime=2000 initLimit=10 syncLimit=5 dataDir=/home/hadoop/tmp/zk2/data dataLogDir=/home/hadoop/tmp/zk2/log clientPort=2182 server.1=localhost:2287:3387 server.2=localhost:2288:3388 server.3=localhost:2289:3389
/home/hadoop/zookeeper-3/conf/zoo.cfg The contents are as follows :
tickTime=2000 initLimit=10 syncLimit=5 dataDir=/home/hadoop/tmp/zk3/data dataLogDir=/home/hadoop/tmp/zk3/log clientPort=2183 server.1=localhost:2287:3387 server.2=localhost:2288:3388 server.3=localhost:2289:3389
notes : Because the cluster is simulated on one machine , So the port cannot be repeated , Here we use 2181~2183,2287~2289, as well as 3387~3389 Stagger each other . And each of them zk Of instance, You need to set up an independent data storage directory 、 Log storage directory , therefore dataDir、dataLogDir The directory corresponding to these two nodes , You need to manually create .
There is also a key setting , At every zk server Profile's dataDir Corresponding directory , You must create a file named myid The file of , The contents must be consistent with zoo.cfg in server.x Medium x identical , namely :
/home/hadoop/tmp/zk1/data/myid is 1, Corresponding server.1 Medium 1
/home/hadoop/tmp/zk2/data/myid is 2, Corresponding server.2 Medium 2
/home/hadoop/tmp/zk3/data/myid is 3, Corresponding server.3 Medium 3
In production environment , The steps of distributed cluster deployment are basically the same as above , Just because each zk server Distributed in different machines , In the above configuration file localhost Replace with the real of each server Ip that will do . Distributed behind different machines , There are no port conflicts , You can make each server zk All use the same port , It's easier to manage in this way .
1.3 Start validation
/home/hadoop/zookeeper-1/bin/zkServer.sh start /home/hadoop/zookeeper-2/bin/zkServer.sh start /home/hadoop/zookeeper-3/bin/zkServer.sh start
When enabled successfully , Input jps Look at the process
20351 ZooKeeperMain
20791 QuorumPeerMain
20822 QuorumPeerMain
20865 QuorumPeerMain
You should be able to see at least the above processes .
You can start the client test :
bin/zkCli.sh -server localhost:2181
( notes : If it's a remote connection , hold localhost Replace with the specified IP that will do )
After success , It should go to the prompt , Similar to the following :
[zk: localhost:2181(CONNECTED) 0]
then , You can use some basic commands , such as ls ,create ,delete ,get To test ( About these orders , You can view the document ), In particular, a very useful command rmr Used to recursively delete a node and all its children
Two 、java And zk Connection example for
2.1 maven Project pom.xml Add the following dependencies first
<!--zk--> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency>
2.2 The most basic example program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package
yjmyzz;
import
java.io.IOException;
import
org.apache.zookeeper.*;
import
org.apache.zookeeper.data.Stat;
public
class
ZooKeeperHello {
public
static
void
main(String[] args)
throws
IOException, InterruptedException, KeeperException {
ZooKeeper zk =
new
ZooKeeper(
"172.28.20.102:2181"
,
300000
,
new
DemoWatcher());
// Connect zk server
String node =
"/app1"
;
Stat stat = zk.exists(node,
false
);
// testing /app1 Whether there is
if
(stat ==
null
) {
// Create nodes
String createResult = zk.create(node,
"test"
.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(createResult);
}
// Gets the value of the node
byte
[] b = zk.getData(node,
false
, stat);
System.out.println(
new
String(b));
zk.close();
}
static
class
DemoWatcher
implements
Watcher {
@Override
public
void
process(WatchedEvent event) {
System.out.println(
"----------->"
);
System.out.println(
"path:"
+ event.getPath());
System.out.println(
"type:"
+ event.getType());
System.out.println(
"stat:"
+ event.getState());
System.out.println(
"<-----------"
);
}
}
}
|
2.3 And zk Cluster connectivity
zk One of the advantages of , It's high availability , The above code connects a single zk server, If this one server Hang up , Natural code will go wrong , in fact zk Of API With that in mind , Change the connection code to the following :
ZooKeeper zk = new ZooKeeper("172.28.20.102:2181,172.28.20.102:2182,172.28.20.102:2183", 300000, new DemoWatcher());// Connect zk server
namely :IP1:port1,IP2:port2,IP3:port3... Just connect to the cluster in this way , As long as more than half zk server Still alive , Application is generally no problem . But there is also an extremely rare case , For example, when this line of code executes , Just finished initializing , Preparing to connect ip1 when , Because of network failure ip1 Corresponding server Hang up , Still report a mistake ( here ,zk It's too late to choose a new leader), This problem is detailed in :http://segmentfault.com/q/1010000002506725/a-1020000002507402, Refer to the practice of this article , Change to :
1 ZooKeeper zk = new ZooKeeper("172.28.20.102:2181,172.28.20.102:2182,172.28.20.102:2183", 300000, new DemoWatcher());// Connect zk server 2 if (!zk.getState().equals(ZooKeeper.States.CONNECTED)) { 3 while (true) { 4 if (zk.getState().equals(ZooKeeper.States.CONNECTED)) { 5 break; 6 } 7 try { 8 TimeUnit.SECONDS.sleep(5); 9 } catch (InterruptedException e) { 10 e.printStackTrace(); 11 } 12 } 13 }
But this code is too lengthy , Open source is recommended zkClient, Official address : https://github.com/sgroschupf/zkclient, The method is simple to use :
<!--zkclient--> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency>
pom.xml Add this lump first , Then use :