You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
769 B
40 lines
769 B
2 years ago
|
package test
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
clientv3 "go.etcd.io/etcd/client/v3"
|
||
|
"log"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func PutGet() {
|
||
|
// 1.连接etcd
|
||
|
// a. 实例化Etcd客户端
|
||
|
// b. 给定连接信息
|
||
|
cli, err := clientv3.New(clientv3.Config{
|
||
|
// Etcd的endpoint
|
||
|
Endpoints: []string{"localhost:2379"},
|
||
|
// 连接超时时长
|
||
|
DialTimeout: 3 * time.Second,
|
||
|
})
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
|
||
|
//context.Background() 默认的上下文环境
|
||
|
// 2.put测试
|
||
|
putResp, err := cli.Put(context.Background(), "testKey", "testValue")
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
fmt.Println(putResp)
|
||
|
|
||
|
// 3.get测试
|
||
|
getResp, err := cli.Get(context.Background(), "testKey")
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
fmt.Println(string(getResp.Kvs[0].Key), string(getResp.Kvs[0].Value))
|
||
|
}
|