搭完集群后,我开始体验下etcd如何使用:
1,设置key,进行前缀查询操作
ENDPOINTS=http://127.0.0.1:22379etcdctl --endpoints=$ENDPOINTS put web1 value1etcdctl --endpoints=$ENDPOINTS put web2 value2OKetcdctl --endpoints=$ENDPOINTS put web3 value3
查询
etcdctl --endpoints=$ENDPOINTS get web --prefixweb1value1web2value2web3value3
2,前缀删除
etcdctl --endpoints=$ENDPOINTS put key myvalueetcdctl --endpoints=$ENDPOINTS del keyetcdctl --endpoints=$ENDPOINTS put k1 value1etcdctl --endpoints=$ENDPOINTS put k2 value2etcdctl --endpoints=$ENDPOINTS del k --prefix2
3,事务操作txn
etcdctl --endpoints=$ENDPOINTS put user1 badetcdctl --endpoints=$ENDPOINTS txn --interactiveOKcompares:value("user1") = "bad"success requests (get, put, del):del user1failure requests (get, put, del):put user1 goodSUCCESS1
4,watch操作
在一个console进行watch,另一个console进行key的操作
etcdctl --endpoints=$ENDPOINTS watch stock1
etcdctl --endpoints=$ENDPOINTS put stock1 1000
当然也可以watch前缀
etcdctl --endpoints=$ENDPOINTS watch stock --prefixetcdctl --endpoints=$ENDPOINTS put stock1 10etcdctl --endpoints=$ENDPOINTS put stock2 20
5,租期
% etcdctl --endpoints=$ENDPOINTS lease grant 300lease 414688a849143a15 granted with TTL(300s)
% etcdctl --endpoints=$ENDPOINTS put sample value --lease=414688a849143a15etcdctl --endpoints=$ENDPOINTS get sampleOKsamplevalue
操作的时候需要指定返回的租期id
% etcdctl --endpoints=$ENDPOINTS get samplesamplevalue
6,分布式锁
% etcdctl --endpoints=$ENDPOINTS lock mutex1mutex1/414688a849143a1d
在另一个console尝试加锁,会一直阻塞,直到前者释放锁
% ENDPOINTS=http://127.0.0.1:22379% etcdctl --endpoints=$ENDPOINTS lock mutex1mutex1/414688a849143a21
7,选举
% etcdctl --endpoints=$ENDPOINTS elect one p1one/414688a849143a2ap1
% etcdctl --endpoints=$ENDPOINTS elect one p2p2
8,查看数据状态
% 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 | |+------------------------+------------------+---------------+-----------------+---------+----------------+-----------+------------+-----------+------------+--------------------+--------+
% etcdctl --endpoints=$ENDPOINTS endpoint healthhttp://127.0.0.1:22379 is healthy: successfully committed proposal: took = 15.993027ms
9,数据持久化备份
% 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.dbServer version 3.6.0
% etcdctl --write-out=table --endpoints=$ENDPOINTS snapshot status my.db
10,版本v2和v3之间迁移
# write key in etcd version 2 storeexport ETCDCTL_API=2etcdctl --endpoints=http://$ENDPOINT set foo bar# read key in etcd v2etcdctl --endpoints=$ENDPOINTS --output="json" get foo# stop etcd node to migrate, one by one# migrate v2 dataexport ETCDCTL_API=3etcdctl --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 migratedetcdctl --endpoints=$ENDPOINTS get foo


文章转载自golang算法架构leetcode技术php,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




