etcd api常用操作

如果需要使用v2 version api,启动etcd时候需要加入“ETCD_ENABLE_V2=true”参数,否则会报错“404 page not found”

获取etcd信息

版本信息

# curl -L http://172.16.101.55:2379/version
{"etcdserver":"3.4.1","etcdcluster":"3.4.0"}

健康状态

# curl -L http://172.16.101.55:2379/health
{"health":"true"}

key操作

新建key

新建key值为message value为“Hello world”

# curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello world"
{"action":"set","node":{"key":"/message","value":"Hello world","modifiedIndex":18,"createdIndex":18}}

查看key

# curl http://127.0.0.1:2379/v2/keys/message
{"action":"get","node":{"key":"/message","value":"Hello world","modifiedIndex":18,"createdIndex":18}}

删除key

# curl http://127.0.0.1:2379/v2/keys/message -XDELETE
{"action":"delete","node":{"key":"/message","modifiedIndex":19,"createdIndex":18},"prevNode":{"key":"/message","value":"Hello world","modifiedIndex":18,"createdIndex":18}}

新建带有TTL的key

# curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello world" -d ttl=30
{"action":"set","node":{"key":"/message","value":"Hello world","expiration":"2019-09-29T08:08:10.674930705Z","ttl":30,"modifiedIndex":20,"createdIndex":20}}
# curl http://127.0.0.1:2379/v2/keys/message
{"action":"get","node":{"key":"/message","value":"Hello world","expiration":"2019-09-29T08:08:10.674930705Z","ttl":2,"modifiedIndex":20,"createdIndex":20}}

取消key的TTL

# curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello world" -d ttl=30
{"action":"set","node":{"key":"/message","value":"Hello world","expiration":"2019-09-29T08:10:23.220573683Z","ttl":30,"modifiedIndex":22,"createdIndex":22}}
# curl http://127.0.0.1:2379/v2/keys/message
{"action":"get","node":{"key":"/message","value":"Hello world","expiration":"2019-09-29T08:10:23.220573683Z","ttl":20,"modifiedIndex":22,"createdIndex":22}}
# curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello world" -d ttl= -d prevExist=true
{"action":"update","node":{"key":"/message","value":"Hello world","modifiedIndex":23,"createdIndex":22},"prevNode":{"key":"/message","value":"Hello world","expiration":"2019-09-29T08:10:23.220573683Z","ttl":16,"modifiedIndex":22,"createdIndex":22}}
# curl http://127.0.0.1:2379/v2/keys/message
{"action":"get","node":{"key":"/message","value":"Hello world","modifiedIndex":23,"createdIndex":22}}

重置key的TTL

# curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello world" -d ttl=30
{"action":"set","node":{"key":"/message","value":"Hello world","expiration":"2019-09-29T08:15:01.34698273Z","ttl":30,"modifiedIndex":25,"createdIndex":25}}
# curl http://127.0.0.1:2379/v2/keys/message
{"action":"get","node":{"key":"/message","value":"Hello world","expiration":"2019-09-29T08:15:01.34698273Z","ttl":16,"modifiedIndex":25,"createdIndex":25}}
# curl http://127.0.0.1:2379/v2/keys/message -XPUT -d ttl=30 -d refresh=true -d prevExist=true
{"action":"update","node":{"key":"/message","value":"Hello world","expiration":"2019-09-29T08:15:29.569276199Z","ttl":30,"modifiedIndex":26,"createdIndex":25},"prevNode":{"key":"/message","value":"Hello world","expiration":"2019-09-29T08:15:01.34698273Z","ttl":2,"modifiedIndex":25,"createdIndex":25}}
# curl http://127.0.0.1:2379/v2/keys/message
{"action":"get","node":{"key":"/message","value":"Hello world","expiration":"2019-09-29T08:15:29.569276199Z","ttl":27,"modifiedIndex":26,"createdIndex":25}}

新建带有TTL的目录

# curl http://127.0.0.1:2379/v2/keys/dir -d ttl=30 -d dir=true
{"action":"create","node":{"key":"/dir/00000000000000000048","dir":true,"expiration":"2019-10-03T01:40:55.939690704Z","ttl":30,"modifiedIndex":48,"createdIndex":48}} 

在TTL到期前更新该目录的TTL

# curl http://127.0.0.1:2379/v2/keys/dir -XPUT -d ttl=60 -d dir=true -d prevExist=true
{"action":"update","node":{"key":"/dir","dir":true,"expiration":"2019-10-03T01:42:49.310924328Z","ttl":60,"modifiedIndex":50,"createdIndex":48},"prevNode":{"key":"/dir","dir":true,"modifiedIndex":48,"createdIndex":48}}

向该目录插入数据

# curl http://127.0.0.1:2379/v2/keys/dir/message -XPUT -d value="Hello world"
{"action":"set","node":{"key":"/dir/message","value":"Hello world","modifiedIndex":51,"createdIndex":51}}

到期之前该目录的数据和普通数据一样,但是该目录到期后数据会被自动删除

# curl http://127.0.0.1:2379/v2/keys/dir/message
{"action":"get","node":{"key":"/dir/message","value":"Hello world","modifiedIndex":51,"createdIndex":51}}
# curl http://127.0.0.1:2379/v2/keys/dir/message
{"errorCode":100,"message":"Key not found","cause":"/dir","index":52}

自动创建有序的key

注意,下图的/queue为目录,创建方法为POST,不是PUT 

# curl http://127.0.0.1:2379/v2/keys/queue -XPOST -d value=Job1
{"action":"create","node":{"key":"/queue/00000000000000000042","value":"Job1","modifiedIndex":42,"createdIndex":42}}
# curl http://127.0.0.1:2379/v2/keys/queue -XPOST -d value=Job2
{"action":"create","node":{"key":"/queue/00000000000000000043","value":"Job2","modifiedIndex":43,"createdIndex":43}}
# curl http://127.0.0.1:2379/v2/keys/queue -XPOST -d value=Job3
{"action":"create","node":{"key":"/queue/00000000000000000044","value":"Job3","modifiedIndex":44,"createdIndex":44}}
# curl http://127.0.0.1:2379/v2/keys/queue -XPOST -d value=Job4
{"action":"create","node":{"key":"/queue/00000000000000000045","value":"Job4","modifiedIndex":45,"createdIndex":45}}
# curl http://127.0.0.1:2379/v2/keys/queue -XPOST -d value=Job5
{"action":"create","node":{"key":"/queue/00000000000000000046","value":"Job5","modifiedIndex":46,"createdIndex":46}}
# curl http://127.0.0.1:2379/v2/keys/queue -XPOST -d value=Job6
{"action":"create","node":{"key":"/queue/00000000000000000047","value":"Job6","modifiedIndex":47,"createdIndex":47}}

 查看

# curl 'http://127.0.0.1:2379/v2/keys/queue?recursive=true&sorted=true'
{"action":"get","node":{"key":"/queue","dir":true,"nodes":[{"key":"/queue/00000000000000000042","value":"Job1","modifiedIndex":42,"createdIndex":42},{"key":"/queue/00000000000000000043","value":"Job2","modifiedIndex":43,"createdIndex":43},{"key":"/queue/00000000000000000044","value":"Job3","modifiedIndex":44,"createdIndex":44},{"key":"/queue/00000000000000000045","value":"Job4","modifiedIndex":45,"createdIndex":45},{"key":"/queue/00000000000000000046","value":"Job5","modifiedIndex":46,"createdIndex":46},{"key":"/queue/00000000000000000047","value":"Job6","modifiedIndex":47,"createdIndex":47}],"modifiedIndex":42,"createdIndex":42}}

一次性watch

开启一个终端

# curl http://127.0.0.1:2379/v2/keys/message?wait=true

 重新打开二个终端,新建key

# curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello world"
{"action":"set","node":{"key":"/message","value":"Hello world","modifiedIndex":28,"createdIndex":28}}

此时第一个终端显示如下

{"action":"set","node":{"key":"/message","value":"Hello world","modifiedIndex":28,"createdIndex":28}}

再此在第一个终端执行watch

在第二个终端更新key

# curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Nice Day"
{"action":"set","node":{"key":"/message","value":"Nice Day","modifiedIndex":29,"createdIndex":29},"prevNode":{"key":"/message","value":"Hello world","modifiedIndex":28,"createdIndex":28}}

第一个终端显示如下

{"action":"set","node":{"key":"/message","value":"Nice Day","modifiedIndex":29,"createdIndex":29},"prevNode":{"key":"/message","value":"Hello world","modifiedIndex":28,"createdIndex":28}}

 原子CAS操作(Compare And Swap)

CAS操作的基本用途就是创建分布式的锁服务,即选主,仅当客户端提供的条件等于当前etcd的条件时,才会修改一个key的值。当前提供的可以比较的条件有:

  • prevExist: 检查key是否存在。如果prevExist为true, 则这是一个更新请求,如果prevExist的值是false, 这是一个创建请求
  • prevValue:检查key之前的value
  • prevIndex:检查key以前的modifiedIndex

插入一个测试key为foo,值为one

# curl http://127.0.0.1:2379/v2/keys/foo -XPUT -d value=one
{"action":"set","node":{"key":"/foo","value":"one","modifiedIndex":56,"createdIndex":56}}
# curl http://127.0.0.1:2379/v2/keys/foo
{"action":"get","node":{"key":"/foo","value":"one","modifiedIndex":56,"createdIndex":56}}

 插入一个已存在的key并添加参数prevExist=false,因为已经有存在的key

# curl http://127.0.0.1:2379/v2/keys/foo?prevExist=false -XPUT -d value=two
{"errorCode":105,"message":"Key already exists","cause":"/foo","index":56}

将插入条件换成prevValue,即检查key的value值,条件相等就替换,否则就提示条件不匹配

# curl http://127.0.0.1:2379/v2/keys/foo?prevValue=three -XPUT -d value=two
{"errorCode":101,"message":"Compare failed","cause":"[three != one]","index":56}
# curl http://127.0.0.1:2379/v2/keys/foo?prevValue=one -XPUT -d value=two
{"action":"compareAndSwap","node":{"key":"/foo","value":"two","modifiedIndex":57,"createdIndex":56},"prevNode":{"key":"/foo","value":"one","modifiedIndex":56,"createdIndex":56}}
# curl http://127.0.0.1:2379/v2/keys/foo
{"action":"get","node":{"key":"/foo","value":"two","modifiedIndex":57,"createdIndex":56}}
原文地址:https://www.cnblogs.com/ilifeilong/p/11608501.html