标签:
当前云计算流行, 单一机器额的处理能力已经不能满足我们的需求,不得不采用大量的服务集群。服务集群对外提供服务的过程中,有很多的配置需要随时更新,服务间需要协调工作,那么这些信息如何推送到各个节点?并且保证信息的一致性和可靠性?我们知道分布式协调服务很难正确无误地实现, 因为他们很容易在竞争条件和死锁上犯错误。那么zookeeper将是一个不错的选择
引用官方的说法:“Zookeeper是一个高性能,分布式的,开源分布式应用协调服务。它提供了简单原始的功能,分布式应用可以基于它实现更高级 的服务,比如同步,配置管理,集群管理,名空间。它被设计为易于编程,使用文件系统目录树作为数据模型
dsd
Zookeeper服务自身组成一个集群(2n+1个服务允许n个失效)。Zookeeper服务有两个角色,一个是leader,负责写服务和数据同步,剩下的是follower,提供读服务,leader失效后会在follower中重新选举新的leader。
Zookeeper逻辑图如下,
安装和配置:
下载zookeeper的安装包解压以后,进入conf子目录,创建zoo.cfg(这个若是参数不清楚可以参考conf目录下自带的zoo_sanple.cfg文件)
#tickTime:zookeeper中使用的基本时间单位,毫秒值 tickTime=2000 #数据目录 dataDir=/home/balfish/zookeeper #监听client连接的端口 clientPort=2181
参数说明:
至此,zookeeper的单机模式已经配置好了,启动server需要运行如下:
xxx/bin/zkServer.sh start
不加start会报错提示你可以加的命令
Usage: ./zkServer.sh {start|start-foreground|stop|restart|status|upgrade|print-cmd}
比如status查看状态等等
成功了的话大致打印如下的信息:
balfish@balfish-Latitude-E5440:~/下载/zookeeper-3.4.5/bin$ ./zkServer.sh start JMX enabled by default Using config: /home/balfish/下载/zookeeper-3.4.5/bin/../conf/zoo.cfg Starting zookeeper ... already running as process 6083.
也可以通过netstat -anp | grep 2181来看一看tcp状态是 ESTABLISHED 还是 TIME——WAIT 神马的
之后便可以启动client连接server进行测试了
bin/zkCli.sh
打印下面这一大堆就成功了
Connecting to localhost:2181 2015-10-09 17:11:56,858 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.5-1392090, built on 09/30/2012 17:52 GMT 2015-10-09 17:11:56,862 [myid:] - INFO [main:Environment@100] - Client environment:host.name=balfish-Latitude-E5440 2015-10-09 17:11:56,862 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.7.0_40 2015-10-09 17:11:56,863 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation 2015-10-09 17:11:56,863 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/home/balfish/Download/jdk1.7.0_40/jre 2015-10-09 17:11:56,864 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/home/balfish/下载/zookeeper-3.4.5/bin/../build/classes:/home/balfish/下载/zookeeper-3.4.5/bin/../build/lib/*.jar:/home/balfish/下载/zookeeper-3.4.5/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/balfish/下载/zookeeper-3.4.5/bin/../lib/slf4j-api-1.6.1.jar:/home/balfish/下载/zookeeper-3.4.5/bin/../lib/netty-3.2.2.Final.jar:/home/balfish/下载/zookeeper-3.4.5/bin/../lib/log4j-1.2.15.jar:/home/balfish/下载/zookeeper-3.4.5/bin/../lib/jline-0.9.94.jar:/home/balfish/下载/zookeeper-3.4.5/bin/../zookeeper-3.4.5.jar:/home/balfish/下载/zookeeper-3.4.5/bin/../src/java/lib/*.jar:/home/balfish/下载/zookeeper-3.4.5/bin/../conf: 2015-10-09 17:11:56,864 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib 2015-10-09 17:11:56,865 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp 2015-10-09 17:11:56,865 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA> 2015-10-09 17:11:56,866 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux 2015-10-09 17:11:56,867 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64 2015-10-09 17:11:56,867 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.13.0-65-generic 2015-10-09 17:11:56,867 [myid:] - INFO [main:Environment@100] - Client environment:user.name=balfish 2015-10-09 17:11:56,868 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/home/balfish 2015-10-09 17:11:56,868 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/home/balfish/下载/zookeeper-3.4.5/bin 2015-10-09 17:11:56,870 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@7b238fa5 Welcome to ZooKeeper! 2015-10-09 17:11:56,892 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@966] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) 2015-10-09 17:11:56,897 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@849] - Socket connection established to localhost/127.0.0.1:2181, initiating session JLine support is enabled 2015-10-09 17:11:56,914 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1207] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x1504bdec2a90000, negotiated timeout = 30000 WATCHER:: WatchedEvent state:SyncConnected type:None path:null
所谓伪集群, 是指在单台机器中启动多个zookeeper进程, 并组成一个集群. 以启动3个zookeeper进程为例.
将zookeeper的目录再拷贝2份
|--zookeeper0
|--zookeeper1
|--zookeeper2
更改zookeeper0/conf/zoo.cfg文件为:
#tickTime:zookeeper中使用的基本时间单位,毫秒值 tickTime=2000 initLimit=5 syncLimit=2 #数据目录 dataDir=/home/balfish/zookeeper #监听client连接的端口 clientPort=4180 server.0=127.0.0.1:8880:7770 server.1=127.0.0.1:8881:7771 server.2=127.0.0.1:8882:7772
新增了几个参数, 其含义如下:
参照zookeeper0/conf/zoo.cfg, 配置zookeeper1/conf/zoo.cfg, 和zookeeper2/conf/zoo.cfg文件. 只需更改dataDir, clientPort参数即可.
在之前设置的dataDir中新建myid文件, 写入一个数字, 该数字表示这是第几号server. 该数字必须和zoo.cfg文件中的server.X中的X一一对应.
/home/balfish/zookeeper0/myid文件中写入0,/home/balfish/zookeeper1/myid文件中写入1, /home/balfish/zookeeper2/myid文件中写入2.
分别进入/Users/apple/zookeeper0/bin, /Users/apple/zookeeper1/bin, /Users/apple/zookeeper2/bin三个目录, 启动server.
任意选择一个server目录, 启动客户端:
bin/zkCli.sh -server localhost:4180
集群模式的配置和伪集群基本一致.
由于集群模式下, 各server部署在不同的机器上, 因此各server的conf/zoo.cfg文件可以完全一样.
下面是一个示例:
#tickTime:zookeeper中使用的基本时间单位,毫秒值 tickTime=2000 initLimit=5 syncLimit=2 #数据目录 dataDir=/home/zookeeper #监听client连接的端口 clientPort=4180 server.43=10.1.39.43:2888:3888 server.47=10.1.39.47:2888:3888 server.48=10.1.39.48:2888:3888
示例中部署了3台zookeeper server, 分别部署在10.1.39.43, 10.1.39.47, 10.1.39.48上. 需要注意的是, 各server的dataDir目录下的myid文件中的数字必须不同.10.1.39.43 server的myid为43, 10.1.39.47 server的myid为47, 10.1.39.48 server的myid为48.
标签:
原文地址:http://www.cnblogs.com/balfish/p/4864588.html