-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo8.go
70 lines (56 loc) · 1.19 KB
/
demo8.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"context"
"fmt"
"github.com/coreos/etcd/clientv3"
"time"
)
/**
* @Author: super
* @Date: 2021-02-01 22:16
* @Description:
**/
func main() {
var (
config clientv3.Config
client *clientv3.Client
err error
kv clientv3.KV
putOp clientv3.Op
getOp clientv3.Op
opResp clientv3.OpResponse
)
// 客户端配置
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
}
kv = clientv3.NewKV(client)
// 创建Op: operation
putOp = clientv3.OpPut("/cron/jobs/job8", "123123123")
// 执行OP
if opResp, err = kv.Do(context.TODO(), putOp); err != nil {
fmt.Println(err)
return
}
// kv.Do(op)
// kv.Put
// kv.Get
// kv.Delete
fmt.Println("写入Revision:", opResp.Put().Header.Revision)
// 创建Op
getOp = clientv3.OpGet("/cron/jobs/job8")
// 执行OP
if opResp, err = kv.Do(context.TODO(), getOp); err != nil {
fmt.Println(err)
return
}
// 打印
fmt.Println("数据Revision:", opResp.Get().Kvs[0].ModRevision) // create rev == mod rev
fmt.Println("数据value:", string(opResp.Get().Kvs[0].Value))
}