key/value做用web
除了提供服务发现和综合健康检查,Consul还提供了一个易于使用的键/值存储。这能够用来保存动态配置,协助服务协调,创建领导人选举,并启用其余开发人员能够想构建的任何其余内容。redis
有两种方法可使用:经过HTTP API和经过CLI API。spring
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv get redis/config/minconns Error! No key exists at: redis/config/minconns
你将看到没有结果返回,因为KV存储中没有该键返回了一个错误,接下来咱们将插入或”put”一个值到KV存储中。json
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv put redis/config/minconns 1 Success! Data written to: redis/config/minconns
如今再次查询该键你将看到以下结果:bootstrap
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv get redis/config/minconns 1
Consul保留额外的元数据在该字段,你可使用-detailed标志检索详细信息:windows
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv get -detailed redis/config/minconns CreateIndex 74 Flags 0 Key redis/config/minconns LockIndex 0 ModifyIndex 74 Session - Value 1
在web UI上能够看到用CLI API建立的keyapp
在web UI上建立一个“duan”的key:curl
再经过CLI API查询结果:测试
设置值的时候,还可使用-flags标志
- -flags=<uint>
Unsigned integer value to assign to this key-value pair. This value is not read by Consul, so clients can use this value however makes sense for their use case. The default value is 0 (no flags).ui
flags用来作客户端自定义标志,consul并不使用它,你能够在你本身的程序中随便定义
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv put -flags=42 redis/config/users/admin abcd1234 Success! Data written to: redis/config/users/admin
设置flag值为42,想设置成什么就设置成什么.全部的键都支持设置一个64位的整型值。
使用-recurse选项能够列出KV存储中全部keys,返回的结果将按照字母排序。
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv get -recurse redis/config/minconns:1 redis/config/users/admin:abcd1234
使用delete命令删除KV存储中指定的key。
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv delete redis/config/minconns Success! Deleted key: redis/config/minconns
还可使用recurse选项递归选项删除含某个前缀的全部keys:
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv delete -recurse redis Success! Deleted keys with prefix: redis
若是要更新一个存在键的值,能够put一个新值在一样的路径上。
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv put foo bar Success! Data written to: foo D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv get foo bar D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv put foo zip Success! Data written to: foo
Consul可使用Check_And_Set提供原子键更新操做。执行CAS操做时需指定-cas标志。至于什么是CAS,请自行百度吧
- -modify-index=<uint>
Unsigned integer representing the ModifyIndex of the key. This is used in combination with the -cas flag.
首先查询foo这个key的详细信息
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv get -detailed foo CreateIndex 131 Flags 0 Key foo LockIndex 0 ModifyIndex 133 Session - Value zip
看到foo的索引编号ModifyIndex是133。而后使用CAS操做的方式来修改它
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv put -cas -modify-index=133 foo bar Success! Data written to: foo
修改为功,再查询
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv get -detailed foo CreateIndex 131 Flags 0 Key foo LockIndex 0 ModifyIndex 141 Session - Value bar
ModifyIndex变成141了。依然使用上面那个修改命令试试
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv put -cas -modify-index=133 foo bar Error! Did not write to foo: CAS failed
失败了。缘由是第一次CAS操做成功,由于ModifyIndex的值是141,咱们输入的也是-modify-index=133。
第二次操做失败,ModifyIndex已经变成141了,咱们还用-modify-index=133,Check_And_SetS中的Check这步就失败了,不会再Set了。
2.一、查看所有key/value http://127.0.0.1:8500/v1/kv/?recurse
说明:
2.二、添加key/value
说明:flags--用于为任意一个KV添加一个有意义的metadata。
注意:上边的这个就是有问题的,必定要注意是flags而非flag。
2.三、查看单个key/value
说明:value是test的base64编码(使用base64编码是为了容许非UTF-8的字符)
2.四、修改key/value
cas的值若是与ModifyIndex相等,则修改为功,若不相等,则修改失败。
2.五、删除key/value
2.5.一、删除单一KV
2.5.二、删除必定范围的KV(指定前缀范围内的KV)
说明:
因为consul自带kv存储,彻底能够取代config server。
步骤以下:
1、先添加jar依赖
//compile 'org.springframework.cloud:spring-cloud-starter-config' compile 'org.springframework.cloud:spring-cloud-starter-consul-config'
以前config server的依赖去掉,换成consul-config的依赖便可。
2、修改bootstrap.yml文件
1 spring: 2 ... 3 cloud: 4 consul: 5 host: 127.0.0.1 6 port: 8500 7 discovery: 8 tags: version=1.0,author=yjmyzz 9 healthCheckPath: /info.json 10 healthCheckInterval: 5s 11 instanceId: ${spring.application.name}:${spring.cloud.client.ipAddress} 12 enabled: true 13 config: 14 enabled: true 15 format: YAML 16 prefix: config 17 defaultContext: application 18 profileSeparator: ',' 19 data-key: data 20 # config: 21 # label: dev 22 # discovery: 23 # enabled: true 24 # service-id: my-config-server 25 # fail-fast: true 26 # retry: 27 # max-interval: 1500 28 # max-attempts: 5 29 # multiplier: 1.2
关键是13-19行,解释一下:
15行 format:YAML 表示consul中的key-value中的value内容,采用YAML格式
16行 prefix: config 表示consul用于存储配置的文件夹根目录名为config
17行 defaultContext: application 表示配置文件对应的应用名称(eg: 你的服务若是打算取名为myApp,则这里的application就要换成myApp)
18行 profileSeparator: ',' 表示若是有多个profile(eg: 开发环境dev,测试环境test...) ,则key名中的profile与defaultContext之间,用什么分隔符来表示(这里有点费解,后面还会详细解释)
19行 data-key: data 表示最后一层节点的key值名称,通常默认为data
3、consul中建立kv配置节点
不少文章,包括官方文档这一步都讲得不明不白,关键是 节点名称的命名规则,要与bootstrap.yml中的配置同样,好比咱们要建立一个test环境的配置,key名能够取为:
config/application,test/data
这里每个部分,都要与上一步bootstrap.yml中的一致,上图中5个剪头所指,你们结合上一步中15-19行的解释体会一下。
而后Value值的部分,把配置内容按yml格式填进去就行:
tips: 平时开发时,通常使用consul dev模式,开发模式下kv存储不会持久化存储,全在内存中(重启consul就丢了!),因此通常建议yml配置文件内容,在项目中单独存一个文件,启动调试时,直接把配置文件内容贴到Value框里便可。
好了,如今你能够试着启动下,顺利的话,应该就能够了,是否是很简单,关键还省掉了config server的部署,帮公司省了机器,别忘了让领导给你加绩效哦^_^
若是但愿用代码的方式来读/写 KV存储,能够用下面的方式:
读:
curl http://localhost:8500/v1/kv/config/application,dev/data?raw=true