存入值
我们先存入一些值,然后再取回
创建用 PUT 方法
代码语言:javascript复制[root@h104 consul]# curl -X PUT -d 'soft.dog' http://localhost:8500/v1/kv/web/key1
true[root@h104 consul]# curl -X PUT -d 'soft.dog' http://localhost:8500/v1/kv/web/key2?flags=42
true[root@h104 consul]# curl -X PUT -d 'soft.dog' http://localhost:8500/v1/kv/web/sub/key3
true[root@h104 consul]# curl http://localhost:8500/v1/kv/?recurse
[{"LockIndex":0,"Key":"web/key1","Flags":0,"Value":"c29mdC5kb2c=","CreateIndex":909,"ModifyIndex":909},{"LockIndex":0,"Key":"web/key2","Flags":42,"Value":"c29mdC5kb2c=","CreateIndex":912,"ModifyIndex":912},{"LockIndex":0,"Key":"web/sub/key3","Flags":0,"Value":"c29mdC5kb2c=","CreateIndex":917,"ModifyIndex":917}][root@h104 consul]#
[root@h104 consul]#
[root@h104 consul]#
查询值
查询用 GET 方法
?recurse
参数是递归返回所有KV的意思, 如果要单独返回指定值可以使用指定key的方式
[root@h104 consul]# curl http://localhost:8500/v1/kv/web/key2
[{"LockIndex":0,"Key":"web/key2","Flags":42,"Value":"c29mdC5kb2c=","CreateIndex":912,"ModifyIndex":912}][root@h104 consul]#
[root@h104 consul]#
删除值
删除用 DELETE 方法
代码语言:javascript复制[root@h104 consul]# curl -X DELETE http://localhost:8500/v1/kv/web/sub?recurse
true[root@h104 consul]# curl http://localhost:8500/v1/kv/web?recurse
[{"LockIndex":0,"Key":"web/key1","Flags":0,"Value":"c29mdC5kb2c=","CreateIndex":909,"ModifyIndex":909},{"LockIndex":0,"Key":"web/key2","Flags":42,"Value":"c29mdC5kb2c=","CreateIndex":912,"ModifyIndex":912}][root@h104 consul]#
[root@h104 consul]#
更新值
更新和存值一样使用 PUT 方法,只是提供一个与原值不同的内容就可以了
代码语言:javascript复制[root@h104 consul]# curl http://localhost:8500/v1/kv/web/key1
[{"LockIndex":0,"Key":"web/key1","Flags":0,"Value":"c29mdC5kb2c=","CreateIndex":909,"ModifyIndex":909}][root@h104 consul]#
[root@h104 consul]#
[root@h104 consul]# curl -X PUT -d 'great' http://localhost:8500/v1/kv/web/key1
true[root@h104 consul]#
[root@h104 consul]# curl http://localhost:8500/v1/kv/web/key1
[{"LockIndex":0,"Key":"web/key1","Flags":0,"Value":"Z3JlYXQ=","CreateIndex":909,"ModifyIndex":1000}][root@h104 consul]#
[root@h104 consul]#
ModifyIndex 会增加
条件更新
也就是检查更新, Check-And-Set , 当 cas 指定的值与 ModifyIndex 相等时,才能成功更新,否则更新失败
代码语言:javascript复制[root@h104 consul]# curl http://localhost:8500/v1/kv/web/key1
[{"LockIndex":0,"Key":"web/key1","Flags":0,"Value":"Z3JlYXQ=","CreateIndex":909,"ModifyIndex":1061}][root@h104 consul]#
[root@h104 consul]#
[root@h104 consul]# curl -X PUT -d 'great' http://localhost:8500/v1/kv/web/key1?cas=1061
true[root@h104 consul]#
[root@h104 consul]# curl -X PUT -d 'great' http://localhost:8500/v1/kv/web/key1?cas=1061
false[root@h104 consul]#
[root@h104 consul]# curl http://localhost:8500/v1/kv/web/key1
[{"LockIndex":0,"Key":"web/key1","Flags":0,"Value":"Z3JlYXQ=","CreateIndex":909,"ModifyIndex":1076}][root@h104 consul]#
[root@h104 consul]#