zookeeper分布式安装及简单的例子

本文介绍ZooKeeper 3.5.2版本的分布式部署过程,包括环境搭建、配置文件编辑、环境变量设置等步骤,并提供了一个简单的Java客户端示例,展示如何创建节点、读取数据及监听事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

zookeeper分布式安装及简单的例子

Zookeeper分布式安装

1、版本及环境

 zookeeper 3.5.2  下载地址为:http://mirror.bit.edu.cn/apache/zookeeper/

Linux version 2.6.32-504.el6.x86_64

三台机器:10.202.15.193  10.202.15.195  10.202.15.198

jdk1.8

2、下载后,上传至linux /usr/local/zookeeper  然后解压,tar -zxvf  zookeeper-3.5.2-alpha.tar.gz

3、解压后的文件含有如下

total 1960
drwxr-xr-x 2 1000 1000    4096 Jul  1  2016 bin
-rw-rw-r-- 1 1000 1000   80660 Jun 30  2016 build.xml
-rw-rw-r-- 1 1000 1000  115528 Jun 30  2016 CHANGES.txt
drwxr-xr-x 2 1000 1000    4096 Apr 12 19:43 conf
drwxr-xr-x 9 1000 1000    4096 Jun 30  2016 contrib
drwxr-xr-x 2 1000 1000    4096 Jul  1  2016 dist-maven
drwxr-xr-x 6 1000 1000    4096 Jul  1  2016 docs
-rw-rw-r-- 1 1000 1000    1953 Jun 30  2016 ivysettings.xml
-rw-rw-r-- 1 1000 1000    3898 Jun 30  2016 ivy.xml
drwxr-xr-x 4 1000 1000    4096 Jul  1  2016 lib
-rw-rw-r-- 1 1000 1000   12085 Jun 30  2016 LICENSE.txt
drwxr-xr-x 2 root root    4096 Apr 12 19:43 logs
-rw-rw-r-- 1 1000 1000    3133 Jun 30  2016 NOTICE.txt
-rw-rw-r-- 1 1000 1000    1347 Jun 30  2016 README_packaging.txt
-rw-rw-r-- 1 1000 1000    1557 Jun 30  2016 README.txt
drwxr-xr-x 5 1000 1000    4096 Jun 30  2016 recipes
drwxr-xr-x 7 1000 1000    4096 Jul  1  2016 src
-rw-rw-r-- 1 1000 1000 1722478 Jun 30  2016 zookeeper-3.5.2-alpha.jar
-rw-rw-r-- 1 1000 1000     836 Jul  1  2016 zookeeper-3.5.2-alpha.jar.asc
-rw-rw-r-- 1 1000 1000      33 Jun 30  2016 zookeeper-3.5.2-alpha.jar.md5
-rw-rw-r-- 1 1000 1000      41 Jun 30  2016 zookeeper-3.5.2-alpha.jar.sha1

3,、进入conf 目录 cd /usr/local/zookeeper/zookeeper-3.5.2-alpha/conf,然后执行 cp zoo_sample.cfg zoo.cfg ,相当于更改zoo_sample.cfg 成 zoo.cfg,然后vim zoo.cfg

4、编辑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=/usr/local/zookeeper/data
dataLogDir=/usr/local/zookeeper/logs

#master slave1 slave2分别为三台机器的用户名,即相当于ip地址10.202.15.193  10.202.15.195  10.202.15.198    

#193 195 198相当于id,这个必须和后面的myid的值对应 2888是leader与follower直接的通讯端口,而3888是选举端口

server.193=master:2888:3888
server.195=slave1:2888:3888
server.198=slave2:2888:3888


# 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


5、配置完成后保存,然后再data目录下新建文件myid,然后写入193,保存。命令为:vim myid

6、配置zookeeper环境变量  命令为 vim /etc/profile

ZOOKEEPER_HOME=/usr/local/zookeeper/zookeeper-3.5.2-alpha
JAVA_HOME=/usr/local/jdk/jdk1.8.0_121/
PATH=$PATH:$JAVA_HOME/bin:$ZOOKEEPER_HOME/bin
CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export JAVA_HOME CLASSPATH PATH

然后保存,执行 resource /etc/profile 使之生效

7、拷贝文件夹 /usr/local/zookeeper/  到其他两台机器,并配置环境变量

8、更改对应机器的myid的值,机器10.202.15.195为195,  机器10.202.15.198为198   

9、启动,切换到bin目录,然后执行./zkServer.sh start ,执行完后,可以看到 STARTED,然后执行jps,可以看到QuorumPeerMain,如果没有看到这个进程,则没有启动,也可以通过./zkServer.sh status 查看状态。还可以看到当前机器是leader还是follower,类似信息如下:

ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/zookeeper-3.5.2-alpha/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower

10、linux客户端连接

      在bin目录下执行 ./zkCli.sh 即可,这样就可以操作了,如create /path  set /path hello 

java 简单例子

1、eclipse zookeeper插件安装

      打开eclipse,help->install New Software,输入地址http://www.massedynamic.org/eclipse/updates/即可,重启eclipse,连接上zookeeper客户端

2、新建一个类,简单例子如下,

    public class Master implements Watcher{

    private ZooKeeper zk;
    private String hostPort;
    
    public Master(String hostPort) {
        this.hostPort = hostPort;
    }


    public ZooKeeper startZK() throws IOException{
   
        zk = new ZooKeeper(hostPort, 15000, this);
        return zk;
    }
    
    public void process(WatchedEvent event) {
        System.out.println("你已经出发了"+event.getType()+"事件!");
    }
    
    public static void main(String[] args) throws Exception{
        String add = "10.202.15.193:2181";
        Master m = new Master(add);
        ZooKeeper zk = m.startZK();
        //zk.create("/testRootPath", "testRootPath".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        zk.create("/testRootPath/testChildOnePath", "testChildOne".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        //zk.create("/testRootPath/testChildTwoPath", "testChildTwo".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        //取出指定目录数据
        System.out.println(new String(zk.getData("/testRootPath", false, null)));
        //取出子节点目录数据 返回List 获得是节点的名称,而非数据
//        System.out.println(zk.getChildren("/testRootPath", new Watcher(){
// public void process(WatchedEvent event) {
// System.out.println("hello,event "+event);
// }
//        }));
        
        //zk.setData("/testRootPath/testChildOnePath", "testChildModifyOne".getBytes(), -1);
        zk.setData("/testRootPath/testChildTwoPath", "testChildModifyTwo".getBytes(), -1);
        zk.getData("/testRootPath/testChildTwoPath", new Watcher(){


public void process(WatchedEvent event) {
System.out.println("hello,event "+event);//当对此节点进行操作时才会发生这个watch事件,仅仅查询数据是不会发生watch时间的

}
       
        }, null);
        zk.setData("/testRootPath/testChildTwoPath","testChildTwo111".getBytes(),-1);
        //删除 -1表示删除所有版本
        zk.delete("/testRootPath/testChildOnePath", -1);
        //Thread.sleep(10000);
        zk.close();
        
        
    }


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值