[hadoop][基本原理]zookeeper场景使用

1. 简介

  zookeeper的特性决定他适用到某些场景非常合适,比如典型的应用场景:

  1.集群管理(Group Membership)

  2.统一命名服务(Name Service)

  3.配置管理(Configuration Management)

  4.共享锁(Locks)

  5.队列管理

2.集群管理

   在hadoop中主备节点的概念大家都应该不默认,比如HBase中,我们可以启动多个master节点,但是在某时刻只有一个主master,当这个主master节点退出后

其他的背master将会去争取成为主master。一般流程为:

  如上,一开始的时候多个master都会去zookeeper服务上创建相同的临时路径,其中某个master会节点会成功创建,那么这个节点就是主master了,其他创建不成功的将会观察这个临时路径。

当主master由于宕机或者网络原因失去和zookeeper的连接(session)那么这个临时节点就会被zookeeper删除,随后其他备master将会感知到,再去争抢这个临时路径的创建权。

  如下代码利用多线程来模拟多个master的场景:

public class Master implements Runnable {
    // master节点路径
    private String masterPath = null;

    // 当前master的信息
    private MasterInfo myinfo = null;

    // 当前主master的信息
    private MasterInfo activeMasterInfo = null;

    // 控制当前master的运行时间
    private long runningTime = 0;

    // 监控handler
    private IZkDataListener masterPathListener = null;

    // 当前session
    private ZkClient zc = new ZkClient("xufeng-1:2181,xufeng-2:2181,xufeng-3:2181", 10000, 10000, new SerializableSerializer());
    public Master(MasterInfo info, long runningTime, String masterPath)
    {
        this.myinfo = info;
        this.runningTime = runningTime;
        this.masterPath = masterPath;
        this.masterPathListener = new IZkDataListener() {
            // 当节点被删除的时候触发此方法
            public void handleDataDeleted(String dataPath) throws Exception {
                attackMaster();
            }

            public void handleDataChange(String dataPath, Object data) throws Exception {
                // do nothing

            }
        };

        // 订阅节点
        zc.subscribeDataChanges(masterPath, masterPathListener);
    }

    public void run() {
        attackMaster();
        try {
            Thread.sleep(runningTime);
        } catch (InterruptedException e) {
            // do nothing
        }
    }

    // 去争抢这个节点创建(注册)
    private void attackMaster()
    {
        try {
            // 注册节点
            zc.create(masterPath, myinfo, CreateMode.EPHEMERAL);
            // 如果当前注册成功了,那么他就是主master
            activeMasterInfo = myinfo;
            System.out.println("the active master is : " + activeMasterInfo);
        }
        catch (ZkNodeExistsException e)
        {
            // 当节点已经被其他master注册了
            activeMasterInfo = zc.readData(masterPath);
            // 当无法读取到节点信息则认为其他master可能宕机了,再去抢注
            if (null == activeMasterInfo)
            {
                attackMaster();
            }
            else{
                System.out.println(activeMasterInfo + " has become active! " + myinfo +  " wait for next time to be active!");
            }
        }
        catch (Exception e)
        {
            // 当发生其他错误的时候,不去例会
        }

    }
}

  当我们建立多个线程去启动的时候,多个master就会去抢注/master节点:

the active master is : MasterInfo [id=C, name=masterC]
MasterInfo [id=C, name=masterC] has become active! MasterInfo [id=G, name=masterG] wait for next time to be active!
MasterInfo [id=C, name=masterC] has become active! MasterInfo [id=F, name=masterF] wait for next time to be active!
MasterInfo [id=C, name=masterC] has become active! MasterInfo [id=B, name=masterB] wait for next time to be active!
MasterInfo [id=C, name=masterC] has become active! MasterInfo [id=E, name=masterE] wait for next time to be active!
MasterInfo [id=C, name=masterC] has become active! MasterInfo [id=A, name=masterA] wait for next time to be active!
MasterInfo [id=C, name=masterC] has become active! MasterInfo [id=H, name=masterH] wait for next time to be active!
MasterInfo [id=C, name=masterC] has become active! MasterInfo [id=D, name=masterD] wait for next time to be active!

  这个时候我们模拟网络问题,手动地去删除/master节点多次的时候,各个master感知到节点删除会再次抢注:

the active master is : MasterInfo [id=C, name=masterC]
MasterInfo [id=C, name=masterC] has become active! MasterInfo [id=G, name=masterG] wait for next time to be active!
MasterInfo [id=C, name=masterC] has become active! MasterInfo [id=F, name=masterF] wait for next time to be active!
MasterInfo [id=C, name=masterC] has become active! MasterInfo [id=B, name=masterB] wait for next time to be active!
MasterInfo [id=C, name=masterC] has become active! MasterInfo [id=E, name=masterE] wait for next time to be active!
MasterInfo [id=C, name=masterC] has become active! MasterInfo [id=A, name=masterA] wait for next time to be active!
MasterInfo [id=C, name=masterC] has become active! MasterInfo [id=H, name=masterH] wait for next time to be active!
MasterInfo [id=C, name=masterC] has become active! MasterInfo [id=D, name=masterD] wait for next time to be active!
the active master is : MasterInfo [id=B, name=masterB]
MasterInfo [id=B, name=masterB] has become active! MasterInfo [id=F, name=masterF] wait for next time to be active!
MasterInfo [id=B, name=masterB] has become active! MasterInfo [id=H, name=masterH] wait for next time to be active!
MasterInfo [id=B, name=masterB] has become active! MasterInfo [id=G, name=masterG] wait for next time to be active!
MasterInfo [id=B, name=masterB] has become active! MasterInfo [id=E, name=masterE] wait for next time to be active!
MasterInfo [id=B, name=masterB] has become active! MasterInfo [id=C, name=masterC] wait for next time to be active!
MasterInfo [id=B, name=masterB] has become active! MasterInfo [id=A, name=masterA] wait for next time to be active!
MasterInfo [id=B, name=masterB] has become active! MasterInfo [id=D, name=masterD] wait for next time to be active!
the active master is : MasterInfo [id=E, name=masterE]
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=C, name=masterC] wait for next time to be active!
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=H, name=masterH] wait for next time to be active!
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=F, name=masterF] wait for next time to be active!
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=A, name=masterA] wait for next time to be active!
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=D, name=masterD] wait for next time to be active!
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=B, name=masterB] wait for next time to be active!
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=G, name=masterG] wait for next time to be active!

  我们可以看到每一次抢注成功的master都不一样,这样如果是由于网络问题而不是当前主master真的宕机了,那么会造成不必要的主备切换。所以说我们还可以进行如下的程序优化:  

  如上图,当由于网络原因原来的主master其实并没有宕机,那么为了减小由于主备切换带来的集群抖动,可以让其他备master延迟一定时间去争抢,而当前的主master则马上去争抢。所以说即使

成为了主master也要观察这个临时路径。

  所以在程序中我们可以在抢注的时候判断如果当前master并不是之前的主master,则延迟一定时间去抢注,使得当前主master能够再次成功的抢的节点,具体优化代码如下:

            // 当节点被删除的时候触发此方法
            public void handleDataDeleted(String dataPath) throws Exception {
                if (null != activeMasterInfo && activeMasterInfo.equals(myinfo))
                {
                    // 如果当前master就是主master的时候,直接去抢注
                    attackMaster();
                }
                else
                {
                    // 如果不是则延迟5秒去抢注,给原先的主master一个机会
                    delayExector.schedule(new Runnable() {

                        public void run() {
                            attackMaster();

                        }
                    }, 5, TimeUnit.SECONDS);
                }

            }

  手动的去删除/master节点,结果:masterE始终很稳定的抢注到了/master节点。

the active master is : MasterInfo [id=E, name=masterE]
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=H, name=masterH] wait for next time to be active!
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=C, name=masterC] wait for next time to be active!
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=A, name=masterA] wait for next time to be active!
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=B, name=masterB] wait for next time to be active!
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=D, name=masterD] wait for next time to be active!
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=F, name=masterF] wait for next time to be active!
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=G, name=masterG] wait for next time to be active!
the active master is : MasterInfo [id=E, name=masterE]
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=B, name=masterB] wait for next time to be active!
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=G, name=masterG] wait for next time to be active!
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=F, name=masterF] wait for next time to be active!
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=D, name=masterD] wait for next time to be active!
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=H, name=masterH] wait for next time to be active!
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=C, name=masterC] wait for next time to be active!
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=A, name=masterA] wait for next time to be active!
the active master is : MasterInfo [id=E, name=masterE]
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=F, name=masterF] wait for next time to be active!
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=H, name=masterH] wait for next time to be active!
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=C, name=masterC] wait for next time to be active!
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=A, name=masterA] wait for next time to be active!
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=D, name=masterD] wait for next time to be active!
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=B, name=masterB] wait for next time to be active!
MasterInfo [id=E, name=masterE] has become active! MasterInfo [id=G, name=masterG] wait for next time to be active!

小结:

  除了以上抢注某个临时节点的方式去进行主备切换实现外,我们也可以让每一个master在某个永久节点下各自注册自己的临时节点(CreateMode.EPHEMERAL_SEQUENTIAL)

方式,当观察到这个永久节点下znode有变动的时候,查看自己是不是后缀最小的一个,是,则将变成主master。

  除了主备切换场景外,集群管理中的节点发现,任务分发,也同样可以有zookeeper来处理,这种灵活的使用方式可以解决很多分布式场景的问题。

3.其他

// 略

时间: 2024-10-07 03:02:41

[hadoop][基本原理]zookeeper场景使用的相关文章

[hadoop][基本原理]zookeeper简单使用

1.简介 zookeeper的基本原理和使用场景描述可参考:[hadoop][基本原理]zookeeper基本原理 本文主要讲解zookeeper节点的增删除改查,以及watcher的使用. 2.工程准备 除了zookeeper的自身API外,有两个开源的api更加方便的去让开发者使用----ZkClient和Curator. 上述两个开源API中个人感觉ZkClient使用起来更加直观明了,所以在这里我会使用ZkClient来做讲解. 新建maven工程,在pom文件按照版本需求增加下列依赖:

[hadoop][基本原理]zookeeper基本原理

1.简介 https://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/ 2. 数据模型 Zookeeper 会维护一个具有层次关系的数据结构,它非常类似于一个标准的文件系统,如图所示: Zookeeper 这种数据结构有如下这些特点: 每个子目录项如 NameService 都被称作为 znode,这个 znode 是被它所在的路径唯一标识,如 Server1 这个 znode 的标识为 /NameService/Server

一步一步学习大数据:Hadoop 生态系统与场景

Hadoop概要 到底是业务推动了技术的发展,还是技术推动了业务的发展,这个话题放在什么时候都会惹来一些争议. 随着互联网以及物联网的蓬勃发展,我们进入了大数据时代.IDC预测,到2020年,全球会有44ZB的数据量. 传统存储和技术架构无法满足需求 .在2013年出版的<大数据时代>一书中,定义了大数据的5V特点:Volume(大量).Velocity(高速).Variety(多样).Value(低价值密度).Veracity(真实性). 大数据学习群:119599574 当我们把时间往回看

Hadoop基本原理之一:MapReduce

1.为什么需要Hadoop 目前,一块硬盘容量约为1TB,读取速度约为100M/S,因此完成一块硬盘的读取需时约2.5小时(写入时间更长).若把数据放在同一硬盘上,且全部数据均需要同一个程序进行处理,此程序的处理时间将主要浪费在I/O时间上. 在过去几十年,硬盘的读取速度并未明显增长,而网络传输速度此飞速上升. 因此,若把数据分散到多个硬盘上进行存储(如分成100份存储在100个硬盘上),则读取数据所需时间大大减少,并将各节点处理好的结果通过网络进行传输. 但这将导致2个问题 (1)数据被分散到

Hadoop,HBase,Zookeeper源码编译并导入eclipse

基本理念:尽可能的参考官方英文文档 Hadoop:  http://wiki.apache.org/hadoop/FrontPage HBase:  http://hbase.apache.org/book.html Zookeeper:  https://cwiki.apache.org/confluence/display/ZOOKEEPER/Index 环境介绍 Ubuntu 14.04LTS, 32位 接下来则按照Hadoop,HBase,Zookeeper顺序来进行源码编译,建立文件夹

Hadoop加zookeeper搭建高可靠集群

前期准备 1.修改Linux主机名,每台都得配置 vim /etc/sysconfig/network NETWORKING=yes HOSTNAME=hadoop-server1 2.修改IP /etc/sysconfig/network-scripts/ifcfg-eth0 3.修改主机名和IP的映射关系 vim /etc/hosts 192.168.146.181 hadoop-server1 192.168.146.182 hadoop-server2 192.168.146.183 h

Hadoop加zookeeper构建高可靠集群

事前准备 1.更改Linux主机名,每个人都有配置 vim /etc/sysconfig/network NETWORKING=yes HOSTNAME=hadoop-server1 2.改动IP /etc/sysconfig/network-scripts/ifcfg-eth0 3.改动主机名和IP的映射关系 vim /etc/hosts 192.168.146.181 hadoop-server1 192.168.146.182 hadoop-server2 192.168.146.183

[推荐]Hadoop+HBase+Zookeeper集群的配置

Hadoop+HBase+Zookeeper集群的配置  http://wenku.baidu.com/view/991258e881c758f5f61f67cc.html?re=view HDFS+MapReduce+Hive+HBase十分钟快速入门   http://wenku.baidu.com/view/7db2fcd276eeaeaad1f33055.html Hadoop+Zookeeper+HBase部署指南  http://wenku.baidu.com/view/02e4ad

大数据学习系列之七 ----- Hadoop+Spark+Zookeeper+HBase+Hive集群搭建 图文详解

引言 在之前的大数据学习系列中,搭建了Hadoop+Spark+HBase+Hive 环境以及一些测试.其实要说的话,我开始学习大数据的时候,搭建的就是集群,并不是单机模式和伪分布式.至于为什么先写单机的搭建,是因为作为个人学习的话,单机已足以,好吧,说实话是自己的电脑不行,使用虚拟机实在太卡了... 整个的集群搭建是在公司的测试服务搭建的,在搭建的时候遇到各种各样的坑,当然也收获颇多.在成功搭建大数据集群之后,零零散散的做了写笔记,然后重新将这些笔记整理了下来.于是就有了本篇博文. 其实我在搭