如果需要使用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常用操作的更多相关文章

  1. etcd安装常用操作

    etcd安装 etcd 是基于 Raft 的分布式 key-value 存储系统,由 CoreOS 开发,常用于服务发现.共享配置以及并发控制(如 leader 选举.分布式锁等).kubernete ...

  2. pandas数据分析API常用操作

    1.导入数据 df = pd.read_csv( # 该参数为数据在电脑中的路径,可以不填写 filepath_or_buffer='/Users/Weidu/Desktop/sz000002.csv ...

  3. HDFS Java API 常用操作

    package com.luogankun.hadoop.hdfs.api; import java.io.BufferedInputStream; import java.io.File; impo ...

  4. Java8 Stream流API常用操作

    Java版本现在已经发布到JDK13了,目前公司还是用的JDK8,还是有必要了解一些JDK8的新特性的,例如优雅判空的Optional类,操作集合的Stream流,函数式编程等等;这里就按操作例举一些 ...

  5. 【JDK8】Java8 Stream流API常用操作

    Java版本现在已经发布到JDK13了,目前公司还是用的JDK8,还是有必要了解一些JDK8的新特性的,例如优雅判空的Optional类,操作集合的Stream流,函数式编程等等;这里就按操作例举一些 ...

  6. easyui api常用操作

    一.FORM表单类 一.textbox validatebox 验证 1.验证规则:validType : 验证规则,类型STRING|ARRAY:1个规则就直接一个字符串,多个规则写在数组里 例如: ...

  7. Elasticsearch(ES)API 增删查改常用操作

    常用操作 查询所有数据 POST http://192.168.97.173:27009/logstash_test_2018/doc/_search { "query": { & ...

  8. 『无为则无心』Python序列 — 18、Python列表概念及常用操作API

    目录 1.列表的概念 (1)列表的定义 (2)列表的应用场景 (3)列表的定义格式 2.列表的常用操作 (1)列表的查找 1)通过下标查找 2)通过方法查找 3)判断是否存在 (2)列表的增加 @1. ...

  9. 【三】用Markdown写blog的常用操作

    本系列有五篇:分别是 [一]Ubuntu14.04+Jekyll+Github Pages搭建静态博客:主要是安装方面 [二]jekyll 的使用 :主要是jekyll的配置 [三]Markdown+ ...

随机推荐

  1. SparkStreaming HA高可用性

    1.UpdateStateByKey.windows等有状态的操作时,自动进行checkpoint,必须设置checkpoint目录,数据保留一份在容错的文件系统中,一旦内存中的数据丢失,可以从文件系 ...

  2. 织梦dedecms做的网站首页标题篡改跳转赌博网站解决方案

    织梦dedecms因其强大功能,简单实用的优点常常被用来做企业网站,程序开源使用的人多了网站漏洞多会有中毒的情况,常见的有一种,首页标题关键词描述被篡改,百度快照收录点击后跳转的赌博网站,怎么解决这个 ...

  3. wx小程序知识点(六)

    六.生命周期 (1)onLoad    —— 加载时触发,只调用一次,可用来发送请求绑定数据.获取url中参数 (2)onShow   —— 页面显示时触发,每次显示都会执行,用来获取需要频繁更新的数 ...

  4. count(列) count(*)

    总结:但是真的结论是这样的么.其实不然.其实在数据库中count(*)和count(列)根本就是不等价的,count(*)是针对于全表的,而count(列)是针对于某一列的,如果此列值为空的话,cou ...

  5. IE ActiveObject

    ActiveObject只能用于基于IE内核的浏览器 需要添加信任站点 并设置对ActiveObject的启用 问题: 1.用javascript 创建ActiveX对象时出现:Automation ...

  6. Springboot 使用mybatis

    Springboot 使用mybatis 依赖 pom.xml <?xml version="1.0" encoding="UTF-8"?> < ...

  7. 大哥带的DOM-XSS进阶eval构造XSS

    0X01源码分析 那么我们可以如何来构造语法呐??已知eval函数可以执行js 最终的输出是eval(this.xss="你提交的参数";) 那我们是不是可以从参数这里构造一个js ...

  8. oracle条件参数中 IN函数中的值最大只能为1000个

    delete from dep where id in(1,2,3.....) 括号里面字段个数最大只能为1000个

  9. Python 入门知识

    一python 语言介绍 特点 简单清晰明确   目前是全球最火的高级编程语言  应用领域很广 NASA FACEBOOK 谷歌 春雨医生 金山 腾讯,Python是一门解释性弱类型编程语言.编译性得 ...

  10. vue开发多页面应用

    1.添加多页面配置 在工程根路径下(package.json同目录)添加添加vue.config.js配置文件,内容为: module.exports = { pages: { index: 'src ...