1.API常用操作
如果需要使用v2 version api,启动etcd时候需要加入“ETCD_ENABLE_V2=true”参数,否则会报错“404 page not found”
1.1 获取etcd信息
版本信息
bash
# 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/version
{"etcdserver":"3.4.1","etcdcluster":"3.4.0"}
健康状态
bash
# curl -L http://172.16.101.55:2379/health
{"health":"true"}
# curl -L http://172.16.101.55:2379/health
{"health":"true"}
1.2 key操作
新建key
新建key值为message value为“Hello world”
bash
# 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}}
# 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
bash
# curl http://127.0.0.1:2379/v2/keys/message
{"action":"get","node":{"key":"/message","value":"Hello world","modifiedIndex":18,"createdIndex":18}}
# curl http://127.0.0.1:2379/v2/keys/message
{"action":"get","node":{"key":"/message","value":"Hello world","modifiedIndex":18,"createdIndex":18}}
删除key
bash
# curl http://127.0.0.1:2379/v2/keys/message -XDELETE
# curl http://127.0.0.1:2379/v2/keys/message -XDELETE
新建带有TTL的key
bash
#创建
curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello world" -d ttl=30
#查看
curl http://127.0.0.1:2379/v2/keys/message
#创建
curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello world" -d ttl=30
#查看
curl http://127.0.0.1:2379/v2/keys/message
取消key的TTL
bash
# 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}}
# 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
bash
# 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}}
# 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的目录
bash
curl http://127.0.0.1:2379/v2/keys/dir -d ttl=30 -d dir=true
curl http://127.0.0.1:2379/v2/keys/dir -d ttl=30 -d dir=true
- 在TTL到期前更新该目录的TTL
bash
curl http://127.0.0.1:2379/v2/keys/dir -XPUT -d ttl=60 -d dir=true -d prevExist=true
curl http://127.0.0.1:2379/v2/keys/dir -XPUT -d ttl=60 -d dir=true -d prevExist=true
- 向该目录插入数据
bash
curl http://127.0.0.1:2379/v2/keys/dir/message -XPUT -d value="Hello world"
curl http://127.0.0.1:2379/v2/keys/dir/message -XPUT -d value="Hello world"
自动创建有序的key
注意,下图的/queue为目录,创建方法为POST,不是PUT
bash
# 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 -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}}
查看
bash
curl 'http://127.0.0.1:2379/v2/keys/queue?recursive=true&sorted=true'
curl 'http://127.0.0.1:2379/v2/keys/queue?recursive=true&sorted=true'
1.3 监控watch
开启一个终端,监控key 名字
bash
curl http://127.0.0.1:2379/v2/keys/message?wait=true
curl http://127.0.0.1:2379/v2/keys/message?wait=true
重新打开二个终端,新建key
bash
curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello world"
curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello world"