5-12|etcd api常用操作

简介: 5-12|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"}

1.

2.

健康状态

# curl -L http://172.16.101.55:2379/health

{"health":"true"}

1.

2.

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":18,"createdIndex":18}}

1.

2.

查看key

# curl http://127.0.0.1:2379/v2/keys/message

{"action":"get","node":{"key":"/message","value":"Hello world","modifiedIndex":18,"createdIndex":18}}

1.

2.

删除key

# curl http://127.0.0.1:2379/v2/keys/message -XDELETE

{"action":"delete","node":{"key":"/message","modifiedIndex":19,"createdIndex":18},"prevNode":{"key":"/message","value":"Hello world","modifiedIndex":18,"createdIndex":18}}

1.

2.

新建带有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":30,"modifiedIndex":20,"createdIndex":20}}


# 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":2,"modifiedIndex":20,"createdIndex":20}}

取消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":30,"modifiedIndex":22,"createdIndex":22}}
# 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":20,"modifiedIndex":22,"createdIndex":22}}
# 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":23,"createdIndex":22},"prevNode":{"key":"/message","value":"Hello world","expiration":"2019-09-29T08:10:23.220573683Z","ttl":16,"modifiedIndex":22,"createdIndex":22}}
# curl http://127.0.0.1:2379/v2/keys/message
{"action":"get","node":{"key":"/message","value":"Hello world","modifiedIndex":23,"createdIndex":22}}

重置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":30,"modifiedIndex":25,"createdIndex":25}}
# 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":16,"modifiedIndex":25,"createdIndex":25}}
# 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":30,"modifiedIndex":26,"createdIndex":25},"prevNode":{"key":"/message","value":"Hello world","expiration":"2019-09-29T08:15:01.34698273Z","ttl":2,"modifiedIndex":25,"createdIndex":25}}
# 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":27,"modifiedIndex":26,"createdIndex":25}}

新建带有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":30,"modifiedIndex":48,"createdIndex":48}}

在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":60,"modifiedIndex":50,"createdIndex":48},"prevNode":{"key":"/dir","dir":true,"modifiedIndex":48,"createdIndex":48}}

向该目录插入数据

# 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":51,"createdIndex":51}}

到期之前该目录的数据和普通数据一样,但是该目录到期后数据会被自动删除

# curl http://127.0.0.1:2379/v2/keys/dir/message
{"action":"get","node":{"key":"/dir/message","value":"Hello world","modifiedIndex":51,"createdIndex":51}}
# curl http://127.0.0.1:2379/v2/keys/dir/message
{"errorCode":100,"message":"Key not found","cause":"/dir","index":52}

自动创建有序的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":42,"createdIndex":42}}
# curl http://127.0.0.1:2379/v2/keys/queue -XPOST -d value=Job2
{"action":"create","node":{"key":"/queue/00000000000000000043","value":"Job2","modifiedIndex":43,"createdIndex":43}}
# curl http://127.0.0.1:2379/v2/keys/queue -XPOST -d value=Job3
{"action":"create","node":{"key":"/queue/00000000000000000044","value":"Job3","modifiedIndex":44,"createdIndex":44}}
# curl http://127.0.0.1:2379/v2/keys/queue -XPOST -d value=Job4
{"action":"create","node":{"key":"/queue/00000000000000000045","value":"Job4","modifiedIndex":45,"createdIndex":45}}
# curl http://127.0.0.1:2379/v2/keys/queue -XPOST -d value=Job5
{"action":"create","node":{"key":"/queue/00000000000000000046","value":"Job5","modifiedIndex":46,"createdIndex":46}}
# curl http://127.0.0.1:2379/v2/keys/queue -XPOST -d value=Job6
{"action":"create","node":{"key":"/queue/00000000000000000047","value":"Job6","modifiedIndex":47,"createdIndex":47}}

查看

# 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":42,"createdIndex":42},{"key":"/queue/00000000000000000043","value":"Job2","modifiedIndex":43,"createdIndex":43},{"key":"/queue/00000000000000000044","value":"Job3","modifiedIndex":44,"createdIndex":44},{"key":"/queue/00000000000000000045","value":"Job4","modifiedIndex":45,"createdIndex":45},{"key":"/queue/00000000000000000046","value":"Job5","modifiedIndex":46,"createdIndex":46},{"key":"/queue/00000000000000000047","value":"Job6","modifiedIndex":47,"createdIndex":47}],"modifiedIndex":42,"createdIndex":42}}

一次性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":28,"createdIndex":28}}

此时第一个终端显示如下

{"action":"set","node":{"key":"/message","value":"Hello world","modifiedIndex":28,"createdIndex":28}}

再此在第一个终端执行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":29,"createdIndex":29},"prevNode":{"key":"/message","value":"Hello world","modifiedIndex":28,"createdIndex":28}}

第一个终端显示如下

{"action":"set","node":{"key":"/message","value":"Nice Day","modifiedIndex":29,"createdIndex":29},"prevNode":{"key":"/message","value":"Hello world","modifiedIndex":28,"createdIndex":28}}

原子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":56,"createdIndex":56}}
# curl http://127.0.0.1:2379/v2/keys/foo
{"action":"get","node":{"key":"/foo","value":"one","modifiedIndex":56,"createdIndex":56}}

插入一个已存在的key并添加参数prevExist=false,因为已经有存在的key

# curl http://127.0.0.1:2379/v2/keys/foo?prevExist=false -XPUT -d value=two
{"errorCode":105,"message":"Key already exists","cause":"/foo","index":56}

将插入条件换成prevValue,即检查key的value值,条件相等就替换,否则就提示条件不匹配

# curl http://127.0.0.1:2379/v2/keys/foo?prevValue=three -XPUT -d value=two
{"errorCode":101,"message":"Compare failed","cause":"[three != one]","index":56}
# curl http://127.0.0.1:2379/v2/keys/foo?prevValue=one -XPUT -d value=two
{"action":"compareAndSwap","node":{"key":"/foo","value":"two","modifiedIndex":57,"createdIndex":56},"prevNode":{"key":"/foo","value":"one","modifiedIndex":56,"createdIndex":56}}
# curl http://127.0.0.1:2379/v2/keys/foo
{"action":"get","node":{"key":"/foo","value":"two","modifiedIndex":57,"createdIndex":56}}
相关文章
|
9月前
|
Kubernetes 容器
ETCD和api-server证书过期时间证书生成
ETCD和api-server证书过期时间证书生成
128 4
|
存储 分布式计算 Hadoop
分布式数据库HBase的常用操作的对应的API编程接口
HBase是一个分布式数据库系统,基于Google的BigTable和Apache Hadoop的HDFS构建。它提供了一个高性能、可扩展的数据库平台,适用于大规模的数据存储和处理。在阿里云开发者社区中,很多开发者都会使用HBase进行数据存储和处理。本文将介绍HBase的常用操作及其对应的API编程接口。
334 0
|
存储 JSON 移动开发
etcd通信接口:客户端 API 实践与核心方法
你好,我是 aoho,今天我和你分享的主题是通信接口:客户端 API 实践与核心方法。 我们在前面一课时介绍了 etcd 的整体架构。学习客户端与 etcd 服务端的通信以及 etcd 集群节点的内部通信接口对于我们更好地使用和掌握 etcd 组件很有帮助,也是所必需了解的内容。本课时我们将会介绍 etcd 的 gRPC 通信接口以及客户端的实践。
408 0
|
JavaScript API 索引
|
1天前
|
JSON API 数据格式
阿里巴巴商品详情接口(阿里巴巴 API 系列)
在电商开发中,获取阿里巴巴商品详情信息对数据分析、竞品研究等至关重要。通过调用其商品详情接口,开发者可获取标题、价格、图片、描述等数据,满足多种业务需求。接口采用HTTPS协议,支持GET/POST请求,返回JSON格式数据。示例代码展示了如何使用Python的requests库进行接口请求,需传递商品ID和访问令牌。实际应用时,请依据官方文档调整参数并确保安全性。
27 10
|
2天前
|
JSON API 数据格式
eBay商品详情接口(ebay API系列)
eBay 商品详情接口是电商从业者、开发者和数据分析师获取商品详细信息的重要工具,涵盖标题、价格、库存、卖家信息等。使用前需在 eBay 开发者平台注册并获取 API 凭证,通过 HTTP GET 请求调用接口,返回 JSON 格式数据。Python 示例代码展示了如何发送请求并解析响应,确保合法合规使用数据。
31 12
|
4天前
|
监控 供应链 搜索推荐
亚马逊商品详情接口(亚马逊 API 系列)
亚马逊作为全球最大的电商平台之一,提供了丰富的商品资源。开发者和电商从业者可通过亚马逊商品详情接口获取商品的描述、价格、评论、排名等数据,对市场分析、竞品研究、价格监控及业务优化具有重要价值。接口基于MWS服务,支持HTTP/HTTPS协议,需注册并获得API权限。Python示例展示了如何使用mws库调用接口获取商品详情。应用场景包括价格监控、市场调研、智能选品、用户推荐和库存管理等,助力电商运营和决策。
49 23
|
5天前
|
JSON 前端开发 API
以项目登录接口为例-大前端之开发postman请求接口带token的请求测试-前端开发必学之一-如果要学会联调接口而不是纯写静态前端页面-这个是必学-本文以优雅草蜻蜓Q系统API为实践来演示我们如何带token请求接口-优雅草卓伊凡
以项目登录接口为例-大前端之开发postman请求接口带token的请求测试-前端开发必学之一-如果要学会联调接口而不是纯写静态前端页面-这个是必学-本文以优雅草蜻蜓Q系统API为实践来演示我们如何带token请求接口-优雅草卓伊凡
29 5
以项目登录接口为例-大前端之开发postman请求接口带token的请求测试-前端开发必学之一-如果要学会联调接口而不是纯写静态前端页面-这个是必学-本文以优雅草蜻蜓Q系统API为实践来演示我们如何带token请求接口-优雅草卓伊凡
|
5天前
|
JSON 数据挖掘 API
lazada商品详情接口 (lazada API系列)
Lazada 是东南亚知名电商平台,提供海量商品资源。通过其商品详情接口,开发者和商家可获取商品标题、价格、库存、描述、图片、用户评价等详细信息,助力市场竞争分析、商品优化及库存管理。接口采用 HTTP GET 请求,返回 JSON 格式的响应数据,支持 Python 等语言调用。应用场景包括竞品分析、价格趋势研究、用户评价分析及电商应用开发,为企业决策和用户体验提升提供有力支持。
52 21

热门文章

最新文章