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
71
72
73
74
75
76
77
78
79
|
package main
import (
"fmt"
"time"
"github.com/go-zookeeper/zk"
)
var (
hosts = []string{"127.0.0.1:2181"}
path = "/wtzk"
flags int32 = zk.FlagEphemeral
data = []byte("zk data 001")
acls = zk.WorldACL(zk.PermAll)
)
func main() {
// 创建监听的option,用于初始化zk
eventCallbackOption := zk.WithEventCallback(callback)
// 连接zk
conn, _, err := zk.Connect(hosts, time.Second*5, eventCallbackOption)
defer conn.Close()
if err != nil {
fmt.Println(err)
return
}
// 开始监听path
_, _, _, err = conn.ExistsW(path)
if err != nil {
fmt.Println(err)
return
}
// 触发创建数据操作
create(conn, path, data)
//再次监听path
_, _, _, err = conn.ExistsW(path)
if err != nil {
fmt.Println(err)
return
}
// 触发删除数据操作
del(conn, path)
}
// zk watch 回调函数
func callback(event zk.Event) {
// zk.EventNodeCreated
// zk.EventNodeDeleted
fmt.Println("###########################")
fmt.Println("path: ", event.Path)
fmt.Println("type: ", event.Type.String())
fmt.Println("state: ", event.State.String())
fmt.Println("---------------------------")
}
// 创建数据
func create(conn *zk.Conn, path string, data []byte) {
_, err := conn.Create(path, data, flags, acls)
if err != nil {
fmt.Printf("创建数据失败: %v\n", err)
return
}
fmt.Println("创建数据成功")
}
// 删除数据
func del(conn *zk.Conn, path string) {
_, stat, _ := conn.Get(path)
err := conn.Delete(path, stat.Version)
if err != nil {
fmt.Printf("删除数据失败: %v\n", err)
return
}
fmt.Println("删除数据成功")
}
|