Consul - KV

最后更新:2019-04-05

1. 键/值存储

Consul提供了非常容易使用的键/值对存储。它能被用于存储动态配置信息,帮助服务协作,建构leader选举机制,以及开发者可以想到的建构任何其它的东西。

有两种与Consul的KV存储交互的方式:HTTP API或 Consul KV命令行

查询redis/config/minconns的值

$ consul kv get redis/config/minconns
Error! No key exists at: redis/config/minconns

插入数据

$ consul kv put redis/config/minconns 1
Success! Data written to: redis/config/minconns
$ consul kv put redis/config/maxconns 25
Success! Data written to: redis/config/maxconns
$ consul kv put -flags=42 redis/config/users/admin abcd1234
Success! Data written to: redis/config/users/admin

再次查询

$ consul kv get redis/config/minconns
1

Consul同时维护了K/V对应的元数据,可以通过-detailed获取

$ consul kv get -detailed redis/config/minconns
CreateIndex      502
Flags            0
Key              redis/config/minconns
LockIndex        0
ModifyIndex      514
Session          -
Value            1

对于redis/config/users/admin,我们设置了一个42的flag属性,所有键都支持设置64位整数标记值。这个不是由Consul内部使用,但它可以用于客户端添加任何有意义的元数据

可以使用recurse选项查询所有的K/V,结果将按字典顺序返回

$ consul kv get -recurse
redis/config/maxconns:25
redis/config/minconns:1
redis/config/users/admin:abcd1234

delete用于删除K/V

$ consul kv delete redis/config/minconns
Success! Deleted key: redis/config/minconns

使用recurse选项可以递归删除某个前缀

$ consul kv delete -recurse redis
Success! Deleted keys with prefix: redis 

put用于更新

$ consul kv put foo bar
Success! Data written to: foo
$ consul kv get foo
bar
$ consul kv put foo zip
Success! Data written to: foo
$ consul kv get foo
zip

Consul提供了通过CAS操作来对K/V的原子更新,使用-cas选项

$ consul kv get -detailed foo
CreateIndex      571
Flags            0
Key              foo
LockIndex        0
ModifyIndex      573
Session          -
Value            zip
$ consul kv put -cas -modify-index=573 foo bar
Success! Data written to: foo
$ consul kv put -cas -modify-index=573 foo bar
Error! Did not write to foo: CAS failed # 2. HTTP 
  • 查看KV
$ consul kv put  redis/config/minconns 1
Success! Data written to: redis/config/minconns
$ curl http://127.0.0.1:8500/v1/kv/redis/config/minconns
[
    {
        "LockIndex": 0,
        "Key": "redis/config/minconns",
        "Flags": 0,
        "Value": "MQ==",
        "CreateIndex": 4063,
        "ModifyIndex": 4063
    }
]
$ consul kv put  redis/config/maxconns 10
Success! Data written to: redis/config/maxconns
$ curl http://127.0.0.1:8500/v1/kv/redis?recurse
[
    {
        "LockIndex": 0,
        "Key": "redis/config/maxconns",
        "Flags": 0,
        "Value": "MTA=",
        "CreateIndex": 4065,
        "ModifyIndex": 4065
    },
    {
        "LockIndex": 0,
        "Key": "redis/config/minconns",
        "Flags": 0,
        "Value": "MQ==",
        "CreateIndex": 4063,
        "ModifyIndex": 4063
    }
]
  • 创建/更新KV
$ curl -X PUT -d 'bar' http://127.0.0.1:8500/v1/kv/foo
true
$ curl http://127.0.0.1:8500/v1/kv/foo
[
    {
        "LockIndex": 0,
        "Key": "foo",
        "Flags": 0,
        "Value": "YmFy",
        "CreateIndex": 4120,
        "ModifyIndex": 4120
    }
]

  • CAS
$ curl -X PUT -d 'baz' http://127.0.0.1:8500/v1/kv/foo?cas=4000
false
$ curl -X PUT -d 'baz' http://127.0.0.1:8500/v1/kv/foo?cas=4120
true
$ curl http://127.0.0.1:8500/v1/kv/foo
[
    {
        "LockIndex": 0,
        "Key": "foo",
        "Flags": 0,
        "Value": "YmF6",
        "CreateIndex": 4120,
        "ModifyIndex": 4128
    }
]

  • 基于SESSION
# curl -X PUT -d 'baz' http://127.0.0.1:8500/v1/kv/foo?acquire=92906932-e67e-da29-7255-0411fa4e1141
invalid session "92906932-e67e-da29-7255-0411fa4e1141"
$ curl -X PUT -d 'baz' http://127.0.0.1:8500/v1/kv/foo?acquire=92906932-e67e-da29-7255-0411fa4e1142
true
$ curl http://127.0.0.1:8500/v1/kv/foo
[
    {
        "LockIndex": 1,
        "Key": "foo",
        "Flags": 0,
        "Value": "YmF6",
        "Session": "92906932-e67e-da29-7255-0411fa4e1142",
        "CreateIndex": 4120,
        "ModifyIndex": 4139
    }
]
$ curl -X PUT -d 'baz' http://127.0.0.1:8500/v1/kv/foo?release=92906932-e67e-da29-7255-0411fa4e1141
false
$ curl -X PUT -d 'baz' http://127.0.0.1:8500/v1/kv/foo?release=92906932-e67e-da29-7255-0411fa4e1142
true
  • 删除KEY
$ curl -X DELETE http://127.0.0.1:8500/v1/kv/foo?cas=4000
false
$ curl -X DELETE http://127.0.0.1:8500/v1/kv/foo?cas=4143
true
$ curl -X DELETE http://127.0.0.1:8500/v1/kv/redis?recurse
true
Edgar

Edgar
一个略懂Java的小菜比