分布式系统是同时跨越多个物理主机,独立运行的多个软件组件所组成的系统。采用分布式去设计系统的原因有很多,分布式系统能够利用多处理器的计算能力,来运行组件,比如并行复制任务。一个系统也许由于战略原因,需要分布在不同的地点,比如一个应用由多个不同地点的服务器提供服务。
设定s0为客户端,s1,s2,s3为zookeeper管理的集群
1.下载并解压zookeeper
2.配置环境变量
[etc/enviroment]
ZOOKEEPER_HOME=”/usr/soft/zookeeper-3.4.6”
PATH=”/usr/soft/zookeeper-3.4.6/bin”
/usr/soft/zookeeper-3.4.6/conf/zoo.cfg //可以复制zoo_sample.cfg
//心跳周期毫秒数,2x时间时session过期
tickTime=2000
//存放数据目录
dataDir=/tmp/zookeeper-3.3.1/snapshot/data
//供客户端连接的端口
clientPort=2181
1.配置zoo.cfg文件
/usr/soft/zookeeper-3.4.6/conf/zoo.cfg]
tickTime=2000
initLimit=5
syncLimit=2
//server.n=host:port1:port2,数字n必须是myid中的值
//port1:leader端口,作为leader时,供follower连接的端口
//port2:选举端口,选举leader时供其他follower连接的端口
server.1=s1:2888:3888
server.2=s2:2888:3888
server.3=s3:2888:3888
注:zk处理集群故障的算法是2n + 1,最好在奇数机器数部署。
2.标识自己-配置myid文件
文件位于dataDir指定目录下,只有一个数字n
//内容只有一行,数字,介于1~255
/默认配置启动
$>zkServer.sh start
//指定配置启动
>zkServer.shstartzoo1.cfg >zkServer.sh stop
>zkServer.shstatus >zkServer.sh restart
分别启动s1,s2,s3三台主机zookeeper服务后,查看三台主机当前状态
root@s1:/# zkServer.sh status
JMX enabled by default
Using config: /usr/soft/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: follower
root@s2:~# zkServer.sh status
JMX enabled by default
Using config: /usr/soft/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: leader
root@s3:/# zkServer.sh status
JMX enabled by default
Using config: /usr/soft/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: follower
发现此时s2为leader,s1,s3为follower
倘若此时s2宕机,则在s1与s3中会选取一个新领导
远程获得server的信息
$>echo conf | nc ip 2181
conf //配置信息
cons //连接信息
dump //未处理会话节点
envi //环境信息
reqs //未处理请求
ruok //are you ok? imok
stat //统计信息
wchs //服务器watch的详细信息
wchp //列出指定路径下的服务器信息
使用s0主机作为客户端查看s1主机信息,如下,使用nc指令,端口号为2181
root@s0:~# echo conf | nc s1 2181
clientPort=2181
dataDir=/tmp/zookeeper/version-2
dataLogDir=/tmp/zookeeper/version-2
tickTime=2000
maxClientCnxns=60
minSessionTimeout=4000
maxSessionTimeout=40000
serverId=1
initLimit=10
syncLimit=5
electionAlg=3
electionPort=3888
quorumPort=2888
peerType=0
root@s0:~# echo stat | nc s1 2181
Zookeeper version: 3.4.6-1569965, built on 02/20/2014 09:09 GMT
Clients:
/192.168.190.135:51988[0](queued=0,recved=1,sent=0)
Latency min/avg/max: 0/0/0
Received: 4
Sent: 3
Connections: 1
Outstanding: 0
Zxid: 0x100000000
Mode: follower
Node count: 4
通过客户端脚本zkCli.sh连接到服务器
[…]ls / //列出zk中包含的内容
[…]create /node1 helloworld //创建/node1节点并指定 关联字符串
[…]get /node1 //查询节点数据
[…]set /node1 howareyou //向节点写入数据
[…]delete /node1 //删除节点
[…]ls / //列出根节点
通过客户端脚本zkCli.sh连接到服务器
$>zkCli.sh -server s1:2181
//连接成功后如下信息
Welcome to ZooKeeper…
root@s0:~# zkCli.sh -server s1:2181
Connecting to s1:2181
2017-05-24 23:04:25,578 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT
2017-05-24 23:04:25,584 [myid:] - INFO [main:Environment@100] - Client environment:host.name=s0
2017-05-24 23:04:25,584 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_111
2017-05-24 23:04:25,598 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2017-05-24 23:04:25,598 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/soft/jdk1.8.0_111/jre
2017-05-24 23:04:25,598 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/usr/soft/zookeeper-3.4.6/bin/../build/classes:/usr/soft/zookeeper-3.4.6/bin/../build/lib/*.jar:/usr/soft/zookeeper-3.4.6/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/soft/zookeeper-3.4.6/bin/../lib/slf4j-api-1.6.1.jar:/usr/soft/zookeeper-3.4.6/bin/../lib/netty-3.7.0.Final.jar:/usr/soft/zookeeper-3.4.6/bin/../lib/log4j-1.2.16.jar:/usr/soft/zookeeper-3.4.6/bin/../lib/jline-0.9.94.jar:/usr/soft/zookeeper-3.4.6/bin/../zookeeper-3.4.6.jar:/usr/soft/zookeeper-3.4.6/bin/../src/java/lib/*.jar:/usr/soft/zookeeper-3.4.6/bin/../conf:
2017-05-24 23:04:25,598 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2017-05-24 23:04:25,598 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2017-05-24 23:04:25,598 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=
2017-05-24 23:04:25,598 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2017-05-24 23:04:25,599 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2017-05-24 23:04:25,599 [myid:] - INFO [main:Environment@100] - Client environment:os.version=4.4.0-21-generic
2017-05-24 23:04:25,599 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
2017-05-24 23:04:25,599 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
2017-05-24 23:04:25,600 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/root
2017-05-24 23:04:25,601 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=s1:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@446cdf90
Welcome to ZooKeeper!
2017-05-24 23:04:25,701 [myid:] - INFO [main-SendThread(s1:2181):ClientCnxn$SendThread@975] - Opening socket connection to server s1/192.168.190.136:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2017-05-24 23:04:25,865 [myid:] - INFO [main-SendThread(s1:2181):ClientCnxn$SendThread@852] - Socket connection established to s1/192.168.190.136:2181, initiating session
[zk: s1:2181(CONNECTING) 0] 2017-05-24 23:04:25,957 [myid:] - INFO [main-SendThread(s1:2181):ClientCnxn$SendThread@1235] - Session establishment complete on server s1/192.168.190.136:2181, sessionid = 0x15c3e1b6c080000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: s1:2181(CONNECTED) 0]