如果需要使用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. Beyong Compare 2.4.10刷新剩余日期bat批处理文件

    @echo off % start )(window.close)&&exit REG DELETE "HKCU\Software\Scooter Software\Beyo ...

  2. [游记]CSP2019-S

    Day 1 开局看到T1格雷码,哇塞这不是初赛原题???10分钟高精打完离场. T2是个什么题目,看起来不难,15分钟码完,调了5分钟,过了样例2 欸,为什么样例3过不掉?仔细一看发现爆栈了,一慌忘记 ...

  3. (转载)rabbitmq与springboot的安装与集成

    原文地址:https://segmentfault.com/a/1190000016991529 一.前言 RabbitMQ是一个开源的消息代理软件(面向消息的中间件),它的核心作用就是创建消息队列, ...

  4. Codeforces 482E ELCA (LCT)

    题目链接 http://codeforces.com/contest/482/problem/E 题解 T2智商题T3大LCT题,我一个也不会= = CF的标算好像是分块?反正现在LCT都普及了就用L ...

  5. 微信小程序_(组件)form表单

    Form表单.switch开关.数值选择器效果 官方文档:传送门 点击提交表单(按钮,提交开关,数值选择器,输入文本中)的值,显示在控制台上,点击重置,重置表单中的值. 实现过程 form表单,添加f ...

  6. Linux下不同组件运行命令

    Spark优化配置 添加外包路径 1.1 vim $SPARK_HOME/conf/spark-defaults.conf spark.executor.extraClassPath file:/// ...

  7. String、toString、String.valueOf()三个有啥区别?

    今天在使用这个的时候发现,他们三者好像在某些场所都是可以用的,但是不免会让人想到那既然它们三者这么的相似,那么总有些什么区别吧.我也在网上找了一些资料看.自己也看了API文档,就将他们三的区别总结一下 ...

  8. ES6 方法函数

    1.模块的整体加载 除了指定加载某个输出值,还可以使用整体加载,即用星号(*)指定一个对象,所有输出值都加载在这个对象上面. 下面是一个circle.js文件,它输出两个方法area和circumfe ...

  9. Why 0.1 + 0.2 === 0.30000000000000004 ?

    Why 0.1 + 0.2 === 0.30000000000000004 ? 在浮点数运算中产生误差值的示例中,最出名应该是0.1 + 0.2 === 0.30000000000000004了,到底 ...

  10. Java学习之==>注释、数据类型、变量、运算符

    一.注释 在Java中有3种标记注释的方式,最常用的方式是 // ,其注释的内容从 // 开始,到本行结束.但需要注意的是,我们不建议把注释写在代码的尾部(即尾注释),我们建议把注释写在代码的上一行, ...