当前位置:首页 > 开发 > 开源软件 > 正文

[Zookeeper学习笔记之二]Zookeeper部署脚本

发表于: 2014-07-26   作者:bit1129   来源:转载   浏览:
摘要: Zookeeper伪分布式安装脚本(此脚本在一台机器上创建Zookeeper三个进程,即创建具有三个节点的Zookeeper集群。这个脚本和zookeeper的tar包放在同一个目录下,脚本中指定的名字是zookeeper的3.4.6版本,需要根据实际情况修改):   #!/bin/bash #!!!Change the name!!! #The zookeepe

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可以由用户指定

[Zookeeper学习笔记之二]Zookeeper部署脚本

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
一、三个概念 1.CreateMode 在create的时候可以设置znode的类型 主要有四种: PERSISTENT (持续的,
一、三个概念 1.CreateMode 在create的时候可以设置znode的类型 主要有四种: PERSISTENT (持续的,
最近在工作中,为了完善公司集群服务的架构,提高可用性,降低运维成本,因此开始学习ZooKeeper。
ZooKeeper是什么? 高可用的高性能的分布式系统协调服务。局部不可用是分布式系统的固有特征,ZooKe
zookeeper功能点: 统一命名空间(Name Service) 配置推送 (Watch) 集群管理(Group membership) 统一
Unlike a typical file system, which is designed for storage, ZooKeeper data is kept in-memory
http://blog.csdn.net/shirdrn/article/details/7183503 参照前公司的目录结构组织dubbo应用的环境
ZooKeeper系列之二:Zookeeper常用命令 http://blog.csdn.net/xiaolang85/article/details/13021339
http://blog.csdn.net/xiaolang85/article/category/1248060 ZooKeeper服务命令: 在准备好相应的配
ZooKeeper服务命令: 在准备好相应的配置之后,可以直接通过zkServer.sh 这个脚本进行服务的相关操作
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号