0


Zookeeper基础命令操作

cversion = 0

dataVersion = 0

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 8

numChildren = 0

create -s 创建顺序节点 自动累加

创建顺序节点,顺序节点会自动累加

[zk: localhost:2181(CONNECTED) 2] create -s /merryyou/sec seq

Created /merryyou/sec0000000001

[zk: localhost:2181(CONNECTED) 3] create -s /merryyou/sec seq

Created /merryyou/sec0000000002

set path data [version] 修改节点

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

merryyou

cZxid = 0x200000004

ctime = Sat Jun 02 14:20:06 UTC 2018

mZxid = 0x200000004

mtime = Sat Jun 02 14:20:06 UTC 2018

pZxid = 0x200000009

cversion = 4

dataVersion = 0

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 8

numChildren = 2

修改节点内容为new-merryyou

[zk: localhost:2181(CONNECTED) 7] set /merryyou new-merryyou

cZxid = 0x200000004

ctime = Sat Jun 02 14:20:06 UTC 2018

mZxid = 0x20000000a

mtime = Sat Jun 02 14:29:23 UTC 2018

pZxid = 0x200000009

cversion = 4

dataVersion = 1

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 12

numChildren = 2

#再次查询,节点内容已经修改

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

new-merryyou

cZxid = 0x200000004

ctime = Sat Jun 02 14:20:06 UTC 2018

mZxid = 0x20000000a

mtime = Sat Jun 02 14:29:23 UTC 2018

pZxid = 0x200000009

cversion = 4

dataVersion = 1

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 12

numChildren = 2

#set 根据版本号更新 dataVersion 乐观锁

[zk: localhost:2181(CONNECTED) 9] set /merryyou test-merryyou 1

cZxid = 0x200000004

ctime = Sat Jun 02 14:20:06 UTC 2018

mZxid = 0x20000000b

mtime = Sat Jun 02 14:31:30 UTC 2018

pZxid = 0x200000009

cversion = 4

dataVersion = 2

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 13

numChildren = 2

#因为数据的版本号已经修改为2 再次使用版本号1修改节点提交错误

[zk: localhost:2181(CONNECTED) 10] set /merryyou test-merryyou 1

version No is not valid : /merryyou

delete path [version] 删除节点

[zk: localhost:2181(CONNECTED) 13] delete /merryyou/sec000000000

sec0000000001 sec0000000002

[zk: localhost:2181(CONNECTED) 13] delete /merryyou/sec0000000001

[zk: localhost:2181(CONNECTED) 14] ls /merryyou

[sec0000000002]

[zk: localhost:2181(CONNECTED) 15]

#版本号操作与set类似 version

watcher通知机制

关于watcher机制大体的理解可以为,当每个节点发生变化,都会触发watcher事件,类似于mysql的触发器。zk中 watcher是一次性的,触发后立即销毁。可以参考https://blog.csdn.net/hohoo1990/article/details/78617336

  • stat path [watch] 设置watch事件

  • get path [watch]设置watch事件

  • 子节点创建和删除时触发watch事件,子节点修改不会触发该事件

stat path [watch] 设置watch事件

#添加watch 事件

[zk: localhost:2181(CONNECTED) 18] stat /longfei watch

Node does not exist: /longfei

#创建longfei节点时触发watcher事件

[zk: localhost:2181(CONNECTED) 19] create /longfei test

WATCHER::

WatchedEvent state:SyncConnected type:NodeCreated path:/longfei

Created /longfei

get path [watch] 设置watch事件

#使用get命令添加watch事件

[zk: localhost:2181(CONNECTED) 20] get /longfei watch

test

cZxid = 0x20000000e

ctime = Sat Jun 02 14:43:15 UTC 2018

mZxid = 0x20000000e

mtime = Sat Jun 02 14:43:15 UTC 2018

pZxid = 0x20000000e

cversion = 0

dataVersion = 0

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 4

numChildren = 0

#修改节点触发watcher事件

[zk: localhost:2181(CONNECTED) 21] set /longfei new_test

WATCHER::

WatchedEvent state:SyncConnected type:NodeDataChanged path:/longfei

cZxid = 0x20000000e

ctime = Sat Jun 02 14:43:15 UTC 2018

mZxid = 0x20000000f

mtime = Sat Jun 02 14:45:06 UTC 2018

pZxid = 0x20000000e

cversion = 0

dataVersion = 1

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 8

numChildren = 0

[zk: localhost:2181(CONNECTED) 22]

#删除触发watcher事件

[zk: localhost:2181(CONNECTED) 23] get /longfei watch

new_test

cZxid = 0x20000000e

ctime = Sat Jun 02 14:43:15 UTC 2018

mZxid = 0x20000000f

mtime = Sat Jun 02 14:45:06 UTC 2018

pZxid = 0x20000000e

cversion = 0

dataVersion = 1

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 8

numChildren = 0

[zk: localhost:2181(CONNECTED) 24] delete /longfei

WATCHER::

WatchedEvent state:SyncConnected type:NodeDeleted path:/longfei

[zk: localhost:2181(CONNECTED) 25]

ACL权限控制

ZK的节点有5种操作权限:CREATE、READ、WRITE、DELETE、ADMIN 也就是 增、删、改、查、管理权限,这5种权限简写为crwda(即:每个单词的首字符缩写)。

注:这5种权限中,delete是指对子节点的删除权限,其它4种权限指对自身节点的操作权限

身份的认证有4种方式:

  • world:默认方式,相当于全世界都能访问

  • auth:代表已经认证通过的用户(cli中可以通过addauth digest user:pwd 来添加当前上下文中的授权用户)

  • digest:即用户名:密码这种方式认证,这也是业务系统中最常用的

  • ip:使用Ip地址认证

使用[scheme🆔permissions]来表示acl权限

getAcl:获取某个节点的acl权限信息

#获取节点权限信息默认为 world:cdrwa任何人都可以访问

[zk: localhost:2181(CONNECTED) 34] getAcl /merryyou

'world,'anyone

cdrwa

[zk: localhost:2181(CONNECTED) 35]

setAcl 设置权限

[zk: localhost:2181(CONNECTED) 35] create /merryyou/test test

Created /merryyou/test

[zk: localhost:2181(CONNECTED) 36] getAcl /merryyou/test

'world,'anyone

cdrwa

#设置节点权限 crwa 不允许删除

[zk: localhost:2181(CONNECTED) 37] setAcl /merryyou/test world:anyone:crwa

cZxid = 0x200000018

ctime = Sat Jun 02 16:18:18 UTC 2018

mZxid = 0x200000018

mtime = Sat Jun 02 16:18:18 UTC 2018

pZxid = 0x200000018

cversion = 0

dataVersion = 0

aclVersion = 1

ephemeralOwner = 0x0

dataLength = 4

numChildren = 0

#查询刚才设置的acl权限信息 crwa 没有删除权限

[zk: localhost:2181(CONNECTED) 38] getAcl /merryyou/test

'world,'anyone

crwa

[zk: localhost:2181(CONNECTED) 39]

[zk: localhost:2181(CONNECTED) 39] create /merryyou/test/abc abc

Created /merryyou/test/abc

#删除子节点的时候提交权限不足

[zk: localhost:2181(CONNECTED) 40] delete /merryyou/test/abc

Authentication is not valid : /merryyou/test/abc

#设置节点的权限信息为rda

[zk: localhost:2181(CONNECTED) 41] setAcl /merryyou/test world:anyone:rda

cZxid = 0x200000018

ctime = Sat Jun 02 16:18:18 UTC 2018

mZxid = 0x200000018

mtime = Sat Jun 02 16:18:18 UTC 2018

pZxid = 0x20000001a

cversion = 1

dataVersion = 0

aclVersion = 2

ephemeralOwner = 0x0

dataLength = 4

numChildren = 1

[zk: localhost:2181(CONNECTED) 42] getAcl /merryyou/test

'world,'anyone

dra

#可以成功删除

[zk: localhost:2181(CONNECTED) 43] delete /merryyou/test/abc

[zk: localhost:2181(CONNECTED) 46] ls /merryyou/test

[]

[zk: localhost:2181(CONNECTED) 47]

#设置节点信息为a admin

[zk: localhost:2181(CONNECTED) 47] setAcl /merryyou/test world:anyone:a

cZxid = 0x200000018

ctime = Sat Jun 02 16:18:18 UTC 2018

mZxid = 0x200000018

mtime = Sat Jun 02 16:18:18 UTC 2018

pZxid = 0x20000001d

cversion = 2

dataVersion = 0

aclVersion = 3

ephemeralOwner = 0x0

dataLength = 4

numChildren = 0

#获取 设置都提示权限不足

[zk: localhost:2181(CONNECTED) 49] get /merryyou/test

Authentication is not valid : /merryyou/test

[zk: localhost:2181(CONNECTED) 50] set /merryyou/test 123

Authentication is not valid : /merryyou/test

[zk: localhost:2181(CONNECTED) 51]

acl Auth 密码明文设置

[zk: localhost:2181(CONNECTED) 53] create /niocoder/merryyou merryyou

Created /niocoder/merryyou

#查询默认节点权限信息

[zk: localhost:2181(CONNECTED) 54] getAcl /niocoder/merryyou

'world,'anyone

cdrwa

[zk: localhost:2181(CONNECTED) 55]

#使用auth设置节点权限信息

[zk: localhost:2181(CONNECTED) 2] setAcl /niocoder/merryyou auth:test:test:cdrwa

Acl is not valid : /niocoder/merryyou

注册test:test 账号密码

[zk: localhost:2181(CONNECTED) 3] addauth digest test:test

[zk: localhost:2181(CONNECTED) 4] setAcl /niocoder/merryyou auth:test:test:cdrwa

cZxid = 0x200000020

ctime = Sat Jun 02 16:32:08 UTC 2018

mZxid = 0x200000020

mtime = Sat Jun 02 16:32:08 UTC 2018

pZxid = 0x200000020

cversion = 0

dataVersion = 0

aclVersion = 1

ephemeralOwner = 0x0

dataLength = 8

numChildren = 0

#查询节点权限信息 密码为密文格式

[zk: localhost:2181(CONNECTED) 5] getAcl /niocoder/merryyou

'digest,'test:V28q/NynI4JI3Rk54h0r8O5kMug=

cdrwa

[zk: localhost:2181(CONNECTED) 6]

acl digest 密码密文设置

[zk: localhost:2181(CONNECTED) 13] create /names test

Created /names

[zk: localhost:2181(CONNECTED) 14] getAcl /names

'world,'anyone

cdrwa

#使用digest设置节点的权限信息 密码为test密文

[zk: localhost:2181(CONNECTED) 15] setAcl /names digest:test:V28q/NynI4JI3Rk54h0r8O5kMug=:cdra

cZxid = 0x400000006

ctime = Sun Jun 03 01:01:17 UTC 2018

mZxid = 0x400000006

mtime = Sun Jun 03 01:01:17 UTC 2018

pZxid = 0x400000006

cversion = 0

dataVersion = 0

aclVersion = 1

ephemeralOwner = 0x0

dataLength = 4

numChildren = 0

#查询节点权限信息

[zk: localhost:2181(CONNECTED) 16] getAcl /names

'digest,'test:V28q/NynI4JI3Rk54h0r8O5kMug=

cdra

#获取节点信息提示权限不足

[zk: localhost:2181(CONNECTED) 5] get /names

Authentication is not valid : /names

注册账户

[zk: localhost:2181(CONNECTED) 4] addauth digest test:test

可以正常获取

[zk: localhost:2181(CONNECTED) 17] get /names

test

cZxid = 0x400000006

ctime = Sun Jun 03 01:01:17 UTC 2018

mZxid = 0x400000006

mtime = Sun Jun 03 01:01:17 UTC 2018

pZxid = 0x400000006

cversion = 0

dataVersion = 0

aclVersion = 1

ephemeralOwner = 0x0

dataLength = 4

numChildren = 0

#由于没有设置写权限不能修改节点 w

[zk: localhost:2181(CONNECTED) 18] set /names 111

Authentication is not valid : /names

[zk: localhost:2181(CONNECTED) 19] delete /names

[zk: localhost:2181(CONNECTED) 20]

acl ip 控制客户端

[zk: localhost:2181(CONNECTED) 22] create /niocoder/ip aa

Created /niocoder/ip

[zk: localhost:2181(CONNECTED) 23] get /niocoder/ip

aa

cZxid = 0x40000000a

ctime = Sun Jun 03 01:06:47 UTC 2018

mZxid = 0x40000000a

mtime = Sun Jun 03 01:06:47 UTC 2018

pZxid = 0x40000000a

cversion = 0

dataVersion = 0

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 2

numChildren = 0

添加ip控制的权限信息

[zk: localhost:2181(CONNECTED) 24] setAcl /niocoder/ip ip:192.168.0.68:cdrwa

cZxid = 0x40000000a

ctime = Sun Jun 03 01:06:47 UTC 2018

mZxid = 0x40000000a

mtime = Sun Jun 03 01:06:47 UTC 2018

pZxid = 0x40000000a

cversion = 0

dataVersion = 0

aclVersion = 1

ephemeralOwner = 0x0

dataLength = 2

numChildren = 0

[zk: localhost:2181(CONNECTED) 25] getAcl /niocoder/ip

'ip,'192.168.0.68

cdrwa

[zk: localhost:2181(CONNECTED) 26]

acl super超级管理员

使用

  1. super

权限需要修改

  1. zkServer.sh

,添加

  1. super

管理员,重启

  1. zkServer.sh

“-Dzookeeper.DigestAuthenticationProvider.superDigest=test:V28q/NynI4JI3Rk54h0r8O5kMug=”

nohup “

  1. J
  2. A
  3. V
  4. A
  5. "
  6. "
  7. D
  8. z
  9. o
  10. o
  11. k
  12. e
  13. e
  14. p
  15. e
  16. r
  17. .
  18. l
  19. o
  20. g
  21. .
  22. d
  23. i
  24. r
  25. =
  26. JAVA" "-Dzookeeper.log.dir=
  27. JAVA""Dzookeeper.log.dir={ZOO_LOG_DIR}” “-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}” “-Dzookeeper.DigestAuthenticationprovider.superDigest=test:V28q/NynI4JI3Rk54h0r8O5kMug=” \

-cp “$CLASSPATH” $JVMFLAGS

  1. Z
  2. O
  3. O
  4. M
  5. A
  6. I
  7. N
  8. "
  9. ZOOMAIN "
  10. ZOOMAIN"ZOOCFG" > $_ZOO_DAEMON_OUT 2>&1 < /dev/null &

#重启进入zkCli

#由于之前设置ip权限,所以不允许访问

[zk: localhost:2181(CONNECTED) 2] ls /niocoder/ip

Authentication is not valid : /niocoder/ip

#登录账号信息,即为管理员账号

[zk: localhost:2181(CONNECTED) 3] addauth digest test:test

#正常访问,节点内容为空

[zk: localhost:2181(CONNECTED) 4] ls /niocoder/ip

[]

[zk: localhost:2181(CONNECTED) 5] get /niocoder/ip

aa

cZxid = 0x40000000a

ctime = Sun Jun 03 01:06:47 UTC 2018

mZxid = 0x40000000a

mtime = Sun Jun 03 01:06:47 UTC 2018

pZxid = 0x40000000a

cversion = 0

dataVersion = 0

aclVersion = 1

ephemeralOwner = 0x0

dataLength = 2

numChildren = 0

[zk: localhost:2181(CONNECTED) 6]

四字命令Four Letter Words

  • 使用四字命令需要安装nc命令,(yum install nc)

stat 查看状态信息

[root@localhost bin]# echo stat | nc 192.168.0.68 2181

Zookeeper version: 3.4.11-37e277162d567b55a07d1755f0b31c32e93c01a0, built on 11/01/2017 18:06 GMT

Clients:

/192.168.0.68:493460

Latency min/avg/max: 0/0/4

Received: 62

Sent: 61

Connections: 1

Outstanding: 0

Zxid: 0x50000000a

Mode: follower

Node count: 10

[root@localhost bin]#

ruok 查看zookeeper是否启动

[root@localhost bin]# echo ruok | nc 192.168.0.68 2181

imok[root@localhost bin]#

dump 列出没有处理的节点,临时节点

imok[root@localhost bin]# echo dump | nc 192.168.0.68 2181

SessionTracker dump:

org.apache.zookeeper.server.quorum.LearnerSessionTracker@29805957

ephemeral nodes dump:

Sessions with Ephemerals (0):

[root@localhost bin]#

conf 查看服务器配置

[root@localhost bin]# echo conf | nc 192.168.0.68 2181

clientPort=2181

dataDir=/usr/home/zookeeper-3.4.11/data/version-2

dataLogDir=/usr/home/zookeeper-3.4.11/data/version-2

tickTime=2000

maxClientCnxns=60

minSessionTimeout=4000

maxSessionTimeout=40000

serverId=2

initLimit=10

syncLimit=5

electionAlg=3

electionPort=3888

quorumPort=2888

peerType=0

[root@localhost bin]#

cons 显示连接到服务端的信息

[root@localhost bin]# echo cons | nc 192.168.0.68 2181

/192.168.0.68:493540

[root@localhost bin]#

envi 显示环境变量信息

[root@localhost bin]# echo envi | nc 192.168.0.68 2181

Environment:

zookeeper.version=3.4.11-37e277162d567b55a07d1755f0b31c32e93c01a0, built on 11/01/2017 18:06 GMT

host.name=localhost

java.version=1.8.0_111

java.vendor=Oracle Corporation

最后

为什么我不完全主张自学?
平台上的大牛基本上都有很多年的工作经验了,你有没有想过之前行业的门槛是什么样的,现在行业门槛是什么样的?以前企业对于程序员能力要求没有这么高,甚至十多年前你只要会写个“Hello World”,你都可以入门这个行业,所以以前要入门是完全可以入门的。
②现在也有一些优秀的年轻大牛,他们或许也是自学成才,但是他们一定是具备优秀的学习能力,优秀的自我管理能力(时间管理,静心坚持等方面)以及善于发现问题并总结问题。
如果说你认为你的目标十分明确,能做到第②点所说的几个点,以目前的市场来看,你才真正的适合去自学。

除此之外,对于绝大部分人来说,报班一定是最好的一种快速成长的方式。但是有个问题,现在市场上的培训机构质量参差不齐,如果你没有找准一个好的培训班,完全是浪费精力,时间以及金钱,这个需要自己去甄别选择。

我个人建议线上比线下的性价比更高,线下培训价格基本上没2W是下不来的,线上教育现在比较成熟了,此次疫情期间,学生基本上都感受过线上的学习模式。相比线下而言,线上的优势以我的了解主要是以下几个方面:
①价格:线上的价格基本上是线下的一半;
②老师:相对而言线上教育的师资力量比线下更强大也更加丰富,资源更好协调;
③时间:学习时间相对而言更自由,不用裸辞学习,适合边学边工作,降低生活压力;
④课程:从课程内容来说,确实要比线下讲的更加深入。

应该学哪些技术才能达到企业的要求?(下图总结)

加入社区:https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0
68.0.68 2181

Environment:

zookeeper.version=3.4.11-37e277162d567b55a07d1755f0b31c32e93c01a0, built on 11/01/2017 18:06 GMT

host.name=localhost

java.version=1.8.0_111

java.vendor=Oracle Corporation

最后

为什么我不完全主张自学?
平台上的大牛基本上都有很多年的工作经验了,你有没有想过之前行业的门槛是什么样的,现在行业门槛是什么样的?以前企业对于程序员能力要求没有这么高,甚至十多年前你只要会写个“Hello World”,你都可以入门这个行业,所以以前要入门是完全可以入门的。
②现在也有一些优秀的年轻大牛,他们或许也是自学成才,但是他们一定是具备优秀的学习能力,优秀的自我管理能力(时间管理,静心坚持等方面)以及善于发现问题并总结问题。
如果说你认为你的目标十分明确,能做到第②点所说的几个点,以目前的市场来看,你才真正的适合去自学。

除此之外,对于绝大部分人来说,报班一定是最好的一种快速成长的方式。但是有个问题,现在市场上的培训机构质量参差不齐,如果你没有找准一个好的培训班,完全是浪费精力,时间以及金钱,这个需要自己去甄别选择。

我个人建议线上比线下的性价比更高,线下培训价格基本上没2W是下不来的,线上教育现在比较成熟了,此次疫情期间,学生基本上都感受过线上的学习模式。相比线下而言,线上的优势以我的了解主要是以下几个方面:
①价格:线上的价格基本上是线下的一半;
②老师:相对而言线上教育的师资力量比线下更强大也更加丰富,资源更好协调;
③时间:学习时间相对而言更自由,不用裸辞学习,适合边学边工作,降低生活压力;
④课程:从课程内容来说,确实要比线下讲的更加深入。

应该学哪些技术才能达到企业的要求?(下图总结)

[外链图片转存中…(img-IdgXRmWt-1725513364435)]

[外链图片转存中…(img-YL6WCvtd-1725513364436)]

加入社区:https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0


本文转载自: https://blog.csdn.net/2401_86963643/article/details/141928389
版权归原作者 2401_86963643 所有, 如有侵权,请联系我们删除。

“Zookeeper基础命令操作”的评论:

还没有评论