zookeeper 学习笔记(一) 安装、部署、部分java客户端代码

很多大型系统基本都需要一个无单点问题的分布式框架,zookeeper便满足这一需求。这次的博客,我们先进行一个最基本的安装、部署、连接。

 

1、首先下载zookeeper,解压,我这里选择最新的稳定版3.4.8,然后我们使用单机模拟集群,可以专门建立一个文件夹,用来放置你的zookeeper,比如:

/Users/zk_chs/something/hadoop/zookeeper-3.4.8-001

/Users/zk_chs/something/hadoop/zookeeper-3.4.8-002

/Users/zk_chs/something/hadoop/zookeeper-3.4.8-003

 

 

2、接下来,配置每个zookeeper的cfg文件,这里需要更改conf/zoo_sample.cfg为zoo.cfg,需要更改的内容为:

zookeeper-3.4.8-001/conf/zoo.cfg
=============before============

dataDir=/tmp/zookeeper
# the port at which the clients will connect
clientPort=2181

=============bofore============
=============now===============

dataDir=/Users/zk_chs/something/hadoop/tmp/zk-001/data
dataLogDir=/Users/zk_chs/something/hadoop/tmp/zk-001/log
# the port at which the clients will connect
clientPort=2181
server.1=localhost:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389

=============now================

clientPort:客户端连接端口,在我们的模拟集群环境中,每个zookeeper服务不能重复。

server.1=localhost:2287:3387中,2287表示leader通信端口,3387表示leader选举端口。

dataDir表示数据存储目录、dataLogDir表示日志存储目录,需要我们先手动创建好。

之后,再在每个dataDir中分别创建一个myid文件,文件中只有一个数字,即server Id。比如,我们的server.1对应的myid文件内容便是1

 

另外2个zookeeper如下配置:

zookeeper-3.4.8-002/conf/zoo.cfg
=============before============

dataDir=/tmp/zookeeper
# the port at which the clients will connect
clientPort=2182

=============bofore============

=============now===============

dataDir=/Users/zk_chs/something/hadoop/tmp/zk-002/data
dataLogDir=/Users/zk_chs/something/hadoop/tmp/zk-002/log
# the port at which the clients will connect
clientPort=2182
server.1=localhost:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389

=============now================

 

zookeeper-3.4.8-003/conf/zoo.cfg
=============before============

dataDir=/tmp/zookeeper
# the port at which the clients will connect
clientPort=2183

=============bofore============

=============now===============

dataDir=/Users/zk_chs/something/hadoop/tmp/zk-003/data
dataLogDir=/Users/zk_chs/something/hadoop/tmp/zk-003/log
# the port at which the clients will connect
clientPort=2183
server.1=localhost:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389

=============now================

 

 

3、接下来,通过bin/zkServer.sh进行启动:

/Users/zk_chs/something/hadoop/zookeeper-3.4.8-001/bin/zkServer.sh start
/Users/zk_chs/something/hadoop/zookeeper-3.4.8-002/bin/zkServer.sh start
/Users/zk_chs/something/hadoop/zookeeper-3.4.8-003/bin/zkServer.sh start

使用jps查看java进程:

837 QuorumPeerMain

824 QuorumPeerMain

835 QuorumPeerMain

如果启动成功,这些便是能看到的zookeeper进程了,

 

还可以使用zkCli.sh进行客户端连接:

/Users/zk_chs/something/hadoop/zookeeper-3.4.8-002/bin/zkCli.sh -server localhost:2183

 成功后显示:

[zk: localhost:2183(CONNECTED) 0]

这样以来我们便能使用ls ,create ,delete ,get, set来测试了,使用方式与linux大同小异,另外,rmr可以进行递归删除。

 

 

4、使用java连接zookeeper:

个人建议使用开源的zkClient来进行连接,

<dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.8</version>
</dependency>

 

接下来使用zkClient进行zookeeper集群的连接:

public class TestZkClient {

    private static ZkClient zkClient = new ZkClient("127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183");
    private static final String node = "/zk_chs";

    public static void main(String[] args) throws InterruptedException {
        testZkClient();
    }

    private static void testZkClient (){
        if ( !zkClient.exists(node) ){
            zkClient.createPersistent(node, "this is first node");
        }
        System.out.println((String) zkClient.readData(node));
    }

}

连接zookeeper时,使用单个地址也是可以的,我们在这里使用","对地址进行分隔,是为了容错。

我们还定义了一个"/zk_chs"节点,关于节点的更多内容后续讲解。

上述程序输出:

this is first node

 

使用zookeeper还可以对节点数据变化进行监听,对上述程序进行扩展:

public class TestZkClient {

    private static ZkClient zkClient = new ZkClient("127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183");
    private static final String node = "/zk_chs";

    public static void main(String[] args) throws InterruptedException {
        testZkClient();
        subscribeDataChange();
        zkClient.writeData(node, "new data");
        zkClient.delete(node);
        Thread.sleep(1000);
    }

    private static void testZkClient (){
        if ( !zkClient.exists(node) ){
            zkClient.createPersistent(node, "this is first node");
        }
        System.out.println((String) zkClient.readData(node));
    }

    private static void subscribeDataChange (){
        zkClient.subscribeDataChanges(node, new IZkDataListener() {
            @Override
            public void handleDataChange(String dataPath, Object data) throws Exception {
                System.out.println("node data changed: " + dataPath + ", data: " + data);
            }

            @Override
            public void handleDataDeleted(String dataPath) throws Exception {
                System.out.println("node data deleted: " + dataPath);
            }
        });
    }

}

最后输出:

this is first node
node data changed: /zk_chs, data: new data
node data deleted: /zk_chs

 

 

这次我们对zookeeper先做一个基本的了解,后续再介绍更多的内容。

你可能感兴趣的:(zookeeper)