zookeeper部署与使用API
zookeeper介绍 zookeeper它是一个分布式的服务程序协调系统,简单的说它是一个以节点资源管理为中心,围绕节点资源操作来实现事件监听,触发而衍生出各种服务调度管理的系统。比如说,我创建了一个节点/zk_test,然后使用zookeeper的API程序可以监听这个/zk_test节点,当节点有事件发生的时候会通知这个监听程序,然后监听程序就可以实现自己的业务逻辑。
安装配置JDK 本文使用的是Ubuntu Server 14.04,安装配置JDK相对比较简单 sudo aptitude install -y default-jdk
部署zookeeper 访问官网下载二进制压缩包 http://zookeeper.apache.org/releases.html#download 本文用的是zookeeper-3.4.6.tar.gz,下载到/opt目录,然后解压 cd /opt tar zxf zookeeper-3.4.6.tar.gz cd zookeeper-3.4.6/ #配置文件 vim conf/zoo.cfg
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# 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/lib/zookeeper # the port at which the clients will connect clientPort=2181 # 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 |
启动zookeeper命令行 bin/zkCli.sh -server 127.0.0.1:2181
使用zookeeper #使用zookeeper命令行创建节点znode /zk_test 其中”my_data”是绑定到该znode的字符串 create /zk_test my_data
#查看当前节点列表 ls /
#设置节点数据 set /zk_test test123
#查看节点数据 get /zk_test
测zookeeper API java程序
该测试程序是zookeeper提供的一个监听节点znode时间,然后调用用户指定命令及参数的例子,我们继续使用上面创建的节点/zk_test作为测试对象,然后我们指定事件响应的时候执行的是cat显示文件内容,然后这个文件内容是节点的数据内容。cd /tmp mkdir lib #把zookeeper压缩包里面zookeeper-3.4.6.jar 和 slf4j-api-1.6.1.jar copy到lib目录下 cp /opt/zookeeper-3.4.6/dist-maven/zookeeper-3.4.6.jar /tmp/lib cp /opt/zookeeper-3.4.6/lib/slf4j-api-1.6.1.jar /tmp/lib
vim Executor.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
/** * A simple example program to use DataMonitor to start and * stop executables based on a znode. The program watches the * specified znode and saves the data that corresponds to the * znode in the filesystem. It also starts the specified program * with the specified arguments when the znode exists and kills * the program if the znode goes away. */ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
/** * A simple class that monitors the data and existence of a ZooKeeper * node. It uses asynchronous ZooKeeper APIs. */ import java.util.Arrays; |
1 2 |
javac -cp .:lib/* Executor.java DataMonitor.java java -cp .:lib/* Executor 127.0.0.1:2181 /zk_test /tmp/test cat /tmp/test |
1 2 3 4 5 |
USAGE: Executor hostPort znode filename program [args ...] hostPort是zookeeper的地址端口 filename是输出节点内容到指定的文件 program是用户自定义执行的命令 [args...]是用户自定义执行的命令的输入参数 |