官网html
Zookeeper是一个分布式协调服务,为分布式应用提供配置维护,命名,分布式同步,组服务等服务。
ZooKeeper的目标就是封装好复杂易出错的关键服务,将简单易用的接口和性能高效、功能稳定的系统提供给用户。node
overview
ACL: access control list
Design goal:
simple, replicated, ordered, fast
hierarchical namespace:
much like a standard file system
Nodes and ephemeral nodes
Conditional updates and watches
Guarantees
Sequential Consistency, Atomicity, Single System Image, Reliability, Timeliness
Simple API
create, delete, exists, get data, set data, get children, sync数据库
环境:CentOS 6.7
必须的软件:jdk 1.7+,JDK安装过程
zk下载地址(国内推荐):查看apache
# 上传至服务器/usr/local/src中 $ cd /usr/local/src # 解压缩 $ tar -zxvf zookeeper-3.4.10.tar.gz # 建立zookeeper目录 $ mkdir ../zookeeper # 将解压后的zookeeper移动到新建的目录中 $ mv zookeeper-3.4.10/ ../zookeeper/ # 转到zookeeper的配置目录下 $ cd ../zookeeper/zookeeper-3.4.10/conf # 建立一个配置文件(能够直接复制zoo_sample.cfg) $ cp zoo_sample.cfg zoo.cfg # tickTime: zk使用的毫秒级别的基本时间单元。用来作心跳,最小的session超时时间是它的两倍。 # dataDir: 数据目录,用来存储内存中的数据库快照,若是没有特别指定,也用来存储数据库更新事务日志。 # clientPort: 监听客户端链接的端口。 # 启动zk $ ../bin/zkServer.sh start # 不出意外,能够看见启动成功~
至少要三个服务器,强烈建议使用奇数个服务器。2个服务器还不如一个,没有一个稳定,由于有两个单点故障。vim
# both standlone and replicated tickTime=2000 dataDir=/var/lib/zookeeper clientPort=2181 # only replicated initLimit=5 syncLimit=2 server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
initLimit: 在这个例子中是5 * tickTime,也就是10s,The number of ticks that the initial synchronization phase can take syncLimit: 在这个例子中是2 * tickTime,也就是4s,The number of ticks that can pass between sending a request and getting an acknowledgement 2888: connect followers to the leader, peers can communicate with each other 3888: leader election的端口 生产环境中dataDir不要配置在tmp下,生产环境的各类配置都该注意 server.n 后面接hostname更容易记忆,n是服务器ID,在集合体中惟一,而且取值范围在1~255之间。
3台server分别使用2.1的方式安装zk(10.1.50.218, 10.1.50.220, 10.1.50.222),配置文件zoo.cfg为:bash
# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=/tmp/zookeeper # the port at which the clients will connect clientPort=2181 # replicated servers server.1=10.1.50.218:2888:10888 server.2=10.1.50.220:2888:10888 server.3=10.1.50.222:2888:10888
在zoo.cfg中配置的dataDir目录下建立myid,值为server.n中的n:服务器
# 以10.1.50.218为例 $ cd /tmp/zookeeper $ echo "1" > myid
打开防火墙session
$ vim /etc/sysconfig/iptables # 添加以下三行 -A INPUT -p tcp -m state --state NEW -m tcp --dport 2888 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 10888 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 2181 -j ACCEPT $ service iptables restart
依次启动三个zk,完成~~tcp
to be continued: 生产环境配置注意
--安装了三台虚拟机,一样的安装方式,其中两台都正常,还有一台刚装好ping www.baidu.com正常,
--运行yum -y install lrzsz出错后,ping www.baidu.com返回结果的时候变成ping 127.0.0.1
--ping ip正常,ping域名就不正常,yum也不能正常使用,重装四、5遍,问题依然,真是郁闷无比
--先记录在此,但愿以后能找到缘由分布式
对于客户端来讲,ZooKeeper是一个总体(ensemble),链接到ZooKeeper集群实际上感受在独享整个集群的服务,因此能够在任何一个结点上创建到服务集群的链接
# 转到zk目录下 $ cd /usr/local/zookeeper/zookeeper-3.4.10 # 链接 $ bin/zkCli.sh -server 127.0.0.1:2181 # 输入help能够看见一系列能够从客户端执行的操做命令 # 敲一些命令找找感受: $ ls / # [zookeeper] $ create /zk_test my_data # 建立一个叫zk_test的新znode,将"my_data"与它关联 # create /zk_test my_data $ ls / # [zookeeper, zk_test] $ get /zk_test # 查看和znode相关联的数据 $ set /zk_test junk # 改变和znode关联的数据,再次get /zk_test,能够确认数据确实被改了 $ delete /zk_test # 删除znode