Zookeeper

匿名 (未验证) 提交于 2019-12-02 23:52:01

Zookeeper

1.1

ZookeeperApacheHadoopHbase

1.2

1Zookeeperleaderfollower

2Leader

3FollowerLeader

4奇数台服务器Zookeeper

5serverclientserver

6client

7

8client

1.3

ZooKeeperUnixZNodeZNode1MBԪZNode

1.4

统一命名、统一软

1.4.1

1.4.2 ͳһ

1

hadoop集群

2ZK

1ZKZnode

2ZNode

3ZnodeZK

1.4.3 ͳһ

Zk

ZNode

Znode可获取它的实时状态变化

Master

1.4.4

1.4.5 软负载均衡

1.5

1

https://zookeeper.apache.org/

2

Zookeeper

2.1

1

1jdk

2上传zookeeperlinux

3tar

[itstar@bigdata111 software]$ chmod u+x zookeeper-3.4.10.tar.gz

4

[itstar@bigdata111 software]$ tar -zxvf zookeeper-3.4.10.tar.gz -C /opt/module/

5

[root@bigdata111 software]$ vi /etc/profile

export ZOOKEEPER_HOME=/opt/module/zookeeper-3.4.10

export PATH=$PATH:$ZOOKEEPER_HOME/bin

2

/opt/module/zookeeper-3.4.10/confzoo_sample.cfgzoo.cfg

zoo.cfgvim zoo.cfg

dataDir

dataDir=/opt/module/zookeeper-3.4.10/zkData

/opt/module/zookeeper-3.4.10/zkData

[itstar@bigdata111 zookeeper-3.4.10]$ mkdir zkData

3zookeeper

1zookeeper

[itstar@bigdata111 zookeeper-3.4.10]$ bin/zkServer.sh start

2

[itstar@bigdata111 zookeeper-3.4.10]$ jps

4020 Jps

4001 QuorumPeerMain

3

[itstar@bigdata111 zookeeper-3.4.10]$ bin/zkServer.sh status

ZooKeeper JMX enabled by default

Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg

Mode: standalone

4

[itstar@bigdata111 zookeeper-3.4.10]$ bin/zkCli.sh

5

[zk: localhost:2181(CONNECTED) 0] quit

6zookeeper

[itstar@bigdata111 zookeeper-3.4.10]$ bin/zkServer.sh stop

2.2

zoo.cfg

1tickTime=2000Zookeeper

ZookeepertickTime

session(session2*tickTime)

2initLimit=10Leader和Follower

followerleadertickTimeZookeeperLeader

leader

FollowerLeader

LeaderFollowerinitLimit

3syncLimit=5Leader和Follower

LeaderFollowersyncLimit * tickTimeLeader璁や负FollwerFollwer

LeaderZK

LsyncLimitFF

4dataDir+

5clientPort=2181

Zookeeper

3.1

1Paxos zookeeper

2ZookeepermasterslavezookeeperleaderfollowerLeader

3

zookeeperid1-5

11LOOKING

221id2(3)12LOOKING

333123leader

44412343

554

3.2

1Znode

ephemeral

persistent

2Znodepersistent

1PERSISTENT(persistent)

zookeeper。

2PERSISTENT_SEQUENTIAL

persistent_sequential)

zookeeperZookeeper

3EPHEMERAL(ephemeral)

zookeeper。

4EPHEMERAL_SEQUENTIAL(ephemeral_sequential)

zookeeperZookeeper

3znodeznode

4

3.3 stat

1czxid- znodezxidzxid

ZooKeeperzxidZooKeeperID

IDZooKeeperzxidzxid1zxid2zxid1zxid2

2ctime - znode(1970)

3mzxid - znodezxid

4mtime - znode(1970)

5pZxid-znodezxid

6cversion - znodeznode

7dataversion - znode

8aclVersion - znode

9ephemeralOwner- znodesession id0

10dataLength- znode

11numChildren - znode

0.4

main

mainZKconnect,listener

ZK

ZK

listener线程

process()方法

  1. Ls path [watch]

3.5

clientserverclientserverleaderleaderserver

Zookeeper实战

4.1

0

bigdata111bigdata112bigdata113Zookeeper

1

1zookeeper/opt/module/

[itstar@bigdata111 software]$ tar -zxvf zookeeper-3.4.10.tar.gz -C /opt/module/

2/opt/module/zookeeper-3.4.10/zkData

mkdir -p zkData

3/opt/module/zookeeper-3.4.10/confzoo_sample.cfgzoo.cfg

mv zoo_sample.cfg zoo.cfg

2zoo.cfg

1

dataDir=/opt/module/zookeeper-3.4.10/zkData

#######################cluster##########################

server.1=bigdata111:2888:3888

server.2=bigdata112:2888:3888

server.3=bigdata113:2888:3888

2

Server.A=B:C:D

A

Bip

CLeader

DLeaderLeader

myiddataDirAZookeeperzoo.cfgserver

3

1/opt/module/zookeeper-3.4.10/zkDatamyid

touch myid

myidlinuxnotepad++

2myid

vi myid

server2

3zookeeper

scp -r zookeeper-3.4.10/ root@bigdata112.itstar.com:/opt/app/

scp -r zookeeper-3.4.10/ root@bigdata113.itstar.com:/opt/app/

myid34

4zookeeper

[root@bigdata111 zookeeper-3.4.10]# bin/zkServer.sh start

[root@bigdata112 zookeeper-3.4.10]# bin/zkServer.sh start

[root@bigdata113 zookeeper-3.4.10]# bin/zkServer.sh start

5

[root@bigdata111 zookeeper-3.4.10]# bin/zkServer.sh status

JMX enabled by default

Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg

Mode: follower

[root@bigdata112 zookeeper-3.4.10]# bin/zkServer.sh status

JMX enabled by default

Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg

[root@bigdata113 zookeeper-3.4.5]# bin/zkServer.sh status

JMX enabled by default

Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg

Mode: follower

4.2

命令基本语法

功能描述

help

显示所有操作命令

ls path [watch]

使用 ls znode

ls2 path [watch]

查看当前节点数据并能看到更新次数等数据

create

普通创建()

get path [watch]

获得节点的值

set

设置节点的具体值

stat

查看节点状态

delete

删除节点

rmr

递归删除节点

1

[itstar@bigdata112 zookeeper-3.4.10]$ bin/zkCli.sh

2

[zk: localhost:2181(CONNECTED) 1] help

3znode

[zk: localhost:2181(CONNECTED) 0] ls /

[zookeeper]

4

[zk: localhost:2181(CONNECTED) 1] ls2 /

[zookeeper]

cZxid = 0x0

ctime = Thu Jan 01 08:00:00 CST 1970

mZxid = 0x0

mtime = Thu Jan 01 08:00:00 CST 1970

pZxid = 0x0

cversion = -1

dataVersion = 0

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 0

numChildren = 1

5

[zk: localhost:2181(CONNECTED) 2] create /app1 "hello app1"

Created /app1

[zk: localhost:2181(CONNECTED) 4] create /app1/server101 "192.168.1.101"

Created /app1/server101

6

[zk: localhost:2181(CONNECTED) 6] get /app1

hello app1

cZxid = 0x20000000a

ctime = Mon Jul 17 16:08:35 CST 2017

mZxid = 0x20000000a

mtime = Mon Jul 17 16:08:35 CST 2017

pZxid = 0x20000000b

cversion = 1

dataVersion = 0

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 10

numChildren = 1

[zk: localhost:2181(CONNECTED) 8] get /app1/server101

192.168.1.101

cZxid = 0x20000000b

ctime = Mon Jul 17 16:11:04 CST 2017

mZxid = 0x20000000b

mtime = Mon Jul 17 16:11:04 CST 2017

pZxid = 0x20000000b

cversion = 0

dataVersion = 0

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 13

numChildren = 0

7

[zk: localhost:2181(CONNECTED) 9] create -e /app-emphemeral 8888

1

[zk: localhost:2181(CONNECTED) 10] ls /

[app1, app-emphemeral, zookeeper]

2

[zk: localhost:2181(CONNECTED) 12] quit

[itstar@bigdata113 zookeeper-3.4.10]$ bin/zkCli.sh

3

[zk: localhost:2181(CONNECTED) 0] ls /

[app1, zookeeper]

8

1app2

[zk: localhost:2181(CONNECTED) 11] create /app2 "app2"

2

[zk: localhost:2181(CONNECTED) 13] create -s /app2/aa 888

Created /app2/aa0000000000

[zk: localhost:2181(CONNECTED) 14] create -s /app2/bb 888

Created /app2/bb0000000001

[zk: localhost:2181(CONNECTED) 15] create -s /app2/cc 888

Created /app2/cc0000000002

11

[zk: localhost:2181(CONNECTED) 16] create -s /app1/aa 888

Created /app1/aa0000000001

9

[zk: localhost:2181(CONNECTED) 2] set /app1 999

10

1104/app1

[zk: localhost:2181(CONNECTED) 26] get /app1 watch

2103/app1

3104

WATCHER::

WatchedEvent state:SyncConnected type:NodeDataChanged path:/app1

11

1104/app1

[zk: localhost:2181(CONNECTED) 1] ls /app1 watch

[aa0000000001, server101]

2103/app1

[zk: localhost:2181(CONNECTED) 6] create /app1/bb 666

Created /app1/bb

3104

WATCHER::

WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/app1

12

[zk: localhost:2181(CONNECTED) 4] delete /app1/bb

13

[zk: localhost:2181(CONNECTED) 7] rmr /app2

14

[zk: localhost:2181(CONNECTED) 12] stat /app1

cZxid = 0x20000000a

ctime = Mon Jul 17 16:08:35 CST 2017

mZxid = 0x200000018

mtime = Mon Jul 17 16:54:38 CST 2017

pZxid = 0x20000001c

cversion = 4

dataVersion = 2

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 3

numChildren = 2

4.3 API

4.3.1 IDEA

1

Pom.xml

</dependencies>

4.3.2 ZooKeeper

privatestaticconnectString

privatestaticintsessionTimeout

privatenull;

@Before

publicvoidthrows

zkClient = newconnectString, sessionTimeout, new

@Override

publicvoid

//

System.out.println(event.getType() + "--" + event.getPath());

//

try

zkClient.getChildren("/", true);

} catch

e.printStackTrace();

}

}

});

}

4.3.3

//

@Test

publicvoidthrows

//

// 1234

String nodeCreatedOPEN_ACL_UNSAFE,CreateMode.PERSISTENT);

}

4.3.4

//

@Test

publicvoidthrows

List<String> children = zkClient.getChildren("/", true);

for

System.out.println(child);

}

//

Thread.sleep(Long.MAX_VALUE);

}

4.3.5 znode

// znode是否存在

@Test

publicvoidthrows

Stat stat = zkClient.exists("/eclipse", false);

System.out.println(stat == null

}

4.4

ZkPom

-->

TestDistributedLock

import org.apache.curator.RetryPolicy;

import org.apache.curator.framework.CuratorFramework;

import org.apache.curator.framework.CuratorFrameworkFactory;

import org.apache.curator.framework.recipes.locks.InterProcessMutex;

import org.apache.curator.retry.ExponentialBackoffRetry;

public class TestDistributedLock {

" + count);

2

ZK

ZK---> ZK

10

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!