Zookeeper(3) Use Zookeeper in the Application - Clients

Zookeeper(3) Use Zookeeper in the Application - Clients

1. Idea of ZooKeeper

ZNode, we can put data on any node and this node also can have sub nodes.

The purpose of zookeeper is not to store DB data, big data, it is only the configuration data, it is always small, several KB, not more than 1MB.

There are 2 types of nodes

Ephemeral, if the create close the connection, this type of node will be deleted.

Persistent   store the data

2. Install the latest Zookeeper

http://zookeeper.apache.org/doc/r3.4.5/

Download and get the latest file zookeeper-3.4.5.tar.gz

Soft link the directory

>sudo ln -s /Users/carl/tool/zookeeper-3.4.5 /opt/zookeeper-3.4.5

>sudo ln -s /opt/zookeeper-3.4.5 /opt/zookeeper

Add this to the system path

>vi ~/.profile

export PATH=/opt/zookeeper/bin:$PATH

>. ~/.profile

Put the default configuration file

>cp conf/zoo_sample.cfg conf/zoo.cfg

And start the server like this

>zkServer.sh start zoo.cfg

Use JPS to check if the server is running

>jps

1957

10014 QuorumPeerMain

2260

10050 Jps

Connecting with client

>zkCli.sh -server localhost:2181

zookeeper>help

zookeeper>quit

Stop the server

>zkServer.sh stop

There are status, restart, upgrade, start, stop...

Configure to Cluster as follow

zoo1.cfg

dataDir=/tmp/zookeeper/zoo1 

server.1=localhost:2888:3888

server.2=localhost:2889:3889

server.3=localhost:2890:3890

clientPort=2181 

zoo2.cfg

dataDir=/tmp/zookeeper/zoo2  

server.1=localhost:2888:3888

server.2=localhost:2889:3889

server.3=localhost:2890:3890

clientPort=2182

zoo3.cfg

dataDir=/tmp/zookeeper/zoo3  

server.1=localhost:2888:3888

server.2=localhost:2889:3889

server.3=localhost:2890:3890

clientPort=2183 

>vi /tmp/zookeeper/zoo1/myid

1

>vi /tmp/zookeeper/zoo2/myid

2

>vi /tmp/zookeeper/zoo3/myid

3

Start 3 nodes

>zkServer.sh start zoo1.cfg

>zkServer.sh start zoo2.cfg

>zkServer.sh start zoo3.cfg

>jps

Or use client to connect

>zkCli.sh -server localhost:2181

>zkServer.sh stop zoo1.cfg

>zkServer.sh stop zoo2.cfg

>zkServer.sh stop zoo3.cfg

Here is the status of all the server

>zkServer.sh status conf/zoo1.cfg

JMX enabled by default

Using config: conf/zoo1.cfg

Mode: follower

>zkServer.sh status conf/zoo2.cfg

JMX enabled by default

Using config: conf/zoo2.cfg

Mode: leader

>zkServer.sh status conf/zoo3.cfg

JMX enabled by default

Using config: conf/zoo3.cfg

Mode: follower

4. Open the ZooInspector

>cd /opt/zookeeper/contrib/ZooInspector

>java -cp zookeeper-3.4.5-ZooInspector.jar:lib/*:/opt/zookeeper/*:/opt/zookeeper/lib/* org.apache.zookeeper.inspector.ZooInspector

Or terminatively

>vi inspector.sh

#!/bin/sh

java -cp zookeeper-3.4.5-ZooInspector.jar:lib/*:/opt/zookeeper/*:/opt/zookeeper/lib/* org.apache.zookeeper.inspector.ZooInspector

>chmod a+x inspector.sh

>./inspector.sh

5. Clients

Get the source codes

>git clone https://git-wip-us.apache.org/repos/asf/incubator-curator.git

It will go on...

References:

zookeeper

http://sillycat.iteye.com/blog/1556108

http://sillycat.iteye.com/blog/1556141

http://rdc.taobao.com/team/jm/archives/665

http://blog.javachen.com/hadoop/2013/08/23/publish-proerties-using-zookeeper/

http://rdc.taobao.com/team/jm/archives/tag/zookeeper

https://github.com/alibaba/taokeeper

zookeeper client

https://github.com/twitter/scala-zookeeper-client

http://zookeeper.apache.org/doc/trunk/javaExample.html

https://github.com/twitter/util

https://github.com/sleberknight/zookeeper-samples

http://curator.incubator.apache.org/

http://braveo.blogspot.com/2013/05/how-to-run-zooinspector.html

http://www.taobaotesting.com/blogs/qa?bid=15305

http://mvnrepository.com/artifact/org.apache.curator/curator-recipes/2.2.0-incubating

https://github.com/sleberknight/zookeeper-samples/blob/master/etc/running-zooinspector.txt

猜你喜欢

转载自sillycat.iteye.com/blog/2090969