-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo2.go
52 lines (45 loc) · 1006 Bytes
/
demo2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"context"
"fmt"
"github.com/coreos/etcd/clientv3"
"time"
)
/**
* @Author: super
* @Date: 2021-02-01 15:28
* @Description: 写入kv
**/
func main() {
var(
config clientv3.Config
client *clientv3.Client
err error
kv clientv3.KV
)
config = clientv3.Config{
Endpoints:[]string{"127.0.0.1:2379"},
DialTimeout:5 * time.Second,
}
//创建客户端
if client, err = clientv3.New(config); err != nil{
fmt.Println(err)
return
}
// 用于操作etcdkv
kv = clientv3.NewKV(client)
if putResp, err := kv.Put(context.TODO(), "/cron/jobs/job1", "hello1"); err != nil{
fmt.Println(err)
}else{
fmt.Println("revision", putResp.Header.Revision)
}
//获取之前revision的值
if putResp, err := kv.Put(context.TODO(), "/cron/jobs/job2", "hellohello", clientv3.WithPrevKV()); err != nil{
fmt.Println(err)
}else{
fmt.Println("revision", putResp.Header.Revision)
if putResp.PrevKv != nil{
fmt.Println("prevValue", string(putResp.PrevKv.Value))
}
}
}