Hadoop系统应用之Zookeeper分布式集群部署

一、Zoopkeeper安装包下载安装【Zookeeper版本型号为3.4.10】

1.下载Zookeeper安装包(地址)

https://archive.apache.org/dist/zookeeper/zookeeper-3.4.10/

2.上传安装包(SecureCRT&FX)

通过软件FX将安装包上传到linux系统的/export/software/目录下

Hadoop系统应用之Zookeeper分布式集群部署_第1张图片

3.解压Zookeeper安装包 

执行命令tar -zxvf zookeeper-3.4.10.tar.gz -C /export/servers/将安装包解压到指定文件目录/export/servers

 

 二、Zoopkeeper 相关配置

1.修改Zookeeper的配置文件

(1)修改Zookeeper解压目录下的conf目录,复制其下zoo_sample.cfg文件并重命名为zoo_cfg,命令如下:

cp zoo_sample.cfg zoo.cfg

Hadoop系统应用之Zookeeper分布式集群部署_第2张图片

(2).修改配置文件zoo.cfg,设置dataDir目录,配置服务器编号与主机映射关系,设置与主机连接的心跳端口和选举端口 ,内容如下:

# The number of milliseconds of each tick
#设置通信心跳数
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
#设置初始通信时限
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
#设置数据文件目录+数据持久化路径/

dataDir=/export/data/zookeeper/zkdata
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
#配置zookeeper集群的服务器编号以及对应的主机名、通讯端口号(心跳端口号)和选举端口号
server.1=yangjian00:2888:3888
server.2=yangjian01:2888:3888
server.3=yangjian02:2888:3888
server.4=yangjian03:2888:3888

PS: server.1=yangjian00:2888:3888中,1代表服务器编号,yangjian00表示服务器IP地址,2888表示通讯端口号,3888表示leader选举端口号

2.创建myid文件

(1).首先据文件zoo.cfg中设置的dataDir目录,创建zkdata文件夹

 mkdir -p /export/data/zookeeper/zkdata

 (2).在zkdata文件夹下创建myid文件,该文件内容为服务器编号

cd /export/data/zookeeper/zkdata
 echo 1>myid

3.配置环境变量

(1) 执行命令vi /etc/profile对profile文件进行修改,添加Zookeeper的环境变量

export ZK_HOME=/export/servers/zookeeper-3.4.10
export PATH=$PATH:$JAVA_HOME/bin:$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$ZK_HOME/bin

 4、分发Zookeeper相关文件到其他服务器

(1)首先将Zookeeper安装目录分发到yangjian01 yangjian02 yangjian03服务器上

scp -r /export/servers/zookeeper-3.4.10/ yangjian01:/export/servers/
scp -r /export/servers/zookeeper-3.4.10/ yangjian02:/export/servers/
scp -r /export/servers/zookeeper-3.4.10/ yangjian03:/export/servers/

(2)将myid文件分发到yangjian01 yangjian02 yangjian03服务器,并修改myid文件依次对服务器号进行设置

scp -r /export/data/zookeeper/  yangjian01:/export/data
scp -r /export/data/zookeeper/  yangjian02:/export/data
scp -r /export/data/zookeeper/  yangjian03:/export/data

 (3)将profile文件也分发到yangjian01 yangjian02 yangjian03服务器

scp /etc/profile yangjian01:/etc/profile
scp /etc/profile yangjian02:/etc/profile
scp /etc/profile yangjian03:/etc/profile

 5、环境变量生效

分别在yangjian01/02/03服务器下刷新profile文件,使环境变量生效

source /etc/profile

三、Zookeeper服务的启动和关闭

1、启动Zookeeper服务

依次在yangjian00/01/02/03服务器上启动zookeeper服务

zkServer.sh start

 

 2、查看Zookeeper角色状态

zkServer.sh status

 3、关闭Zookeeper服务

zkServer.sh stop

 四、Zookeeper的shell操作

1.连接Zookeeper服务

zkCli.sh -server localhost:2181

Hadoop系统应用之Zookeeper分布式集群部署_第3张图片

 2.显示所有命令

Hadoop系统应用之Zookeeper分布式集群部署_第4张图片

 3 .查看当前Zookeeper中所含的内容和当前节点数据

#查看当前Zookeeper中所含的内容
ls /

#查看当前节点数据
ls -s /

 4.创建节点:

#创建序列化永久节点:
create -s /testnode test
#创建临时节点
create -e /testnode-temp testtemp
#创建永久节点:
create /testnode-p testp

 5.获取节点:

ls path [watch]
get path [watch]
ls -s path [watch]

 6.修改节点:

ls -s /
get -s /testnode-temp
set /testnode-temp 123
get -s /testnode-temp

Hadoop系统应用之Zookeeper分布式集群部署_第5张图片

Hadoop系统应用之Zookeeper分布式集群部署_第6张图片

 PS:data表示修改的新内容,version表示数据版本。从而得知,testtemp变为123、

7.监听节点:

get /testnode-temp watch
set  /testnode-temp testwatch
#他会回调Watch得到触发结果

8.删除节点:

#普通删除的命令
delete path [version]
#递归删除的命令
rmr path [version]

五、使用java API操作Zookeeper(IDEA版)

1.使用idea创建相应的项目

Hadoop系统应用之Zookeeper分布式集群部署_第7张图片

2.修改配置文件pom.xml,创建依赖Hadoop系统应用之Zookeeper分布式集群部署_第8张图片

所有代码如下:



    4.0.0

    org.example
    Zookeeper
    1.0-SNAPSHOT

    
    
        org.apache.zookeeper
        zookeeper
        3.4.10
    
    

    
        8
        8
        UTF-8
    

 依赖如下

Hadoop系统应用之Zookeeper分布式集群部署_第9张图片

 3.创建日志文件

Hadoop系统应用之Zookeeper分布式集群部署_第10张图片

代码如下

log4j.rootLogger=INFO, stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/inverted index.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

4.从Linux中下载Jar包 

Hadoop系统应用之Zookeeper分布式集群部署_第11张图片

5.创建测试文件Zookeeper

Hadoop系统应用之Zookeeper分布式集群部署_第12张图片

6.在Zookeeper中添加代码

import org.apache.zookeeper.*;

public class Zookpeeper {
    public static void main(String[] args) throws Exception{
        //创建zookeeper客户端
        ZooKeeper zk=new ZooKeeper("yangjian00:2181,yangjian01:2181,yangjian02:2181", 30000, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("事件类型为:"+ watchedEvent.getType());
                System.out.println("事件发生的路径:"+ watchedEvent.getPath());
                System.out.println("通知状态为:"+ watchedEvent.getState());
            }
        });
        //创建目录节点
        zk.create("/testRootPath","testRootData".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        //创建子节点目录
        zk.create("/testRootPath/testChildPathOne","testChildDataOne".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
        //获取目录节点数据
        System.out.println("testRootData 节点数据为:"+new String(zk.getData("/testRootPath",false,null)));
        //获取子目录节点数据
        System.out.println(zk.getChildren("/testRootPath",true));
        //修改子目录节点数据,使得监听触发
        zk.setData("/testRootPath/testChildPathOne","modifyChildDataOne".getBytes(),-1);
        //判断目录节点是否存在
        System.out.println("目录节点状态:["+zk.exists("/testRootPath",true)+"]");
        //删除子目录节点
        zk.delete("/testRootPath/testChildPathOne",-1);
        //删除目录节点
        zk.delete("/testRootPath",-1);
        zk.close();
    }

}

 7.进行测试(ok)

Hadoop系统应用之Zookeeper分布式集群部署_第13张图片

 

 

你可能感兴趣的:(分布式,hadoop,java-zookeeper)