Zookeeper伪分布式安装脚本(此脚本在一台机器上创建Zookeeper三个进程,即创建具有三个节点的Zookeeper集群。这个脚本和zookeeper的tar包放在同一个目录下,脚本中指定的名字是zookeeper的3.4.6版本,需要根据实际情况修改):
#!/bin/bash #!!!Change the name!!! #The zookeeper file should be figured out in the same directory with this script ZOOKEEPER_FILE=zookeeper-3.4.6 tar -xzvf ${ZOOKEEPER_FILE}.tar.gz cd ${ZOOKEEPER_FILE} mkdir cluster cd cluster mkdir -p z1/data mkdir -p z2/data mkdir -p z3/data echo 1 > z1/data/myid echo 2 > z2/data/myid echo 3 > z3/data/myid echo "initLimit=10\nsyncLimit=5\ndataDir=../cluster/z1/data\nclientPort=2181\nserver.1=127.0.0.1:2222:2223\nserver.2=127.0.0.1:3333:3334\nserver.3=127.0.0.1:4444:4445" > z1/z1.cfg echo "initLimit=10\nsyncLimit=5\ndataDir=../cluster/z2/data\nclientPort=2182\nserver.1=127.0.0.1:2222:2223\nserver.2=127.0.0.1:3333:3334\nserver.3=127.0.0.1:4444:4445" > z2/z2.cfg echo "initLimit=10\nsyncLimit=5\ndataDir=../cluster/z3/data\nclientPort=2183\nserver.1=127.0.0.1:2222:2223\nserver.2=127.0.0.1:3333:3334\nserver.3=127.0.0.1:4444:4445" > z3/z3.cfg echo "The 3 nodes zookeeper has been configured successfully" cd ../bin echo "Start to start node1\n" ./zkServer.sh start ../cluster/z1/z1.cfg echo "!!!!!!!!!!!!!!!!!Node1 is started!!!!!!!" echo "Start to start node2\n" ./zkServer.sh start ../cluster/z2/z2.cfg echo "!!!!!!!!!!!!!!!!!Node2 is started!!!!!!!" echo "Start to start node3\n" ./zkServer.sh start ../cluster/z3/z3.cfg echo "!!!!!!!!!!!!!!!!!Node3 is started!!!!!!!"
执行完脚本后,三个node已经启动,停止三个node的命令:
bin/zkServer.sh stop ../cluster/z1/z1.cfg
bin/zkServer.sh stop ../cluster/z2/z2.cfg
bin/zkServer.sh stop ../cluster/z3/z3.cfg
客户端验证程序:
import org.apache.zookeeper.*; import java.io.IOException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ThreadLocalRandom; public class ZKApplication implements Watcher { private static final int SESSION_TIMEOUT = 3000; private volatile static boolean shutdown; private ZooKeeper zk; private CountDownLatch connectedSignal = new CountDownLatch(1); public void connect(String hosts) throws IOException, InterruptedException { zk = new ZooKeeper(hosts, SESSION_TIMEOUT, this); connectedSignal.await(); } @Override public void process(WatchedEvent event) { // Watcher interface if (event.getState() == Event.KeeperState.SyncConnected) { connectedSignal.countDown(); } } public void create(String groupName) throws KeeperException, InterruptedException { String path = "/" + groupName; String createdPath = zk.create(path, null/*data*/, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); //The znode will be deleted upon the session is closed. System.out.println("Created " + createdPath); } public void close() throws InterruptedException { zk.close(); } public static void main(String[] args) throws Exception { final ZKApplication createGroup = new ZKApplication(); String groupName = "zoo" + ThreadLocalRandom.current().nextInt(); createGroup.connect(Host.HOST); createGroup.create(groupName); createGroup.close(); } }
单机部署Zookeeper集群的脚本还有很大的改进空间,应该根据需要很方便的创建n个节点,其中n可以由用户指定