This time with you to build a complete zookeeper distributed cluster.
What is the zookeeper:
ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. Every time implement these services, there is much work to resolve the inevitable errors and competitive conditions. During the implementation of such services, the application typically ignores these services, which makes them vulnerable when changes occur, and difficult to manage. Even if these services are performed correctly, when you deploy the application, different implementations of these services can lead to complexity management.
Simply put:
Consistent, first, the data tree
zookeeper uses:
# Solve the problem of distributed system data consistency (Consistency the ACID)
# coordinate a bunch of animals
# hadoop / Impala / Shark / Hive / ...
#mahout
zookeeper cluster structure:
There is a machine leader and the rest are follower (single room recommended number zookeeper cluster as a single number, a minimum of three)
Distributed Source:
google的三篇论文
article | Derived technology |
---|---|
GFS | HDFS |
BigTable | HBase |
MapReduce | HadoopMR |
step:
-
Download zookeeper, recommended to download more stable version 1.4.10
-
Uploaded to the virtual machine liunx
-
Decompression
Command: tar axvf filename .tar.gz (self-placement)
after decompression directory:
-
scp -r copied to three machines
In addition to copy a file to change the configuration file used:
cp zoo_sample.cfg zoo.cfg -
Configuration:
Change the configuration file:
vim zoo.cfg
profile content:
tickTime = 2000: is a time unit, a two-second time unit
initLimit = 10: the number of time unit, multiplied by 10 is 10 2000, ie 20 seconds
dataDir = / tam / zookeeper: zookeeper data stored on the hard disk place
clientport = 2181: client port is 2181
In the end of the file plus three virtual machine IP address:
:2888:3888为端口号
Server.1对应的是0号机
Server.2对应的是1号机
Server.3对应的是2号机
Then change the individual virtual machines myid:
vim myid
Written into the corresponding ID can be good, Server.1 corresponding machine number is 0, write 0 in the machine myid 1 can
each needs to be changed
- start up
Vim myid execute the following command to reach the bin directory execute:
./zkServer.sh start 启动zookeeper
-
Observe the start:
jps
appear QuorumPeerMain prove Started
-
shut down
./zkServer.sh stop
-
Observing the run:
bin/zkServer.sh status
-
Create a node command:
create /app/config
-
zookeeper 的 API
A zookeeper API provided by different clusters can change the zookeeper API to access the data tree
zookeeper application scenarios:
# Configure consistent
#HA
# Pub / Sub
#naming Service
#load Balance
# Distributed Lock
...
Let's write the code
Join jar package:
the jar packages are needed to run the zookeeper
zookeeper .xml file:
Write a small program from a remote connection zookeeper:
public class ZKClient{
public static void main(String[] args){
String CONN = "192.168.56.2181:200,192.168.56.201:2181,192.168.56.202:2181";
Zookeeper zk = new Zookeeper(CONN,5000,null);
//睡三秒钟等待zookeeper的连接
Thread.sleep(3000);
zk.create("/yxxy","yxxy".getBytes(),Ids.OPEN_ACL_UNSAFE,CreateMode.EPHEMERAL);
//在睡两秒
Thread.sleep(2000);
//关闭连接
zk.close();
}
}
Thank attention Tell me what master's, willing to share, the cause of love; I am a cute little seven horse! Let the next section goodbye!