前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >golang源码分析:etcd(3)

golang源码分析:etcd(3)

作者头像
golangLeetcode
发布2023-08-09 15:14:50
1340
发布2023-08-09 15:14:50
举报

搭完集群后,我开始体验下etcd如何使用

1,设置key,进行前缀查询操作

代码语言:javascript
复制
ENDPOINTS=http://127.0.0.1:22379
etcdctl --endpoints=$ENDPOINTS put web1 value1
etcdctl --endpoints=$ENDPOINTS put web2 value2
OK
etcdctl --endpoints=$ENDPOINTS put web3 value3

查询

代码语言:javascript
复制
etcdctl --endpoints=$ENDPOINTS get web --prefix
web1
value1
web2
value2
web3
value3

2,前缀删除

代码语言:javascript
复制
etcdctl --endpoints=$ENDPOINTS put key myvalue
etcdctl --endpoints=$ENDPOINTS del key

etcdctl --endpoints=$ENDPOINTS put k1 value1
etcdctl --endpoints=$ENDPOINTS put k2 value2
etcdctl --endpoints=$ENDPOINTS del k --prefix
2

3,事务操作txn

代码语言:javascript
复制
etcdctl --endpoints=$ENDPOINTS put user1 bad
etcdctl --endpoints=$ENDPOINTS txn --interactive
OK
compares:
value("user1") = "bad"

success requests (get, put, del):
del user1

failure requests (get, put, del):
put user1 good

SUCCESS

1

4,watch操作

在一个console进行watch,另一个console进行key的操作

代码语言:javascript
复制
etcdctl --endpoints=$ENDPOINTS watch stock1
代码语言:javascript
复制
etcdctl --endpoints=$ENDPOINTS put stock1 1000

当然也可以watch前缀

代码语言:javascript
复制
etcdctl --endpoints=$ENDPOINTS watch stock --prefix
etcdctl --endpoints=$ENDPOINTS put stock1 10
etcdctl --endpoints=$ENDPOINTS put stock2 20

5,租期

代码语言:javascript
复制
% etcdctl --endpoints=$ENDPOINTS lease grant 300
lease 414688a849143a15 granted with TTL(300s)
代码语言:javascript
复制
 % etcdctl --endpoints=$ENDPOINTS put sample value --lease=414688a849143a15
etcdctl --endpoints=$ENDPOINTS get sample

OK
sample
value

操作的时候需要指定返回的租期id

代码语言:javascript
复制
% etcdctl --endpoints=$ENDPOINTS get sample                               
sample
value

6,分布式锁

代码语言:javascript
复制
 % etcdctl --endpoints=$ENDPOINTS lock mutex1
mutex1/414688a849143a1d

在另一个console尝试加锁,会一直阻塞,直到前者释放锁

代码语言:javascript
复制
% ENDPOINTS=http://127.0.0.1:22379
% etcdctl --endpoints=$ENDPOINTS lock mutex1
mutex1/414688a849143a21

7,选举

代码语言:javascript
复制
% etcdctl --endpoints=$ENDPOINTS elect one p1
one/414688a849143a2a
p1
代码语言:javascript
复制
% etcdctl --endpoints=$ENDPOINTS elect one p2
p2

8,查看数据状态

代码语言:javascript
复制

% etcdctl --write-out=table --endpoints=$ENDPOINTS endpoint status

+------------------------+------------------+---------------+-----------------+---------+----------------+-----------+------------+-----------+------------+--------------------+--------+
|        ENDPOINT        |        ID        |    VERSION    | STORAGE VERSION | DB SIZE | DB SIZE IN USE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+------------------------+------------------+---------------+-----------------+---------+----------------+-----------+------------+-----------+------------+--------------------+--------+
| http://127.0.0.1:22379 | 91bc3c398fb3c146 | 3.6.0-alpha.0 |           3.6.0 |   33 kB |          33 kB |     false |      false |         2 |         37 |                 37 |        |
+------------------------+------------------+---------------+-----------------+---------+----------------+-----------+------------+-----------+------------+--------------------+--------+
代码语言:javascript
复制
% etcdctl --endpoints=$ENDPOINTS endpoint health
http://127.0.0.1:22379 is healthy: successfully committed proposal: took = 15.993027ms

9,数据持久化备份

代码语言:javascript
复制
% etcdctl --endpoints=$ENDPOINTS snapshot save my.db

{"level":"info","ts":"2023-06-11T16:48:51.176005+0800","caller":"snapshot/v3_snapshot.go:67","msg":"created temporary db file","path":"my.db.part"}
{"level":"info","ts":"2023-06-11T16:48:51.178707+0800","logger":"client","caller":"v3@v3.6.0-alpha.0/maintenance.go:237","msg":"opened snapshot stream; downloading"}
{"level":"info","ts":"2023-06-11T16:48:51.214419+0800","caller":"snapshot/v3_snapshot.go:75","msg":"fetching snapshot","endpoint":"http://127.0.0.1:22379"}
{"level":"info","ts":"2023-06-11T16:48:51.230865+0800","logger":"client","caller":"v3@v3.6.0-alpha.0/maintenance.go:302","msg":"completed snapshot read; closing"}
{"level":"info","ts":"2023-06-11T16:48:51.245575+0800","caller":"snapshot/v3_snapshot.go:90","msg":"fetched snapshot","endpoint":"http://127.0.0.1:22379","size":"33 kB","took":"68.257765ms","etcd-version":"3.6.0"}
{"level":"info","ts":"2023-06-11T16:48:51.247532+0800","caller":"snapshot/v3_snapshot.go:100","msg":"saved","path":"my.db"}
Snapshot saved at my.db
Server version 3.6.0
代码语言:javascript
复制
% etcdctl --write-out=table --endpoints=$ENDPOINTS snapshot status my.db

10,版本v2和v3之间迁移

代码语言:javascript
复制

# write key in etcd version 2 store
export ETCDCTL_API=2
etcdctl --endpoints=http://$ENDPOINT set foo bar

# read key in etcd v2
etcdctl --endpoints=$ENDPOINTS --output="json" get foo

# stop etcd node to migrate, one by one

# migrate v2 data
export ETCDCTL_API=3
etcdctl --endpoints=$ENDPOINT migrate --data-dir="default.etcd" --wal-dir="default.etcd/member/wal"

# restart etcd node after migrate, one by one

# confirm that the key got migrated
etcdctl --endpoints=$ENDPOINTS get /foo
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-06-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 golang算法架构leetcode技术php 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档