# 要在 KV 存储中为名为“config/buubiu/serviceconfig”的键插入值“test” $ consul kv put -http-addr="http://127.0.0.1:8500" -token="p2BE1AtpwPbrxZdC6k+eXA==" config/buubiu/serviceconfig "test" Success! Data written to: config/buubiu/serviceconfig
# 或者放到前面,通过指定符号-从标准输入读取值 $ echo"test" | consul kv put -http-addr="http://127.0.0.1:8500" -token="p2BE1AtpwPbrxZdC6k+eXA==" config/buubiu/serviceconfig -
# 或者监听键盘录入 $ consul kv put -http-addr="http://127.0.0.1:8500" -token="p2BE1AtpwPbrxZdC6k+eXA==" config/buubiu/serviceconfig - hello world buubiu 我是谁 <CTRL+D> Success! Data written to: config/buubiu/serviceconfig
# docker exec 执行以上命令 # 要在 KV 存储中为名为“config/buubiu/serviceconfig”的键插入值“test” $ docker exec -i consul consul kv put -http-addr="http://127.0.0.1:8500" -token="p2BE1AtpwPbrxZdC6k+eXA==" config/buubiu/serviceconfig "test" Success! Data written to: config/buubiu/serviceconfig
# 或者放到前面,通过指定符号-从标准输入读取值 $ echo"test" | docker exec -i consul consul kv put -http-addr="http://127.0.0.1:8500" -token="p2BE1AtpwPbrxZdC6k+eXA==" config/buubiu/serviceconfig -
# 或者监听键盘录入 $ docker exec -i consul consul kv put -http-addr="http://127.0.0.1:8500" -token="p2BE1AtpwPbrxZdC6k+eXA==" config/buubiu/serviceconfig - hello world buubiu 我是谁 <CTRL+D> Success! Data written to: config/buubiu/serviceconfig
Base64编码的值
如果-base64设置了标志,则给定数据将在写入之前进行 Base64 解码:
1 2 3 4 5 6
$ consul kv put -base64 -http-addr="http://127.0.0.1:8500" -token="p2BE1AtpwPbrxZdC6k+eXA==" config/buubiu/serviceconfig "c2VydmljZS5wb3J0OiA4MDgwCmFwcGxpY2F0aW9uOgoJbmFtZTogZGVtbw==" Success! Data written to: config/buubiu/serviceconfig
# docker exec $ docker exec -i consul consul kv put -base64 -http-addr="http://127.0.0.1:8500" -token="p2BE1AtpwPbrxZdC6k+eXA==" config/buubiu/serviceconfig "c2VydmljZS5wb3J0OiA4MDgwCmFwcGxpY2F0aW9uOgoJbmFtZTogZGVtbw==" Success! Data written to: config/buubiu/serviceconfig
较长字符
不推荐,格式可能会出现问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14
$ consul kv put -http-addr="http://127.0.0.1:8500" -token="p2BE1AtpwPbrxZdC6k+eXA==" config/buubiu/serviceconfig - <<EOF service.port: 8080 application: name: demo EOF Success! Data written to: config/buubiu/serviceconfig
# docker exec $ docker exec -i consul consul kv put -http-addr="http://127.0.0.1:8500" -token="p2BE1AtpwPbrxZdC6k+eXA==" config/buubiu/serviceconfig - <<EOF service.port: 8080 application: name: demo EOF Success! Data written to: config/buubiu/serviceconfig
读取文件
也可以通过 有符号 @的标识来读取文件,支持 JSON YAML YML
文件示例:
config_buubiu_serviceconfig.yml
1 2 3
service.port:8080 application: name:demo
1 2 3 4
$ consul kv put -http-addr="http://127.0.0.1:8500" -token="p2BE1AtpwPbrxZdC6k+eXA==" config/buubiu/serviceconfig @config_buubiu_serviceconfig.yml Success! Data written to: config/buubiu/serviceconfig
# docker exec不支持
扩展
导入文件夹下面所有yml文件
1
$ for i in $(ls *); do var=$i; filename=${var%*.yml}; cat${filename}.yml | docker exec -i consul consul kv put -http-addr="http://127.0.0.1:8500" -token="p2BE1AtpwPbrxZdC6k+eXA==" config/${filename}/serviceconfig -; done