共计 3438 个字符,预计需要花费 9 分钟才能阅读完成。
1. 装置
wget 下载地址 https://mirrors.huaweicloud.c…
etcd.config
name: "etcd"
data-dir: "/var/lib/etcd/data"
listen-client-urls: "http://0.0.0.0:2379"
advertise-client-urls: "http://0.0.0.0:2379"
启动
./etcd --config-file=/opt/etcd/etcd.config
2. 根本命令
设置 key
etcdctl put slq1 1
获取 key
etcdctl get slq1 // 获取 key 为 slq1 的 value
etcdctl get slq1 --print-value-only // 只打印对应的值
etcdctl get slq1 --hex // 以十六进制显示
etcdctl get slq1 slq5 // 获取肯定范畴的 key 不包含 slq5
etcdctl get --prefix slq // 获取以 slq 为前缀的 key
etcdctl get --limit=2 --prefix slq // 限度数量为 2
etcdctl get --from-key s // 查找大于 s 的所有 key
etcdctl get --prefix --rev=4 slq1 // 获取 slq1 key version 为 4 时候对应的值
删除 key
etcdctl del slq1 // 删除 slq1
etcdctl del slq1 slq5 // 删除 slq1 到 slq5 的 key
etcdctl del --prefix slq // 删除前缀为 slq 的 key
etcdctl del --from-key s // 删除大于 s 的所有 key
监听 key 变动
etcdctl watch slq1 // 监测 slq1
etcdctl watch slq1 slq5 // 监测 slq1 到 slq5 的 key
etcdctl watch --prefix slq // 监测前缀为 slq 的 key
etcdctl watch --from-key s // 监测大于 s 的所有 key
对 key 进行加锁
etcdctl lock slq1
------------------------------------
[root@node3 opt]# etcdctl lock slq1
slq1/694d7afa84f70318
------------------------------------
[root@node3 etcd]# etcdctl lock slq1
产生阻塞
------------------------------------
事务 txn
etcdctl txn -i //- i 应用交互式
清理历史版本信息 compact
etcdctl compact 5
[root@prometheus ~]# etcdctl compact 3
compacted revision 3
[root@prometheus ~]#
lease 和 ttl
[root@prometheus ~]# etcdctl lease grant 100
lease 694d7afa84f70339 granted with TTL(100s)
[root@prometheus ~]#
[root@prometheus ~]#
[root@prometheus ~]# etcdctl put --lease=694d7afa84f70339 slqslq slq
OK
[root@prometheus ~]#
[root@prometheus ~]# etcdctl get slqslq
slqslq
slq
[root@prometheus ~]#
撤销租期
[root@prometheus ~]# etcdctl lease revoke 694d7afa84f70339
lease 694d7afa84f70339 revoked
[root@prometheus ~]#
[root@prometheus ~]#
[root@prometheus ~]# etcdctl get slqslq
[root@prometheus ~]#
查看租期无效工夫
[root@prometheus ~]# etcdctl lease timetolive 694d7afa84f70343
lease 694d7afa84f70343 granted with TTL(100s), remaining(66s)
放弃租期无效
etcdctl lease keep-alive 694d7afa84f70339
[root@prometheus ~]# etcdctl lease keep-alive 694d7afa84f70343
lease 694d7afa84f70343 keepalived with TTL(100)
3.clientv3 操作 api
下载包
go get -u go.etcd.io/etcd/clientv3@v3.3.24
程序
package main
import(
"context"
"fmt"
"go.etcd.io/etcd/clientv3"
"log"
"time"
)
func main() {
client, err := clientv3.New(clientv3.Config{Endpoints: []string{"http://192.168.56.111:2379"},
DialTimeout: 2 * time.Second,
})
if err != nil {log.Println("create client err:",err)
}
defer client.Close()
keyName := "slq0909"
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
_, err = client.Put(ctx, keyName, "xxxxxx")
cancel()
if err != nil {log.Printf("put to etcd failed,err:%v \n",err)
return
}
ctx, cancel = context.WithTimeout(context.Background(), time.Second)
resp,err := client.Get(ctx, keyName)
cancel()
if err != nil {log.Println("get keyname err:",err)
}
for _,ev := range resp.Kvs {fmt.Printf("%s:%s \n",ev.Key,ev.Value)
}
}
后果
问题
装置 clientv3 遇到的问题
go: downloading golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985
# github.com/coreos/etcd/clientv3/balancer/picker
..\..\go\pkg\mod\github.com\coreos\etcd@v3.3.25+incompatible\clientv3\balancer\picker\err.go:37:44: undefined: balancer.PickOptions
..\..\go\pkg\mod\github.com\coreos\etcd@v3.3.25+incompatible\clientv3\balancer\picker\roundrobin_balanced.go:55:54: undefined: balancer.PickOptions
# github.com/coreos/etcd/clientv3/balancer/resolver/endpoint
..\..\go\pkg\mod\github.com\coreos\etcd@v3.3.25+incompatible\clientv3\balancer\resolver\endpoint\endpoint.go:114:78: undefined: resolver.BuildOption
..\..\go\pkg\mod\github.com\coreos\etcd@v3.3.25+incompatible\clientv3\balancer\resolver\endpoint\endpoint.go:182:31: undefined: resolver.ResolveNowOption
go.mod
replace google.golang.org/grpc => google.golang.org/grpc v1.26.0
正文完