先用 go get go.etcd.io/etcd/clientv3 获得etcd的客户端
下面为测试代码
package main
import (
"fmt"
"time"
"go.etcd.io/etcd/clientv3"
context "golang.org/x/net/context"
)
func main() {
cli, err := clientv3.New(clientv3.Config{
Endpoints: []string{"localhost:2379", "localhost:22379", "localhost:32379"},
DialTimeout: 5 * time.Second,
})
if err != nil {
fmt.Println("clientv3.New faild", err)
return
}
fmt.Println("clientv3.New success")
defer cli.Close()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*50)
defer cancel()
i := 0
for {
resp, err := cli.Put(ctx, fmt.Sprintf("sample_key_%d", i), "sample_value")
if err != nil {
fmt.Println("clientv3 put key faild", err)
return
}
fmt.Println("clientv3 put key success, return", resp)
if i == 10 {
break
}
i++
time.Sleep(time.Second)
}
time.Sleep(time.Second * 3)
}