GO ZK WATCH监听
2016-07-13 10:55
465 查看
package main import ( "fmt" "github.com/samuel/go-zookeeper/zk" "strings" "time" ) func must(err error) { if err != nil { panic(err) } } func connect() *zk.Conn { servers := strings.Split("127.0.0.1:2181", ",") conn, _, err := zk.Connect(servers, time.Second) must(err) return conn } func mirror(conn *zk.Conn, path string) (chan []string, chan error) { snapshots := make(chan []string) errors := make(chan error) go func() { for { snapshot, _, events, err := conn.ChildrenW(path) if err != nil { errors <- err return } snapshots <- snapshot evt := <-events if evt.Err != nil { errors <- evt.Err return } } }() return snapshots, errors } func main() { conn1 := connect() defer conn1.Close() flags := int32(zk.FlagEphemeral) acl := zk.WorldACL(zk.PermAll) _, err := conn1.Create("/mirror", nil, int32(0), acl) if err != nil { fmt.Printf("create: %+v\n", err) } snapshots, errors := mirror(conn1, "/mirror") go func() { for { select { case snapshot := <-snapshots: fmt.Printf("%+v\n", snapshot) case err := <-errors: fmt.Printf("%+v\n", err) } } }() conn2 := connect() time.Sleep(time.Second) _, err = conn2.Create("/mirror/one", []byte("one"), flags, acl) must(err) time.Sleep(time.Second) _, err = conn2.Create("/mirror/two", []byte("two"), flags, acl) must(err) time.Sleep(time.Second) err = conn1.Delete("/mirror/two", 0) must(err) time.Sleep(time.Second) _, err = conn2.Create("/mirror/three", []byte("three"), flags, acl) must(err) time.Sleep(time.Second) conn2.Close() time.Sleep(time.Second) }
相关文章推荐
- 在leangoo里怎么插入泳道,上下移动泳道和删除泳道?
- 用leangoo怎么添加泳道,拖拽移动泳道?
- 在leangoo里怎么复制列表,删除列表,插入列表?
- 在leangoo里怎么添加和移动列表,怎么修改列表名称?
- 在leangoo里列表一般怎么设置?
- leangoo怎么导入导出,归档和删除看板?
- 在leangoo里怎么设置看板周期,过滤看板数据?
- 在leangoo里怎么创建看板,更改看板名称?
- Google Glass 2.0
- Django1.9开发博客(6)- 模板继承
- codeforce #630 D. Hexagons!
- Genymotion模拟器常见问题——Unable to start the virtual device,The virtual device got no IP address
- 我写的GO的基础语法书,希望对喜欢Go的有帮助
- go语言学习-变量的问题
- Google新闻推荐系统
- 多数投票算法 Majority Vote Algorithm
- 怎么把顶部的Django administration去掉!!
- Go:操作注册表
- 解决chrom字体小于12号都显示为12号的办法
- django后台入门--引言