etcd 基础8

2022-01-20 10:41:50 浏览数 (1)

创建序列

因为是使用的etcd序列,所以确保了有序性

代码语言:javascript复制
[root@docker ~]# curl http://127.0.0.1:2379/v2/keys/queue -XPOST -d value=Job1
{"action":"create","node":{"key":"/queue/00000000000000000055","value":"Job1","modifiedIndex":55,"createdIndex":55}}
[root@docker ~]# curl http://127.0.0.1:2379/v2/keys/queue -XPOST -d value=Job2
{"action":"create","node":{"key":"/queue/00000000000000000056","value":"Job2","modifiedIndex":56,"createdIndex":56}}
[root@docker ~]# curl http://127.0.0.1:2379/v2/keys/queue -XPOST -d value=Job3
{"action":"create","node":{"key":"/queue/00000000000000000057","value":"Job3","modifiedIndex":57,"createdIndex":57}}
[root@docker ~]#

读取序列

代码语言:javascript复制
[root@docker ~]# curl -s 'http://127.0.0.1:2379/v2/keys/queue?recursive=true&sorted=true'
{"action":"get","node":{"key":"/queue","dir":true,"nodes":[{"key":"/queue/00000000000000000055","value":"Job1","modifiedIndex":55,"createdIndex":55},{"key":"/queue/00000000000000000056","value":"Job2","modifiedIndex":56,"createdIndex":56},{"key":"/queue/00000000000000000057","value":"Job3","modifiedIndex":57,"createdIndex":57}],"modifiedIndex":55,"createdIndex":55}}
[root@docker ~]# 

设定目录过期时间

目录也可以像key一样设定过期

代码语言:javascript复制
[root@docker ~]# date  %s ;curl http://127.0.0.1:2379/v2/keys/testdir -XPUT -d ttl=10 -d dir=true
1454394081
{"action":"set","node":{"key":"/testdir","dir":true,"expiration":"2016-02-02T06:21:31.631806205Z","ttl":10,"modifiedIndex":81,"createdIndex":81}}
[root@docker ~]# date  %s ;curl http://127.0.0.1:2379/v2/keys/testdir 
1454394086
{"action":"get","node":{"key":"/testdir","dir":true,"expiration":"2016-02-02T06:21:31.631806205Z","ttl":5,"modifiedIndex":81,"createdIndex":81}}
[root@docker ~]# date  %s ;curl http://127.0.0.1:2379/v2/keys/testdir 
1454394088
{"action":"get","node":{"key":"/testdir","dir":true,"expiration":"2016-02-02T06:21:31.631806205Z","ttl":3,"modifiedIndex":81,"createdIndex":81}}
[root@docker ~]# date  %s ;curl http://127.0.0.1:2379/v2/keys/testdir 
1454394094
{"errorCode":100,"message":"Key not found","cause":"/testdir","index":82}
[root@docker ~]# 

0 人点赞