概述
zookeeper应用:
You can use it off-the-shelf to implement consensus, group management, leader election, and presence protocols. And you can build on it for your own, specific needs.
集群节点为什么建议奇数个?
只有超过一半的节点正常,集群才正常,才可以对外服务。
所以,1个节点可以;2个节点,死一个集群就不能用了,所以和一个是一样的;
三个节点,四个节点死一个可以用,死两个不能用,所以和三个是一样的。
所以,需要配置奇数个节点。
zookeeper如何选举?投票结果超过一半立马成为leader,其余的自动成为follower
假如有三个节点
- 启动集群时:
1、集群启动时,发送投票,自己选举自己为leader,只有一台可选举,但是没有超过半数,进入looking状态;
2、第二台启动,发送投票,自己选举自己为leader,与第一台交换信息,会进行比较epoch、zxid(新启动这两个值都相等),然后比较myid,选举出myid大的那个,此时一定会有一个节点票数超过2,会产生leader
3、第三个一定是follower
- 集群运行时,发现leader死了进行重新选举,每个节点都发起一轮投票,此时每个节点投票的方式就是先比较epoch、再比较zxid、最后比较myid
znode注册事件模式:
/***
* CreateMode value determines how the znode is created on ZooKeeper.
*/
public enum CreateMode {
/**
* The znode will not be automatically deleted upon client's disconnect.
*/
PERSISTENT (0, false, false),
/**
* The znode will not be automatically deleted upon client's disconnect,
* and its name will be appended with a monotonically increasing number.
*/
PERSISTENT_SEQUENTIAL (2, false, true),
/**
* The znode will be deleted upon the client's disconnect.
*/
EPHEMERAL (1, true, false),
/**
* The znode will be deleted upon the client's disconnect, and its name
* will be appended with a monotonically increasing number.
*/
EPHEMERAL_SEQUENTIAL (3, true, true);
demo注册,监听到进行业务处理
监听端:
public class ClientManagerAUTO {
// zk对象
static ZooKeeper zk;
// 用来阻止程序运行
private static CountDownLatch countDown = new CountDownLatch(1);
/* 创建连接相关信息 */
// 连接地址
static String connect = "focuson1:2181,focuson2:2181,focuson3:2181";
// 连接超时时间
static int TIME_OUT = 2000;
// 连接监控器
Watcher connectWatcher = new Watcher() {
public void process(WatchedEvent event) {
System.out.println("接收到zookeeper服务端通知,会话真正创建完成!");
}
};
// 节点是否存在监控器
Watcher existsWatcher = new Watcher() {
public void process(WatchedEvent event) {
try {
System.out.println(event.getState()+"========="+event.getType());
if(event.getState().equals(KeeperState.SyncConnected)){
System.out.println("连接上了");
}
if (event.getType().toString().equals("None")) {
System.out.println("none");
}else if (event.getType().toString().equals("NodeCreated")) {
System.out.println("节点1上线");
}else if (event.getType().toString().equals("NodeDeleted")) {
System.out.println("节点1下线");
}else if (event.getType().toString().equals("NodeDataChanged")) {
System.out.println("节点1中的数据已经被修改");
}else if (event.getType().toString().equals("NodeChildrenChanged")) {
System.out.println("节点1的子节点被修改");
}else{
System.out.println("event的信息:"+event);
}
//重复注册watcher
zk.exists("/big1507/node1", existsWatcher);
} catch (Exception e) {
e.printStackTrace();
}
}
};
/**
* 初始化zk对象
*/
public void init() throws Exception{
zk = new ZooKeeper(connect,TIME_OUT,connectWatcher);
}
/**
* 向zk服务器注册对节点1是否存在(即上线)的通知
* @param zk 客户端
* @throws Exception
*/
public void checkExists(ZooKeeper zk) throws Exception{
zk.exists("/focuson_", existsWatcher);
}
public static void main(String[] args) throws Exception{
ClientManagerAUTO auto = new ClientManagerAUTO();
auto.init();
auto.checkExists(zk);
countDown.await();
}
}
//注册端
public class Client1 {
//zk对象
static ZooKeeper zk;
//用来阻止程序运行
private static CountDownLatch countDown = new CountDownLatch(1);
/* 创建连接相关信息 */
// 连接地址
static String connect = "focuson1:2181,focuson2:2181,focuson3:2181";
// 连接超时时间
static int TIME_OUT = 2000;
// 监控器
static Watcher watcher = new Watcher() {
public void process(WatchedEvent event) {
System.out.println("接收到zookeeper服务端通知,会话真正创建完成!");
System.out.println("此时zk对象信息:"+zk);
try {
//先创建管理所有客户端的节点
Stat s = zk.exists("/focuson_", true);
if (null==s) {
System.out.println("管理客户端节点不存在,开始创建");
zk.create("/focuson_", "aaa".getBytes(),Ids.OPEN_ACL_UNSAFE,CreateMode.EPHEMERAL);
}
System.out.println("node1开始向zk服务端注册>>>>>>");
//String path = zk.create("/big1507/node1", "node1".getBytes(),Ids.OPEN_ACL_UNSAFE,CreateMode.EPHEMERAL);
System.out.println("向zk服务端注册成功!!!");
} catch (Exception e) {
System.out.println("会话创建完成后创建临时节点时出现异常:"+e.getMessage());
}
}
};
public static void main(String[] args) throws Exception {
System.out.println("客户端1程序开始启动……");
zk = new ZooKeeper(connect, TIME_OUT, watcher);
System.out.println("zk对象创建完成!正在异步创建和zk服务器之间的连接:");
System.out.println("zk对象的信息:" + zk);
countDown.await();
}
}
测试一把:
先启动监听端,再启动注册端,会输出如下信息:
接收到zookeeper服务端通知,会话真正创建完成!
SyncConnected=========NodeCreated
连接上了
节点1上线
在zookeeper集群上查看(使用zkCli.sh):
[zk: localhost:2181(CONNECTED) 0] ls /
[focuson_, zookeeper, yarn-leader-election, spark, hadoop-ha, hbase]
关闭client1,focuson_文件夹不存在,说明注册的是临时节点。
zookeeper应用场景如下等:
1.1、各种HA,比如NameNode HA、HMaster HA、Spark Master HA、ResourceManager HA;
1.2、各种监控,比如,HRegionServer失败后HMaster能监控到,执行重启、等待重启等;
2、hbase的Meta表信息(或是root表信息)
应用场景举yarn例
yarn在zookeeper节点创建的文件夹:
/yarn-leader-election
--yrc(yarn集群的名字)
--ActiveBreadCrumb(持久化的节点)
--ActiveStandbyElectorLock(临时节点)
当yarn ResourceManager没有启动(yarn之前启动过)时,没有ActiveStandbyElectorLock文件夹,ActiveBreadCrumb文件夹存在,值为上次alive ResourceManager的值,如yrcrm2(根据配置文件的配置来的)。
[zk: localhost:2181(CONNECTED) 101] get /yarn-leader-election/yrc/ActiveBreadCrumb
yrcrm2
cZxid = 0x1b00000055
ctime = Sun May 06 07:21:40 PDT 2018
mZxid = 0x1c00000019
mtime = Fri May 11 21:05:28 PDT 2018
pZxid = 0x1b00000055
cversion = 0
dataVersion = 3
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 10
numChildren = 0
当启动ResourceManager时,文件夹ActiveStandbyElectorLock被创建,值为yrcrm1(根据配置文件的配置来的),此时ActiveBreadCrumb也是yrcrm1
[zk: localhost:2181(CONNECTED) 102] get /yarn-leader-election/yrc/ActiveBreadCrumb
yrcrm1
cZxid = 0x1b00000055
ctime = Sun May 06 07:21:40 PDT 2018
mZxid = 0x1c0000001f
mtime = Fri May 11 21:18:53 PDT 2018
pZxid = 0x1b00000055
cversion = 0
dataVersion = 4
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 10
numChildren = 0
[zk: localhost:2181(CONNECTED) 103] get /yarn-leader-election/yrc/ActiveStandbyElectorLock
yrcrm1
cZxid = 0x1c0000001e
ctime = Fri May 11 21:18:53 PDT 2018
mZxid = 0x1c0000001e
mtime = Fri May 11 21:18:53 PDT 2018
pZxid = 0x1c0000001e
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x1000003187b0002
dataLength = 10
numChildren = 0
此时rm2正在监听ActiveStandbyElectorLock临时节点,当该临时节点不存在时,会自己创建一个值为yrcrm2的ActiveStandbyElectorLock临时节点。
最后
以上就是长情百褶裙为你收集整理的zookeeper内部机制与注册监听机制zookeeper应用:集群节点为什么建议奇数个?demo注册,监听到进行业务处理zookeeper应用场景如下等:的全部内容,希望文章能够帮你解决zookeeper内部机制与注册监听机制zookeeper应用:集群节点为什么建议奇数个?demo注册,监听到进行业务处理zookeeper应用场景如下等:所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复