基于http的API非常易用,这个指导将展示etcdctl和curl例子, 注意必须为curl加上-L标志, 因为etcd会自动透明地将写操作重定向到etcd集群的master。
设定key : message 的值为: Hello, 如下:
$ etcdctl set /message Hello
Hello
$ curl -L -X PUT http://127.0.0.1:2379/v2/keys/message -d value="Hello"
{"action":"set","node":{"key":"/message","value":"Hello","modifiedIndex":4,"createdIndex":4}}
读取key: message的值
$ etcdctl get /message
Hello
$ curl -L http://127.0.0.1:2379/v2/keys/message
{"action":"get","node":{"key":"/message","value":"Hello","modifiedIndex":4,"createdIndex":4}}
删除key:
$ etcdctl rm /message
$ curl -L -X DELETE http://127.0.0.1:2379/v2/keys/message
{"action":"delete","node":{"key":"/message","modifiedIndex":19,"createdIndex":4}}
$ etcdctl mkdir /foo-service
Cannot print key [/foo-service: Is a directory]
$ etcdctl set /foo-service/container1 localhost:1111
localhost:1111
$ curl -L -X PUT http://127.0.0.1:2379/v2/keys/foo-service/container1 -d value="localhost:1111"
{"action":"set","node":{"key":"/foo-service/container1","value":"localhost:1111","modifiedIndex":17,"createdIndex":17}}
读取目录:
$ etcdctl ls /foo-service
/foo-service/container1
$ curl -L http://127.0.0.1:2379/v2/keys/foo-service
{"action":"get","node":{"key":"/foo-service","dir":true,"nodes":[{"key":"/foo-service/container1","value":"localhost:1111","modifiedIndex":17,"createdIndex":17}],"modifiedIndex":17,"createdIndex":17}}
Many events can trigger a change, including a new, updated, deleted or expired key.
当此目录有变化时,如新建,更新,删除,过期,监视者将得到通知
$ etcdctl watch --recursive /foo-service
$ curl -L http://127.0.0.1:2379/v2/keys/foo-service?wait=true\&recursive=true
打开另一个shell:
$ etcdctl set /foo-service/container2 localhost:2222
localhost:2222
$ curl -L -X PUT http://127.0.0.1:2379/v2/keys/foo-service/container2 -d value="localhost:2222"
{"action":"set","node":{"key":"/foo-service/container2","value":"localhost:2222","modifiedIndex":23,"createdIndex":23}}
这样当目录变化时,你就可以看到通知,当然现实的系统通常会更新自己本地的配置缓存。
$ etcdctl watch --recursive /foo-service
localhost:2222
$ curl -L http://127.0.0.1:2379/v2/keys/foo-service?wait=true\&recursive=true
{"action":"set","node":{"key":"/foo-service/container2","value":"localhost:2222","modifiedIndex":23,"createdIndex":23}}
监视目录并触发执行一段代码
$ etcdctl exec-watch --recursive /foo-service -- sh -c 'echo "\"$ETCD_WATCH_KEY\" key was updated to \"$ETCD_WATCH_VALUE\" value by \"$ETCD_WATCH_ACTION\" action"'
打开一个新shell
$ etcdctl set /foo-service/container3 localhost:2222
localhost:2222
在第一个shell中将看到变化处发的执行结果 . 可以使用环境变量 $ETCD_WATCH_* , 这些环境变量由
etcdctl设定
.
$ etcdctl exec-watch --recursive /foo-service -- sh -c 'echo "\"$ETCD_WATCH_KEY\" key was updated to \"$ETCD_WATCH_VALUE\" value by \"$ETCD_WATCH_ACTION\" action"'
"/foo-service/container3" key was updated to "localhost:2222" value by "set" action
如果之前的值与"Hello"匹配, 则用新值““Hi””替换之
$ etcdctl set /message "Hi" --swap-with-value "Hello"
Hi
$ curl -L -X PUT http://127.0.0.1:2379/v2/keys/message?prevValue=Hello -d value=Hi
{"action":"compareAndSwap","node":{"key":"/message","value":"Hi","modifiedIndex":28,"createdIndex":27}}
设定目录过期时间, 单位为秒
$ etcdctl set /foo "Expiring Soon" --ttl 20
Expiring Soon
curl的响应中包括:过期的绝对时间点,以及相对的时间差。
$ curl -L -X PUT http://127.0.0.1:2379/v2/keys/foo?ttl=20 -d value=bar
{"action":"set","node":{"key":"/foo","value":"bar","expiration":"2014-02-10T19:54:49.357382223Z","ttl":20,"modifiedIndex":31,"createdIndex":31}}
如果你去读取了个过期的目录,将得到100错误码
$ etcdctl get /foo
Error: 100: Key not found (/foo) [32]
$ curl -L http://127.0.0.1:2379/v2/keys/foo
{"errorCode":100,"message":"Key not found","cause":"/foo","index":32}
注意: 此文章只是我个人笔记, 如有错漏,请一定指正, 共同学习, 我的邮箱: [email protected]