etcd v3版本和以前的v2在使用和操作上面还是有些区别的,首先是命令区别其次是使用上面。先看命令行使用,我尽量用命令,不需要太多的文字描述,最大的区别莫过于ls命令没有了。
etcdctl put /test/ok 11
OK
etcdctl put /test/ok 22
OK
etcdctl del /test/gg
1
#删除所有/test前缀的节点
etcdctl del /test --prefix
4
etcdctl get /test/ok
/test/ok
11
# 前缀查询
etcdctl get /test/ok --prefix
/test/ok
11
/test/ok/gg
88
etcdctl watch /test/ok
PUT
/test/ok
11
#如果监听子节点
etcdctl watch /test/ok --prefix
PUT
/test/ok
11
PUT
/test/ok/first
11
从申请开始计算时间
etcdctl lease grant 40
lease 4e5e5b853f528859 granted with TTL(40s)
节点的生命伴随着租约到期将会被DELETE
etcdctl put --lease=4e5e5b853f528859 /test/ok/first xx
OK
撤销租约和租约到期一样,节点都会被删除
etcdctl lease revoke 4e5e5b853f5286cc
lease 4e5e5b853f5286cc revoked
每当到期将会续约
etcdctl lease keep-alive 4e5e5b853f52892b
lease 4e5e5b853f52892b keepalived with TTL(40)
lease 4e5e5b853f52892b keepalived with TTL(40)
. . .
上面介绍了命令行的基本使用。补充一下,当前默认还是v2版本通过设定环境变量export ETCDCTL_API=3,设置成V3版本。
v3版本支持rpc的远程调用,这样比http的方式效率更高
先看API定义
type KV interface {
Put(ctx context.Context, key, val string, opts ...OpOption) (*PutResponse, error)
Get(ctx context.Context, key string, opts ...OpOption) (*GetResponse, error)
Delete(ctx context.Context, key string, opts ...OpOption) (*DeleteResponse, error)
Compact(ctx context.Context, rev int64, opts ...CompactOption) (*CompactResponse, error)
Do(ctx context.Context, op Op) (OpResponse, error)
Txn(ctx context.Context) Txn
}
type Lease interface {
Grant(ctx context.Context, ttl int64) (*LeaseGrantResponse, error)
Revoke(ctx context.Context, id LeaseID) (*LeaseRevokeResponse, error)
TimeToLive(ctx context.Context, id LeaseID, opts ...LeaseOption) (*LeaseTimeToLiveResponse, error)
KeepAlive(ctx context.Context, id LeaseID) (<-chan *LeaseKeepAliveResponse, error)
KeepAliveOnce(ctx context.Context, id LeaseID) (*LeaseKeepAliveResponse, error)
Close() error
}
还有认证和watch等,不一一列举,看看client里面统一由哪些方法
type Client struct {
Cluster
KV
Lease
Watcher
Auth
Maintenance
conn *grpc.ClientConn
cfg Config
creds *credentials.TransportCredentials
balancer *simpleBalancer
retryWrapper retryRpcFunc
retryAuthWrapper retryRpcFunc
ctx context.Context
cancel context.CancelFunc
// Username is a username for authentication
Username string
// Password is a password for authentication
Password string
// tokenCred is an instance of WithPerRPCCredentials()'s argument
tokenCred *authTokenCredential
}
看看具体怎么使用吧,由于篇幅问题下面之介绍一下简单使用:
package main
import (
"github.com/coreos/etcd/clientv3"
"time"
"golang.org/x/net/context"
"fmt"
)
var (
dialTimeout = 5 * time.Second
requestTimeout = 10 * time.Second
endpoints = []string{"10.39.0.6:2379",}
)
func main() {
cli, err := clientv3.New(clientv3.Config{
Endpoints: []string{"10.39.0.6:2379"},
DialTimeout: dialTimeout,
})
if err != nil {
println(err)
}
defer cli.Close()
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
_, err = cli.Put(ctx, "/test/hello", "world")
cancel()
ctx, cancel = context.WithTimeout(context.Background(), requestTimeout)
resp,err := cli.Get(ctx, "/test/hello")
cancel()
for _, ev := range resp.Kvs {
fmt.Printf("%s : %s\n", ev.Key, ev.Value)
}
_, err = cli.Put(context.TODO(), "key", "xyz")
ctx, cancel = context.WithTimeout(context.Background(), requestTimeout)
_, err = cli.Txn(ctx).
If(clientv3.Compare(clientv3.Value("key"), ">", "abc")).
Then(clientv3.OpPut("key", "XYZ")).
Else(clientv3.OpPut("key", "ABC")).
Commit()
cancel()
rch := cli.Watch(context.Background(), "/test/hello", clientv3.WithPrefix())
for wresp := range rch {
for _, ev := range wresp.Events {
fmt.Printf("%s %q : %q\n", ev.Type, ev.Kv.Key, ev.Kv.Value)
}
}
if err != nil {
println(err)
}
}
最后面是一个事务处理Txn,watch是会等待监控,程序不会结束!