您好,登錄后才能下訂單哦!
測試代碼如下
package main import ( "fmt" "log" "time" "go.etcd.io/etcd/clientv3" "golang.org/x/net/context" ) var ( dialTimeout = 5 * time.Second requestTimeout = 2 * time.Second endpoints = []string{"192.168.99.106:2379", "192.168.99.107:2379", "192.168.99.108:2379"} ) func main() { cli, err := clientv3.New(clientv3.Config{ Endpoints: endpoints, DialTimeout: dialTimeout, }) if err != nil { log.Fatal(err) } defer cli.Close() key1, value1 := "testkey1", "value" ctx, cancel := context.WithTimeout(context.Background(), requestTimeout) _, err = cli.Put(ctx, key1, value1) cancel() if err != nil { log.Println("Put failed. ", err) } else { log.Printf("Put {%s:%s} succeed\n", key1, value1) } ctx, cancel = context.WithTimeout(context.Background(), requestTimeout) resp, err := cli.Get(ctx, key1) cancel() if err != nil { log.Println("Get failed. ", err) return } for _, kv := range resp.Kvs { log.Printf("Get {%s:%s} \n", kv.Key, kv.Value) } done := make(chan bool) go func() { wch := cli.Watch(context.Background(), key1) for item := range wch { for _, ev := range item.Events { log.Printf("Type:%s, key:%s, value:%s\n", ev.Type, ev.Kv.Key, ev.Kv.Value) } } }() go func() { for cnt := 0; cnt < 11; cnt++ { value := fmt.Sprintf("%s%d", "value", cnt) _, err = cli.Put(context.Background(), key1, value) if err != nil { log.Println("Put failed. ", err) } else { log.Printf("Put {%s:%s} succeed\n", key1, value) } } }() <-done log.Println("Done!") }
測試結果如下
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。