ZooKeeper
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。(百度百科)
1、下载与解压
下载:apache-zookeeper-3.5.5.tar.gz
解压:tar -xzvf apache-zookeeper-3.5.5.tar.gz -C /usr/local/zookeeper
进入:cd /usr/local/zookeeper/apache-zookeeper-3.5.5/
目录:
命令:
2、启动
2.1、单个节点启动
2.1.1、配置
cat conf/zoo.cfg
dataDir=/var/zookeeper
clientPort=2181
2.1.2、启动
bin/zkServer.sh start
查看进程:
未发现服务成功启动!查看日志如下:
cat logs/zookeeper-root-server-localhost.out
错误: 找不到或无法加载主类 org.apache.zookeeper.server.quorum.QuorumPeerMain
查看cat README.md文件:需要mvn打包源码生成jar包,注意跳过测试类(太耗时!)
mvn clean install -DskipTests
再次启动:
bin/zkServer.sh start
2.1.3、客户端连接
连接服务端:
bin/zkCli.sh -server localhost:2181
查看节点:
[zk: localhost:2181(CONNECTED) 4] ls /
[zookeeper]
创建节点:
[zk: localhost:2181(CONNECTED) 5] create /zk_test my_data
Created /zk_test
[zk: localhost:2181(CONNECTED) 6] ls /
[zk_test, zookeeper]
[zk: localhost:2181(CONNECTED) 7] get /zk_test
my_data
修改节点:
[zk: localhost:2181(CONNECTED) 8] set /zk_test junk
[zk: localhost:2181(CONNECTED) 9] get /zk_test
junk
删除节点:
[zk: localhost:2181(CONNECTED) 11] delete /zk_test
junk
2.2、多个节点启动
2.2.1、配置
序号 |
服务与端口 |
数据目录 |
|
1 |
192.168.220.13:2181 |
/var /zookeeper |
|
2 |
192.168.220.14:2181 |
/var /zookeeper |
|
3 |
192.168.220.15:2181 |
/var /zookeeper |
zoo.cfg配置文件:
# 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=/var/zookeeper # the port at which the clients will connect clientPort=2181 server.1=192.168.229.13:2888:3888 server.2=192.168.229.14:2888:3888 server.3=192.168.229.15:2888:3888 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval=1 |
分别在每个服务器/var /zookeeper下创建myid文件,内容如下:
1,2,3(对应zoo.cfg中的server.x)。
端口说明:
1、2181:对cline端提供服务
2、3888:选举leader使用
3、2888:集群内机器通讯使用(Leader监听此端口)
zk各节点防火墙设置:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2181 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 2888 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 3888 -j ACCEPT |
2.2.2、启动
登录各节点启动服务:bin/zkServer.sh start
节点操作:bin/zkCli.sh -server 192.168.229.13:2181
[zk: 192.168.229.13:2181(CONNECTED) 5] ls /
[admin, brokers, cluster, config, consumers, controller, controller_epoch, isr_change_notification, latest_producer_id_block, log_dir_event_notification, zookeeper]
[zk: 192.168.229.13:2181(CONNECTED) 6] create /zk_test zk_test_data
Created /zk_test
[zk: 192.168.229.13:2181(CONNECTED) 7] ls /
[admin, brokers, cluster, config, consumers, controller, controller_epoch, isr_change_notification, latest_producer_id_block, log_dir_event_notification, zk_test, zookeeper]
[zk: 192.168.229.13:2181(CONNECTED) 8] get /zk_test
zk_test_data
[zk: 192.168.229.13:2181(CONNECTED) 9] set /zk_test my_data
[zk: 192.168.229.13:2181(CONNECTED) 10] get /zk_test
my_data
[zk: 192.168.229.13:2181(CONNECTED) 11]