type Client struct {
Cluster
KV
Lease
Watcher
Auth
Maintenance
// Username is a user name for authentication.
Username string
// Password is a password for authentication.
Password string
}
import (
"fmt"
"go.etcd.io/etcd/clientv3"
"time"
)
//连接
func main() {
//客户端配置
config := clientv3.Config{
Endpoints: []string{"192.168.1.109:2379"},
DialTimeout: 5 * time.Second,
}
//建立连接
if client, err := clientv3.New(config); err != nil {
fmt.Println(err)
return
}
fmt.Println("connect success")
defer client.Close()
}
//控制超时
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
//增-存值
_, err = client.Put(ctx, "/demo/demo1_key", "demo1_value")
//操作完毕,cancel掉
cancel()
if err != nil {
fmt.Println("put failed, err:", err)
return
}
Put(ctx context.Context, key, val string, opts ...OpOption) (*PutResponse, error)
ctx, cancel = context.WithTimeout(context.Background(), time.Second)
resp, err := client.Get(ctx, "/demo/demo1_key")
// Get查询还可以增加WithPrefix选项,获取某个目录下的所有子元素
//eg: resp, err := client.Get(ctx, "/demo/", clientv3.WithPrefix())
cancel()
if err != nil {
fmt.Println("get failed err:", err)
return
}
for _, item := range resp.Kvs { //Kvs 返回key的列表
fmt.Printf("%s : %s \n", item.Key, item.Value)
}
ctx, _ = context.WithTimeout(context.Background(), time.Second)
resp, err := client.Delete(ctx, "/demo/demo1_key")
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.PrevKvs)
Watch 方法返回一个WatchChan 类似的变量, WatchChan是一个channel
import (
"context"
"fmt"
"go.etcd.io/etcd/clientv3"
"time"
)
func main() {
client, err := clientv3.New(clientv3.Config{
Endpoints: []string{"192.168.1.109:2379"},
DialTimeout: time.Second,
})
if err != nil {
fmt.Println("connect failed err : ", err)
return
}
defer client.Close()
client.Put(context.Background(), "/demo/demo2_key", "demo2_value")
go func() {
//watch
watchKey := client.Watch(context.Background(), "/demo/demo2_key")
for resp := range watchKey {
for _, item := range resp {
fmt.Printf("%s %q : %q \n", item.Type, item.Kv.key, item.Kv.Value)
}
}
}
if resp, err := client.Put(context.TODO(), "/demo/demo2_key/", "demo2_watch"); err != nil {
fmt.Println(err)
} else {
fmt.Println(resp)
}
}
import (
"context"
"fmt"
"go.etcd.io/etcd/clientv3"
"log"
"sync"
"time"
)
func main() {
client, err := clientv3.New(clientv3.Config{
Endpoints: 5 * time.Second,
DialTimeout: 3 * time.Second,
})
if err != nil {
fmt.Println("connect failed err: ", err)
return
}
client.Close()
var w sync.WaitGroup
w.Add(10)
key10 := "setnx"
for i := 0; i < 10; i++ {
go func(i int) {
time.Sleep(5 * time.Millisecond)
//通过key的Create_Revision 是否为 0 来判断key是否存在。其中If,Then 以及 Else 分支都可以包含多个操作。
//返回的数据包含一个successed字段,当为 true 时代表 If 为真
_, err := client.Txn(context.Background()).
If(clientv3.Compare(clientv3.CreateRevision(key10), "=", 0)).
Then(clientv3.OpPut(key10, fmt.Sprintf("%d", i))).
Commit()
if err != nil {
fmt.Println(err)
}
w.Done()
}(i)
}
w.Wait()
if resp, err := client.Get(context.TODO(), key10); err != nil {
log.Fatal(err)
} else {
log.Println(resp)
}
}
import (
"time"
)
var (
dialTimeout = 2 * time.Second
requestTimeout = 10 * time.Second
)
func main() {
ctx, _ := context.WithTimeout(context.Background(), requestTimeout)
client, err := clientv3.New(clientv3.Config{
DialTimeout: dialTimeout,
Endpoints: []string{"localhost:2379"},
})
if err != nil {
log.Fatal(err)
}
kv := clientv3.NewKv(client)
lease, err := client.Grant(ctx, 3)
if err != nil {
log.Fatal(err)
}
//Insert key with a lease of 3 second TTL
kv.Put(ctx, "/demo/demo1_key", "demo1_value", clientv3.WithLease(lease.ID))
gr, _ = kv.Get(ctx, "/demo/demo1_key")
if len(gr.Kvs) == 1 {
fmt.Println("Found key")
}
//let the TTL expire
time.Sleep(3 * time.Second)
gr, _ = kv.Get(ctx, "/demo/demo1_key")
if len(gr.Kvs) == 0 {
fmt.Println("no more key")
}
}