Zookeeper notes (1) installation, deployment and hello world

mb60f93b6c4c5e3 2021-07-29 20:13:46
zookeeper notes installation deployment hello


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 :

 Copy code
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
 Copy code

/home/hadoop/zookeeper-2/conf/zoo.cfg The contents are as follows :

 Copy code
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
 Copy code

/home/hadoop/zookeeper-3/conf/zoo.cfg The contents are as follows :

 Copy code
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
 Copy code

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

 Copy code
 <!--zk-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
 Copy code

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 :

 Copy code
 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 }
 Copy code

  But this code is too lengthy , Open source is recommended zkClient, Official address : https://github.com/sgroschupf/zkclient, The method is simple to use :

 Copy code
 <!--zkclient-->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
 Copy code

pom.xml Add this lump first , Then use :

 Copy code
1 @Test
2 public void testZkClient() {
3 ZkClient zkClient = new ZkClient("172.28.20.102:2181,172.28.20.102:2182,172.28.20.102:2183");
4 String node = "/app2";
5 if (!zkClient.exists(node)) {
6 zkClient.createPersistent(node, "hello zk");
7 }
8 System.out.println(zkClient.readData(node));
9 }
 Copy code
版权声明
本文为[mb60f93b6c4c5e3]所创,转载请带上原文链接,感谢
https://javamana.com/2021/07/20210726142245889g.html

  1. redis 监控的一些构思
  2. Some ideas of redis monitoring
  3. Spring中获取request的几种方法,及其线程安全性分析
  4. Several methods of getting request in spring and its thread security analysis
  5. 20分钟带你掌握JavaScript Promise和 Async/Await
  6. 20 minutes with async and async
  7. 20 minutes to master JavaScript promise and async / await
  8. Mybatis - introduction, simple entry program
  9. Java Concurrent Programming: concurrent HashMap
  10. Mybatis: one to one association query
  11. Java IO
  12. Object oriented [day07]: review of knowledge points (11)
  13. Chapter 2 spring
  14. Process of building minicube in local k8s environment
  15. Interpretation of jQuery source code
  16. Making web full screen effect with jquery
  17. Java 7 exception handling new features - address suppressed () method
  18. Spring 4 uses websocket
  19. Using openldap to answer questions and using java to complete LDAP authentication
  20. Implementation of MySQL high availability cluster with corosync + pacemaker and DRBD
  21. Java production of a simple tank war
  22. Design pattern day02
  23. Hadoop pseudo Distributed installation and running test example
  24. Java Concurrent Programming: callable, future and futuretask
  25. Understanding the three characteristics of Java encapsulation
  26. Understanding closure of JavaScript
  27. Java set excel worksheet to read only
  28. Distributed Java service platform baratine
  29. Java barcode generation technology barcode4j
  30. Use springboot + MySQL + JPA to add, delete, change, query and page the database
  31. Distributed services framework Dubbo / dubbox getting started example
  32. Oracle database trigger Usage Summary
  33. MySQL master replication with slave server