golang对etcd存取和数值监测
测试代码如下
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!") }
测试结果如下
相关推荐
CurrentJ 2020-08-18
JustHaveTry 2020-07-17
Dannyvon 2020-07-13
Dannyvon 2020-07-04
###host字段指定授权使用该证书的etcd节点IP或子网列表,需要将etcd集群的3个节点都添加其中。cp etcd-v3.3.13-linux-amd64/etcd* /opt/k8s/bin/
xiunai 2020-07-04
breezegao 2020-07-02
微微一笑 2020-06-14
微微一笑 2020-06-12
CurrentJ 2020-06-06
lenchio 2020-06-04
微微一笑 2020-06-03
工作中的点点滴滴 2020-06-01
Rcvisual 2020-05-28
Dannyvon 2020-05-28
微微一笑 2020-05-26
wishli 2020-05-19
工作中的点点滴滴 2020-05-14