如果需要使用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}}