企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
> ### 十一例 Zookeeper操作 ~~~ package main import ( "github.com/samuel/go-zookeeper/zk" "time" "fmt" ) var conn *zk.Conn func callback(event zk.Event) { //fmt.Println("*******************") //fmt.Println("path:", event.Path) //fmt.Println("type:", event.Type.String()) //fmt.Println("state:", event.State.String()) //fmt.Println("-------------------") } func init() { servers := []string{"192.168.1.170:2181", "192.168.1.171:2181", "192.168.1.172:2181"} option := zk.WithEventCallback(callback) var err error if conn, _, err = zk.Connect(servers, time.Second*5, option); err != nil { fmt.Println(err) return } } /** path 节点 data 节点数据 flags有4种取值: 0:永久,除非手动删除 zk.FlagEphemeral = 1:短暂,session断开则改节点也被删除 zk.FlagSequence = 2:会自动在节点后面添加序号 3:Ephemeral和Sequence,即,短暂且自动添加序号 */ func zkCreate(path string, data []byte, flags int32, ) { p, err := conn.Create(path, data, flags, zk.WorldACL(zk.PermAll)) if err != nil { fmt.Println(err) } fmt.Println(p) } func zkGet(path string) { b, stat, err := conn.Get(path) if err != nil { fmt.Println(err) } fmt.Println("节点数据:", string(b)) fmt.Println("节点信息", stat.Czxid, stat.Version) } func zkGetChildren(path string) { b, stat, err := conn.Children(path) if err != nil { fmt.Println(err) } fmt.Println("节点数据:", b) fmt.Println("节点信息", stat.Czxid, stat.Version) } func zkSet(path string, data []byte) { _, stat, err := conn.Get(path) if err != nil { fmt.Println(err) } stat, err = conn.Set(path, data, stat.Version) if err != nil { fmt.Println(err) } fmt.Println(stat) } func zkDelete(path string, version int32) { if version == -1 { _, stat, err := conn.Get(path) if err != nil { fmt.Println(err) } version = stat.Version } if err := conn.Delete(path, version); err != nil { fmt.Println(path, err) } else { fmt.Println("Delete OK") } } func zkListenOne(path string) { //调用conn.ExistsW(path) 或GetW(path)为对应节点设置监听,该监听只生效一次 _, _, _, err := conn.ExistsW(path) if err != nil { fmt.Println(err) return } } func zkListenOneChannel(path string) { _, _, ch, err := conn.ExistsW(path) if err != nil { fmt.Println(err) return } go func() { event := <-ch fmt.Println("*******************") fmt.Println("path:", event.Path) fmt.Println("type:", event.Type.String()) fmt.Println("state:", event.State.String()) fmt.Println("-------------------") }() } func main() { //TODO : 监听事件(只生效一次) //zkListenOne("/data") //TODO : 创建节点 zkCreate("/data", []byte("123"), 0) zkCreate("/data/u1", []byte("123"), 2) zkCreate("/data/u1", []byte("123"), 2) zkCreate("/data/u1", []byte("123"), 2) zkCreate("/data/u1", []byte("123"), 2) time.Sleep(time.Second * 100) //time.Sleep(time.Second * 60) //TODO : 往节点添加数据 //zkSet("/data/4", []byte("b3")) //TODO : 开启一个协程处理chanel中传来的event事件 //zkListenOneChannel("/data") //TODO : 删除节点 //zkDelete("/data", -1) //TODO : 获取节点数据 //zkGet("/data") //TODO : 获取子节点 //zkGetChildren("/data") //zkDeleteAll("/") } func zkDeleteAll(path string) { b, _, _, err := conn.ChildrenW(path) if err != nil { return } for _, v := range b { zkDeleteAll2(path + v) zkDelete(path+v, -1) } } func zkDeleteAll2(path string) { b, _, _, err := conn.ChildrenW(path) if err != nil { return } for _, v := range b { zkDeleteAll2(path + "/" + v) zkDelete(path+"/"+v, -1) } } ~~~