Differences between revisions 1 and 2
Revision 1 as of 2015-09-05 17:42:14
Size: 3558
Editor: twotwo
Comment: ZooKeeper知识整理
Revision 2 as of 2019-05-15 10:32:10
Size: 4150
Editor: twotwo
Comment: 增加基于 docker 的操作,针对最新稳定版 v3.4.14
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
||Back to [[Java]] ||See Also [[dubbo|Dubbo]]、[[]] || ||Back to [[Microservices|微服务]] ||See Also [[dubbo|Dubbo]]、[[Spring_Cloud|Spring Cloud]]、[[Docker]] ||
Line 75: Line 75:
== Run in Docker ==
[[https://hub.docker.com/_/zookeeper||target="_blank"]] [[attachment:Dockerfile.3.4.14]]

{{{#!highlight bash numbers=disable
➜ ~ docker pull zookeeper:3.4.14 # Stable Version
## Start one Zookeeper server instance
➜ ~ docker run --name zookeeper -p 2181:2181 --restart always -d zookeeper:3.4.14 #expose client port
## Connect to Zookeeper from the Zookeeper command line client
➜ ~ docker run -it --rm --link zookeeper:zookeeper-client zookeeper:3.4.14 zkCli.sh -server zookeeper-client
[zk: zookeeper-client(CONNECTED) 0] quit
Quitting...
}}}
Line 96: Line 109:
 * [[http://zookeeper.apache.org/||target="_blank"]]
* [[http://zookeeper.apache.org/doc/trunk/||target="_blank"]]
 * [[http://zookeeper.apache.org/doc/current/||target="_blank"]]
Line 101: Line 113:

Back to 微服务

See Also DubboSpring CloudDocker

ZooKeeper

Apache ZooKeeper是Apache软件基金会的一个软件项目,他为大型分布式计算提供开源的分布式配置服务、同步服务和命名注册。ZooKeeper曾经是Hadoop的一个子项目,但现在是一个独立的顶级项目。

ZooKeeper的架构通过冗余服务实现高可用性。因此,如果第一次无应答,客户端就可以询问另一台ZooKeeper主机。ZooKeeper节点将它们的数据存储于一个分层的命名空间,非常类似于一个文件系统或一个前缀树结构。客户端可以在节点读写,从而以这种方式拥有一个共享的配置服务。更新是全序的。

1. ZooKeeper Administrator's Guide

Start by installing ZooKeeper on a single machine: http://zookeeper.apache.org/doc/trunk/zookeeperStarted.html

➜  ZooKeeper  wget http://www.eu.apache.org/dist/zookeeper/stable/zookeeper-3.4.6.tar.gz
➜  ZooKeeper  tar xzf zookeeper-3.4.6.tar.gz
➜  ZooKeeper  cd zookeeper-3.4.6
➜  zookeeper-3.4.6  cp conf/zoo_sample.cfg conf/zoo.cfg
➜  zookeeper-3.4.6  bin/zkServer.sh start
JMX enabled by default
Using config: /Users/liyan/app/java/ZooKeeper/zookeeper-3.4.6/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
## Connecting to ZooKeeper
➜  zookeeper-3.4.6  bin/zkCli.sh -server 127.0.0.1:2181

1.1. zkCli.sh

$ zkCli.sh
[zk: localhost:2181(CONNECTED) 1] ls /   
[zookeeper]
[zk: localhost:2181(CONNECTED) 2] get /zookeeper

cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x0
cversion = -1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1
[zk: localhost:2181(CONNECTED) 3] create /test aa
Created /test
[zk: localhost:2181(CONNECTED) 4] get /test     
aa
cZxid = 0x87
ctime = Wed Jan 27 19:03:48 CST 2016
mZxid = 0x87
mtime = Wed Jan 27 19:03:48 CST 2016
pZxid = 0x87
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 2
numChildren = 0

1.2. ZooKeeper JMX

http://zookeeper.apache.org/doc/current/zookeeperJMX.html

1.3. Clustered (Multi-Server) Setup

tickTime=2000
dataDir=/var/lib/zookeeper/
clientPort=2181
initLimit=5
syncLimit=2
server.1=zoo1:2888:3888
server.2=zoo2:2888:3888
server.3=zoo3:2888:3888

2. Run in Docker

https://hub.docker.com/_/zookeeper Dockerfile.3.4.14

➜  ~ docker pull zookeeper:3.4.14 # Stable Version
## Start one Zookeeper server instance
➜  ~ docker run --name zookeeper -p 2181:2181 --restart always -d zookeeper:3.4.14 #expose client port
## Connect to Zookeeper from the Zookeeper command line client
➜  ~ docker run -it --rm --link zookeeper:zookeeper-client zookeeper:3.4.14 zkCli.sh -server zookeeper-client
[zk: zookeeper-client(CONNECTED) 0] quit
Quitting...

3. Java Samples

http://zookeeper.apache.org/doc/trunk/javaExample.html

https://zookeeper.apache.org/doc/r3.4.6/api/org/apache/zookeeper/ZooKeeper.html

zookeeper

import org.apache.zookeeper.ZooKeeper;
public class TestZK {
   public static void main(String[] args) {
      //Connecting to server on localhost
      ZooKeeper zk = new ZooKeeper("localhost", 10000, null);
      String path = "zookeeper";
      System.out.println("Connection to server sucessfully");
      //check whether path exists or not
      System.out.println(zk.exists(path,false));
 }
}

4. Reference


CategoryDev

MainWiki: ZooKeeper (last edited 2019-05-15 10:32:10 by twotwo)