Talk about ZooKeeper
ZooKeeper is distributed coordination service
which is configured with
zoo.cfg
datadir =
cd / etc / Profile ----->
server.1=bin02:2888:3888
server.2=bin03:2888:3888
server.3=bin04:2888:3888
2888 wherein the external communication port provides zookeeper,
3888 Port: hang up after leader, leader reselection time to provide external communication;
Remember: turn off the firewall! ! ! ! !
Role:
leader leader, initiate a request and start voting
follower is a follower, in response to a request leader and poll
state observer monitoring leader and leader told follower
They are: - "election mechanism, and atomic broadcast protocol zab, sensing mechanism
is as follows:
1: an external client write request, the request is passed follower
2: follower forwards the request to the Leader
. 3: Leader sends the requests to the respective follower, vote
4: follower own ideas back to the leader
principle (majority) more than half of principles: the best number of nodes is singular;
3 + 4 is atomic broadcast
zab协议:
广播模式-------已经选举出来的leader,开始对外提供服务------------原子广播
恢复模式----还没有leader(这个情况可能是集群刚启动或者是leader挂掉了新的还没有起来)
-------looking 观望
-------following----跟从但是有想法
-----leading 继承人(准备被继承)leader
observing--------observer
Sensing mechanism
follower view the status of leader. Then updates the status information and the transmission
zookeeper file node ---------- znode
start the client: zkCli.sh -server 192.168.35.125:2181
Good operation:
[zk: 192.168.35.125:2181(CONNECTED) 21] ls /
[name, zookeeper, name20000000002, name10000000001]
[zk: 192.168.35.125:2181(CONNECTED) 22] rmr /name
[zk: 192.168.35.125:2181(CONNECTED) 23] ls /
[zookeeper, name20000000002, name10000000001]
[zk: 192.168.35.125:2181(CONNECTED) 19] set /name buzhiushi123
cZxid = 0x200000004
ctime = Tue Jun 18 01:45:41 CST 2019
mZxid = 0x200000008
mtime = Tue Jun 18 01:51:03 CST 2019
pZxid = 0x200000004
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x16b66d95ad00001
dataLength = 12
numChildren = 0
[zk: 192.168.35.125:2181(CONNECTED) 20] get /name
buzhiushi123
cZxid = 0x200000004
ctime = Tue Jun 18 01:45:41 CST 2019
mZxid = 0x200000008
mtime = Tue Jun 18 01:51:03 CST 2019
pZxid = 0x200000004
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x16b66d95ad00001
dataLength = 12
numChildren = 0
[zk: 192.168.35.125:2181(CONNECTED) 2] create -e /name "123"
Created /name
[zk: 192.168.35.125:2181(CONNECTED) 3] ls /
[name, zookeeper]
[zk: 192.168.35.125:2181(CONNECTED) 4] create -s /name1 xiaopengyou
Created /name10000000001
[zk: 192.168.35.125:2181(CONNECTED) 5] create -s /name2 xiaojiejie
Created /name20000000002
[zk: 192.168.35.125:2181(CONNECTED) 6] ls /
[name, zookeeper, name20000000002, name10000000001]
Use zookeeper in eclipse
public class TestZ {
public static void main(String[] args) throws Exception {
ZooKeeper zooKeeper = new ZooKeeper("192.168.35.125", 1000, null);
//创建主节点
//String create = zooKeeper.create("/name", "dididididi".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
//创建子节点
/*String create = zooKeeper.create("/name/sex", "didididadada".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(create);*/
//获取节点内容
/*byte[] data = zooKeeper.getData("/name", true, null);
String string = new String(data);
System.out.println(string);//dididididi
*/ //删除节点
//zooKeeper.delete("/name/sex", -1);
//设置节点内容
//zooKeeper.setData("/name", "2222222222".getBytes(), zooKeeper.exists("/name", true).getAversion());
byte[] data = zooKeeper.getData("/name", true, null);
String string = new String(data);
System.out.println(string);
}
}
public class TestWatcher implements Watcher{
private static final int SESSION_TIMEOUT=1000;
private ZooKeeper zk=null;
/**
* @throws Exception
* @throws Exception
*
*/
public static void main(String[] args) throws Exception {
ZooKeeper zooKeeper = new ZooKeeper("192.168.35.125", 1000, null);
byte[] data = zooKeeper.getData("/learning", new TestWatcher(), null);
System.out.println(new String(data));
Thread.sleep(Long.MAX_VALUE);
}
@Override
public void process(WatchedEvent event) {
System.out.println(event.getPath());
}
}