etcd api常用操作
如果需要使用v2 version api,启动etcd时候需要加入“ETCD_ENABLE_V2=true”参数,否则会报错“404 page not found”
获取etcd信息
版本信息
# curl -L http://172.16.101.55:2379/version
{"etcdserver":"3.4.1","etcdcluster":"3.4.0"}
健康状态
# curl -L http://172.16.101.55:2379/health
{"health":"true"}
key操作
新建key
新建key值为message value为“Hello world”
# curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello world"
{"action":"set","node":{"key":"/message","value":"Hello world","modifiedIndex":,"createdIndex":}}
查看key
# curl http://127.0.0.1:2379/v2/keys/message
{"action":"get","node":{"key":"/message","value":"Hello world","modifiedIndex":,"createdIndex":}}
删除key
# curl http://127.0.0.1:2379/v2/keys/message -XDELETE
{"action":"delete","node":{"key":"/message","modifiedIndex":,"createdIndex":},"prevNode":{"key":"/message","value":"Hello world","modifiedIndex":,"createdIndex":}}
新建带有TTL的key
# curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello world" -d ttl=30
{"action":"set","node":{"key":"/message","value":"Hello world","expiration":"2019-09-29T08:08:10.674930705Z","ttl":,"modifiedIndex":,"createdIndex":}}
# curl http://127.0.0.1:2379/v2/keys/message
{"action":"get","node":{"key":"/message","value":"Hello world","expiration":"2019-09-29T08:08:10.674930705Z","ttl":,"modifiedIndex":,"createdIndex":}}
取消key的TTL
# curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello world" -d ttl=30
{"action":"set","node":{"key":"/message","value":"Hello world","expiration":"2019-09-29T08:10:23.220573683Z","ttl":,"modifiedIndex":,"createdIndex":}}
# curl http://127.0.0.1:2379/v2/keys/message
{"action":"get","node":{"key":"/message","value":"Hello world","expiration":"2019-09-29T08:10:23.220573683Z","ttl":,"modifiedIndex":,"createdIndex":}}
# curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello world" -d ttl= -d prevExist=true
{"action":"update","node":{"key":"/message","value":"Hello world","modifiedIndex":,"createdIndex":},"prevNode":{"key":"/message","value":"Hello world","expiration":"2019-09-29T08:10:23.220573683Z","ttl":,"modifiedIndex":,"createdIndex":}}
# curl http://127.0.0.1:2379/v2/keys/message
{"action":"get","node":{"key":"/message","value":"Hello world","modifiedIndex":,"createdIndex":}}
重置key的TTL
# curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello world" -d ttl=30
{"action":"set","node":{"key":"/message","value":"Hello world","expiration":"2019-09-29T08:15:01.34698273Z","ttl":,"modifiedIndex":,"createdIndex":}}
# curl http://127.0.0.1:2379/v2/keys/message
{"action":"get","node":{"key":"/message","value":"Hello world","expiration":"2019-09-29T08:15:01.34698273Z","ttl":,"modifiedIndex":,"createdIndex":}}
# curl http://127.0.0.1:2379/v2/keys/message -XPUT -d ttl=30 -d refresh=true -d prevExist=true
{"action":"update","node":{"key":"/message","value":"Hello world","expiration":"2019-09-29T08:15:29.569276199Z","ttl":,"modifiedIndex":,"createdIndex":},"prevNode":{"key":"/message","value":"Hello world","expiration":"2019-09-29T08:15:01.34698273Z","ttl":,"modifiedIndex":,"createdIndex":}}
# curl http://127.0.0.1:2379/v2/keys/message
{"action":"get","node":{"key":"/message","value":"Hello world","expiration":"2019-09-29T08:15:29.569276199Z","ttl":,"modifiedIndex":,"createdIndex":}}
新建带有TTL的目录
# curl http://127.0.0.1:2379/v2/keys/dir -d ttl=30 -d dir=true
{"action":"create","node":{"key":"/dir/00000000000000000048","dir":true,"expiration":"2019-10-03T01:40:55.939690704Z","ttl":,"modifiedIndex":,"createdIndex":}}
在TTL到期前更新该目录的TTL
# curl http://127.0.0.1:2379/v2/keys/dir -XPUT -d ttl=60 -d dir=true -d prevExist=true
{"action":"update","node":{"key":"/dir","dir":true,"expiration":"2019-10-03T01:42:49.310924328Z","ttl":,"modifiedIndex":,"createdIndex":},"prevNode":{"key":"/dir","dir":true,"modifiedIndex":,"createdIndex":}}
向该目录插入数据
# curl http://127.0.0.1:2379/v2/keys/dir/message -XPUT -d value="Hello world"
{"action":"set","node":{"key":"/dir/message","value":"Hello world","modifiedIndex":,"createdIndex":}}
到期之前该目录的数据和普通数据一样,但是该目录到期后数据会被自动删除
# curl http://127.0.0.1:2379/v2/keys/dir/message
{"action":"get","node":{"key":"/dir/message","value":"Hello world","modifiedIndex":,"createdIndex":}}
# curl http://127.0.0.1:2379/v2/keys/dir/message
{"errorCode":,"message":"Key not found","cause":"/dir","index":}
自动创建有序的key
注意,下图的/queue为目录,创建方法为POST,不是PUT
# curl http://127.0.0.1:2379/v2/keys/queue -XPOST -d value=Job1
{"action":"create","node":{"key":"/queue/00000000000000000042","value":"Job1","modifiedIndex":,"createdIndex":}}
# curl http://127.0.0.1:2379/v2/keys/queue -XPOST -d value=Job2
{"action":"create","node":{"key":"/queue/00000000000000000043","value":"Job2","modifiedIndex":,"createdIndex":}}
# curl http://127.0.0.1:2379/v2/keys/queue -XPOST -d value=Job3
{"action":"create","node":{"key":"/queue/00000000000000000044","value":"Job3","modifiedIndex":,"createdIndex":}}
# curl http://127.0.0.1:2379/v2/keys/queue -XPOST -d value=Job4
{"action":"create","node":{"key":"/queue/00000000000000000045","value":"Job4","modifiedIndex":,"createdIndex":}}
# curl http://127.0.0.1:2379/v2/keys/queue -XPOST -d value=Job5
{"action":"create","node":{"key":"/queue/00000000000000000046","value":"Job5","modifiedIndex":,"createdIndex":}}
# curl http://127.0.0.1:2379/v2/keys/queue -XPOST -d value=Job6
{"action":"create","node":{"key":"/queue/00000000000000000047","value":"Job6","modifiedIndex":,"createdIndex":}}
查看
# curl 'http://127.0.0.1:2379/v2/keys/queue?recursive=true&sorted=true'
{"action":"get","node":{"key":"/queue","dir":true,"nodes":[{"key":"/queue/00000000000000000042","value":"Job1","modifiedIndex":,"createdIndex":},{"key":"/queue/00000000000000000043","value":"Job2","modifiedIndex":,"createdIndex":},{"key":"/queue/00000000000000000044","value":"Job3","modifiedIndex":,"createdIndex":},{"key":"/queue/00000000000000000045","value":"Job4","modifiedIndex":,"createdIndex":},{"key":"/queue/00000000000000000046","value":"Job5","modifiedIndex":,"createdIndex":},{"key":"/queue/00000000000000000047","value":"Job6","modifiedIndex":,"createdIndex":}],"modifiedIndex":,"createdIndex":}}
一次性watch
开启一个终端
# curl http://127.0.0.1:2379/v2/keys/message?wait=true
重新打开二个终端,新建key
# curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello world"
{"action":"set","node":{"key":"/message","value":"Hello world","modifiedIndex":,"createdIndex":}}
此时第一个终端显示如下
{"action":"set","node":{"key":"/message","value":"Hello world","modifiedIndex":,"createdIndex":}}
再此在第一个终端执行watch
在第二个终端更新key
# curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Nice Day"
{"action":"set","node":{"key":"/message","value":"Nice Day","modifiedIndex":,"createdIndex":},"prevNode":{"key":"/message","value":"Hello world","modifiedIndex":,"createdIndex":}}
第一个终端显示如下
{"action":"set","node":{"key":"/message","value":"Nice Day","modifiedIndex":,"createdIndex":},"prevNode":{"key":"/message","value":"Hello world","modifiedIndex":,"createdIndex":}}
原子CAS操作(Compare And Swap)
CAS操作的基本用途就是创建分布式的锁服务,即选主,仅当客户端提供的条件等于当前etcd的条件时,才会修改一个key的值。当前提供的可以比较的条件有:
- prevExist: 检查key是否存在。如果prevExist为true, 则这是一个更新请求,如果prevExist的值是false, 这是一个创建请求
- prevValue:检查key之前的value
- prevIndex:检查key以前的modifiedIndex
插入一个测试key为foo,值为one
# curl http://127.0.0.1:2379/v2/keys/foo -XPUT -d value=one
{"action":"set","node":{"key":"/foo","value":"one","modifiedIndex":,"createdIndex":}}
# curl http://127.0.0.1:2379/v2/keys/foo
{"action":"get","node":{"key":"/foo","value":"one","modifiedIndex":,"createdIndex":}}
插入一个已存在的key并添加参数prevExist=false,因为已经有存在的key
# curl http://127.0.0.1:2379/v2/keys/foo?prevExist=false -XPUT -d value=two
{"errorCode":,"message":"Key already exists","cause":"/foo","index":}
将插入条件换成prevValue,即检查key的value值,条件相等就替换,否则就提示条件不匹配
# curl http://127.0.0.1:2379/v2/keys/foo?prevValue=three -XPUT -d value=two
{"errorCode":,"message":"Compare failed","cause":"[three != one]","index":}
# curl http://127.0.0.1:2379/v2/keys/foo?prevValue=one -XPUT -d value=two
{"action":"compareAndSwap","node":{"key":"/foo","value":"two","modifiedIndex":,"createdIndex":},"prevNode":{"key":"/foo","value":"one","modifiedIndex":,"createdIndex":}}
# curl http://127.0.0.1:2379/v2/keys/foo
{"action":"get","node":{"key":"/foo","value":"two","modifiedIndex":,"createdIndex":}}
etcd api常用操作的更多相关文章
- etcd安装常用操作
etcd安装 etcd 是基于 Raft 的分布式 key-value 存储系统,由 CoreOS 开发,常用于服务发现.共享配置以及并发控制(如 leader 选举.分布式锁等).kubernete ...
- pandas数据分析API常用操作
1.导入数据 df = pd.read_csv( # 该参数为数据在电脑中的路径,可以不填写 filepath_or_buffer='/Users/Weidu/Desktop/sz000002.csv ...
- HDFS Java API 常用操作
package com.luogankun.hadoop.hdfs.api; import java.io.BufferedInputStream; import java.io.File; impo ...
- Java8 Stream流API常用操作
Java版本现在已经发布到JDK13了,目前公司还是用的JDK8,还是有必要了解一些JDK8的新特性的,例如优雅判空的Optional类,操作集合的Stream流,函数式编程等等;这里就按操作例举一些 ...
- 【JDK8】Java8 Stream流API常用操作
Java版本现在已经发布到JDK13了,目前公司还是用的JDK8,还是有必要了解一些JDK8的新特性的,例如优雅判空的Optional类,操作集合的Stream流,函数式编程等等;这里就按操作例举一些 ...
- easyui api常用操作
一.FORM表单类 一.textbox validatebox 验证 1.验证规则:validType : 验证规则,类型STRING|ARRAY:1个规则就直接一个字符串,多个规则写在数组里 例如: ...
- Elasticsearch(ES)API 增删查改常用操作
常用操作 查询所有数据 POST http://192.168.97.173:27009/logstash_test_2018/doc/_search { "query": { & ...
- 『无为则无心』Python序列 — 18、Python列表概念及常用操作API
目录 1.列表的概念 (1)列表的定义 (2)列表的应用场景 (3)列表的定义格式 2.列表的常用操作 (1)列表的查找 1)通过下标查找 2)通过方法查找 3)判断是否存在 (2)列表的增加 @1. ...
- 【三】用Markdown写blog的常用操作
本系列有五篇:分别是 [一]Ubuntu14.04+Jekyll+Github Pages搭建静态博客:主要是安装方面 [二]jekyll 的使用 :主要是jekyll的配置 [三]Markdown+ ...
随机推荐
- TOP K和Partition对比
TOP k算法适用于海量数据,不用一批装入内存.. partition算法需要全部装入内存排序,需要修改原数据..
- Flink SQL 如何实现数据流的 Join?
无论在 OLAP 还是 OLTP 领域,Join 都是业务常会涉及到且优化规则比较复杂的 SQL 语句.对于离线计算而言,经过数据库领域多年的积累,Join 语义以及实现已经十分成熟,然而对于近年来刚 ...
- Invalid [xxx] in servlet mapping 、 <url-pattern>的匹配规则 、 DefaultServlet介绍
真的是很容易被忽视的错误,servlet 配置url的时候遇到问题,这个之前确实没有详细了解过. 出现这个错误的时候往往伴随着一系列高大上的错误,比如会出现类似[StandardEngine[Cata ...
- windows10禁止系统更新
- Controllers的使用
代码 angularjs.html <!doctype html> <html> <head> <meta charset="UTF-8" ...
- 如何安装 Angular CLI 并且检查 CLI 的版本
想在系统中安装 Angular CLI ,如何进行安装并且如何检查 CLI 的版本? 可以使用命令: npm install -g @angular/cli 进行安装. 使用命令 ng version ...
- Linux命令-文件传输
Linux命令-文件传输 Linux lprm命令 Linux lprm命令用于将一个工作由打印机贮列中移除 尚未完成的打印机工作会被放在打印机贮列之中,这个命令可用来将常未送到打印机的工作取消.由于 ...
- 微信小程序_(视图)简单的swiper容器
swiper容器效果 官方文档:传送门 swiper容器可实现简单的轮播图效果 结构程序 Page({ /** * 页面的初始数据 */ data: { }, /** * 生命周期函数--监听页面加载 ...
- Concurrent包里的其他东西:ArrayBlockingQueue、CountDownLatch等等。
1:本例介绍一个特殊的队列:BlockingQueue,如果BlockingQueue是空的,从BlockingQueue取东西的操作将会被阻断进入等待状态,直到BlockingQueue进了东西才会 ...
- charts_03
table 数值获取: 1.http://www.w3school.com.cn/jsref/dom_obj_all.asp 2.http://blog.csdn.net/xs_zgsc/articl ...